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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Git::BaseHooksService do
include RepoHelpers
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :repository) }
let(:oldrev) { Gitlab::Git::BLANK_SHA }
let(:newrev) { "8a2a6eb295bb170b34c24c76c49ed0e9b2eaf34b" } # gitlab-test: git rev-parse refs/tags/v1.1.0
let(:ref) { 'refs/tags/v1.1.0' }
let(:checkout_sha) { '5937ac0a7beb003549fc5fd26fc247adbce4a52e' }
let(:test_service) do
Class.new(described_class) do
def hook_name
:push_hooks
end
def limited_commits
[]
end
def commits_count
0
end
end
end
subject { test_service.new(project, user, params) }
let(:params) do
{
change: {
oldrev: oldrev,
newrev: newrev,
ref: ref
}
}
end
describe 'push event' do
it 'creates push event' do
expect_next_instance_of(EventCreateService) do |service|
expect(service).to receive(:push)
end
subject.execute
end
context 'create_push_event is set to false' do
before do
params[:create_push_event] = false
end
it 'does not create push event' do
expect(EventCreateService).not_to receive(:new)
subject.execute
end
end
end
describe 'project hooks and integrations' do
context 'hooks' do
before do
expect(project).to receive(:has_active_hooks?).and_return(active)
end
context 'active hooks' do
let(:active) { true }
it 'executes the hooks' do
expect(subject).to receive(:push_data).at_least(:once).and_call_original
expect(project).to receive(:execute_hooks)
subject.execute
end
end
context 'inactive hooks' do
let(:active) { false }
it 'does not execute the hooks' do
expect(subject).not_to receive(:push_data)
expect(project).not_to receive(:execute_hooks)
subject.execute
end
end
end
context 'with integrations' do
before do
expect(project).to receive(:has_active_integrations?).and_return(active)
end
context 'with active integrations' do
let(:active) { true }
it 'executes the services' do
expect(subject).to receive(:push_data).at_least(:once).and_call_original
expect(project).to receive(:execute_integrations)
subject.execute
end
end
context 'with inactive integrations' do
let(:active) { false }
it 'does not execute the services' do
expect(subject).not_to receive(:push_data)
expect(project).not_to receive(:execute_integrations)
subject.execute
end
end
end
context 'when execute_project_hooks param is set to false' do
before do
params[:execute_project_hooks] = false
allow(project).to receive(:has_active_hooks?).and_return(true)
allow(project).to receive(:has_active_integrations?).and_return(true)
end
it 'does not execute hooks and integrations' do
expect(project).not_to receive(:execute_hooks)
expect(project).not_to receive(:execute_integrations)
subject.execute
end
end
end
describe 'Generating CI variables from push options' do
let(:pipeline_params) do
{
after: newrev,
before: oldrev,
checkout_sha: checkout_sha,
push_options: push_options, # defined in each context
ref: ref,
variables_attributes: variables_attributes # defined in each context
}
end
shared_examples 'creates pipeline with params and expected variables' do
let(:pipeline_service) { double(execute: service_response) }
let(:service_response) { double(error?: false, payload: pipeline, message: "Error") }
let(:pipeline) { double(persisted?: true) }
it 'calls the create pipeline service' do
expect(Ci::CreatePipelineService)
.to receive(:new)
.with(project, user, pipeline_params)
.and_return(pipeline_service)
expect(subject).not_to receive(:log_pipeline_errors)
subject.execute
end
end
context 'with empty push options' do
let(:push_options) { {} }
let(:variables_attributes) { [] }
it_behaves_like 'creates pipeline with params and expected variables'
end
context 'with push options not specifying variables' do
let(:push_options) do
{
mr: {
create: true
}
}
end
let(:variables_attributes) { [] }
before do
params[:push_options] = push_options
end
it_behaves_like 'creates pipeline with params and expected variables'
end
context 'with push options specifying variables' do
let(:push_options) do
{
ci: {
variable: {
"FOO=123": 1,
"BAR=456": 1,
"MNO=890=ABC": 1
}
}
}
end
let(:variables_attributes) do
[
{ "key" => "FOO", "variable_type" => "env_var", "secret_value" => "123" },
{ "key" => "BAR", "variable_type" => "env_var", "secret_value" => "456" },
{ "key" => "MNO", "variable_type" => "env_var", "secret_value" => "890=ABC" }
]
end
before do
params[:push_options] = push_options
end
it_behaves_like 'creates pipeline with params and expected variables'
end
context 'with push options not specifying variables in correct format' do
let(:push_options) do
{
ci: {
variable: {
"FOO=123": 1,
"BAR": 1,
"=MNO": 1
}
}
}
end
let(:variables_attributes) do
[
{ "key" => "FOO", "variable_type" => "env_var", "secret_value" => "123" }
]
end
before do
params[:push_options] = push_options
end
it_behaves_like 'creates pipeline with params and expected variables'
end
end
describe "Pipeline creation" do
let(:pipeline_params) do
{
after: newrev,
before: oldrev,
checkout_sha: checkout_sha,
push_options: push_options,
ref: ref,
variables_attributes: variables_attributes
}
end
let(:pipeline_service) { double(execute: service_response) }
let(:push_options) { {} }
let(:variables_attributes) { [] }
context "when the pipeline is persisted" do
let(:pipeline) { double(persisted?: true) }
context "and there are no errors" do
let(:service_response) { double(error?: false, payload: pipeline, message: "Error") }
it "returns success" do
expect(Ci::CreatePipelineService)
.to receive(:new)
.with(project, user, pipeline_params)
.and_return(pipeline_service)
expect(subject.execute[:status]).to eq(:success)
end
end
context "and there are errors" do
let(:service_response) { double(error?: true, payload: pipeline, message: "Error") }
it "does not log errors and returns success" do
# This behaviour is due to the save_on_errors: true setting that is the default in the execute method.
expect(Ci::CreatePipelineService)
.to receive(:new)
.with(project, user, pipeline_params)
.and_return(pipeline_service)
expect(subject).not_to receive(:log_pipeline_errors).with(service_response.message)
expect(subject.execute[:status]).to eq(:success)
end
end
end
context "when the pipeline wasn't persisted" do
let(:pipeline) { double(persisted?: false) }
context "and there are no errors" do
let(:service_response) { double(error?: false, payload: pipeline, message: nil) }
it "returns success" do
expect(Ci::CreatePipelineService)
.to receive(:new)
.with(project, user, pipeline_params)
.and_return(pipeline_service)
expect(subject).to receive(:log_pipeline_errors).with(service_response.message)
expect(subject.execute[:status]).to eq(:success)
end
end
context "and there are errors" do
let(:service_response) { double(error?: true, payload: pipeline, message: "Error") }
it "logs errors and returns success" do
expect(Ci::CreatePipelineService)
.to receive(:new)
.with(project, user, pipeline_params)
.and_return(pipeline_service)
expect(subject).to receive(:log_pipeline_errors).with(service_response.message)
expect(subject.execute[:status]).to eq(:success)
end
end
end
end
end
|