debian-mirror-gitlab/app/finders/issuable_finder.rb

429 lines
13 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
# IssuableFinder
2014-09-02 18:07:02 +05:30
#
# Used to filter Issues and MergeRequests collections by set of params
#
# Arguments:
# klass - actual class like Issue or MergeRequest
# current_user - which user use
# params:
2018-11-08 19:23:39 +05:30
# scope: 'created_by_me' or 'assigned_to_me' or 'all'
# state: 'opened' or 'closed' or 'locked' or 'all'
2014-09-02 18:07:02 +05:30
# group_id: integer
# project_id: integer
2015-09-11 14:41:01 +05:30
# milestone_title: string
2020-01-01 13:55:28 +05:30
# release_tag: string
2017-09-10 17:25:29 +05:30
# author_id: integer
2019-02-15 15:39:39 +05:30
# author_username: string
2018-12-13 13:39:08 +05:30
# assignee_id: integer or 'None' or 'Any'
2019-02-15 15:39:39 +05:30
# assignee_username: string
2014-09-02 18:07:02 +05:30
# search: string
2019-03-02 22:35:43 +05:30
# in: 'title', 'description', or a string joining them with comma
2014-09-02 18:07:02 +05:30
# label_name: string
# sort: string
2017-08-17 22:00:37 +05:30
# non_archived: boolean
# iids: integer[]
2018-03-17 18:26:18 +05:30
# my_reaction_emoji: string
2018-03-27 19:54:05 +05:30
# created_after: datetime
# created_before: datetime
# updated_after: datetime
# updated_before: datetime
2019-02-15 15:39:39 +05:30
# attempt_group_search_optimizations: boolean
2019-07-07 11:18:12 +05:30
# attempt_project_search_optimizations: boolean
2014-09-02 18:07:02 +05:30
#
2015-04-26 12:48:37 +05:30
class IssuableFinder
2018-03-27 19:54:05 +05:30
prepend FinderWithCrossProjectAccess
include FinderMethods
2017-09-10 17:25:29 +05:30
include CreatedAtFilter
2019-02-15 15:39:39 +05:30
include Gitlab::Utils::StrongMemoize
2017-09-10 17:25:29 +05:30
2020-04-22 19:07:51 +05:30
requires_cross_project_access unless: -> { params.project? }
2019-12-04 20:38:33 +05:30
NEGATABLE_PARAMS_HELPER_KEYS = %i[include_subgroups in].freeze
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
attr_accessor :current_user, :params
2020-04-22 19:07:51 +05:30
delegate(*%i[assignee milestones], to: :params)
2019-12-04 20:38:33 +05:30
class << self
def scalar_params
@scalar_params ||= %i[
2018-03-27 19:54:05 +05:30
assignee_id
assignee_username
author_id
author_username
label_name
milestone_title
2020-01-01 13:55:28 +05:30
release_tag
2018-03-27 19:54:05 +05:30
my_reaction_emoji
search
2019-03-02 22:35:43 +05:30
in
2018-03-27 19:54:05 +05:30
]
2019-12-04 20:38:33 +05:30
end
2018-03-27 19:54:05 +05:30
2019-12-04 20:38:33 +05:30
def array_params
@array_params ||= { label_name: [], assignee_username: [] }
end
# This should not be used in controller strong params!
def negatable_scalar_params
@negatable_scalar_params ||= scalar_params + %i[project_id group_id]
end
# This should not be used in controller strong params!
def negatable_array_params
@negatable_array_params ||= array_params.keys.append(:iids)
end
# This should not be used in controller strong params!
def negatable_params
@negatable_params ||= negatable_scalar_params + negatable_array_params
end
2018-03-27 19:54:05 +05:30
2019-12-04 20:38:33 +05:30
def valid_params
2020-03-13 15:44:24 +05:30
@valid_params ||= scalar_params + [array_params.merge(not: {})]
2019-12-04 20:38:33 +05:30
end
2018-03-27 19:54:05 +05:30
end
2020-04-22 19:07:51 +05:30
def params_class
IssuableFinder::Params
end
2017-01-15 13:20:01 +05:30
def initialize(current_user, params = {})
2014-09-02 18:07:02 +05:30
@current_user = current_user
2020-04-22 19:07:51 +05:30
@params = params_class.new(params, current_user, klass)
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
def execute
2014-09-02 18:07:02 +05:30
items = init_collection
2018-03-27 19:54:05 +05:30
items = filter_items(items)
2019-12-04 20:38:33 +05:30
# Let's see if we have to negate anything
items = by_negation(items)
2019-07-07 11:18:12 +05:30
# This has to be last as we use a CTE as an optimization fence
# for counts by passing the force_cte param and enabling the
# attempt_group_search_optimizations feature flag
2018-11-08 19:23:39 +05:30
# https://www.postgresql.org/docs/current/static/queries-with.html
items = by_search(items)
2018-03-27 19:54:05 +05:30
2019-07-07 11:18:12 +05:30
items = sort(items)
items
2018-03-27 19:54:05 +05:30
end
def filter_items(items)
2018-11-08 19:23:39 +05:30
items = by_project(items)
2019-02-15 15:39:39 +05:30
items = by_group(items)
2014-09-02 18:07:02 +05:30
items = by_scope(items)
2017-09-10 17:25:29 +05:30
items = by_created_at(items)
2018-03-27 19:54:05 +05:30
items = by_updated_at(items)
2019-07-07 11:18:12 +05:30
items = by_closed_at(items)
2014-09-02 18:07:02 +05:30
items = by_state(items)
items = by_group(items)
items = by_assignee(items)
2015-04-26 12:48:37 +05:30
items = by_author(items)
2017-08-17 22:00:37 +05:30
items = by_non_archived(items)
items = by_iids(items)
items = by_milestone(items)
2020-01-01 13:55:28 +05:30
items = by_release(items)
2017-08-17 22:00:37 +05:30
items = by_label(items)
2018-03-27 19:54:05 +05:30
by_my_reaction_emoji(items)
2017-01-15 13:20:01 +05:30
end
2018-03-17 18:26:18 +05:30
def row_count
Gitlab::IssuablesCountForState.new(self).for_state_or_opened(params[:state])
end
2017-08-17 22:00:37 +05:30
# We often get counts for each state by running a query per state, and
# counting those results. This is typically slower than running one query
# (even if that query is slower than any of the individual state queries) and
# grouping and counting within that query.
#
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def count_by_state
2019-07-07 11:18:12 +05:30
count_params = params.merge(state: nil, sort: nil, force_cte: true)
2017-08-17 22:00:37 +05:30
finder = self.class.new(current_user, count_params)
2019-07-07 11:18:12 +05:30
2017-08-17 22:00:37 +05:30
counts = Hash.new(0)
# Searching by label includes a GROUP BY in the query, but ours will be last
# because it is added last. Searching by multiple labels also includes a row
# per issuable, so we have to count those in Ruby - which is bad, but still
# better than performing multiple queries.
#
2018-11-08 19:23:39 +05:30
# This does not apply when we are using a CTE for the search, as the labels
# GROUP BY is inside the subquery in that case, so we set labels_count to 1.
2019-07-07 11:18:12 +05:30
#
# Groups and projects have separate feature flags to suggest the use
# of a CTE. The CTE will not be used if the sort doesn't support it,
# but will always be used for the counts here as we ignore sorting
# anyway.
2020-04-22 19:07:51 +05:30
labels_count = params.label_names.any? ? params.label_names.count : 1
2018-11-08 19:23:39 +05:30
labels_count = 1 if use_cte_for_search?
2019-12-21 20:55:43 +05:30
finder.execute.reorder(nil).group(:state_id).count.each do |key, value|
2018-12-05 23:21:45 +05:30
counts[count_key(key)] += value / labels_count
2017-08-17 22:00:37 +05:30
end
counts[:all] = counts.values.sum
2018-11-18 11:00:15 +05:30
counts.with_indifferent_access
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
2015-09-11 14:41:01 +05:30
def search
params[:search].presence
end
2019-02-15 15:39:39 +05:30
def use_cte_for_search?
strong_memoize(:use_cte_for_search) do
2019-07-07 11:18:12 +05:30
next false unless search
# Only simple unsorted & simple sorts can use CTE
next false if params[:sort].present? && !params[:sort].in?(klass.simple_sorts.keys)
attempt_group_search_optimizations? || attempt_project_search_optimizations?
2019-02-15 15:39:39 +05:30
end
end
2014-09-02 18:07:02 +05:30
private
2019-07-07 11:18:12 +05:30
def force_cte?
!!params[:force_cte]
end
2014-09-02 18:07:02 +05:30
def init_collection
2015-10-24 18:46:33 +05:30
klass.all
2014-09-02 18:07:02 +05:30
end
2019-02-15 15:39:39 +05:30
def attempt_group_search_optimizations?
2019-07-07 11:18:12 +05:30
params[:attempt_group_search_optimizations] &&
Feature.enabled?(:attempt_group_search_optimizations, default_enabled: true)
end
def attempt_project_search_optimizations?
params[:attempt_project_search_optimizations] &&
2019-09-04 21:01:54 +05:30
Feature.enabled?(:attempt_project_search_optimizations, default_enabled: true)
2019-02-15 15:39:39 +05:30
end
2018-12-05 23:21:45 +05:30
def count_key(value)
2019-12-26 22:10:19 +05:30
# value may be an array if the finder used in `count_by_state` added an
# additional `group by`. Anyway we are sure that state will be always the
# last item because it's added as the last one to the query.
2019-12-21 20:55:43 +05:30
value = Array(value).last
klass.available_states.key(value)
2018-12-05 23:21:45 +05:30
end
2019-12-04 20:38:33 +05:30
# Negates all params found in `negatable_params`
# rubocop: disable CodeReuse/ActiveRecord
def by_negation(items)
not_params = params[:not].dup
# API endpoints send in `nil` values so we test if there are any non-nil
return items unless not_params.present? && not_params.values.any?
not_params.keep_if { |_k, v| v.present? }.each do |(key, value)|
# These aren't negatable params themselves, but rather help other searches, so we skip them.
# They will be added into all the NOT searches.
next if NEGATABLE_PARAMS_HELPER_KEYS.include?(key.to_sym)
next unless self.class.negatable_params.include?(key.to_sym)
# These are "helper" params that are required inside the NOT to get the right results. They usually come in
# at the top-level params, but if they do come in inside the `:not` params, they should take precedence.
not_helpers = params.slice(*NEGATABLE_PARAMS_HELPER_KEYS).merge(params[:not].slice(*NEGATABLE_PARAMS_HELPER_KEYS))
2020-03-13 15:44:24 +05:30
not_param = { key => value }.with_indifferent_access.merge(not_helpers).merge(not_query: true)
2019-12-04 20:38:33 +05:30
items_to_negate = self.class.new(current_user, not_param).execute
items = items.where.not(id: items_to_negate)
end
items
end
# rubocop: enable CodeReuse/ActiveRecord
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def by_scope(items)
2020-04-22 19:07:51 +05:30
return items.none if params.current_user_related? && !current_user
2018-03-17 18:26:18 +05:30
2014-09-02 18:07:02 +05:30
case params[:scope]
2018-11-08 19:23:39 +05:30
when 'created_by_me', 'authored'
2014-09-02 18:07:02 +05:30
items.where(author_id: current_user.id)
2018-11-08 19:23:39 +05:30
when 'assigned_to_me'
2017-08-17 22:00:37 +05:30
items.assigned_to(current_user)
2014-09-02 18:07:02 +05:30
else
2016-06-02 11:05:42 +05:30
items
2014-09-02 18:07:02 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2018-03-27 19:54:05 +05:30
def by_updated_at(items)
items = items.updated_after(params[:updated_after]) if params[:updated_after].present?
items = items.updated_before(params[:updated_before]) if params[:updated_before].present?
items
end
2019-07-07 11:18:12 +05:30
def by_closed_at(items)
items = items.closed_after(params[:closed_after]) if params[:closed_after].present?
items = items.closed_before(params[:closed_before]) if params[:closed_before].present?
items
end
2014-09-02 18:07:02 +05:30
def by_state(items)
2017-01-15 13:20:01 +05:30
case params[:state].to_s
when 'closed'
items.closed
when 'merged'
items.respond_to?(:merged) ? items.merged : items.closed
when 'opened'
items.opened
2018-11-08 19:23:39 +05:30
when 'locked'
2019-12-21 20:55:43 +05:30
items.with_state(:locked)
2014-09-02 18:07:02 +05:30
else
2016-11-03 12:29:30 +05:30
items
2014-09-02 18:07:02 +05:30
end
end
def by_group(items)
2016-06-02 11:05:42 +05:30
# Selection by group is already covered by `by_project` and `projects`
2014-09-02 18:07:02 +05:30
items
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def by_project(items)
2020-04-22 19:07:51 +05:30
if params.project?
items.of_projects(params.projects).references_project
elsif params.projects
items.merge(params.projects.reorder(nil)).join_project
else
items.none
end
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def by_search(items)
2018-11-08 19:23:39 +05:30
return items unless search
2019-12-26 22:10:19 +05:30
return items if items.is_a?(ActiveRecord::NullRelation)
2018-11-08 19:23:39 +05:30
if use_cte_for_search?
cte = Gitlab::SQL::RecursiveCTE.new(klass.table_name)
cte << items
items = klass.with(cte.to_arel).from(klass.table_name)
end
2019-09-30 21:07:59 +05:30
items.full_search(search, matched_columns: params[:in], use_minimum_char_limit: !use_cte_for_search?)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def by_iids(items)
params[:iids].present? ? items.where(iid: params[:iids]) : items
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def sort(items)
2015-12-23 02:04:40 +05:30
# Ensure we always have an explicit sort order (instead of inheriting
# multiple orders when combining ActiveRecord::Relation objects).
2020-04-22 19:07:51 +05:30
params[:sort] ? items.sort_by_attribute(params[:sort], excluded_labels: params.label_names) : items.reorder(id: :desc)
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
def by_author(items)
2020-04-22 19:07:51 +05:30
if params.author
items.where(author_id: params.author.id)
elsif params.no_author?
items.where(author_id: nil)
elsif params.author_id? || params.author_username? # author not found
items.none
else
items
2014-09-02 18:07:02 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2019-07-31 22:56:46 +05:30
def by_assignee(items)
2020-04-22 19:07:51 +05:30
return items.assigned_to(params.assignees) if not_query? && params.assignees.any?
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
if params.filter_by_no_assignee?
2019-07-31 22:56:46 +05:30
items.unassigned
2020-04-22 19:07:51 +05:30
elsif params.filter_by_any_assignee?
2019-07-31 22:56:46 +05:30
items.assigned
2020-04-22 19:07:51 +05:30
elsif params.assignee
items.assigned_to(params.assignee)
elsif params.assignee_id? || params.assignee_username? # assignee not found
2019-07-31 22:56:46 +05:30
items.none
else
items
end
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-10-24 18:46:33 +05:30
def by_milestone(items)
2020-04-22 19:07:51 +05:30
return items unless params.milestones?
if params.filter_by_no_milestone?
items.left_joins_milestones.where(milestone_id: [-1, nil])
elsif params.filter_by_any_milestone?
items.any_milestone
elsif params.filter_by_upcoming_milestone?
upcoming_ids = Milestone.upcoming_ids(params.projects, params.related_groups)
items.left_joins_milestones.where(milestone_id: upcoming_ids)
elsif params.filter_by_started_milestone?
items.left_joins_milestones.merge(Milestone.started)
else
items.with_milestone(params[:milestone_title])
2015-10-24 18:46:33 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2015-10-24 18:46:33 +05:30
2020-01-01 13:55:28 +05:30
def by_release(items)
2020-04-22 19:07:51 +05:30
return items unless params.releases?
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
if params.filter_by_no_release?
2020-01-01 13:55:28 +05:30
items.without_release
2020-04-22 19:07:51 +05:30
elsif params.filter_by_any_release?
2020-01-01 13:55:28 +05:30
items.any_release
else
items.with_release(params[:release_tag], params[:project_id])
end
end
2014-09-02 18:07:02 +05:30
def by_label(items)
2020-04-22 19:07:51 +05:30
return items unless params.labels?
2018-03-17 18:26:18 +05:30
2020-04-22 19:07:51 +05:30
if params.filter_by_no_label?
items.without_label
elsif params.filter_by_any_label?
items.any_label
else
items.with_label(params.label_names, params[:sort], not_query: not_query?)
2014-09-02 18:07:02 +05:30
end
2018-12-13 13:39:08 +05:30
end
2020-04-22 19:07:51 +05:30
def by_my_reaction_emoji(items)
return items unless params[:my_reaction_emoji] && current_user
2018-12-13 13:39:08 +05:30
2020-04-22 19:07:51 +05:30
if params.filter_by_no_reaction?
items.not_awarded(current_user)
elsif params.filter_by_any_reaction?
items.awarded(current_user)
else
2020-04-22 19:07:51 +05:30
items.awarded(current_user, params[:my_reaction_emoji])
end
2016-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
def by_non_archived(items)
params[:non_archived].present? ? items.non_archived : items
end
2020-03-13 15:44:24 +05:30
def not_query?
!!params[:not_query]
end
2014-09-02 18:07:02 +05:30
end