2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
module Gitlab
|
|
|
|
class SearchResults
|
2018-03-17 18:26:18 +05:30
|
|
|
attr_reader :current_user, :query, :per_page
|
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
|
|
|
|
|
|
|
|
def initialize(current_user, limit_projects, query, default_project_filter: false, per_page: 20)
|
2016-06-02 11:05:42 +05:30
|
|
|
@current_user = current_user
|
|
|
|
@limit_projects = limit_projects || Project.all
|
2018-03-17 18:26:18 +05:30
|
|
|
@query = query
|
|
|
|
@default_project_filter = default_project_filter
|
|
|
|
@per_page = per_page
|
|
|
|
end
|
|
|
|
|
|
|
|
def objects(scope, page = nil, without_count = true)
|
|
|
|
collection = case scope
|
|
|
|
when 'projects'
|
|
|
|
projects.page(page).per(per_page)
|
|
|
|
when 'issues'
|
|
|
|
issues.page(page).per(per_page)
|
|
|
|
when 'merge_requests'
|
|
|
|
merge_requests.page(page).per(per_page)
|
|
|
|
when 'milestones'
|
|
|
|
milestones.page(page).per(per_page)
|
2019-05-18 00:54:41 +05:30
|
|
|
when 'users'
|
|
|
|
users.page(page).per(per_page)
|
2018-03-17 18:26:18 +05:30
|
|
|
else
|
|
|
|
Kaminari.paginate_array([]).page(page).per(per_page)
|
|
|
|
end
|
|
|
|
|
|
|
|
without_count ? collection.without_count : collection
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def limited_projects_count
|
|
|
|
@limited_projects_count ||= projects.limit(count_limit).count
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
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.
|
|
|
|
sum = issues(public_only: true).limit(count_limit).count
|
|
|
|
@limited_issues_count = sum < count_limit ? issues.limit(count_limit).count : sum
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def limited_merge_requests_count
|
|
|
|
@limited_merge_requests_count ||= merge_requests.limit(count_limit).count
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def limited_milestones_count
|
|
|
|
@limited_milestones_count ||= milestones.limit(count_limit).count
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
# rubocop:disable CodeReuse/ActiveRecord
|
|
|
|
def limited_users_count
|
|
|
|
@limited_users_count ||= users.limit(count_limit).count
|
|
|
|
end
|
|
|
|
# rubocop:enable CodeReuse/ActiveRecord
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def single_commit_result?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def count_limit
|
|
|
|
1001
|
|
|
|
end
|
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
def users
|
|
|
|
return User.none unless Ability.allowed?(current_user, :read_users_list)
|
|
|
|
|
|
|
|
UsersFinder.new(current_user, search: query).execute
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def projects
|
2016-06-02 11:05:42 +05:30
|
|
|
limit_projects.search(query)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def issues(finder_params = {})
|
|
|
|
issues = IssuesFinder.new(current_user, finder_params).execute
|
|
|
|
unless default_project_filter
|
|
|
|
issues = issues.where(project_id: project_ids_relation)
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
issues =
|
|
|
|
if query =~ /#(\d+)\z/
|
|
|
|
issues.where(iid: $1)
|
|
|
|
else
|
|
|
|
issues.full_search(query)
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
issues.reorder('updated_at DESC')
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
|
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
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
def merge_requests
|
2018-03-17 18:26:18 +05:30
|
|
|
merge_requests = MergeRequestsFinder.new(current_user).execute
|
|
|
|
unless default_project_filter
|
|
|
|
merge_requests = merge_requests.in_projects(project_ids_relation)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
merge_requests =
|
|
|
|
if query =~ /[#!](\d+)\z/
|
|
|
|
merge_requests.where(iid: $1)
|
|
|
|
else
|
|
|
|
merge_requests.full_search(query)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
merge_requests.reorder('updated_at DESC')
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
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 =
|
|
|
|
Project.where(id: project_ids).ids_with_milestone_available_for(current_user)
|
|
|
|
|
|
|
|
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
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|