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
|
# 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(: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(: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
it 'writes the merged result into target_ref', :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(target_ref).parents[1].sha)
expect(result[:target_sha]).to eq(project.repository.commit(first_parent_ref).sha)
expect(project.repository.commits(target_ref, limit: 10, order: 'topo').map(&:message)).to(
match(
[
a_string_matching(/Merge branch '#{source_branch}' into '#{first_parent_ref}'/),
'Feature branch commit 2',
'Feature branch commit 1',
'Base parent commit 2',
'Base parent commit 1'
]
)
)
end
context 'when squash is requested' do
let(:squash) { true }
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(target_ref).parents[1].sha)
expect(result[:target_sha]).to eq(project.repository.commit(first_parent_ref).sha)
expect(project.repository.commits(target_ref, limit: 10, order: 'topo').map(&:message)).to(
match(
[
a_string_matching(/Merge branch '#{source_branch}' into '#{first_parent_ref}'/),
"#{merge_request.title}\n",
'Base parent commit 2',
'Base parent commit 1'
]
)
)
end
end
context 'when semi-linear merges are enabled' do
before do
project.merge_method = :rebase_merge
project.save!
end
it 'writes the semi-linear 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(target_ref).parents[1].sha)
expect(result[:target_sha]).to eq(project.repository.commit(first_parent_ref).sha)
expect(project.repository.commits(target_ref, limit: 10, order: 'topo').map(&:message)).to(
match(
[
a_string_matching(/Merge branch '#{source_branch}' into '#{first_parent_ref}'/),
'Feature branch commit 2',
'Feature branch commit 1',
'Base parent commit 2',
'Base parent commit 1'
]
)
)
end
end
context 'when fast-forward merges are enabled' do
before do
project.merge_method = :ff
project.save!
end
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(target_ref).sha)
expect(result[:target_sha]).to eq(project.repository.commit(first_parent_ref).sha)
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
end
end
end
|