Commit 5eeb9976 authored by Tetiana Chupryna's avatar Tetiana Chupryna Committed by Fabien Catteau

Add pagination to dependencies API

Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/335146

Changelog: security
EE: true
parent 3a2f7f7e
...@@ -11,6 +11,9 @@ This API is in an alpha stage and considered unstable. ...@@ -11,6 +11,9 @@ This API is in an alpha stage and considered unstable.
The response payload may be subject to change or breakage The response payload may be subject to change or breakage
across GitLab releases. across GitLab releases.
> - Introduced in GitLab 12.1.
> - Pagination introduced in 14.4.
Every call to this endpoint requires authentication. To perform this call, user should be authorized to read repository. Every call to this endpoint requires authentication. To perform this call, user should be authorized to read repository.
To see vulnerabilities in response, user should be authorized to read To see vulnerabilities in response, user should be authorized to read
[Project Security Dashboard](../user/application_security/security_dashboard/index.md#project-security-dashboard). [Project Security Dashboard](../user/application_security/security_dashboard/index.md#project-security-dashboard).
...@@ -60,3 +63,10 @@ Example response: ...@@ -60,3 +63,10 @@ Example response:
} }
] ]
``` ```
## Dependencies pagination
By default, `GET` requests return 20 results at a time because the API results
are paginated.
Read more on [pagination](index.md#pagination).
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
module API module API
class Dependencies < ::API::Base class Dependencies < ::API::Base
include PaginationParams
feature_category :dependency_scanning feature_category :dependency_scanning
helpers do helpers do
...@@ -31,6 +33,7 @@ module API ...@@ -31,6 +33,7 @@ module API
coerce_with: Validations::Types::CommaSeparatedToArray.coerce, coerce_with: Validations::Types::CommaSeparatedToArray.coerce,
desc: "Returns dependencies belonging to specified package managers: #{::Security::DependencyListService::FILTER_PACKAGE_MANAGERS_VALUES.join(', ')}.", desc: "Returns dependencies belonging to specified package managers: #{::Security::DependencyListService::FILTER_PACKAGE_MANAGERS_VALUES.join(', ')}.",
values: ::Security::DependencyListService::FILTER_PACKAGE_MANAGERS_VALUES values: ::Security::DependencyListService::FILTER_PACKAGE_MANAGERS_VALUES
use :pagination
end end
get ':id/dependencies' do get ':id/dependencies' do
...@@ -39,7 +42,7 @@ module API ...@@ -39,7 +42,7 @@ module API
::Gitlab::Tracking.event(self.options[:for].name, 'view_dependencies', project: user_project, user: current_user, namespace: user_project.namespace) ::Gitlab::Tracking.event(self.options[:for].name, 'view_dependencies', project: user_project, user: current_user, namespace: user_project.namespace)
dependency_params = declared_params(include_missing: false).merge(project: user_project) dependency_params = declared_params(include_missing: false).merge(project: user_project)
dependencies = dependencies_by(dependency_params) dependencies = paginate(::Gitlab::ItemsCollection.new(dependencies_by(dependency_params)))
present dependencies, with: ::EE::API::Entities::Dependency, user: current_user, project: user_project present dependencies, with: ::EE::API::Entities::Dependency, user: current_user, project: user_project
end end
......
...@@ -28,11 +28,12 @@ RSpec.describe API::Dependencies do ...@@ -28,11 +28,12 @@ RSpec.describe API::Dependencies do
request request
end end
it 'returns all dependencies' do it 'returns paginated dependencies' do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('public_api/v4/dependencies', dir: 'ee') expect(response).to match_response_schema('public_api/v4/dependencies', dir: 'ee')
expect(response).to include_pagination_headers
expect(json_response.length).to eq(21) expect(json_response.length).to eq(20)
end end
it 'returns vulnerabilities info' do it 'returns vulnerabilities info' do
...@@ -71,6 +72,17 @@ RSpec.describe API::Dependencies do ...@@ -71,6 +72,17 @@ RSpec.describe API::Dependencies do
end end
end end
end end
context 'with pagination params' do
let(:params) { { per_page: 5, page: 5 } }
it 'returns paginated dependencies' do
expect(response).to match_response_schema('public_api/v4/dependencies', dir: 'ee')
expect(response).to include_pagination_headers
expect(json_response.length).to eq(1)
end
end
end end
context 'without permissions to see vulnerabilities' do context 'without permissions to see vulnerabilities' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment