Commit 7f0431dd authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'fix-project-api-archived' into 'master'

Fix archived parameter for projects API

Closes #32301

See merge request gitlab-org/gitlab-ce!20566
parents c82adcce 209fd864
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
# personal: boolean # personal: boolean
# search: string # search: string
# non_archived: boolean # non_archived: boolean
# archived: 'only' or boolean
# #
class ProjectsFinder < UnionFinder class ProjectsFinder < UnionFinder
include CustomAttributesFilter include CustomAttributesFilter
...@@ -130,7 +131,7 @@ class ProjectsFinder < UnionFinder ...@@ -130,7 +131,7 @@ class ProjectsFinder < UnionFinder
def by_archived(projects) def by_archived(projects)
if params[:non_archived] if params[:non_archived]
projects.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' if params[:archived] == 'only'
projects.archived projects.archived
elsif Gitlab::Utils.to_boolean(params[:archived]) elsif Gitlab::Utils.to_boolean(params[:archived])
......
---
title: Fix archived parameter for projects API
merge_request: 20566
author: Peter Marko
type: fixed
...@@ -385,7 +385,7 @@ module API ...@@ -385,7 +385,7 @@ module API
finder_params[:non_public] = true if params[:membership].present? finder_params[:non_public] = true if params[:membership].present?
finder_params[:starred] = true if params[:starred].present? finder_params[:starred] = true if params[:starred].present?
finder_params[:visibility_level] = Gitlab::VisibilityLevel.level_value(params[:visibility]) if params[:visibility] 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[:search] = params[:search] if params[:search]
finder_params[:user] = params.delete(:user) if params[:user] finder_params[:user] = params.delete(:user) if params[:user]
finder_params[:custom_attributes] = params[:custom_attributes] if params[:custom_attributes] finder_params[:custom_attributes] = params[:custom_attributes] if params[:custom_attributes]
...@@ -496,5 +496,11 @@ module API ...@@ -496,5 +496,11 @@ module API
exception.status == 500 exception.status == 500
end end
def archived_param
return 'only' if params[:archived]
params[:archived]
end
end end
end end
...@@ -30,7 +30,7 @@ module API ...@@ -30,7 +30,7 @@ module API
end end
params :filter_params do 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, optional :visibility, type: String, values: Gitlab::VisibilityLevel.string_values,
desc: 'Limit by visibility' desc: 'Limit by visibility'
optional :search, type: String, desc: 'Return list of projects matching the search criteria' optional :search, type: String, desc: 'Return list of projects matching the search criteria'
......
...@@ -237,6 +237,39 @@ describe API::Projects do ...@@ -237,6 +237,39 @@ describe API::Projects do
end end
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 context 'and using search' do
it_behaves_like 'projects response' do it_behaves_like 'projects response' do
let(:filter) { { search: project.name } } let(:filter) { { search: project.name } }
......
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