Commit aa673364 authored by Nick Kipling's avatar Nick Kipling Committed by Mayra Cabrera

Adds package_name filter to group packages api

Updated group_packages api to include param
Updated group finder to match project version and include name
Updated tests
Added param to documentation
parent f7d46e8a
...@@ -68,6 +68,7 @@ GET /groups/:id/packages ...@@ -68,6 +68,7 @@ GET /groups/:id/packages
| `order_by`| string | no | The field to use as order. One of `created_at` (default), `name`, `version`, `type`, or `project_path`. | | `order_by`| string | no | The field to use as order. One of `created_at` (default), `name`, `version`, `type`, or `project_path`. |
| `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`, `pypi` or `nuget`. (_Introduced in GitLab 12.9_) | | `package_type` | string | no | Filter the returned packages by type. One of `conan`, `maven`, `npm`, `pypi` or `nuget`. (_Introduced in GitLab 12.9_) |
| `package_name` | string | no | Filter the project packages with a fuzzy search by name. (_[Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/30980) in GitLab 13.0_)
```shell ```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/groups/:id/packages?exclude_subgroups=true curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.example.com/api/v4/groups/:id/packages?exclude_subgroups=true
......
...@@ -4,6 +4,8 @@ module Packages ...@@ -4,6 +4,8 @@ module Packages
class GroupPackagesFinder class GroupPackagesFinder
attr_reader :current_user, :group, :params attr_reader :current_user, :group, :params
InvalidPackageTypeError = Class.new(StandardError)
def initialize(current_user, group, params = { exclude_subgroups: false, order_by: 'created_at', sort: 'asc' }) def initialize(current_user, group, params = { exclude_subgroups: false, order_by: 'created_at', sort: 'asc' })
@current_user = current_user @current_user = current_user
@group = group @group = group
...@@ -25,9 +27,9 @@ module Packages ...@@ -25,9 +27,9 @@ module Packages
.has_version .has_version
.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}") .sort_by_attribute("#{params[:order_by]}_#{params[:sort]}")
return packages unless package_type packages = filter_by_package_type(packages)
packages = filter_by_package_name(packages)
packages.with_package_type(package_type) packages
end end
def group_projects_visible_to_current_user def group_projects_visible_to_current_user
...@@ -39,7 +41,7 @@ module Packages ...@@ -39,7 +41,7 @@ module Packages
end end
def package_type def package_type
@params[:package_type].presence params[:package_type].presence
end end
def groups def groups
...@@ -51,5 +53,18 @@ module Packages ...@@ -51,5 +53,18 @@ module Packages
def exclude_subgroups? def exclude_subgroups?
params[:exclude_subgroups] params[:exclude_subgroups]
end end
def filter_by_package_type(packages)
return packages unless package_type
raise InvalidPackageTypeError unless Package.package_types.key?(package_type)
packages.with_package_type(package_type)
end
def filter_by_package_name(packages)
return packages unless params[:package_name].present?
packages.search_by_name(params[:package_name])
end
end end
end end
---
title: Adds package_name filter to group packages API
merge_request: 30980
author:
type: added
...@@ -27,12 +27,14 @@ module API ...@@ -27,12 +27,14 @@ 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: Packages::Package.package_types.keys, optional :package_type, type: String, values: Packages::Package.package_types.keys,
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::GroupPackagesFinder.new( packages = Packages::GroupPackagesFinder.new(
current_user, current_user,
user_group, user_group,
declared(params).slice(:exclude_subgroups, :order_by, :sort, :package_type) declared(params).slice(:exclude_subgroups, :order_by, :sort, :package_type, :package_name)
).execute ).execute
present paginate(packages), with: EE::API::Entities::Package, user: current_user, group: true present paginate(packages), with: EE::API::Entities::Package, user: current_user, group: true
......
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
require 'spec_helper' require 'spec_helper'
describe Packages::GroupPackagesFinder do describe Packages::GroupPackagesFinder do
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:group) { create(:group) } let_it_be(:group) { create(:group) }
let(:project) { create(:project, namespace: group) } let_it_be(:project) { create(:project, namespace: group) }
let(:another_group) { create(:group) } let(:another_group) { create(:group) }
before do before do
...@@ -58,6 +58,25 @@ describe Packages::GroupPackagesFinder do ...@@ -58,6 +58,25 @@ describe Packages::GroupPackagesFinder do
it { is_expected.not_to include(package_without_version) } it { is_expected.not_to include(package_without_version) }
end end
context 'with package_name' do
let_it_be(:named_package) { create(:maven_package, project: project, name: 'maven') }
let(:params) { { package_name: package_name } }
context 'as complete name' do
let(:package_name) { 'maven' }
it { is_expected.to eq([named_package]) }
end
%w[aven mav ave].each do |filter|
context "for fuzzy filter #{filter}" do
let(:package_name) { filter }
it { is_expected.to eq([named_package]) }
end
end
end
end end
context 'group has package of all types' do context 'group has package of all types' do
...@@ -86,6 +105,12 @@ describe Packages::GroupPackagesFinder do ...@@ -86,6 +105,12 @@ describe Packages::GroupPackagesFinder do
it { is_expected.to match_array([package1])} it { is_expected.to match_array([package1])}
end end
context 'with invalid package_type' do
let(:params) { { package_type: 'invalid_type' } }
it { expect { subject }.to raise_exception(described_class::InvalidPackageTypeError) }
end
context 'when project is public' do context 'when project is public' do
let_it_be(:other_user) { create(:user) } let_it_be(:other_user) { create(:user) }
let(:finder) { described_class.new(other_user, group) } let(:finder) { described_class.new(other_user, group) }
......
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