1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# frozen_string_literal: true
# Finders::MergeRequest class
#
# Used to filter MergeRequests collections by set of params
#
# Arguments:
# current_user - which user use
# params:
# scope: 'created_by_me' or 'assigned_to_me' or 'all'
# state: 'open', 'closed', 'merged', 'locked', or 'all'
# group_id: integer
# project_id: integer
# milestone_title: string
# release_tag: string
# author_id: integer
# author_username: string
# assignee_id: integer
# search: string
# in: 'title', 'description', or a string joining them with comma
# label_name: string
# sort: string
# non_archived: boolean
# my_reaction_emoji: string
# source_branch: string
# target_branch: string
# created_after: datetime
# created_before: datetime
# updated_after: datetime
# updated_before: datetime
#
class MergeRequestsFinder < IssuableFinder
extend ::Gitlab::Utils::Override
include MergedAtFilter
def self.scalar_params
@scalar_params ||= super + [
:approved_by_ids,
:deployed_after,
:deployed_before,
:draft,
:environment,
:merged_after,
:merged_before,
:reviewer_id,
:reviewer_username,
:target_branch,
:wip
]
end
def self.array_params
@array_params ||= super.merge(approved_by_usernames: [])
end
def klass
MergeRequest
end
def params_class
MergeRequestsFinder.const_get(:Params, false) # rubocop: disable CodeReuse/Finder
end
def filter_items(_items)
items = by_commit(super)
items = by_source_branch(items)
items = by_draft(items)
items = by_target_branch(items)
items = by_merged_at(items)
items = by_approvals(items)
items = by_deployments(items)
items = by_reviewer(items)
by_source_project_id(items)
end
def filter_negated_items(items)
items = super(items)
items = by_negated_reviewer(items)
items = by_negated_approved_by(items)
by_negated_target_branch(items)
end
private
override :sort
def sort(items)
items = super(items)
return items unless use_grouping_columns?
grouping_columns = klass.grouping_columns(params[:sort])
items.group(grouping_columns) # rubocop:disable CodeReuse/ActiveRecord
end
def by_commit(items)
return items unless params[:commit_sha].presence
items.by_related_commit_sha(params[:commit_sha])
end
def source_branch
@source_branch ||= params[:source_branch].presence
end
# rubocop: disable CodeReuse/ActiveRecord
def by_source_branch(items)
return items unless source_branch
items.where(source_branch: source_branch)
end
# rubocop: enable CodeReuse/ActiveRecord
def target_branch
@target_branch ||= params[:target_branch].presence
end
# rubocop: disable CodeReuse/ActiveRecord
def by_target_branch(items)
return items unless target_branch
items.where(target_branch: target_branch)
end
# rubocop: enable CodeReuse/ActiveRecord
# 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
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
def source_project_id
@source_project_id ||= params[:source_project_id].presence
end
# rubocop: disable CodeReuse/ActiveRecord
def by_source_project_id(items)
return items unless source_project_id
items.where(source_project_id: source_project_id)
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def by_draft(items)
draft_param = Gitlab::Utils.to_boolean(params.fetch(:draft) { params.fetch(:wip, nil) })
return items if draft_param.nil?
if draft_param
items.where(wip_match(items.arel_table))
else
items.where.not(wip_match(items.arel_table))
end
end
# rubocop: enable CodeReuse/ActiveRecord
# WIP is deprecated in favor of Draft. Currently both options are supported
def wip_match(table)
table[:title].matches('WIP:%')
.or(table[:title].matches('WIP %'))
.or(table[:title].matches('[WIP]%'))
.or(table[:title].matches('Draft - %'))
.or(table[:title].matches('Draft:%'))
.or(table[:title].matches('[Draft]%'))
.or(table[:title].matches('(Draft)%'))
end
# 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
def by_deployments(items)
env = params[:environment]
before = parse_datetime(params[:deployed_before])
after = parse_datetime(params[:deployed_after])
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
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
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
def use_grouping_columns?
return false unless params[:sort].present?
params[:approved_by_usernames].present? || params[:approved_by_ids].present?
end
end
MergeRequestsFinder.prepend_mod_with('MergeRequestsFinder')
|