debian-mirror-gitlab/app/finders/group_projects_finder.rb

130 lines
2.8 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
# 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
2020-01-01 13:55:28 +05:30
# limit: integer
2021-01-29 00:20:46 +05:30
# include_subgroups: boolean
2017-08-17 22:00:37 +05:30
# params:
# sort: string
# visibility_level: int
# tags: string[]
# personal: boolean
# search: string
# non_archived: boolean
2020-07-28 23:09:34 +05:30
# with_issues_enabled: boolean
# with_merge_requests_enabled: boolean
# min_access_level: int
2017-08-17 22:00:37 +05:30
#
class GroupProjectsFinder < ProjectsFinder
2020-01-01 13:55:28 +05:30
DEFAULT_PROJECTS_LIMIT = 100
2017-08-17 22:00:37 +05:30
attr_reader :group, :options
def initialize(group:, params: {}, options: {}, current_user: nil, project_ids_relation: nil)
2019-12-04 20:38:33 +05:30
super(
params: params,
current_user: current_user,
project_ids_relation: project_ids_relation
)
@group = group
2016-06-02 11:05:42 +05:30
@options = options
end
2020-01-01 13:55:28 +05:30
def execute
collection = super
limit(collection)
end
2016-06-02 11:05:42 +05:30
private
2020-07-28 23:09:34 +05:30
def filter_projects(collection)
projects = super
2021-04-29 21:17:54 +05:30
by_feature_availability(projects)
2020-07-28 23:09:34 +05:30
end
2020-01-01 13:55:28 +05:30
def limit(collection)
limit = options[:limit]
limit.present? ? collection.with_limit(limit) : collection
end
2017-08-17 22:00:37 +05:30
def init_collection
2020-07-28 23:09:34 +05:30
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
2016-06-02 11:05:42 +05:30
2017-09-10 17:25:29 +05:30
union(projects)
end
2016-06-02 11:05:42 +05:30
2020-07-28 23:09:34 +05:30
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-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
2020-07-28 23:09:34 +05:30
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-09-10 17:25:29 +05:30
else
2020-07-28 23:09:34 +05:30
relation.public_only
2017-09-10 17:25:29 +05:30
end
2016-06-02 11:05:42 +05:30
end
2017-08-17 22:00:37 +05:30
def union(items)
2017-09-10 17:25:29 +05:30
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-03-17 18:26:18 +05:30
# subgroups are supported only for owned projects not for shared
def include_subgroups?
options.fetch(:include_subgroups, false)
end
2017-09-10 17:25:29 +05:30
def owned_projects
2018-03-17 18:26:18 +05:30
if include_subgroups?
2019-12-04 20:38:33 +05:30
Project.for_group_and_its_subgroups(group)
2018-03-17 18:26:18 +05:30
else
group.projects
end
2017-09-10 17:25:29 +05:30
end
def shared_projects
group.shared_projects
2017-08-17 22:00:37 +05:30
end
2016-06-02 11:05:42 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
GroupProjectsFinder.prepend_mod_with('GroupProjectsFinder')