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-05-07 07:08:25 -04:00
|
|
|
COUNT_LIMIT = 1001
|
|
|
|
|
2018-01-31 09:59:59 -05:00
|
|
|
attr_reader :current_user, :query, :per_page
|
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
|
|
|
|
|
2018-01-31 09:59:59 -05:00
|
|
|
def initialize(current_user, limit_projects, query, default_project_filter: false, per_page: 20)
|
2016-03-17 16:48:19 -04:00
|
|
|
@current_user = current_user
|
2016-03-01 11:56:14 -05:00
|
|
|
@limit_projects = limit_projects || Project.all
|
2017-11-24 05:23:14 -05:00
|
|
|
@query = query
|
2017-12-15 05:21:12 -05:00
|
|
|
@default_project_filter = default_project_filter
|
2018-01-31 09:59:59 -05:00
|
|
|
@per_page = per_page
|
2014-08-26 16:32:41 -04:00
|
|
|
end
|
|
|
|
|
2018-01-23 06:03:15 -05:00
|
|
|
def objects(scope, page = nil, without_count = true)
|
|
|
|
collection = case scope
|
|
|
|
when 'projects'
|
2019-05-07 07:08:25 -04:00
|
|
|
projects
|
2018-01-23 06:03:15 -05:00
|
|
|
when 'issues'
|
2019-05-07 07:08:25 -04:00
|
|
|
issues
|
2018-01-23 06:03:15 -05:00
|
|
|
when 'merge_requests'
|
2019-05-07 07:08:25 -04:00
|
|
|
merge_requests
|
2018-01-23 06:03:15 -05:00
|
|
|
when 'milestones'
|
2019-05-07 07:08:25 -04:00
|
|
|
milestones
|
2018-08-14 09:43:46 -04:00
|
|
|
when 'users'
|
2019-05-07 07:08:25 -04:00
|
|
|
users
|
2018-01-23 06:03:15 -05:00
|
|
|
else
|
2019-05-07 07:08:25 -04:00
|
|
|
Kaminari.paginate_array([])
|
|
|
|
end.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
|
|
|
|
|
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
|
|
|
|
|
2017-01-10 23:20:32 -05:00
|
|
|
def single_commit_result?
|
|
|
|
false
|
|
|
|
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
|
|
|
|
|
2014-08-26 16:32:41 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
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
|
2019-05-07 07:08:25 -04:00
|
|
|
issues = issues.where(project_id: project_ids_relation) # rubocop: disable CodeReuse/ActiveRecord
|
2017-12-22 10:44:53 -05:00
|
|
|
end
|
2016-03-01 11:56:14 -05:00
|
|
|
|
2019-05-07 07:08:25 -04:00
|
|
|
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
|
|
|
|
merge_requests = merge_requests.in_projects(project_ids_relation)
|
|
|
|
end
|
|
|
|
|
2019-05-07 07:08:25 -04:00
|
|
|
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 =
|
|
|
|
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-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/
|
|
|
|
params[:iids] = $1
|
|
|
|
else
|
|
|
|
params[:search] = query
|
|
|
|
end
|
|
|
|
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
|