2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-02-25 12:15:08 -05:00
|
|
|
# Finders::MergeRequest class
|
|
|
|
#
|
|
|
|
# Used to filter MergeRequests collections by set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user use
|
|
|
|
# params:
|
2018-05-13 22:07:53 -04:00
|
|
|
# scope: 'created_by_me' or 'assigned_to_me' or 'all'
|
2018-06-26 15:30:29 -04:00
|
|
|
# state: 'open', 'closed', 'merged', 'locked', or 'all'
|
2014-02-25 12:15:08 -05:00
|
|
|
# group_id: integer
|
|
|
|
# project_id: integer
|
2017-03-31 05:38:40 -04:00
|
|
|
# milestone_title: string
|
2019-11-22 13:06:00 -05:00
|
|
|
# release_tag: string
|
2017-07-24 16:07:37 -04:00
|
|
|
# author_id: integer
|
2020-04-01 08:08:00 -04:00
|
|
|
# author_username: string
|
2014-02-25 12:15:08 -05:00
|
|
|
# assignee_id: integer
|
|
|
|
# search: string
|
2019-02-05 03:32:27 -05:00
|
|
|
# in: 'title', 'description', or a string joining them with comma
|
2014-02-25 12:15:08 -05:00
|
|
|
# label_name: string
|
|
|
|
# sort: string
|
2016-11-27 05:33:15 -05:00
|
|
|
# non_archived: boolean
|
2017-09-03 21:43:14 -04:00
|
|
|
# my_reaction_emoji: string
|
2018-02-23 11:04:20 -05:00
|
|
|
# source_branch: string
|
|
|
|
# target_branch: string
|
2018-02-28 06:16:29 -05:00
|
|
|
# created_after: datetime
|
|
|
|
# created_before: datetime
|
|
|
|
# updated_after: datetime
|
|
|
|
# updated_before: datetime
|
2014-02-25 12:15:08 -05:00
|
|
|
#
|
2014-09-02 08:28:27 -04:00
|
|
|
class MergeRequestsFinder < IssuableFinder
|
2020-08-06 14:09:41 -04:00
|
|
|
include MergedAtFilter
|
|
|
|
|
2018-10-03 04:15:00 -04:00
|
|
|
def self.scalar_params
|
2020-10-19 02:09:08 -04:00
|
|
|
@scalar_params ||= super + [
|
|
|
|
:approved_by_ids,
|
|
|
|
:deployed_after,
|
|
|
|
:deployed_before,
|
|
|
|
:draft,
|
|
|
|
:environment,
|
|
|
|
:merged_after,
|
|
|
|
:merged_before,
|
2020-11-23 13:09:14 -05:00
|
|
|
:reviewer_id,
|
|
|
|
:reviewer_username,
|
2020-10-19 02:09:08 -04:00
|
|
|
:target_branch,
|
|
|
|
:wip
|
|
|
|
]
|
2020-09-29 11:10:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.array_params
|
|
|
|
@array_params ||= super.merge(approved_by_usernames: [])
|
2018-10-03 04:15:00 -04:00
|
|
|
end
|
|
|
|
|
2014-02-25 12:15:08 -05:00
|
|
|
def klass
|
|
|
|
MergeRequest
|
|
|
|
end
|
2017-02-07 08:15:07 -05:00
|
|
|
|
2020-11-23 13:09:14 -05:00
|
|
|
def params_class
|
2021-01-11 07:10:41 -05:00
|
|
|
MergeRequestsFinder.const_get(:Params, false) # rubocop: disable CodeReuse/Finder
|
2020-11-23 13:09:14 -05:00
|
|
|
end
|
|
|
|
|
2018-02-23 11:04:20 -05:00
|
|
|
def filter_items(_items)
|
2019-01-25 04:22:48 -05:00
|
|
|
items = by_commit(super)
|
|
|
|
items = by_source_branch(items)
|
2020-07-31 17:10:12 -04:00
|
|
|
items = by_draft(items)
|
2019-04-05 03:29:53 -04:00
|
|
|
items = by_target_branch(items)
|
2020-08-06 14:09:41 -04:00
|
|
|
items = by_merged_at(items)
|
2020-09-29 11:10:08 -04:00
|
|
|
items = by_approvals(items)
|
2020-10-19 02:09:08 -04:00
|
|
|
items = by_deployments(items)
|
2020-11-23 13:09:14 -05:00
|
|
|
items = by_reviewer(items)
|
2020-10-19 02:09:08 -04:00
|
|
|
|
2019-04-05 03:29:53 -04:00
|
|
|
by_source_project_id(items)
|
2018-02-23 11:04:20 -05:00
|
|
|
end
|
|
|
|
|
2020-10-22 08:08:41 -04:00
|
|
|
def filter_negated_items(items)
|
|
|
|
items = super(items)
|
2020-11-23 13:09:14 -05:00
|
|
|
items = by_negated_reviewer(items)
|
2021-07-01 05:07:33 -04:00
|
|
|
items = by_negated_approved_by(items)
|
2020-10-22 08:08:41 -04:00
|
|
|
by_negated_target_branch(items)
|
|
|
|
end
|
|
|
|
|
2017-02-07 08:15:07 -05:00
|
|
|
private
|
|
|
|
|
2019-01-25 04:22:48 -05:00
|
|
|
def by_commit(items)
|
|
|
|
return items unless params[:commit_sha].presence
|
|
|
|
|
2021-01-08 16:10:30 -05:00
|
|
|
items.by_related_commit_sha(params[:commit_sha])
|
2019-01-25 04:22:48 -05:00
|
|
|
end
|
|
|
|
|
2018-02-23 11:04:20 -05:00
|
|
|
def source_branch
|
|
|
|
@source_branch ||= params[:source_branch].presence
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-02-23 11:04:20 -05:00
|
|
|
def by_source_branch(items)
|
|
|
|
return items unless source_branch
|
|
|
|
|
|
|
|
items.where(source_branch: source_branch)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-02-23 11:04:20 -05:00
|
|
|
|
|
|
|
def target_branch
|
|
|
|
@target_branch ||= params[:target_branch].presence
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-02-23 11:04:20 -05:00
|
|
|
def by_target_branch(items)
|
|
|
|
return items unless target_branch
|
|
|
|
|
|
|
|
items.where(target_branch: target_branch)
|
|
|
|
end
|
2020-10-19 02:09:08 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-10-03 04:15:00 -04:00
|
|
|
|
2020-10-22 08:08:41 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def by_negated_target_branch(items)
|
|
|
|
return items unless not_params[:target_branch]
|
|
|
|
|
|
|
|
items.where.not(target_branch: not_params[:target_branch])
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2021-07-01 05:07:33 -04:00
|
|
|
def by_negated_approved_by(items)
|
|
|
|
return items unless not_params[:approved_by_usernames]
|
|
|
|
|
|
|
|
items.not_approved_by_users_with_usernames(not_params[:approved_by_usernames])
|
|
|
|
end
|
|
|
|
|
2019-04-05 03:29:53 -04:00
|
|
|
def source_project_id
|
|
|
|
@source_project_id ||= params[:source_project_id].presence
|
|
|
|
end
|
|
|
|
|
2020-10-19 02:09:08 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-04-05 03:29:53 -04:00
|
|
|
def by_source_project_id(items)
|
|
|
|
return items unless source_project_id
|
|
|
|
|
|
|
|
items.where(source_project_id: source_project_id)
|
|
|
|
end
|
2020-10-19 02:09:08 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2019-04-05 03:29:53 -04:00
|
|
|
|
2020-10-19 02:09:08 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2020-07-31 17:10:12 -04:00
|
|
|
def by_draft(items)
|
|
|
|
draft_param = params[:draft] || params[:wip]
|
|
|
|
|
|
|
|
if draft_param == 'yes'
|
2018-10-03 04:15:00 -04:00
|
|
|
items.where(wip_match(items.arel_table))
|
2020-07-31 17:10:12 -04:00
|
|
|
elsif draft_param == 'no'
|
2018-10-03 04:15:00 -04:00
|
|
|
items.where.not(wip_match(items.arel_table))
|
|
|
|
else
|
|
|
|
items
|
|
|
|
end
|
|
|
|
end
|
2020-10-19 02:09:08 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-10-03 04:15:00 -04:00
|
|
|
|
2020-07-31 17:10:12 -04:00
|
|
|
# WIP is deprecated in favor of Draft. Currently both options are supported
|
2018-10-03 04:15:00 -04:00
|
|
|
def wip_match(table)
|
2021-07-22 17:09:40 -04:00
|
|
|
table[:title].matches('WIP:%')
|
|
|
|
.or(table[:title].matches('WIP %'))
|
|
|
|
.or(table[:title].matches('[WIP]%'))
|
2020-07-31 17:10:12 -04:00
|
|
|
.or(table[:title].matches('Draft - %'))
|
|
|
|
.or(table[:title].matches('Draft:%'))
|
|
|
|
.or(table[:title].matches('[Draft]%'))
|
|
|
|
.or(table[:title].matches('(Draft)%'))
|
2018-10-03 04:15:00 -04:00
|
|
|
end
|
2020-01-13 16:07:39 -05:00
|
|
|
|
2020-09-29 11:10:08 -04:00
|
|
|
# Filter by merge requests that had been approved by specific users
|
|
|
|
# rubocop: disable CodeReuse/Finder
|
|
|
|
def by_approvals(items)
|
|
|
|
MergeRequests::ByApprovalsFinder
|
|
|
|
.new(params[:approved_by_usernames], params[:approved_by_ids])
|
|
|
|
.execute(items)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/Finder
|
2020-10-16 05:09:06 -04:00
|
|
|
|
2020-10-19 02:09:08 -04:00
|
|
|
def by_deployments(items)
|
|
|
|
env = params[:environment]
|
2021-11-26 10:13:11 -05:00
|
|
|
before = parse_datetime(params[:deployed_before])
|
|
|
|
after = parse_datetime(params[:deployed_after])
|
2020-10-19 02:09:08 -04:00
|
|
|
id = params[:deployment_id]
|
|
|
|
|
|
|
|
return items if !env && !before && !after && !id
|
|
|
|
|
|
|
|
# Each filter depends on the same JOIN+WHERE. To prevent this JOIN+WHERE
|
|
|
|
# from being duplicated for every filter, we only produce it once. The
|
|
|
|
# filter methods in turn expect the JOIN+WHERE to already be present.
|
|
|
|
#
|
|
|
|
# This approach ensures that query performance doesn't degrade as the number
|
|
|
|
# of deployment related filters increases.
|
|
|
|
deploys = DeploymentMergeRequest.join_deployments_for_merge_requests
|
|
|
|
deploys = deploys.by_deployment_id(id) if id
|
|
|
|
deploys = deploys.deployed_to(env) if env
|
|
|
|
deploys = deploys.deployed_before(before) if before
|
|
|
|
deploys = deploys.deployed_after(after) if after
|
|
|
|
|
|
|
|
items.where_exists(deploys)
|
|
|
|
end
|
2020-11-23 13:09:14 -05:00
|
|
|
|
|
|
|
def by_reviewer(items)
|
|
|
|
return items unless params.reviewer_id? || params.reviewer_username?
|
|
|
|
|
|
|
|
if params.filter_by_no_reviewer?
|
|
|
|
items.no_review_requested
|
|
|
|
elsif params.filter_by_any_reviewer?
|
|
|
|
items.review_requested
|
|
|
|
elsif params.reviewer
|
|
|
|
items.review_requested_to(params.reviewer)
|
|
|
|
else # reviewer not found
|
|
|
|
items.none
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_negated_reviewer(items)
|
|
|
|
return items unless not_params.reviewer_id? || not_params.reviewer_username?
|
|
|
|
|
|
|
|
if not_params.reviewer.present?
|
|
|
|
items.no_review_requested_to(not_params.reviewer)
|
|
|
|
else # reviewer not found
|
|
|
|
items.none
|
|
|
|
end
|
|
|
|
end
|
2021-11-26 10:13:11 -05:00
|
|
|
|
|
|
|
def parse_datetime(input)
|
|
|
|
# To work around http://www.ruby-lang.org/en/news/2021/11/15/date-parsing-method-regexp-dos-cve-2021-41817/
|
|
|
|
DateTime.parse(input.byteslice(0, 128)) if input
|
|
|
|
rescue Date::Error
|
|
|
|
nil
|
|
|
|
end
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
MergeRequestsFinder.prepend_mod_with('MergeRequestsFinder')
|