Commit 25146309 authored by Nick Kipling's avatar Nick Kipling

Added package_name param to packages api

Added package_name param
Updated finder to support package_name
Added tests
parent 434e3cec
---
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
......
...@@ -26,6 +26,8 @@ module API ...@@ -26,6 +26,8 @@ module API
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
......
...@@ -104,18 +104,28 @@ describe API::ProjectPackages do ...@@ -104,18 +104,28 @@ describe API::ProjectPackages do
context 'package types' do context 'package types' 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
context 'filtering on package_name' do
it 'returns the named package' do
url = package_filter_url(:name, 'nuget')
get api(url, user)
def package_type_url(package_type) expect(json_response.length).to eq(1)
"/projects/#{project.id}/packages?package_type=#{package_type}" 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 end
......
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