debian-mirror-gitlab/lib/gitlab/search_results.rb

240 lines
6.8 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module Gitlab
class SearchResults
2019-12-21 20:55:43 +05:30
COUNT_LIMIT = 100
2019-12-04 20:38:33 +05:30
COUNT_LIMIT_MESSAGE = "#{COUNT_LIMIT - 1}+"
2020-05-24 23:13:21 +05:30
DEFAULT_PAGE = 1
DEFAULT_PER_PAGE = 20
2019-07-07 11:18:12 +05:30
2021-01-29 00:20:46 +05:30
attr_reader :current_user, :query, :order_by, :sort, :filters
2015-04-26 12:48:37 +05:30
2016-06-02 11:05:42 +05:30
# Limit search results by passed projects
2015-04-26 12:48:37 +05:30
# It allows us to search only for projects user has access to
2016-06-02 11:05:42 +05:30
attr_reader :limit_projects
2015-04-26 12:48:37 +05:30
2018-03-17 18:26:18 +05:30
# 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
2021-01-29 00:20:46 +05:30
def initialize(current_user, query, limit_projects = nil, order_by: nil, sort: nil, default_project_filter: false, filters: {})
2016-06-02 11:05:42 +05:30
@current_user = current_user
2018-03-17 18:26:18 +05:30
@query = query
2020-11-24 15:15:51 +05:30
@limit_projects = limit_projects || Project.all
2018-03-17 18:26:18 +05:30
@default_project_filter = default_project_filter
2021-01-29 00:20:46 +05:30
@order_by = order_by
2021-01-03 14:25:43 +05:30
@sort = sort
2020-11-24 15:15:51 +05:30
@filters = filters
2018-03-17 18:26:18 +05:30
end
2020-06-23 00:09:42 +05:30
def objects(scope, page: nil, per_page: DEFAULT_PER_PAGE, without_count: true, preload_method: nil)
2020-07-28 23:09:34 +05:30
should_preload = preload_method.present?
2021-01-03 14:25:43 +05:30
collection = collection_for(scope)
if collection.nil?
should_preload = false
collection = Kaminari.paginate_array([])
end
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
collection = collection.public_send(preload_method) if should_preload # rubocop:disable GitlabSecurity/PublicSend
2020-05-24 23:13:21 +05:30
collection = collection.page(page).per(per_page)
2018-03-17 18:26:18 +05:30
without_count ? collection.without_count : collection
2015-04-26 12:48:37 +05:30
end
2019-10-12 21:52:04 +05:30
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-12-04 20:38:33 +05:30
COUNT_LIMIT_MESSAGE
2019-10-12 21:52:04 +05:30
else
count.to_s
end
end
2018-03-17 18:26:18 +05:30
def limited_projects_count
2019-07-07 11:18:12 +05:30
@limited_projects_count ||= limited_count(projects)
2018-03-17 18:26:18 +05:30
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-07-07 11:18:12 +05:30
sum = limited_count(issues(public_only: true))
@limited_issues_count = sum < count_limit ? limited_count(issues) : sum
2018-03-17 18:26:18 +05:30
end
def limited_merge_requests_count
2019-07-07 11:18:12 +05:30
@limited_merge_requests_count ||= limited_count(merge_requests)
2018-03-17 18:26:18 +05:30
end
def limited_milestones_count
2019-07-07 11:18:12 +05:30
@limited_milestones_count ||= limited_count(milestones)
end
def limited_users_count
@limited_users_count ||= limited_count(users)
2018-03-17 18:26:18 +05:30
end
def count_limit
2019-07-07 11:18:12 +05:30
COUNT_LIMIT
end
def users
return User.none unless Ability.allowed?(current_user, :read_users_list)
UsersFinder.new(current_user, search: query).execute
2018-03-17 18:26:18 +05:30
end
2021-01-03 14:25:43 +05:30
# highlighting is only performed by Elasticsearch backed results
def highlight_map(scope)
{}
end
2015-04-26 12:48:37 +05:30
private
2021-01-03 14:25:43 +05:30
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
# rubocop: disable CodeReuse/ActiveRecord
def apply_sort(scope)
2021-01-29 00:20:46 +05:30
# Due to different uses of sort param we prefer order_by when
# present
case ::Gitlab::Search::SortOptions.sort_and_direction(order_by, sort)
when :created_at_asc
2021-01-03 14:25:43 +05:30
scope.reorder('created_at ASC')
2021-01-29 00:20:46 +05:30
when :created_at_desc
2021-01-03 14:25:43 +05:30
scope.reorder('created_at DESC')
2021-03-11 19:13:27 +05:30
when :updated_at_asc
scope.reorder('updated_at ASC')
when :updated_at_desc
scope.reorder('updated_at DESC')
2021-01-03 14:25:43 +05:30
else
2021-01-29 00:20:46 +05:30
scope.reorder('created_at DESC')
2021-01-03 14:25:43 +05:30
end
end
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
def projects
2016-06-02 11:05:42 +05:30
limit_projects.search(query)
2015-04-26 12:48:37 +05:30
end
2018-03-17 18:26:18 +05:30
def issues(finder_params = {})
2019-07-07 11:18:12 +05:30
issues = IssuesFinder.new(current_user, issuable_params.merge(finder_params)).execute
2018-03-17 18:26:18 +05:30
unless default_project_filter
2019-07-07 11:18:12 +05:30
issues = issues.where(project_id: project_ids_relation) # rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
end
2016-06-02 11:05:42 +05:30
2021-01-03 14:25:43 +05:30
apply_sort(issues)
2015-04-26 12:48:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-09-25 12:07:36 +05:30
def milestones
2019-06-05 12:25:43 +05:30
milestones = Milestone.search(query)
milestones = filter_milestones_by_project(milestones)
2018-03-17 18:26:18 +05:30
milestones.reorder('updated_at DESC')
2015-09-25 12:07:36 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-09-25 12:07:36 +05:30
2015-04-26 12:48:37 +05:30
def merge_requests
2019-07-07 11:18:12 +05:30
merge_requests = MergeRequestsFinder.new(current_user, issuable_params).execute
2018-03-17 18:26:18 +05:30
unless default_project_filter
merge_requests = merge_requests.in_projects(project_ids_relation)
end
2021-01-03 14:25:43 +05:30
apply_sort(merge_requests)
2015-04-26 12:48:37 +05:30
end
def default_scope
'projects'
end
2019-06-05 12:25:43 +05:30
# 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-10-31 01:37:42 +05:30
Project.where(id: project_ids).ids_with_issuables_available_for(current_user)
2019-06-05 12:25:43 +05:30
milestones.where(project_id: authorized_project_ids_relation)
end
# rubocop: enable CodeReuse/ActiveRecord
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-06-02 11:05:42 +05:30
def project_ids_relation
limit_projects.select(:id).reorder(nil)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-07-07 11:18:12 +05:30
def issuable_params
{}.tap do |params|
params[:sort] = 'updated_desc'
if query =~ /#(\d+)\z/
2020-11-24 15:15:51 +05:30
params[:iids] = Regexp.last_match(1)
2019-07-07 11:18:12 +05:30
else
params[:search] = query
end
2020-11-24 15:15:51 +05:30
params[:state] = filters[:state] if filters.key?(:state)
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
if [true, false].include?(filters[:confidential])
2021-01-03 14:25:43 +05:30
params[:confidential] = filters[:confidential]
end
2019-07-07 11:18:12 +05:30
end
end
# rubocop: disable CodeReuse/ActiveRecord
def limited_count(relation)
relation.reorder(nil).limit(count_limit).size
end
# rubocop: enable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
end
end
2020-04-22 19:07:51 +05:30
2021-06-08 01:23:25 +05:30
Gitlab::SearchResults.prepend_mod_with('Gitlab::SearchResults')