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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Git::WikiPushService, services: true do
include RepoHelpers
let_it_be(:key_id) { create(:key, user: current_user).shell_id }
let_it_be(:project) { create(:project, :wiki_repo) }
let_it_be(:current_user) { create(:user) }
let_it_be(:git_wiki) { project.wiki.wiki }
let_it_be(:repository) { git_wiki.repository }
describe '#execute' do
context 'the push contains more than the permitted number of changes' do
def run_service
process_changes { described_class::MAX_CHANGES.succ.times { write_new_page } }
end
it 'creates only MAX_CHANGES events' do
expect { run_service }.to change(Event, :count).by(described_class::MAX_CHANGES)
end
end
context 'default_branch collides with a tag' do
it 'creates only one event' do
base_sha = current_sha
write_new_page
service = create_service(base_sha, ['refs/heads/master', 'refs/tags/master'])
expect { service.execute }.to change(Event, :count).by(1)
end
end
describe 'successfully creating events' do
let(:count) { Event::WIKI_ACTIONS.size }
def run_service
wiki_page_a = create(:wiki_page, project: project)
wiki_page_b = create(:wiki_page, project: project)
process_changes do
write_new_page
update_page(wiki_page_a.title)
delete_page(wiki_page_b.page.path)
end
end
it 'creates one event for every wiki action' do
expect { run_service }.to change(Event, :count).by(count)
end
it 'handles all known actions' do
run_service
expect(Event.last(count).pluck(:action)).to match_array(Event::WIKI_ACTIONS.map(&:to_s))
end
end
context 'two pages have been created' do
def run_service
process_changes do
write_new_page
write_new_page
end
end
it 'creates two events' do
expect { run_service }.to change(Event, :count).by(2)
end
it 'creates two metadata records' do
expect { run_service }.to change(WikiPage::Meta, :count).by(2)
end
it 'creates appropriate events' do
run_service
expect(Event.last(2)).to all(have_attributes(wiki_page?: true, action: 'created'))
end
end
context 'a non-page file as been added' do
it 'does not create events, or WikiPage metadata' do
expect do
process_changes { write_non_page }
end.not_to change { [Event.count, WikiPage::Meta.count] }
end
end
context 'one page, and one non-page have been created' do
def run_service
process_changes do
write_new_page
write_non_page
end
end
it 'creates a wiki page creation event' do
expect { run_service }.to change(Event, :count).by(1)
expect(Event.last).to have_attributes(wiki_page?: true, action: 'created')
end
it 'creates one metadata record' do
expect { run_service }.to change(WikiPage::Meta, :count).by(1)
end
end
context 'one page has been added, and then updated' do
def run_service
process_changes do
title = write_new_page
update_page(title)
end
end
it 'creates just a single event' do
expect { run_service }.to change(Event, :count).by(1)
end
it 'creates just one metadata record' do
expect { run_service }.to change(WikiPage::Meta, :count).by(1)
end
it 'creates a new wiki page creation event' do
run_service
expect(Event.last).to have_attributes(
wiki_page?: true,
action: 'created'
)
end
end
context 'when a page we already know about has been updated' do
let(:wiki_page) { create(:wiki_page, project: project) }
before do
create(:wiki_page_meta, :for_wiki_page, wiki_page: wiki_page)
end
def run_service
process_changes { update_page(wiki_page.title) }
end
it 'does not create a new meta-data record' do
expect { run_service }.not_to change(WikiPage::Meta, :count)
end
it 'creates a new event' do
expect { run_service }.to change(Event, :count).by(1)
end
it 'adds an update event' do
run_service
expect(Event.last).to have_attributes(
wiki_page?: true,
action: 'updated'
)
end
end
context 'when a page we do not know about has been updated' do
def run_service
wiki_page = create(:wiki_page, project: project)
process_changes { update_page(wiki_page.title) }
end
it 'creates a new meta-data record' do
expect { run_service }.to change(WikiPage::Meta, :count).by(1)
end
it 'creates a new event' do
expect { run_service }.to change(Event, :count).by(1)
end
it 'adds an update event' do
run_service
expect(Event.last).to have_attributes(
wiki_page?: true,
action: 'updated'
)
end
end
context 'when a page we do not know about has been deleted' do
def run_service
wiki_page = create(:wiki_page, project: project)
process_changes { delete_page(wiki_page.page.path) }
end
it 'create a new meta-data record' do
expect { run_service }.to change(WikiPage::Meta, :count).by(1)
end
it 'creates a new event' do
expect { run_service }.to change(Event, :count).by(1)
end
it 'adds an update event' do
run_service
expect(Event.last).to have_attributes(
wiki_page?: true,
action: 'destroyed'
)
end
end
it 'calls log_error for every event we cannot create' do
base_sha = current_sha
count = 3
count.times { write_new_page }
message = 'something went very very wrong'
allow_next_instance_of(WikiPages::EventCreateService, current_user) do |service|
allow(service).to receive(:execute)
.with(String, WikiPage, Symbol)
.and_return(ServiceResponse.error(message: message))
end
service = create_service(base_sha)
expect(service).to receive(:log_error).exactly(count).times.with(message)
service.execute
end
describe 'feature flags' do
shared_examples 'a no-op push' do
it 'does not create any events' do
expect { process_changes { write_new_page } }.not_to change(Event, :count)
end
it 'does not even look for events to process' do
base_sha = current_sha
write_new_page
service = create_service(base_sha)
expect(service).not_to receive(:changed_files)
service.execute
end
end
context 'the wiki_events feature is disabled' do
before do
stub_feature_flags(wiki_events: false)
end
it_behaves_like 'a no-op push'
end
context 'the wiki_events_on_git_push feature is disabled' do
before do
stub_feature_flags(wiki_events_on_git_push: false)
end
it_behaves_like 'a no-op push'
context 'but is enabled for a given project' do
before do
stub_feature_flags(wiki_events_on_git_push: project)
end
it 'creates events' do
expect { process_changes { write_new_page } }.to change(Event, :count).by(1)
end
end
end
end
end
# In order to construct the correct GitPostReceive object that represents the
# changes we are applying, we need to describe the changes between old-ref and
# new-ref. Old ref (the base sha) we have to capture before we perform any
# changes. Once the changes have been applied, we can execute the service to
# process them.
def process_changes(&block)
base_sha = current_sha
yield
create_service(base_sha).execute
end
def create_service(base, refs = ['refs/heads/master'])
changes = post_received(base, refs).changes
described_class.new(project, current_user, changes: changes)
end
def post_received(base, refs)
change_str = refs.map { |ref| +"#{base} #{current_sha} #{ref}" }.join("\n")
post_received = ::Gitlab::GitPostReceive.new(project, key_id, change_str, {})
allow(post_received).to receive(:identify).with(key_id).and_return(current_user)
post_received
end
def current_sha
repository.gitaly_ref_client.find_branch('master')&.dereferenced_target&.id || Gitlab::Git::BLANK_SHA
end
# It is important not to re-use the WikiPage services here, since they create
# events - these helper methods below are intended to simulate actions on the repo
# that have not gone through our services.
def write_new_page
generate(:wiki_page_title).tap { |t| git_wiki.write_page(t, 'markdown', 'Hello', commit_details) }
end
# We write something to the wiki-repo that is not a page - as, for example, an
# attachment. This will appear as a raw-diff change, but wiki.find_page will
# return nil.
def write_non_page
params = {
file_name: 'attachment.log',
file_content: 'some stuff',
branch_name: 'master'
}
::Wikis::CreateAttachmentService.new(container: project, current_user: project.owner, params: params).execute
end
def update_page(title)
page = git_wiki.page(title: title)
git_wiki.update_page(page.path, title, 'markdown', 'Hey', commit_details)
end
def delete_page(path)
git_wiki.delete_page(path, commit_details)
end
def commit_details
create(:git_wiki_commit_details, author: current_user)
end
end
|