2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
# ProjectsFinder
|
|
|
|
#
|
|
|
|
# Used to filter Projects by set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user use
|
|
|
|
# project_ids_relation: int[] - project ids to use
|
|
|
|
# params:
|
|
|
|
# trending: boolean
|
2017-05-26 10:31:37 -04:00
|
|
|
# owned: boolean
|
2017-03-03 05:35:04 -05:00
|
|
|
# non_public: boolean
|
|
|
|
# starred: boolean
|
|
|
|
# sort: string
|
|
|
|
# visibility_level: int
|
2021-05-25 14:10:42 -04:00
|
|
|
# tag: string[] - deprecated, use 'topic' instead
|
|
|
|
# topic: string[]
|
2022-07-04 20:09:38 -04:00
|
|
|
# topic_id: int
|
2017-03-03 05:35:04 -05:00
|
|
|
# personal: boolean
|
|
|
|
# search: string
|
2020-03-23 08:09:47 -04:00
|
|
|
# search_namespaces: boolean
|
2020-12-07 16:10:08 -05:00
|
|
|
# minimum_search_length: int
|
2017-03-03 05:35:04 -05:00
|
|
|
# non_archived: boolean
|
2018-07-11 14:17:18 -04:00
|
|
|
# archived: 'only' or boolean
|
2018-07-08 15:43:06 -04:00
|
|
|
# min_access_level: integer
|
2020-04-01 05:07:45 -04:00
|
|
|
# last_activity_after: datetime
|
|
|
|
# last_activity_before: datetime
|
2020-06-19 17:08:32 -04:00
|
|
|
# repository_storage: string
|
2020-08-31 17:10:19 -04:00
|
|
|
# without_deleted: boolean
|
2022-01-27 22:15:57 -05:00
|
|
|
# not_aimed_for_deletion: boolean
|
2017-03-03 05:35:04 -05:00
|
|
|
#
|
2016-03-20 16:03:53 -04:00
|
|
|
class ProjectsFinder < UnionFinder
|
2017-09-18 09:03:24 -04:00
|
|
|
include CustomAttributesFilter
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
attr_accessor :params
|
|
|
|
attr_reader :current_user, :project_ids_relation
|
2014-02-25 12:15:08 -05:00
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
def initialize(params: {}, current_user: nil, project_ids_relation: nil)
|
|
|
|
@params = params
|
|
|
|
@current_user = current_user
|
|
|
|
@project_ids_relation = project_ids_relation
|
2021-05-25 14:10:42 -04:00
|
|
|
|
|
|
|
@params[:topic] ||= @params.delete(:tag) if @params[:tag].present?
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2017-06-29 13:20:59 -04:00
|
|
|
user = params.delete(:user)
|
|
|
|
collection =
|
|
|
|
if user
|
2018-08-27 11:31:01 -04:00
|
|
|
PersonalProjectsFinder.new(user, finder_params).execute(current_user) # rubocop: disable CodeReuse/Finder
|
2017-06-29 13:20:59 -04:00
|
|
|
else
|
|
|
|
init_collection
|
|
|
|
end
|
|
|
|
|
2020-01-27 16:08:47 -05:00
|
|
|
use_cte = params.delete(:use_cte)
|
2020-02-06 10:09:11 -05:00
|
|
|
collection = Project.wrap_with_cte(collection) if use_cte
|
2018-10-12 10:10:34 -04:00
|
|
|
collection = filter_projects(collection)
|
2020-10-06 20:08:24 -04:00
|
|
|
|
2021-07-01 11:07:40 -04:00
|
|
|
if params[:sort] == 'similarity' && params[:search]
|
2020-10-06 20:08:24 -04:00
|
|
|
collection.sorted_by_similarity_desc(params[:search])
|
|
|
|
else
|
|
|
|
sort(collection)
|
|
|
|
end
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
def init_collection
|
2017-06-13 10:44:55 -04:00
|
|
|
if current_user
|
|
|
|
collection_with_user
|
|
|
|
else
|
|
|
|
collection_without_user
|
|
|
|
end
|
|
|
|
end
|
2016-03-10 16:08:11 -05:00
|
|
|
|
2018-10-12 10:10:34 -04:00
|
|
|
# EE would override this to add more filters
|
|
|
|
def filter_projects(collection)
|
|
|
|
collection = by_ids(collection)
|
|
|
|
collection = by_personal(collection)
|
|
|
|
collection = by_starred(collection)
|
|
|
|
collection = by_trending(collection)
|
2019-05-07 07:08:25 -04:00
|
|
|
collection = by_visibility_level(collection)
|
2021-05-25 14:10:42 -04:00
|
|
|
collection = by_topics(collection)
|
2022-07-04 20:09:38 -04:00
|
|
|
collection = by_topic_id(collection)
|
2018-10-12 10:10:34 -04:00
|
|
|
collection = by_search(collection)
|
|
|
|
collection = by_archived(collection)
|
|
|
|
collection = by_custom_attributes(collection)
|
|
|
|
collection = by_deleted_status(collection)
|
2022-01-27 22:15:57 -05:00
|
|
|
collection = by_not_aimed_for_deletion(collection)
|
2020-04-01 05:07:45 -04:00
|
|
|
collection = by_last_activity_after(collection)
|
|
|
|
collection = by_last_activity_before(collection)
|
2021-04-19 11:09:08 -04:00
|
|
|
by_repository_storage(collection)
|
2018-10-12 10:10:34 -04:00
|
|
|
end
|
|
|
|
|
2017-06-13 10:44:55 -04:00
|
|
|
def collection_with_user
|
|
|
|
if owned_projects?
|
|
|
|
current_user.owned_projects
|
2018-07-08 15:43:06 -04:00
|
|
|
elsif min_access_level?
|
2019-05-07 07:08:25 -04:00
|
|
|
current_user.authorized_projects(params[:min_access_level])
|
2017-05-26 10:31:37 -04:00
|
|
|
else
|
2019-11-25 01:06:14 -05:00
|
|
|
if private_only? || impossible_visibility_level?
|
2017-06-13 10:44:55 -04:00
|
|
|
current_user.authorized_projects
|
|
|
|
else
|
2019-04-09 23:36:09 -04:00
|
|
|
Project.public_or_visible_to_user(current_user)
|
2017-06-13 10:44:55 -04:00
|
|
|
end
|
2017-05-26 10:31:37 -04:00
|
|
|
end
|
2017-06-13 10:44:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Builds a collection for an anonymous user.
|
|
|
|
def collection_without_user
|
2018-07-08 15:43:06 -04:00
|
|
|
if private_only? || owned_projects? || min_access_level?
|
2017-06-13 10:44:55 -04:00
|
|
|
Project.none
|
|
|
|
else
|
|
|
|
Project.public_to_user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-25 01:06:14 -05:00
|
|
|
# This is an optimization - surprisingly PostgreSQL does not optimize
|
|
|
|
# for this.
|
|
|
|
#
|
2022-09-06 14:10:28 -04:00
|
|
|
# If the default visibility level and desired visibility level filter cancels
|
2019-11-25 01:06:14 -05:00
|
|
|
# each other out, don't use the SQL clause for visibility level in
|
2022-09-06 14:10:28 -04:00
|
|
|
# `Project.public_or_visible_to_user`. In fact, this then becomes equivalent
|
2019-11-25 01:06:14 -05:00
|
|
|
# to just authorized projects for the user.
|
|
|
|
#
|
|
|
|
# E.g.
|
|
|
|
# (EXISTS(<authorized_projects>) OR projects.visibility_level IN (10,20))
|
|
|
|
# AND "projects"."visibility_level" = 0
|
|
|
|
#
|
|
|
|
# is essentially
|
|
|
|
# EXISTS(<authorized_projects>) AND "projects"."visibility_level" = 0
|
|
|
|
#
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/issues/37007
|
|
|
|
def impossible_visibility_level?
|
|
|
|
return unless params[:visibility_level].present?
|
|
|
|
|
|
|
|
public_visibility_levels = Gitlab::VisibilityLevel.levels_for_user(current_user)
|
|
|
|
|
2021-04-06 11:09:23 -04:00
|
|
|
!public_visibility_levels.include?(params[:visibility_level].to_i)
|
2019-11-25 01:06:14 -05:00
|
|
|
end
|
|
|
|
|
2017-06-13 10:44:55 -04:00
|
|
|
def owned_projects?
|
|
|
|
params[:owned].present?
|
|
|
|
end
|
2015-11-18 06:21:06 -05:00
|
|
|
|
2017-06-13 10:44:55 -04:00
|
|
|
def private_only?
|
|
|
|
params[:non_public].present?
|
2015-11-18 06:21:06 -05:00
|
|
|
end
|
2017-03-03 05:35:04 -05:00
|
|
|
|
2018-07-08 15:43:06 -04:00
|
|
|
def min_access_level?
|
|
|
|
params[:min_access_level].present?
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-03-03 05:35:04 -05:00
|
|
|
def by_ids(items)
|
2019-11-15 01:06:13 -05:00
|
|
|
items = items.where(id: project_ids_relation) if project_ids_relation
|
2020-04-07 17:09:46 -04:00
|
|
|
items = items.where('projects.id > ?', params[:id_after]) if params[:id_after]
|
|
|
|
items = items.where('projects.id < ?', params[:id_before]) if params[:id_before]
|
2019-11-15 01:06:13 -05:00
|
|
|
items
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-03-03 05:35:04 -05:00
|
|
|
|
|
|
|
def union(items)
|
|
|
|
find_union(items, Project).with_route
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_personal(items)
|
2020-04-22 17:10:00 -04:00
|
|
|
params[:personal].present? && current_user ? items.personal(current_user) : items
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
|
|
|
|
2017-05-23 16:38:12 -04:00
|
|
|
def by_starred(items)
|
2020-04-22 17:10:00 -04:00
|
|
|
params[:starred].present? && current_user ? items.starred_by(current_user) : items
|
2017-05-23 16:38:12 -04:00
|
|
|
end
|
|
|
|
|
2017-05-26 09:39:38 -04:00
|
|
|
def by_trending(items)
|
|
|
|
params[:trending].present? ? items.trending : items
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-05-07 07:08:25 -04:00
|
|
|
def by_visibility_level(items)
|
2017-03-03 05:35:04 -05:00
|
|
|
params[:visibility_level].present? ? items.where(visibility_level: params[:visibility_level]) : items
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-03-03 05:35:04 -05:00
|
|
|
|
2021-05-25 14:10:42 -04:00
|
|
|
def by_topics(items)
|
2021-08-24 11:10:36 -04:00
|
|
|
return items unless params[:topic].present?
|
|
|
|
|
2021-09-28 05:11:19 -04:00
|
|
|
topics = params[:topic].instance_of?(String) ? params[:topic].split(',') : params[:topic]
|
|
|
|
topics.map(&:strip).uniq.reject(&:empty?).each do |topic|
|
2022-07-04 20:09:38 -04:00
|
|
|
items = items.with_topic_by_name(topic)
|
2021-08-24 11:10:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
items
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
|
|
|
|
2022-07-04 20:09:38 -04:00
|
|
|
def by_topic_id(items)
|
|
|
|
return items unless params[:topic_id].present?
|
|
|
|
|
|
|
|
topic = Projects::Topic.find_by(id: params[:topic_id]) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
return Project.none unless topic
|
|
|
|
|
|
|
|
items.with_topic(topic)
|
|
|
|
end
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
def by_search(items)
|
|
|
|
params[:search] ||= params[:name]
|
2020-12-07 16:10:08 -05:00
|
|
|
|
2021-09-15 20:11:46 -04:00
|
|
|
return items if Feature.enabled?(:disable_anonymous_project_search, type: :ops) && current_user.nil?
|
2020-12-07 16:10:08 -05:00
|
|
|
return items.none if params[:search].present? && params[:minimum_search_length].present? && params[:search].length < params[:minimum_search_length].to_i
|
|
|
|
|
2020-03-23 08:09:47 -04:00
|
|
|
items.optionally_search(params[:search], include_namespace: params[:search_namespaces].present?)
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
|
|
|
|
2018-09-12 16:52:30 -04:00
|
|
|
def by_deleted_status(items)
|
|
|
|
params[:without_deleted].present? ? items.without_deleted : items
|
|
|
|
end
|
|
|
|
|
2022-01-27 22:15:57 -05:00
|
|
|
def by_not_aimed_for_deletion(items)
|
|
|
|
params[:not_aimed_for_deletion].present? ? items.not_aimed_for_deletion : items
|
|
|
|
end
|
|
|
|
|
2020-04-01 05:07:45 -04:00
|
|
|
def by_last_activity_after(items)
|
|
|
|
if params[:last_activity_after].present?
|
|
|
|
items.where("last_activity_at > ?", params[:last_activity_after]) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_last_activity_before(items)
|
|
|
|
if params[:last_activity_before].present?
|
|
|
|
items.where("last_activity_at < ?", params[:last_activity_before]) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-19 17:08:32 -04:00
|
|
|
def by_repository_storage(items)
|
|
|
|
if params[:repository_storage].present?
|
|
|
|
items.where(repository_storage: params[:repository_storage]) # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
def sort(items)
|
2020-10-06 20:08:24 -04:00
|
|
|
if params[:sort].present?
|
|
|
|
items.sort_by_attribute(params[:sort])
|
|
|
|
else
|
|
|
|
items.projects_order_id_desc
|
|
|
|
end
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def by_archived(projects)
|
2017-08-11 05:09:17 -04:00
|
|
|
if params[:non_archived]
|
|
|
|
projects.non_archived
|
2018-07-11 14:17:18 -04:00
|
|
|
elsif params.key?(:archived)
|
2017-08-11 05:09:17 -04:00
|
|
|
if params[:archived] == 'only'
|
|
|
|
projects.archived
|
|
|
|
elsif Gitlab::Utils.to_boolean(params[:archived])
|
|
|
|
projects
|
|
|
|
else
|
|
|
|
projects.non_archived
|
|
|
|
end
|
|
|
|
else
|
|
|
|
projects
|
|
|
|
end
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
2018-07-08 15:43:06 -04:00
|
|
|
|
|
|
|
def finder_params
|
|
|
|
return {} unless min_access_level?
|
|
|
|
|
|
|
|
{ min_access_level: params[:min_access_level] }
|
|
|
|
end
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
2020-04-10 08:09:36 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
ProjectsFinder.prepend_mod_with('ProjectsFinder')
|