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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe MergeRequests::CreateService, :clean_gitlab_redis_shared_state do
include ProjectForksHelper
let(:project) { create(:project, :repository) }
let(:user) { create(:user) }
let(:user2) { create(:user) }
describe '#execute' do
context 'valid params' do
let(:opts) do
{
title: 'Awesome merge_request',
description: 'please fix',
source_branch: 'feature',
target_branch: 'master',
force_remove_source_branch: '1'
}
end
let(:service) { described_class.new(project: project, current_user: user, params: opts) }
let(:merge_request) { service.execute }
before do
project.add_maintainer(user)
project.add_developer(user2)
allow(service).to receive(:execute_hooks)
end
it 'creates an MR' do
expect(merge_request).to be_valid
expect(merge_request.draft?).to be(false)
expect(merge_request.title).to eq('Awesome merge_request')
expect(merge_request.assignees).to be_empty
expect(merge_request.merge_params['force_remove_source_branch']).to eq('1')
end
it 'executes hooks with default action' do
expect(service).to have_received(:execute_hooks).with(merge_request)
end
it 'refreshes the number of open merge requests', :use_clean_rails_memory_store_caching do
expect { service.execute }
.to change { project.open_merge_requests_count }.from(0).to(1)
end
it 'creates exactly 1 create MR event', :sidekiq_might_not_need_inline do
attributes = {
action: :created,
target_id: merge_request.id,
target_type: merge_request.class.name
}
expect(Event.where(attributes).count).to eq(1)
end
it 'sets the merge_status to preparing' do
expect(merge_request.reload).to be_preparing
end
describe 'when marked with /draft' do
context 'in title and in description' do
let(:opts) do
{
title: 'Draft: Awesome merge_request',
description: "well this is not done yet\n/draft",
source_branch: 'feature',
target_branch: 'master',
assignees: [user2]
}
end
it 'sets MR to draft' do
expect(merge_request.draft?).to be(true)
end
end
context 'in description only' do
let(:opts) do
{
title: 'Awesome merge_request',
description: "well this is not done yet\n/draft",
source_branch: 'feature',
target_branch: 'master',
assignees: [user2]
}
end
it 'sets MR to draft' do
expect(merge_request.draft?).to be(true)
end
end
end
context 'when merge request is assigned to someone' do
let(:opts) do
{
title: 'Awesome merge_request',
description: 'please fix',
source_branch: 'feature',
target_branch: 'master',
assignees: [user2]
}
end
it { expect(merge_request.assignees).to eq([user2]) }
end
context 'when reviewer is assigned' do
let(:opts) do
{
title: 'Awesome merge_request',
description: 'please fix',
source_branch: 'feature',
target_branch: 'master',
reviewers: [user2]
}
end
it { expect(merge_request.reviewers).to eq([user2]) }
it 'invalidates counter cache for reviewers', :use_clean_rails_memory_store_caching do
expect { merge_request }
.to change { user2.review_requested_open_merge_requests_count }
.by(1)
end
end
context 'when head pipelines already exist for merge request source branch', :sidekiq_inline do
let(:shas) { project.repository.commits(opts[:source_branch], limit: 2).map(&:id) }
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[1]) }
let!(:pipeline_2) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[0]) }
let!(:pipeline_3) { create(:ci_pipeline, project: project, ref: "other_branch", project_id: project.id) }
before do
# rubocop: disable Cop/DestroyAll
project.merge_requests
.where(source_branch: opts[:source_branch], target_branch: opts[:target_branch])
.destroy_all
# rubocop: enable Cop/DestroyAll
end
it 'sets head pipeline' do
merge_request = service.execute
expect(merge_request.reload.head_pipeline).to eq(pipeline_2)
expect(merge_request).to be_persisted
end
context 'when the new pipeline is associated with an old sha' do
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[0]) }
let!(:pipeline_2) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[1]) }
it 'sets an old pipeline with associated with the latest sha as the head pipeline' do
merge_request = service.execute
expect(merge_request.reload.head_pipeline).to eq(pipeline_1)
expect(merge_request).to be_persisted
end
end
context 'when there are no pipelines with the diff head sha' do
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[1]) }
let!(:pipeline_2) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[1]) }
it 'does not set the head pipeline' do
merge_request = service.execute
expect(merge_request.reload.head_pipeline).to be_nil
expect(merge_request).to be_persisted
end
end
end
describe 'Pipelines for merge requests', :sidekiq_inline do
before do
stub_ci_pipeline_yaml_file(config)
end
context "when .gitlab-ci.yml has merge_requests keywords" do
let(:config) do
YAML.dump({
test: {
stage: 'test',
script: 'echo',
only: ['merge_requests']
}
})
end
it 'creates a detached merge request pipeline and sets it as a head pipeline' do
expect(merge_request).to be_persisted
merge_request.reload
expect(merge_request.pipelines_for_merge_request.count).to eq(1)
expect(merge_request.actual_head_pipeline).to be_detached_merge_request_pipeline
end
context 'when merge request is submitted from forked project' do
let(:target_project) { fork_project(project, nil, repository: true) }
let(:opts) do
{
title: 'Awesome merge_request',
source_branch: 'feature',
target_branch: 'master',
target_project_id: target_project.id
}
end
before do
stub_feature_flags(ci_disallow_to_create_merge_request_pipelines_in_target_project: false)
target_project.add_developer(user2)
target_project.add_maintainer(user)
end
it 'create detached merge request pipeline for fork merge request' do
merge_request.reload
head_pipeline = merge_request.actual_head_pipeline
expect(head_pipeline).to be_detached_merge_request_pipeline
expect(head_pipeline.project).to eq(target_project)
end
end
context 'when there are no commits between source branch and target branch' do
let(:opts) do
{
title: 'Awesome merge_request',
description: 'please fix',
source_branch: 'not-merged-branch',
target_branch: 'master'
}
end
it 'does not create a detached merge request pipeline' do
expect(merge_request).to be_persisted
merge_request.reload
expect(merge_request.pipelines_for_merge_request.count).to eq(0)
end
end
context "when branch pipeline was created before a merge request pipline has been created" do
before do
create(:ci_pipeline, project: merge_request.source_project,
sha: merge_request.diff_head_sha,
ref: merge_request.source_branch,
tag: false)
merge_request
end
it 'sets the latest detached merge request pipeline as the head pipeline' do
merge_request.reload
expect(merge_request.actual_head_pipeline).to be_merge_request_event
end
end
end
context "when .gitlab-ci.yml does not have merge_requests keywords" do
let(:config) do
YAML.dump({
test: {
stage: 'test',
script: 'echo'
}
})
end
it 'does not create a detached merge request pipeline' do
expect(merge_request).to be_persisted
merge_request.reload
expect(merge_request.pipelines_for_merge_request.count).to eq(0)
end
end
context 'when .gitlab-ci.yml is invalid' do
let(:config) { 'invalid yaml file' }
it 'persists a pipeline with config error' do
expect(merge_request).to be_persisted
merge_request.reload
expect(merge_request.pipelines_for_merge_request.count).to eq(1)
expect(merge_request.pipelines_for_merge_request.last).to be_failed
expect(merge_request.pipelines_for_merge_request.last).to be_config_error
end
end
end
context 'after_save callback to store_mentions' do
let(:labels) { create_pair(:label, project: project) }
let(:milestone) { create(:milestone, project: project) }
let(:req_opts) { { source_branch: 'feature', target_branch: 'master' } }
context 'when mentionable attributes change' do
let(:opts) { { title: 'Title', description: "Description with #{user.to_reference}" }.merge(req_opts) }
it 'saves mentions' do
expect_next_instance_of(MergeRequest) do |instance|
expect(instance).to receive(:store_mentions!).and_call_original
end
expect(merge_request.user_mentions.count).to eq 1
end
end
context 'when mentionable attributes do not change' do
let(:opts) { { label_ids: labels.map(&:id), milestone_id: milestone.id }.merge(req_opts) }
it 'does not call store_mentions' do
expect_next_instance_of(MergeRequest) do |instance|
expect(instance).not_to receive(:store_mentions!).and_call_original
end
expect(merge_request.valid?).to be false
expect(merge_request.user_mentions.count).to eq 0
end
end
context 'when save fails' do
let(:opts) { { label_ids: labels.map(&:id), milestone_id: milestone.id } }
it 'does not call store_mentions' do
expect_next_instance_of(MergeRequest) do |instance|
expect(instance).not_to receive(:store_mentions!).and_call_original
end
expect(merge_request.valid?).to be false
end
end
end
it_behaves_like 'reviewer_ids filter' do
let(:execute) { service.execute }
end
end
it_behaves_like 'issuable record that supports quick actions' do
let(:default_params) do
{
source_branch: 'feature',
target_branch: 'master'
}
end
let(:issuable) { described_class.new(project: project, current_user: user, params: params).execute }
end
context 'Quick actions' do
context 'with assignee and milestone in params and command' do
let(:merge_request) { described_class.new(project: project, current_user: user, params: opts).execute }
let(:milestone) { create(:milestone, project: project) }
let(:opts) do
{
assignee_ids: create(:user).id,
milestone_id: 1,
title: 'Title',
description: %(/assign @#{user2.username}\n/milestone %"#{milestone.name}"),
source_branch: 'feature',
target_branch: 'master'
}
end
before do
project.add_maintainer(user)
project.add_maintainer(user2)
end
it 'assigns and sets milestone to issuable from command' do
expect(merge_request).to be_persisted
expect(merge_request.assignees).to eq([user2])
expect(merge_request.milestone).to eq(milestone)
end
end
end
context 'merge request create service' do
context 'asssignee_id' do
let(:user2) { create(:user) }
before do
project.add_maintainer(user)
end
it 'removes assignee_id when user id is invalid' do
opts = { title: 'Title', description: 'Description', assignee_ids: [-1] }
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
expect(merge_request.assignee_ids).to be_empty
end
it 'removes assignee_id when user id is 0' do
opts = { title: 'Title', description: 'Description', assignee_ids: [0] }
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
expect(merge_request.assignee_ids).to be_empty
end
it 'saves assignee when user id is valid' do
project.add_maintainer(user2)
opts = { title: 'Title', description: 'Description', assignee_ids: [user2.id] }
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
expect(merge_request.assignees).to eq([user2])
end
context 'when assignee is set' do
let(:opts) do
{
title: 'Title',
description: 'Description',
assignee_ids: [user2.id],
source_branch: 'feature',
target_branch: 'master'
}
end
it 'invalidates open merge request counter for assignees when merge request is assigned' do
project.add_maintainer(user2)
described_class.new(project: project, current_user: user, params: opts).execute
expect(user2.assigned_open_merge_requests_count).to eq 1
end
end
context "when issuable feature is private" do
before do
project.project_feature.update!(issues_access_level: ProjectFeature::PRIVATE,
merge_requests_access_level: ProjectFeature::PRIVATE)
end
levels = [Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC]
levels.each do |level|
it "removes not authorized assignee when project is #{Gitlab::VisibilityLevel.level_name(level)}" do
project.update!(visibility_level: level)
opts = { title: 'Title', description: 'Description', assignee_ids: [user2.id] }
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
expect(merge_request.assignee_id).to be_nil
end
end
end
end
end
shared_examples 'when source and target projects are different' do
let(:target_project) { fork_project(project, nil, repository: true) }
let(:opts) do
{
title: 'Awesome merge_request',
source_branch: 'feature',
target_branch: 'master',
target_project_id: target_project.id
}
end
context 'when user can not access source project' do
before do
target_project.add_developer(user2)
target_project.add_maintainer(user)
end
it 'raises an error' do
expect { described_class.new(project: project, current_user: user, params: opts).execute }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
context 'when user can not access target project' do
before do
target_project.add_developer(user2)
target_project.add_maintainer(user)
end
it 'raises an error' do
expect { described_class.new(project: project, current_user: user, params: opts).execute }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
context 'when the user has access to both projects' do
before do
target_project.add_developer(user)
project.add_developer(user)
end
it 'creates the merge request', :sidekiq_might_not_need_inline do
expect_next_instance_of(MergeRequest) do |instance|
expect(instance).to receive(:eager_fetch_ref!).and_call_original
end
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
expect(merge_request).to be_persisted
expect(merge_request.iid).to be > 0
end
it 'does not create the merge request when the target project is archived' do
target_project.update!(archived: true)
expect { described_class.new(project: project, current_user: user, params: opts).execute }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
end
it_behaves_like 'when source and target projects are different'
context 'when user sets source project id' do
let(:another_project) { create(:project) }
let(:opts) do
{
title: 'Awesome merge_request',
source_branch: 'feature',
target_branch: 'master',
source_project_id: another_project.id
}
end
before do
project.add_developer(user2)
project.add_maintainer(user)
end
it 'ignores source_project_id' do
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
expect(merge_request.source_project_id).to eq(project.id)
end
end
end
end
|