2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
# GroupProjectsFinder
|
|
|
|
#
|
|
|
|
# Used to filter Projects by set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user use
|
|
|
|
# project_ids_relation: int[] - project ids to use
|
|
|
|
# group
|
|
|
|
# options:
|
|
|
|
# only_owned: boolean
|
|
|
|
# only_shared: boolean
|
2019-12-07 13:07:50 -05:00
|
|
|
# limit: integer
|
2020-10-28 17:08:42 -04:00
|
|
|
# include_subgroups: boolean
|
2017-03-03 05:35:04 -05:00
|
|
|
# params:
|
|
|
|
# sort: string
|
|
|
|
# visibility_level: int
|
|
|
|
# tags: string[]
|
|
|
|
# personal: boolean
|
|
|
|
# search: string
|
|
|
|
# non_archived: boolean
|
2020-06-30 17:09:47 -04:00
|
|
|
# with_issues_enabled: boolean
|
|
|
|
# with_merge_requests_enabled: boolean
|
|
|
|
# min_access_level: int
|
2017-03-03 05:35:04 -05:00
|
|
|
#
|
|
|
|
class GroupProjectsFinder < ProjectsFinder
|
2019-12-07 13:07:50 -05:00
|
|
|
DEFAULT_PROJECTS_LIMIT = 100
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
attr_reader :group, :options
|
|
|
|
|
|
|
|
def initialize(group:, params: {}, options: {}, current_user: nil, project_ids_relation: nil)
|
2019-08-28 10:26:42 -04:00
|
|
|
super(
|
|
|
|
params: params,
|
|
|
|
current_user: current_user,
|
|
|
|
project_ids_relation: project_ids_relation
|
|
|
|
)
|
|
|
|
@group = group
|
2016-03-20 16:03:53 -04:00
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2019-12-07 13:07:50 -05:00
|
|
|
def execute
|
|
|
|
collection = super
|
|
|
|
limit(collection)
|
|
|
|
end
|
|
|
|
|
2016-03-20 16:03:53 -04:00
|
|
|
private
|
|
|
|
|
2020-06-30 17:09:47 -04:00
|
|
|
def filter_projects(collection)
|
|
|
|
projects = super
|
2021-04-19 11:09:08 -04:00
|
|
|
by_feature_availability(projects)
|
2020-06-30 17:09:47 -04:00
|
|
|
end
|
|
|
|
|
2019-12-07 13:07:50 -05:00
|
|
|
def limit(collection)
|
|
|
|
limit = options[:limit]
|
|
|
|
|
|
|
|
limit.present? ? collection.with_limit(limit) : collection
|
|
|
|
end
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
def init_collection
|
2020-06-30 17:09:47 -04:00
|
|
|
projects =
|
|
|
|
if only_shared?
|
|
|
|
[shared_projects]
|
|
|
|
elsif only_owned?
|
|
|
|
[owned_projects]
|
|
|
|
else
|
|
|
|
[owned_projects, shared_projects]
|
|
|
|
end
|
|
|
|
|
|
|
|
projects.map! do |project_relation|
|
|
|
|
filter_by_visibility(project_relation)
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-06-15 09:19:25 -04:00
|
|
|
union(projects)
|
|
|
|
end
|
2016-03-20 16:03:53 -04:00
|
|
|
|
2020-06-30 17:09:47 -04:00
|
|
|
def by_feature_availability(projects)
|
|
|
|
projects = projects.with_issues_available_for_user(current_user) if params[:with_issues_enabled].present?
|
|
|
|
projects = projects.with_merge_requests_available_for_user(current_user) if params[:with_merge_requests_enabled].present?
|
|
|
|
projects
|
2017-06-15 09:19:25 -04:00
|
|
|
end
|
2016-03-20 16:03:53 -04:00
|
|
|
|
2020-06-30 17:09:47 -04:00
|
|
|
def filter_by_visibility(relation)
|
|
|
|
if current_user
|
|
|
|
if min_access_level?
|
|
|
|
relation.visible_to_user_and_access_level(current_user, params[:min_access_level])
|
|
|
|
else
|
|
|
|
relation.public_or_visible_to_user(current_user)
|
|
|
|
end
|
2017-06-15 09:19:25 -04:00
|
|
|
else
|
2020-06-30 17:09:47 -04:00
|
|
|
relation.public_only
|
2017-06-15 09:19:25 -04:00
|
|
|
end
|
2016-03-20 16:03:53 -04:00
|
|
|
end
|
2017-03-03 05:35:04 -05:00
|
|
|
|
|
|
|
def union(items)
|
2017-06-15 09:19:25 -04:00
|
|
|
if items.one?
|
|
|
|
items.first
|
|
|
|
else
|
|
|
|
find_union(items, Project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_owned?
|
|
|
|
options.fetch(:only_owned, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_shared?
|
|
|
|
options.fetch(:only_shared, false)
|
|
|
|
end
|
|
|
|
|
2018-01-24 01:06:24 -05:00
|
|
|
# subgroups are supported only for owned projects not for shared
|
|
|
|
def include_subgroups?
|
|
|
|
options.fetch(:include_subgroups, false)
|
|
|
|
end
|
|
|
|
|
2017-06-15 09:19:25 -04:00
|
|
|
def owned_projects
|
2018-01-24 01:06:24 -05:00
|
|
|
if include_subgroups?
|
2019-08-28 10:26:42 -04:00
|
|
|
Project.for_group_and_its_subgroups(group)
|
2018-01-24 01:06:24 -05:00
|
|
|
else
|
|
|
|
group.projects
|
|
|
|
end
|
2017-06-15 09:19:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def shared_projects
|
|
|
|
group.shared_projects
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
2016-03-20 16:03:53 -04:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
GroupProjectsFinder.prepend_if_ee('EE::GroupProjectsFinder')
|