From 209fd86442930ffd398849e386d9d13e699799c7 Mon Sep 17 00:00:00 2001 From: "Marko, Peter" Date: Wed, 11 Jul 2018 20:17:18 +0200 Subject: Fix archived parameter for projects API --- app/finders/projects_finder.rb | 3 +- changelogs/unreleased/fix-project-api-archived.yml | 5 ++++ lib/api/helpers.rb | 8 +++++- lib/api/projects.rb | 2 +- spec/requests/api/projects_spec.rb | 33 ++++++++++++++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/fix-project-api-archived.yml diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb index c7d6bc6cfdc..b06595081e7 100644 --- a/app/finders/projects_finder.rb +++ b/app/finders/projects_finder.rb @@ -16,6 +16,7 @@ # personal: boolean # search: string # non_archived: boolean +# archived: 'only' or boolean # class ProjectsFinder < UnionFinder include CustomAttributesFilter @@ -130,7 +131,7 @@ class ProjectsFinder < UnionFinder def by_archived(projects) if params[:non_archived] projects.non_archived - elsif params.key?(:archived) # Back-compatibility with the places where `params[:archived]` can be set explicitly to `false` + elsif params.key?(:archived) if params[:archived] == 'only' projects.archived elsif Gitlab::Utils.to_boolean(params[:archived]) diff --git a/changelogs/unreleased/fix-project-api-archived.yml b/changelogs/unreleased/fix-project-api-archived.yml new file mode 100644 index 00000000000..9d119fd3429 --- /dev/null +++ b/changelogs/unreleased/fix-project-api-archived.yml @@ -0,0 +1,5 @@ +--- +title: Fix archived parameter for projects API +merge_request: 20566 +author: Peter Marko +type: fixed diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 9c53b7c3fe7..f7737468148 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -385,7 +385,7 @@ module API finder_params[:non_public] = true if params[:membership].present? finder_params[:starred] = true if params[:starred].present? finder_params[:visibility_level] = Gitlab::VisibilityLevel.level_value(params[:visibility]) if params[:visibility] - finder_params[:archived] = params[:archived] + finder_params[:archived] = archived_param unless params[:archived].nil? finder_params[:search] = params[:search] if params[:search] finder_params[:user] = params.delete(:user) if params[:user] finder_params[:custom_attributes] = params[:custom_attributes] if params[:custom_attributes] @@ -496,5 +496,11 @@ module API exception.status == 500 end + + def archived_param + return 'only' if params[:archived] + + params[:archived] + end end end diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 8273abe48c9..0888e3befac 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -30,7 +30,7 @@ module API end params :filter_params do - optional :archived, type: Boolean, default: false, desc: 'Limit by archived status' + optional :archived, type: Boolean, desc: 'Limit by archived status' optional :visibility, type: String, values: Gitlab::VisibilityLevel.string_values, desc: 'Limit by visibility' optional :search, type: String, desc: 'Return list of projects matching the search criteria' diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 8389cb7cf9c..f72c01561d8 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -237,6 +237,39 @@ describe API::Projects do end end + context 'and using archived' do + let!(:archived_project) { create(:project, creator_id: user.id, namespace: user.namespace, archived: true) } + + it 'returns archived projects' do + get api('/projects?archived=true', user) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + expect(json_response).to be_an Array + expect(json_response.length).to eq(Project.public_or_visible_to_user(user).where(archived: true).size) + expect(json_response.map { |project| project['id'] }).to include(archived_project.id) + end + + it 'returns non-archived projects' do + get api('/projects?archived=false', user) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + expect(json_response).to be_an Array + expect(json_response.length).to eq(Project.public_or_visible_to_user(user).where(archived: false).size) + expect(json_response.map { |project| project['id'] }).not_to include(archived_project.id) + end + + it 'returns every project' do + get api('/projects', user) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + expect(json_response).to be_an Array + expect(json_response.map { |project| project['id'] }).to contain_exactly(*Project.public_or_visible_to_user(user).pluck(:id)) + end + end + context 'and using search' do it_behaves_like 'projects response' do let(:filter) { { search: project.name } } -- cgit v1.2.1