Commit 90bd0076 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '208675-add-package_name-as-option-to-packages-api' into 'master'

Add package_name as option to packages API

See merge request gitlab-org/gitlab!26291
parents db0d1c9b 154ed83e
---
title: Added package_name as filter parameter to packages API
merge_request: 26291
author:
type: added
...@@ -21,6 +21,7 @@ GET /projects/:id/packages ...@@ -21,6 +21,7 @@ GET /projects/:id/packages
| `order_by`| string | no | The field to use as order. One of `created_at` (default), `name`, `version`, or `type`. | | `order_by`| string | no | The field to use as order. One of `created_at` (default), `name`, `version`, or `type`. |
| `sort` | string | no | The direction of the order, either `asc` (default) for ascending order or `desc` for descending order. | | `sort` | string | no | The direction of the order, either `asc` (default) for ascending order or `desc` for descending order. |
| `package_type` | string | no | Filter the returned packages by type. One of `conan`, `maven`, `npm` or `nuget`. (_Introduced in GitLab 12.9_) | `package_type` | string | no | Filter the returned packages by type. One of `conan`, `maven`, `npm` or `nuget`. (_Introduced in GitLab 12.9_)
| `package_name` | string | no | Filter the project packages with a fuzzy search by name. (_Introduced in GitLab 12.9_)
```shell ```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/projects/:id/packages curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/projects/:id/packages
......
...@@ -15,6 +15,7 @@ module Packages ...@@ -15,6 +15,7 @@ module Packages
def execute def execute
packages = project.packages packages = project.packages
packages = filter_by_package_type(packages) packages = filter_by_package_type(packages)
packages = filter_by_package_name(packages)
packages = order_packages(packages) packages = order_packages(packages)
packages packages
end end
...@@ -27,6 +28,12 @@ module Packages ...@@ -27,6 +28,12 @@ module Packages
packages.with_package_type(params[:package_type]) packages.with_package_type(params[:package_type])
end end
def filter_by_package_name(packages)
return packages unless params[:package_name]
packages.search_by_name(params[:package_name])
end
def order_packages(packages) def order_packages(packages)
packages.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}") packages.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}")
end end
......
...@@ -25,7 +25,9 @@ module API ...@@ -25,7 +25,9 @@ module API
optional :sort, type: String, values: %w[asc desc], default: 'asc', optional :sort, type: String, values: %w[asc desc], default: 'asc',
desc: 'Return packages sorted in `asc` or `desc` order.' desc: 'Return packages sorted in `asc` or `desc` order.'
optional :package_type, type: String, values: %w[conan maven npm nuget], optional :package_type, type: String, values: %w[conan maven npm nuget],
desc: 'Return packages of a certain type' desc: 'Return packages of a certain type'
optional :package_name, type: String,
desc: 'Return packages with this name'
end end
get ':id/packages' do get ':id/packages' do
packages = ::Packages::PackagesFinder packages = ::Packages::PackagesFinder
......
...@@ -65,6 +65,12 @@ describe ::Packages::PackagesFinder do ...@@ -65,6 +65,12 @@ describe ::Packages::PackagesFinder do
end end
end end
context 'with package_name' do
let(:params) { { package_name: 'maven' } }
it { is_expected.to eq([maven_package]) }
end
context 'with nil params' do context 'with nil params' do
it { is_expected.to match_array([conan_package, maven_package]) } it { is_expected.to match_array([conan_package, maven_package]) }
end end
......
...@@ -95,28 +95,38 @@ describe API::ProjectPackages do ...@@ -95,28 +95,38 @@ describe API::ProjectPackages do
end end
end end
describe 'filtering on package type' do context 'filtering on package_type' do
let_it_be(:package1) { create(:conan_package, project: project) } let_it_be(:package1) { create(:conan_package, project: project) }
let_it_be(:package2) { create(:maven_package, project: project) } let_it_be(:package2) { create(:maven_package, project: project) }
let_it_be(:package3) { create(:npm_package, project: project) } let_it_be(:package3) { create(:npm_package, project: project) }
let_it_be(:package4) { create(:nuget_package, project: project) } let_it_be(:package4) { create(:nuget_package, project: project) }
context 'package types' do context 'for each type' do
%w[conan maven npm nuget].each do |package_type| %w[conan maven npm nuget].each do |package_type|
it "returns #{package_type} packages" do it "returns #{package_type} packages" do
url = package_type_url(package_type) url = package_filter_url(:type, package_type)
get api(url, user) get api(url, user)
expect(json_response.length).to eq(1) expect(json_response.length).to eq(1)
expect(json_response.map { |package| package['package_type'] }).to contain_exactly(package_type) expect(json_response.map { |package| package['package_type'] }).to contain_exactly(package_type)
end end
end end
end
end
def package_type_url(package_type) context 'filtering on package_name' do
"/projects/#{project.id}/packages?package_type=#{package_type}" it 'returns the named package' do
end url = package_filter_url(:name, 'nuget')
get api(url, user)
expect(json_response.length).to eq(1)
expect(json_response.first['name']).to include(package2.name)
end end
end end
def package_filter_url(filter, param)
"/projects/#{project.id}/packages?package_#{filter}=#{param}"
end
end end
context 'packages feature disabled' do context 'packages feature disabled' 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