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

499 lines
14 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
#
2021-03-08 18:12:59 +05:30
# Note: This class is NOT meant to be instantiated. Instead you should
# look at IssuesFinder or EpicsFinder, which inherit from this.
#
2014-09-02 18:07:02 +05:30
# 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
2020-11-24 15:15:51 +05:30
prepend OptimizedIssuableLabelFilter
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
2020-05-24 23:13:21 +05:30
NEGATABLE_PARAMS_HELPER_KEYS = %i[project_id scope status include_subgroups].freeze
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
attr_accessor :current_user, :params
2021-04-17 20:07:23 +05:30
attr_reader :original_params
2020-11-24 15:15:51 +05:30
attr_writer :parent
2014-09-02 18:07:02 +05:30
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
2020-05-24 23:13:21 +05:30
@negatable_scalar_params ||= scalar_params - %i[search in]
2019-12-04 20:38:33 +05:30
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
2021-04-17 20:07:23 +05:30
@valid_params ||= scalar_params + [array_params.merge(or: {}, 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
2021-03-08 18:12:59 +05:30
def klass
raise NotImplementedError
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
2021-04-17 20:07:23 +05:30
@original_params = params
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
2021-01-03 14:25:43 +05:30
items = filter_negated_items(items) if should_filter_negated_args?
2019-12-04 20:38:33 +05:30
2019-07-07 11:18:12 +05:30
# This has to be last as we use a CTE as an optimization fence
2020-11-24 15:15:51 +05:30
# for counts by passing the force_cte param and passing the
# attempt_group_search_optimizations param
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
2021-04-29 21:17:54 +05:30
sort(items)
2018-03-27 19:54:05 +05:30
end
def filter_items(items)
2021-04-29 21:17:54 +05:30
# Selection by group is already covered by `by_project` and `projects` for project-based issuables
# Group-based issuables have their own group filter methods
2018-11-08 19:23:39 +05:30
items = by_project(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_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
2021-01-03 14:25:43 +05:30
def should_filter_negated_args?
2020-05-24 23:13:21 +05:30
# API endpoints send in `nil` values so we test if there are any non-nil
2021-01-03 14:25:43 +05:30
not_params.present? && not_params.values.any?
end
2020-05-24 23:13:21 +05:30
2021-01-03 14:25:43 +05:30
# Negates all params found in `negatable_params`
def filter_negated_items(items)
2020-05-24 23:13:21 +05:30
items = by_negated_assignee(items)
items = by_negated_label(items)
items = by_negated_milestone(items)
items = by_negated_release(items)
items = by_negated_my_reaction_emoji(items)
by_negated_iids(items)
end
2018-03-17 18:26:18 +05:30
def row_count
2021-01-03 14:25:43 +05:30
Gitlab::IssuablesCountForState
2021-01-29 00:20:46 +05:30
.new(self, nil, fast_fail: true)
2021-01-03 14:25:43 +05:30
.for_state_or_opened(params[:state])
2018-03-17 18:26:18 +05:30
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
2020-11-24 15:15:51 +05:30
def parent_param=(obj)
@parent = obj
params[parent_param] = parent if parent
end
def parent_param
case parent
when Project
:project_id
when Group
:group_id
else
raise "Unexpected parent: #{parent.class}"
end
end
2014-09-02 18:07:02 +05:30
private
2020-11-24 15:15:51 +05:30
attr_reader :parent
2020-05-24 23:13:21 +05:30
def not_params
strong_memoize(:not_params) do
params_class.new(params[:not].dup, current_user, klass).tap do |not_params|
next unless not_params.present?
# These are "helper" params that modify the results, like :in and :search. They usually come in at the top-level
# params, but if they do come in inside the `:not` params, the inner ones should take precedence.
2021-04-29 21:17:54 +05:30
not_helpers = params.slice(*NEGATABLE_PARAMS_HELPER_KEYS).merge(params[:not].to_h.slice(*NEGATABLE_PARAMS_HELPER_KEYS))
2020-05-24 23:13:21 +05:30
not_helpers.each do |key, value|
not_params[key] = value unless not_params[key].present?
end
end
end
end
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?
2020-11-24 15:15:51 +05:30
params[:attempt_group_search_optimizations]
2019-07-07 11:18:12 +05:30
end
def attempt_project_search_optimizations?
2020-11-24 15:15:51 +05:30
params[:attempt_project_search_optimizations]
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
# 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
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?
2021-06-08 01:23:25 +05:30
cte = Gitlab::SQL::CTE.new(klass.table_name, items)
2018-11-08 19:23:39 +05:30
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
2020-05-24 23:13:21 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def by_negated_iids(items)
not_params[:iids].present? ? items.where.not(iid: not_params[:iids]) : 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 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
2015-04-26 12:48:37 +05:30
def by_author(items)
2021-04-17 20:07:23 +05:30
Issuables::AuthorFilter.new(
items,
params: original_params,
2021-06-08 01:23:25 +05:30
or_filters_enabled: or_filters_enabled?
2021-04-17 20:07:23 +05:30
).filter
2020-05-24 23:13:21 +05:30
end
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
def by_assignee(items)
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
2020-05-24 23:13:21 +05:30
def by_negated_assignee(items)
# We want CE users to be able to say "Issues not assigned to either PersonA nor PersonB"
if not_params.assignees.present?
items.not_assigned_to(not_params.assignees)
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-05-24 23:13:21 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def by_negated_milestone(items)
return items unless not_params.milestones?
if not_params.filter_by_upcoming_milestone?
items.joins(:milestone).merge(Milestone.not_upcoming)
elsif not_params.filter_by_started_milestone?
items.joins(:milestone).merge(Milestone.not_started)
else
items.without_particular_milestone(not_params[:milestone_title])
end
end
# rubocop: enable CodeReuse/ActiveRecord
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?
2021-03-08 18:12:59 +05:30
return items if params.group? # don't allow release filtering at group level
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
2020-05-24 23:13:21 +05:30
def by_negated_release(items)
return items unless not_params.releases?
items.without_particular_release(not_params[:release_tag], not_params[:project_id])
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?
2020-06-23 00:09:42 +05:30
items.any_label(params[:sort])
2020-04-22 19:07:51 +05:30
else
2020-05-24 23:13:21 +05:30
items.with_label(params.label_names, params[:sort])
2014-09-02 18:07:02 +05:30
end
2018-12-13 13:39:08 +05:30
end
2020-05-24 23:13:21 +05:30
def by_negated_label(items)
return items unless not_params.labels?
items.without_particular_labels(not_params.label_names)
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
2020-05-24 23:13:21 +05:30
def by_negated_my_reaction_emoji(items)
return items unless not_params[:my_reaction_emoji] && current_user
items.not_awarded(current_user, not_params[:my_reaction_emoji])
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
def by_non_archived(items)
params[:non_archived].present? ? items.non_archived : items
2020-03-13 15:44:24 +05:30
end
2021-04-17 20:07:23 +05:30
def or_filters_enabled?
strong_memoize(:or_filters_enabled) do
Feature.enabled?(:or_issuable_queries, feature_flag_scope, default_enabled: :yaml)
end
end
def feature_flag_scope
params.group || params.project
end
2014-09-02 18:07:02 +05:30
end