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
| `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. |
| `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
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/projects/:id/packages
......
......@@ -15,6 +15,7 @@ module Packages
def execute
packages = project.packages
packages = filter_by_package_type(packages)
packages = filter_by_package_name(packages)
packages = order_packages(packages)
packages
end
......@@ -27,6 +28,12 @@ module Packages
packages.with_package_type(params[:package_type])
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)
packages.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}")
end
......
......@@ -26,6 +26,8 @@ module API
desc: 'Return packages sorted in `asc` or `desc` order.'
optional :package_type, type: String, values: %w[conan maven npm nuget],
desc: 'Return packages of a certain type'
optional :package_name, type: String,
desc: 'Return packages with this name'
end
get ':id/packages' do
packages = ::Packages::PackagesFinder
......
......@@ -65,6 +65,12 @@ describe ::Packages::PackagesFinder do
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
it { is_expected.to match_array([conan_package, maven_package]) }
end
......
......@@ -95,27 +95,37 @@ describe API::ProjectPackages do
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(:package2) { create(:maven_package, project: project) }
let_it_be(:package3) { create(:npm_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|
it "returns #{package_type} packages" do
url = package_type_url(package_type)
url = package_filter_url(:type, package_type)
get api(url, user)
expect(json_response.length).to eq(1)
expect(json_response.map { |package| package['package_type'] }).to contain_exactly(package_type)
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)
"/projects/#{project.id}/packages?package_type=#{package_type}"
expect(json_response.length).to eq(1)
expect(json_response.first['name']).to include(package2.name)
end
end
def package_filter_url(filter, param)
"/projects/#{project.id}/packages?package_#{filter}=#{param}"
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