2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-08-26 16:32:41 -04:00
|
|
|
module Gitlab
|
|
|
|
class SearchResults
|
2019-10-01 17:06:09 -04:00
|
|
|
COUNT_LIMIT = 100
|
2019-09-09 09:46:24 -04:00
|
|
|
COUNT_LIMIT_MESSAGE = "#{COUNT_LIMIT - 1}+"
|
2020-05-07 17:09:26 -04:00
|
|
|
DEFAULT_PAGE = 1
|
|
|
|
DEFAULT_PER_PAGE = 20
|
2019-05-07 07:08:25 -04:00
|
|
|
|
2021-07-14 08:09:23 -04:00
|
|
|
SCOPE_ONLY_SORT = {
|
|
|
|
popularity_asc: %w[issues],
|
|
|
|
popularity_desc: %w[issues]
|
|
|
|
}.freeze
|
|
|
|
|
2020-11-04 19:09:16 -05:00
|
|
|
attr_reader :current_user, :query, :order_by, :sort, :filters
|
2014-08-26 16:32:41 -04:00
|
|
|
|
2016-03-01 11:56:14 -05:00
|
|
|
# Limit search results by passed projects
|
2014-08-26 16:32:41 -04:00
|
|
|
# It allows us to search only for projects user has access to
|
2016-03-01 11:56:14 -05:00
|
|
|
attr_reader :limit_projects
|
2014-08-26 16:32:41 -04:00
|
|
|
|
2017-12-15 05:21:12 -05:00
|
|
|
# Whether a custom filter is used to restrict scope of projects.
|
|
|
|
# If the default filter (which lists all projects user has access to)
|
|
|
|
# is used, we can skip it when filtering merge requests and optimize the
|
|
|
|
# query
|
|
|
|
attr_reader :default_project_filter
|
|
|
|
|
2020-11-04 19:09:16 -05:00
|
|
|
def initialize(current_user, query, limit_projects = nil, order_by: nil, sort: nil, default_project_filter: false, filters: {})
|
2016-03-17 16:48:19 -04:00
|
|
|
@current_user = current_user
|
2017-11-24 05:23:14 -05:00
|
|
|
@query = query
|
2020-08-20 23:10:16 -04:00
|
|
|
@limit_projects = limit_projects || Project.all
|
2017-12-15 05:21:12 -05:00
|
|
|
@default_project_filter = default_project_filter
|
2020-11-04 19:09:16 -05:00
|
|
|
@order_by = order_by
|
2020-09-30 23:09:55 -04:00
|
|
|
@sort = sort
|
2020-09-03 17:08:18 -04:00
|
|
|
@filters = filters
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
2020-05-20 02:08:06 -04:00
|
|
|
def objects(scope, page: nil, per_page: DEFAULT_PER_PAGE, without_count: true, preload_method: nil)
|
2020-07-10 02:09:23 -04:00
|
|
|
should_preload = preload_method.present?
|
2020-09-29 23:09:46 -04:00
|
|
|
collection = collection_for(scope)
|
|
|
|
|
|
|
|
if collection.nil?
|
|
|
|
should_preload = false
|
|
|
|
collection = Kaminari.paginate_array([])
|
|
|
|
end
|
2020-05-07 17:09:26 -04:00
|
|
|
|
2020-07-10 02:09:23 -04:00
|
|
|
collection = collection.public_send(preload_method) if should_preload # rubocop:disable GitlabSecurity/PublicSend
|
2020-05-07 17:09:26 -04:00
|
|
|
collection = collection.page(page).per(per_page)
|
2018-01-23 06:03:15 -05:00
|
|
|
|
|
|
|
without_count ? collection.without_count : collection
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
2019-07-15 13:59:57 -04:00
|
|
|
def formatted_count(scope)
|
|
|
|
case scope
|
|
|
|
when 'projects'
|
|
|
|
formatted_limited_count(limited_projects_count)
|
|
|
|
when 'issues'
|
|
|
|
formatted_limited_count(limited_issues_count)
|
|
|
|
when 'merge_requests'
|
|
|
|
formatted_limited_count(limited_merge_requests_count)
|
|
|
|
when 'milestones'
|
|
|
|
formatted_limited_count(limited_milestones_count)
|
|
|
|
when 'users'
|
|
|
|
formatted_limited_count(limited_users_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_limited_count(count)
|
|
|
|
if count >= COUNT_LIMIT
|
2019-09-09 09:46:24 -04:00
|
|
|
COUNT_LIMIT_MESSAGE
|
2019-07-15 13:59:57 -04:00
|
|
|
else
|
|
|
|
count.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-23 06:03:15 -05:00
|
|
|
def limited_projects_count
|
2019-05-07 07:08:25 -04:00
|
|
|
@limited_projects_count ||= limited_count(projects)
|
2018-01-23 06:03:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def limited_issues_count
|
|
|
|
return @limited_issues_count if @limited_issues_count
|
|
|
|
|
|
|
|
# By default getting limited count (e.g. 1000+) is fast on issuable
|
|
|
|
# collections except for issues, where filtering both not confidential
|
|
|
|
# and confidential issues user has access to, is too complex.
|
|
|
|
# It's faster to try to fetch all public issues first, then only
|
|
|
|
# if necessary try to fetch all issues.
|
2019-05-07 07:08:25 -04:00
|
|
|
sum = limited_count(issues(public_only: true))
|
|
|
|
@limited_issues_count = sum < count_limit ? limited_count(issues) : sum
|
2018-01-23 06:03:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def limited_merge_requests_count
|
2019-05-07 07:08:25 -04:00
|
|
|
@limited_merge_requests_count ||= limited_count(merge_requests)
|
2018-01-23 06:03:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def limited_milestones_count
|
2019-05-07 07:08:25 -04:00
|
|
|
@limited_milestones_count ||= limited_count(milestones)
|
2018-01-23 06:03:15 -05:00
|
|
|
end
|
|
|
|
|
2018-08-14 09:43:46 -04:00
|
|
|
def limited_users_count
|
2019-05-07 07:08:25 -04:00
|
|
|
@limited_users_count ||= limited_count(users)
|
2018-08-14 09:43:46 -04:00
|
|
|
end
|
|
|
|
|
2018-01-23 06:03:15 -05:00
|
|
|
def count_limit
|
2019-05-07 07:08:25 -04:00
|
|
|
COUNT_LIMIT
|
2018-01-23 06:03:15 -05:00
|
|
|
end
|
|
|
|
|
2019-01-10 13:10:45 -05:00
|
|
|
def users
|
|
|
|
return User.none unless Ability.allowed?(current_user, :read_users_list)
|
|
|
|
|
|
|
|
UsersFinder.new(current_user, search: query).execute
|
|
|
|
end
|
|
|
|
|
2020-10-08 11:08:17 -04:00
|
|
|
# highlighting is only performed by Elasticsearch backed results
|
|
|
|
def highlight_map(scope)
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2014-08-26 16:32:41 -04:00
|
|
|
private
|
|
|
|
|
2020-09-29 23:09:46 -04:00
|
|
|
def collection_for(scope)
|
|
|
|
case scope
|
|
|
|
when 'projects'
|
|
|
|
projects
|
|
|
|
when 'issues'
|
|
|
|
issues
|
|
|
|
when 'merge_requests'
|
|
|
|
merge_requests
|
|
|
|
when 'milestones'
|
|
|
|
milestones
|
|
|
|
when 'users'
|
|
|
|
users
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-30 23:09:55 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-07-14 08:09:23 -04:00
|
|
|
def apply_sort(results, scope: nil)
|
2020-11-04 19:09:16 -05:00
|
|
|
# Due to different uses of sort param we prefer order_by when
|
|
|
|
# present
|
2021-07-14 08:09:23 -04:00
|
|
|
sort_by = ::Gitlab::Search::SortOptions.sort_and_direction(order_by, sort)
|
|
|
|
|
|
|
|
# Reset sort to default if the chosen one is not supported by scope
|
|
|
|
sort_by = nil if SCOPE_ONLY_SORT[sort_by] && !SCOPE_ONLY_SORT[sort_by].include?(scope)
|
|
|
|
|
|
|
|
case sort_by
|
2020-11-04 19:09:16 -05:00
|
|
|
when :created_at_asc
|
2021-07-14 08:09:23 -04:00
|
|
|
results.reorder('created_at ASC')
|
2020-11-04 19:09:16 -05:00
|
|
|
when :created_at_desc
|
2021-07-14 08:09:23 -04:00
|
|
|
results.reorder('created_at DESC')
|
2021-02-10 07:09:45 -05:00
|
|
|
when :updated_at_asc
|
2021-07-14 08:09:23 -04:00
|
|
|
results.reorder('updated_at ASC')
|
2021-02-10 07:09:45 -05:00
|
|
|
when :updated_at_desc
|
2021-07-14 08:09:23 -04:00
|
|
|
results.reorder('updated_at DESC')
|
|
|
|
when :popularity_asc
|
|
|
|
results.reorder('upvotes_count ASC')
|
|
|
|
when :popularity_desc
|
|
|
|
results.reorder('upvotes_count DESC')
|
2020-09-30 23:09:55 -04:00
|
|
|
else
|
2021-07-14 08:09:23 -04:00
|
|
|
results.reorder('created_at DESC')
|
2020-09-30 23:09:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2014-08-26 16:32:41 -04:00
|
|
|
def projects
|
2016-03-01 11:56:14 -05:00
|
|
|
limit_projects.search(query)
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
2018-01-23 06:03:15 -05:00
|
|
|
def issues(finder_params = {})
|
2019-05-07 07:08:25 -04:00
|
|
|
issues = IssuesFinder.new(current_user, issuable_params.merge(finder_params)).execute
|
|
|
|
|
2017-12-22 10:44:53 -05:00
|
|
|
unless default_project_filter
|
2021-08-11 17:10:33 -04:00
|
|
|
issues = issues.in_projects(project_ids_relation)
|
2017-12-22 10:44:53 -05:00
|
|
|
end
|
2016-03-01 11:56:14 -05:00
|
|
|
|
2021-07-14 08:09:23 -04:00
|
|
|
apply_sort(issues, scope: 'issues')
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2015-08-13 10:48:21 -04:00
|
|
|
def milestones
|
2019-05-20 10:08:31 -04:00
|
|
|
milestones = Milestone.search(query)
|
|
|
|
|
|
|
|
milestones = filter_milestones_by_project(milestones)
|
|
|
|
|
|
|
|
milestones.reorder('updated_at DESC')
|
2015-08-13 10:48:21 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-08-13 10:48:21 -04:00
|
|
|
|
2014-08-26 16:32:41 -04:00
|
|
|
def merge_requests
|
2019-05-07 07:08:25 -04:00
|
|
|
merge_requests = MergeRequestsFinder.new(current_user, issuable_params).execute
|
|
|
|
|
2017-12-15 05:21:12 -05:00
|
|
|
unless default_project_filter
|
2021-08-19 17:10:32 -04:00
|
|
|
merge_requests = merge_requests.of_projects(project_ids_relation)
|
2017-12-15 05:21:12 -05:00
|
|
|
end
|
|
|
|
|
2021-07-14 08:09:23 -04:00
|
|
|
apply_sort(merge_requests, scope: 'merge_requests')
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_scope
|
|
|
|
'projects'
|
|
|
|
end
|
|
|
|
|
2019-05-20 10:08:31 -04:00
|
|
|
# Filter milestones by authorized projects.
|
|
|
|
# For performance reasons project_id is being plucked
|
|
|
|
# to be used on a smaller query.
|
|
|
|
#
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def filter_milestones_by_project(milestones)
|
|
|
|
project_ids =
|
|
|
|
milestones.where(project_id: project_ids_relation)
|
|
|
|
.select(:project_id).distinct
|
|
|
|
.pluck(:project_id)
|
|
|
|
|
|
|
|
return Milestone.none if project_ids.nil?
|
|
|
|
|
|
|
|
authorized_project_ids_relation =
|
2019-09-12 12:14:43 -04:00
|
|
|
Project.where(id: project_ids).ids_with_issuables_available_for(current_user)
|
2019-05-20 10:08:31 -04:00
|
|
|
|
|
|
|
milestones.where(project_id: authorized_project_ids_relation)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-03-01 11:56:14 -05:00
|
|
|
def project_ids_relation
|
2016-03-03 12:35:17 -05:00
|
|
|
limit_projects.select(:id).reorder(nil)
|
2016-03-01 11:56:14 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2019-05-07 07:08:25 -04:00
|
|
|
|
|
|
|
def issuable_params
|
|
|
|
{}.tap do |params|
|
|
|
|
params[:sort] = 'updated_desc'
|
|
|
|
|
|
|
|
if query =~ /#(\d+)\z/
|
2020-09-03 05:08:20 -04:00
|
|
|
params[:iids] = Regexp.last_match(1)
|
2019-05-07 07:08:25 -04:00
|
|
|
else
|
|
|
|
params[:search] = query
|
|
|
|
end
|
2020-09-03 17:08:18 -04:00
|
|
|
|
|
|
|
params[:state] = filters[:state] if filters.key?(:state)
|
2020-09-25 05:09:40 -04:00
|
|
|
|
2020-10-22 23:08:37 -04:00
|
|
|
if [true, false].include?(filters[:confidential])
|
2020-10-15 08:09:06 -04:00
|
|
|
params[:confidential] = filters[:confidential]
|
2020-09-25 05:09:40 -04:00
|
|
|
end
|
2019-05-07 07:08:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def limited_count(relation)
|
|
|
|
relation.reorder(nil).limit(count_limit).size
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Gitlab::SearchResults.prepend_mod_with('Gitlab::SearchResults')
|