@@ -26,6 +26,9 @@ def post_app_payload(payload)
26
26
context "when GitHub hook" do
27
27
let ( :workflow ) { double ( Workflow , :id => 111 , :update_attribute => nil ) }
28
28
let ( :payload ) { RepoHost ::Github ::Responses ::Payload . post_receive_hook_pull_request }
29
+ let ( :signature ) do
30
+ "sha256=#{ OpenSSL ::HMAC . hexdigest ( OpenSSL ::Digest . new ( "sha256" ) , "secret" , payload ) } "
31
+ end
29
32
let ( :payload_request ) do
30
33
body = StringIO . new
31
34
body . puts payload
@@ -34,14 +37,15 @@ def post_app_payload(payload)
34
37
"User-Agent" => "GitHub-Hookshot/xxx" ,
35
38
"X-Github-Event" => event ,
36
39
"X-GitHub-Hook-Installation-Target-Type" => installation_target_type ,
37
- "X-Hub-Signature-256" => "sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17"
40
+ "X-Hub-Signature-256" => signature
38
41
} , :body => body , :raw_post => "Hello, World!" )
39
42
end
40
43
41
44
before do
42
45
allow ( controller ) . to receive ( :repo_host_request ) { payload_request }
43
46
allow ( Semaphore ::RepoHost ::Hooks ::Recorder ) . to receive ( :record_hook ) . and_return ( workflow )
44
47
allow ( Semaphore ::RepoHost ::Hooks ::Handler ) . to receive ( :enqueue_job )
48
+ allow ( Semaphore ::GithubApp ::Credentials ) . to receive ( :github_app_webhook_secret ) . and_return ( "secret" )
45
49
46
50
@organization = FactoryBot . create ( :organization )
47
51
@project = FactoryBot . create ( :project ,
@@ -110,7 +114,7 @@ def post_app_payload(payload)
110
114
111
115
it "calls execute on post commit service" do
112
116
expect ( Semaphore ::RepoHost ::Hooks ::Recorder ) . to receive ( :record_hook ) . and_return ( workflow )
113
- expect ( Semaphore ::RepoHost ::Hooks ::Handler ::Worker ) . to receive ( :perform_async ) . with ( 111 , "Hello, World!" , "sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17" , 0 )
117
+ expect ( Semaphore ::RepoHost ::Hooks ::Handler ::Worker ) . to receive ( :perform_async ) . with ( 111 , "Hello, World!" , signature , 0 )
114
118
115
119
post_payload ( payload )
116
120
end
@@ -148,61 +152,62 @@ def post_app_payload(payload)
148
152
149
153
context "when issue comment event occurs" do
150
154
let ( :event ) { "issue_comment" }
155
+ let ( :payload ) { RepoHost ::Github ::Responses ::Payload . post_receive_hook_issue_comment }
151
156
152
157
it "doesn't required authenticated_user" do
153
158
expect ( controller ) . not_to receive ( :authenticate_user! )
154
159
155
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
160
+ post_payload ( payload )
156
161
end
157
162
158
163
it "doesn't publish event" do
159
164
expect ( Tackle ) . not_to receive ( :publish )
160
165
expect ( Semaphore ::GithubApp ::Collaborators ::Worker ) . not_to receive ( :perform_async )
161
166
162
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
167
+ post_payload ( payload )
163
168
end
164
169
165
170
it "saves the hook payload" do
166
171
expect ( Semaphore ::RepoHost ::Hooks ::Recorder ) . to receive ( :record_hook )
167
172
168
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
173
+ post_payload ( payload )
169
174
end
170
175
171
176
it "calls execute on post commit service" do
172
177
expect ( Semaphore ::RepoHost ::Hooks ::Recorder ) . to receive ( :record_hook ) . and_return ( workflow )
173
- expect ( Semaphore ::RepoHost ::Hooks ::Handler ::Worker ) . to receive ( :perform_async ) . with ( 111 , "Hello, World!" , "sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17" , 0 )
178
+ expect ( Semaphore ::RepoHost ::Hooks ::Handler ::Worker ) . to receive ( :perform_async ) . with ( 111 , "Hello, World!" , signature , 0 )
174
179
175
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
180
+ post_payload ( payload )
176
181
end
177
182
178
183
it "saves workflow's result as OK" do
179
184
expect ( workflow ) . to receive ( :update_attribute ) . with ( :result , Workflow ::RESULT_OK )
180
185
181
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
186
+ post_payload ( payload )
182
187
end
183
188
184
189
it "responds with OK" do
185
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
190
+ post_payload ( payload )
186
191
187
192
expect ( response ) . to be_ok
188
193
end
189
194
190
195
it "measures the execution duration" do
191
196
expect ( Watchman ) . to receive ( :benchmark ) . with ( "repo_host_post_commit_hooks.controller.duration" ) . and_call_original
192
197
193
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
198
+ post_payload ( payload )
194
199
end
195
200
196
201
it "increments the handled hooks count" do
197
202
expect ( Watchman ) . to receive ( :increment ) . with ( "IncommingHooks.processed" , { external : true } ) . and_call_original
198
203
199
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
204
+ post_payload ( payload )
200
205
end
201
206
202
207
it "increments the external metric for hooks count" do
203
208
expect ( Watchman ) . to receive ( :increment ) . with ( "IncommingHooks.received" , { external : true } ) . and_call_original
204
209
205
- post_payload ( RepoHost :: Github :: Responses :: Payload . post_receive_hook_issue_comment )
210
+ post_payload ( payload )
206
211
end
207
212
end
208
213
@@ -271,17 +276,18 @@ def post_app_payload(payload)
271
276
context "when repository event occurs" do
272
277
let ( :event ) { "repository" }
273
278
let ( :installation_target_type ) { "integration" }
279
+ let ( :payload ) { RepoHost ::Github ::Responses ::Payload . repository_renamed_app_hook }
274
280
275
281
it "response with head ok" do
276
- post_payload ( RepoHost :: Github :: Responses :: Payload . repository_renamed_app_hook )
282
+ post_payload ( payload )
277
283
278
284
expect ( response ) . to be_ok
279
285
end
280
286
281
287
it "measures the execution duration" do
282
288
expect ( Watchman ) . to receive ( :benchmark ) . with ( "repo_host_post_commit_hooks.controller.duration" ) . and_call_original
283
289
284
- post_payload ( RepoHost :: Github :: Responses :: Payload . repository_renamed_app_hook )
290
+ post_payload ( payload )
285
291
end
286
292
287
293
it "increments the repository_webhook count" do
@@ -290,35 +296,36 @@ def post_app_payload(payload)
290
296
expect ( Watchman ) . to receive ( :increment ) . with ( "repo_host_post_commit_hooks.controller.github_app_webhook" ) . and_call_original
291
297
expect ( Watchman ) . to receive ( :increment ) . with ( "repo_host_post_commit_hooks.controller.repository_webhook" ) . and_call_original
292
298
293
- post_payload ( RepoHost :: Github :: Responses :: Payload . repository_renamed_app_hook )
299
+ post_payload ( payload )
294
300
end
295
301
296
302
it "perform repository sync" do
297
303
expect ( Semaphore ::GithubApp ::Repositories ::Worker ) . to receive ( :perform_async ) . and_call_original
298
304
299
- post_payload ( RepoHost :: Github :: Responses :: Payload . repository_renamed_app_hook )
305
+ post_payload ( payload )
300
306
end
301
307
end
302
308
303
309
context "when repository renamed event occurs" do
304
310
let ( :event ) { "repository" }
311
+ let ( :payload ) { RepoHost ::Github ::Responses ::Payload . repository_renamed_hook }
305
312
306
313
it "response with head ok" do
307
- post_payload ( RepoHost :: Github :: Responses :: Payload . repository_renamed_hook )
314
+ post_payload ( payload )
308
315
309
316
expect ( response ) . to be_ok
310
317
end
311
318
312
319
it "measures the execution duration" do
313
320
expect ( Watchman ) . to receive ( :benchmark ) . with ( "repo_host_post_commit_hooks.controller.duration" ) . and_call_original
314
321
315
- post_payload ( RepoHost :: Github :: Responses :: Payload . repository_renamed_hook )
322
+ post_payload ( payload )
316
323
end
317
324
318
325
it "increments the repository_renamed_webhook count" do
319
326
expect ( Watchman ) . to receive ( :increment ) . with ( "repo_host_post_commit_hooks.controller.repository_webhook" ) . and_call_original
320
327
321
- post_payload ( RepoHost :: Github :: Responses :: Payload . repository_renamed_hook )
328
+ post_payload ( payload )
322
329
end
323
330
324
331
it "publish event" do
@@ -328,29 +335,30 @@ def post_app_payload(payload)
328
335
329
336
expect ( Semaphore ::Events ::RemoteRepositoryChanged ) . to receive ( :emit ) . and_call_original
330
337
331
- post_payload ( RepoHost :: Github :: Responses :: Payload . repository_renamed_hook )
338
+ post_payload ( payload )
332
339
end
333
340
end
334
341
335
342
context "when default branch changed event occurs" do
336
343
let ( :event ) { "repository" }
344
+ let ( :payload ) { RepoHost ::Github ::Responses ::Payload . default_branch_changed }
337
345
338
346
it "response with head ok" do
339
- post_payload ( RepoHost :: Github :: Responses :: Payload . default_branch_changed )
347
+ post_payload ( payload )
340
348
341
349
expect ( response ) . to be_ok
342
350
end
343
351
344
352
it "measures the execution duration" do
345
353
expect ( Watchman ) . to receive ( :benchmark ) . with ( "repo_host_post_commit_hooks.controller.duration" ) . and_call_original
346
354
347
- post_payload ( RepoHost :: Github :: Responses :: Payload . default_branch_changed )
355
+ post_payload ( payload )
348
356
end
349
357
350
358
it "increments the default_branch_changed count" do
351
359
expect ( Watchman ) . to receive ( :increment ) . with ( "repo_host_post_commit_hooks.controller.repository_webhook" ) . and_call_original
352
360
353
- post_payload ( RepoHost :: Github :: Responses :: Payload . default_branch_changed )
361
+ post_payload ( payload )
354
362
end
355
363
356
364
it "publish event" do
@@ -360,63 +368,65 @@ def post_app_payload(payload)
360
368
361
369
expect ( Semaphore ::Events ::RemoteRepositoryChanged ) . to receive ( :emit ) . and_call_original
362
370
363
- post_payload ( RepoHost :: Github :: Responses :: Payload . default_branch_changed )
371
+ post_payload ( payload )
364
372
end
365
373
end
366
374
367
375
context "when github_app installation event occurs" do
368
376
let ( :event ) { "installation" }
377
+ let ( :payload ) { RepoHost ::Github ::Responses ::Payload . installation_created }
369
378
370
379
it "response with head ok" do
371
- post_app_payload ( RepoHost :: Github :: Responses :: Payload . installation_created )
380
+ post_app_payload ( payload )
372
381
373
382
expect ( response ) . to be_ok
374
383
end
375
384
376
385
it "measures the execution duration" do
377
386
expect ( Watchman ) . to receive ( :benchmark ) . with ( "repo_host_post_commit_hooks.controller.duration" ) . and_call_original
378
387
379
- post_app_payload ( RepoHost :: Github :: Responses :: Payload . installation_created )
388
+ post_app_payload ( payload )
380
389
end
381
390
382
391
it "increments the github_app_webhook count" do
383
392
expect ( Watchman ) . to receive ( :increment ) . with ( "repo_host_post_commit_hooks.controller.github_app_webhook" ) . and_call_original
384
393
385
- post_app_payload ( RepoHost :: Github :: Responses :: Payload . installation_created )
394
+ post_app_payload ( payload )
386
395
end
387
396
388
397
it "calls GithubApp hook processor" do
389
398
expect ( Semaphore ::GithubApp ::Hook ) . to receive ( :process ) . and_call_original
390
399
391
- post_app_payload ( RepoHost :: Github :: Responses :: Payload . installation_created )
400
+ post_app_payload ( payload )
392
401
end
393
402
end
394
403
395
404
context "when github_app installation_repositories event occurs" do
396
405
let ( :event ) { "installation_repositories" }
406
+ let ( :payload ) { RepoHost ::Github ::Responses ::Payload . installation_repositories_added }
397
407
398
408
it "response with head ok" do
399
- post_app_payload ( RepoHost :: Github :: Responses :: Payload . installation_repositories_added )
409
+ post_app_payload ( payload )
400
410
401
411
expect ( response ) . to be_ok
402
412
end
403
413
404
414
it "measures the execution duration" do
405
415
expect ( Watchman ) . to receive ( :benchmark ) . with ( "repo_host_post_commit_hooks.controller.duration" ) . and_call_original
406
416
407
- post_app_payload ( RepoHost :: Github :: Responses :: Payload . installation_repositories_added )
417
+ post_app_payload ( payload )
408
418
end
409
419
410
420
it "increments the github_app_webhook count" do
411
421
expect ( Watchman ) . to receive ( :increment ) . with ( "repo_host_post_commit_hooks.controller.github_app_webhook" ) . and_call_original
412
422
413
- post_app_payload ( RepoHost :: Github :: Responses :: Payload . installation_repositories_added )
423
+ post_app_payload ( payload )
414
424
end
415
425
416
426
it "calls GithubApp hook processor" do
417
427
expect ( Semaphore ::GithubApp ::Hook ) . to receive ( :process ) . and_call_original
418
428
419
- post_app_payload ( RepoHost :: Github :: Responses :: Payload . installation_repositories_added )
429
+ post_app_payload ( payload )
420
430
end
421
431
end
422
432
0 commit comments