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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe MergeRequests::CreateRefService, feature_category: :merge_trains do
using RSpec::Parameterized::TableSyntax
describe '#execute' do
let_it_be_with_reload(:project) { create(:project, :empty_repo) }
let_it_be(:user) { project.creator }
let_it_be(:first_parent_ref) { project.default_branch_or_main }
let_it_be(:source_branch) { 'branch' }
let(:target_ref) { "refs/merge-requests/#{merge_request.iid}/train" }
let(:source_sha) { project.commit(source_branch).sha }
let(:squash) { false }
let(:default_commit_message) { merge_request.default_merge_commit_message(user: user) }
let(:merge_request) do
create(
:merge_request,
title: 'Merge request ref test',
author: user,
source_project: project,
target_project: project,
source_branch: source_branch,
target_branch: first_parent_ref,
squash: squash
)
end
subject(:result) do
described_class.new(
current_user: user,
merge_request: merge_request,
target_ref: target_ref,
source_sha: source_sha,
first_parent_ref: first_parent_ref
).execute
end
context 'when there is a user-caused gitaly error' do
let(:source_sha) { '123' }
it 'returns an error response' do
expect(result[:status]).to eq :error
end
end
context 'with valid inputs' do
before_all do
# ensure first_parent_ref is created before source_sha
project.repository.create_file(
user,
'README.md',
'',
message: 'Base parent commit 1',
branch_name: first_parent_ref
)
project.repository.create_branch(source_branch, first_parent_ref)
# create two commits source_branch to test squashing
project.repository.create_file(
user,
'.gitlab-ci.yml',
'',
message: 'Feature branch commit 1',
branch_name: source_branch
)
project.repository.create_file(
user,
'.gitignore',
'',
message: 'Feature branch commit 2',
branch_name: source_branch
)
# create an extra commit not present on source_branch
project.repository.create_file(
user,
'EXTRA',
'',
message: 'Base parent commit 2',
branch_name: first_parent_ref
)
end
shared_examples_for 'writing with a merge commit' do
it 'merges with a merge commit', :aggregate_failures do
expect(result[:status]).to eq :success
expect(result[:commit_sha]).to eq(project.repository.commit(target_ref).sha)
expect(result[:source_sha]).to eq(project.repository.commit(source_branch).sha)
expect(result[:target_sha]).to eq(project.repository.commit(first_parent_ref).sha)
expect(result[:merge_commit_sha]).to be_present
expect(result[:squash_commit_sha]).not_to be_present
expect(project.repository.commits(target_ref, limit: 10, order: 'topo').map(&:message)).to(
match(
[
expected_merge_commit,
'Feature branch commit 2',
'Feature branch commit 1',
'Base parent commit 2',
'Base parent commit 1'
]
)
)
end
end
shared_examples_for 'writing with a squash and merge commit' do
it 'writes the squashed result', :aggregate_failures do
expect(result[:status]).to eq :success
expect(result[:commit_sha]).to eq(project.repository.commit(target_ref).sha)
expect(result[:source_sha]).to eq(project.repository.commit(source_branch).sha)
expect(result[:target_sha]).to eq(project.repository.commit(first_parent_ref).sha)
expect(result[:merge_commit_sha]).to be_present
expect(result[:squash_commit_sha]).to be_present
expect(project.repository.commits(target_ref, limit: 10, order: 'topo').map(&:message)).to(
match(
[
expected_merge_commit,
"#{merge_request.title}\n",
'Base parent commit 2',
'Base parent commit 1'
]
)
)
end
end
shared_examples_for 'writing with a squash and no merge commit' do
it 'writes the squashed result without a merge commit', :aggregate_failures do
expect(result[:status]).to eq :success
expect(result[:commit_sha]).to eq(project.repository.commit(target_ref).sha)
expect(result[:source_sha]).to eq(project.repository.commit(source_branch).sha)
expect(result[:target_sha]).to eq(project.repository.commit(first_parent_ref).sha)
expect(result[:merge_commit_sha]).not_to be_present
expect(result[:squash_commit_sha]).to be_present
expect(project.repository.commits(target_ref, limit: 10, order: 'topo').map(&:message)).to(
match(
[
"#{merge_request.title}\n",
'Base parent commit 2',
'Base parent commit 1'
]
)
)
end
end
shared_examples_for 'writing without a merge commit' do
it 'writes the rebased merged result', :aggregate_failures do
expect(result[:status]).to eq :success
expect(result[:commit_sha]).to eq(project.repository.commit(target_ref).sha)
expect(result[:source_sha]).to eq(project.repository.commit(source_branch).sha)
expect(result[:target_sha]).to eq(project.repository.commit(first_parent_ref).sha)
expect(result[:merge_commit_sha]).not_to be_present
expect(result[:squash_commit_sha]).not_to be_present
expect(project.repository.commits(target_ref, limit: 10, order: 'topo').map(&:message)).to(
eq(
[
'Feature branch commit 2',
'Feature branch commit 1',
'Base parent commit 2',
'Base parent commit 1'
]
)
)
end
end
shared_examples 'merge commits without squash' do
context 'with a custom template' do
let(:expected_merge_commit) { 'This is the merge commit' } # could also be default_commit_message
before do
project.project_setting.update!(merge_commit_template: expected_merge_commit)
end
it_behaves_like 'writing with a merge commit'
end
context 'with no custom template' do
let(:expected_merge_commit) { default_commit_message }
before do
project.project_setting.update!(merge_commit_template: nil)
end
it_behaves_like 'writing with a merge commit'
end
end
shared_examples 'merge commits with squash' do
context 'when squash set' do
let(:squash) { true }
let(:expected_merge_commit) { merge_request.default_merge_commit_message(user: user) }
before do
project.project_setting.update!(merge_commit_template: 'This is the merge commit')
end
it_behaves_like 'writing with a squash and merge commit'
end
end
context 'when the merge commit message is provided at time of merge' do
let(:expected_merge_commit) { 'something custom' }
before do
merge_request.merge_params['commit_message'] = expected_merge_commit
end
it 'writes the merged result', :aggregate_failures do
expect(result[:status]).to eq :success
expect(project.repository.commits(target_ref, limit: 1, order: 'topo').map(&:message)).to(
match([expected_merge_commit])
)
end
context 'when squash set' do
let(:squash) { true }
it_behaves_like 'writing with a squash and merge commit'
end
end
context 'when merged commit strategy' do
include_examples 'merge commits without squash'
include_examples 'merge commits with squash'
end
context 'when semi-linear merge strategy' do
before do
project.merge_method = :rebase_merge
project.save!
end
include_examples 'merge commits without squash'
include_examples 'merge commits with squash'
context 'when the target ref changes between rebase and merge' do
# this tests internal handling of expected_old_oid
it 'returns an error', :aggregate_failures do
expect_next_instance_of(described_class) do |instance|
original = instance.method(:maybe_merge!)
expect(instance).to receive(:maybe_merge!) do |*args, **kwargs|
# Corrupt target_ref before the merge, simulating a race with
# another instance of the service for the same MR. source_sha is
# just an arbitrary valid commit that differs from what was just
# written.
project.repository.write_ref(target_ref, source_sha)
original.call(*args, **kwargs)
end
end
expect(result[:status]).to eq :error
expect(result[:message]).to eq "9:Could not update #{target_ref}. Please refresh and try again."
end
end
end
context 'when fast-forward merge strategy' do
before do
project.merge_method = :ff
project.save!
end
it_behaves_like 'writing without a merge commit'
context 'when squash set' do
let(:squash) { true }
it_behaves_like 'writing with a squash and no merge commit'
end
end
end
end
end
|