Commit 2291fe4c authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'grp-finder-code-refactor' into 'master'

Move filter code to finder

Closes #31418

See merge request gitlab-org/gitlab!34470
parents 55ac007e d9585d3b
...@@ -19,6 +19,9 @@ ...@@ -19,6 +19,9 @@
# personal: boolean # personal: boolean
# search: string # search: string
# non_archived: boolean # non_archived: boolean
# with_issues_enabled: boolean
# with_merge_requests_enabled: boolean
# min_access_level: int
# #
class GroupProjectsFinder < ProjectsFinder class GroupProjectsFinder < ProjectsFinder
DEFAULT_PROJECTS_LIMIT = 100 DEFAULT_PROJECTS_LIMIT = 100
...@@ -42,6 +45,12 @@ class GroupProjectsFinder < ProjectsFinder ...@@ -42,6 +45,12 @@ class GroupProjectsFinder < ProjectsFinder
private private
def filter_projects(collection)
projects = super
projects = by_feature_availability(projects)
projects
end
def limit(collection) def limit(collection)
limit = options[:limit] limit = options[:limit]
...@@ -49,35 +58,37 @@ class GroupProjectsFinder < ProjectsFinder ...@@ -49,35 +58,37 @@ class GroupProjectsFinder < ProjectsFinder
end end
def init_collection def init_collection
projects = if current_user projects =
collection_with_user if only_shared?
else [shared_projects]
collection_without_user elsif only_owned?
end [owned_projects]
else
[owned_projects, shared_projects]
end
projects.map! do |project_relation|
filter_by_visibility(project_relation)
end
union(projects) union(projects)
end end
def collection_with_user def by_feature_availability(projects)
if only_shared? projects = projects.with_issues_available_for_user(current_user) if params[:with_issues_enabled].present?
[shared_projects.public_or_visible_to_user(current_user)] projects = projects.with_merge_requests_available_for_user(current_user) if params[:with_merge_requests_enabled].present?
elsif only_owned? projects
[owned_projects.public_or_visible_to_user(current_user)]
else
[
owned_projects.public_or_visible_to_user(current_user),
shared_projects.public_or_visible_to_user(current_user)
]
end
end end
def collection_without_user def filter_by_visibility(relation)
if only_shared? if current_user
[shared_projects.public_only] if min_access_level?
elsif only_owned? relation.visible_to_user_and_access_level(current_user, params[:min_access_level])
[owned_projects.public_only] else
relation.public_or_visible_to_user(current_user)
end
else else
[shared_projects.public_only, owned_projects.public_only] relation.public_only
end end
end end
......
---
title: Move filter code into finder
merge_request: 34470
author: Ravishankar
type: other
...@@ -76,9 +76,6 @@ module API ...@@ -76,9 +76,6 @@ module API
params: project_finder_params, params: project_finder_params,
options: finder_options options: finder_options
).execute ).execute
projects = projects.with_issues_available_for_user(current_user) if params[:with_issues_enabled]
projects = projects.with_merge_requests_enabled if params[:with_merge_requests_enabled]
projects = projects.visible_to_user_and_access_level(current_user, params[:min_access_level]) if params[:min_access_level]
projects = reorder_projects(projects) projects = reorder_projects(projects)
paginate(projects) paginate(projects)
end end
......
...@@ -528,6 +528,8 @@ module API ...@@ -528,6 +528,8 @@ module API
def project_finder_params_ce def project_finder_params_ce
finder_params = project_finder_params_visibility_ce finder_params = project_finder_params_visibility_ce
finder_params[:with_issues_enabled] = true if params[:with_issues_enabled].present?
finder_params[:with_merge_requests_enabled] = true if params[:with_merge_requests_enabled].present?
finder_params[:without_deleted] = true finder_params[:without_deleted] = true
finder_params[:search] = params[:search] if params[:search] finder_params[:search] = params[:search] if params[:search]
finder_params[:search_namespaces] = true if params[:search_namespaces].present? finder_params[:search_namespaces] = true if params[:search_namespaces].present?
......
...@@ -46,6 +46,18 @@ RSpec.describe GroupProjectsFinder do ...@@ -46,6 +46,18 @@ RSpec.describe GroupProjectsFinder do
context 'without subgroups projects' do context 'without subgroups projects' do
it { is_expected.to match_array([shared_project_3, shared_project_2, shared_project_1, private_project, public_project]) } it { is_expected.to match_array([shared_project_3, shared_project_2, shared_project_1, private_project, public_project]) }
end end
context "with min access level" do
let!(:shared_project_4) { create(:project, :internal, path: '8') }
before do
shared_project_4.project_group_links.create(group_access: Gitlab::Access::REPORTER, group: group)
end
let(:params) { { min_access_level: Gitlab::Access::MAINTAINER } }
it { is_expected.to match_array([shared_project_3, shared_project_2, shared_project_1, private_project, public_project]) }
end
end end
end end
...@@ -171,6 +183,38 @@ RSpec.describe GroupProjectsFinder do ...@@ -171,6 +183,38 @@ RSpec.describe GroupProjectsFinder do
end end
end end
describe 'feature availability' do
let!(:project_with_issues_disabled) { create(:project, :issues_disabled, :internal, path: '9') }
let!(:project_with_merge_request_disabled) { create(:project, :merge_requests_disabled, :internal, path: '10') }
before do
project_with_issues_disabled.project_group_links.create!(group_access: Gitlab::Access::REPORTER, group: group)
project_with_merge_request_disabled.project_group_links.create!(group_access: Gitlab::Access::REPORTER, group: group)
end
context 'without issues and merge request enabled' do
it { is_expected.to match_array([public_project, shared_project_1, shared_project_3, project_with_issues_disabled, project_with_merge_request_disabled]) }
end
context 'with issues enabled' do
let(:params) { { with_issues_enabled: true } }
it { is_expected.to match_array([public_project, shared_project_1, shared_project_3, project_with_merge_request_disabled]) }
end
context 'with merge request enabled' do
let(:params) { { with_merge_requests_enabled: true } }
it { is_expected.to match_array([public_project, shared_project_1, shared_project_3, project_with_issues_disabled]) }
end
context 'with issues and merge request enabled' do
let(:params) { { with_merge_requests_enabled: true, with_issues_enabled: true } }
it { is_expected.to match_array([public_project, shared_project_1, shared_project_3]) }
end
end
describe 'limiting' do describe 'limiting' do
context 'without limiting' do context 'without limiting' do
it 'returns all projects' do it 'returns all projects' do
......
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