@@ -10,14 +10,18 @@ import (
10
10
"io"
11
11
"io/ioutil"
12
12
"net/http"
13
+ "os"
13
14
"regexp"
14
15
"time"
15
16
16
17
"github.com/jenkins-x/go-scm/scm"
17
18
"github.com/jenkins-x/go-scm/scm/driver/internal/hmac"
18
19
"github.com/jenkins-x/go-scm/scm/driver/internal/null"
20
+ "github.com/sirupsen/logrus"
19
21
)
20
22
23
+ var logWebHooks = os .Getenv ("GO_SCM_LOG_WEBHOOKS" ) == "true"
24
+
21
25
type webhookService struct {
22
26
client * wrapper
23
27
}
@@ -30,6 +34,15 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
30
34
return nil , err
31
35
}
32
36
37
+ if logWebHooks {
38
+ log := logrus .WithFields (map [string ]interface {}{
39
+ "URL" : req .URL ,
40
+ "Headers" : req .Header ,
41
+ "Body" : string (data ),
42
+ })
43
+ log .Infof ("received webhook" )
44
+ }
45
+
33
46
guid := req .Header .Get ("X-GitHub-Delivery" )
34
47
if guid == "" {
35
48
return nil , scm.MissingHeader {"X-GitHub-Delivery" }
@@ -53,7 +66,7 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
53
66
// case "issues":
54
67
case "issue_comment" :
55
68
hook , err = s .parseIssueCommentHook (data )
56
- case "installation" :
69
+ case "installation" , "integration_installation" :
57
70
hook , err = s .parseInstallationHook (data )
58
71
default :
59
72
return nil , scm.UnknownWebhook {event }
@@ -206,10 +219,11 @@ func (s *webhookService) parseInstallationHook(data []byte) (*scm.InstallationHo
206
219
type (
207
220
// github create webhook payload
208
221
createDeleteHook struct {
209
- Ref string `json:"ref"`
210
- RefType string `json:"ref_type"`
211
- Repository repository `json:"repository"`
212
- Sender user `json:"sender"`
222
+ Ref string `json:"ref"`
223
+ RefType string `json:"ref_type"`
224
+ Repository repository `json:"repository"`
225
+ Sender user `json:"sender"`
226
+ Installation * installationRef `json:"installation"`
213
227
}
214
228
215
229
pushCommit struct {
@@ -281,8 +295,9 @@ type (
281
295
CloneURL string `json:"clone_url"`
282
296
DefaultBranch string `json:"default_branch"`
283
297
} `json:"repository"`
284
- Pusher user `json:"pusher"`
285
- Sender user `json:"sender"`
298
+ Pusher user `json:"pusher"`
299
+ Sender user `json:"sender"`
300
+ Installation * installationRef `json:"installation"`
286
301
}
287
302
288
303
pullRequestHookChanges struct {
@@ -297,13 +312,14 @@ type (
297
312
}
298
313
299
314
pullRequestHook struct {
300
- Action string `json:"action"`
301
- Number int `json:"number"`
302
- PullRequest pr `json:"pull_request"`
303
- Repository repository `json:"repository"`
304
- Label label `json:"label"`
305
- Sender user `json:"sender"`
306
- Changes pullRequestHookChanges `json:"changes"`
315
+ Action string `json:"action"`
316
+ Number int `json:"number"`
317
+ PullRequest pr `json:"pull_request"`
318
+ Repository repository `json:"repository"`
319
+ Label label `json:"label"`
320
+ Sender user `json:"sender"`
321
+ Changes pullRequestHookChanges `json:"changes"`
322
+ Installation * installationRef `json:"installation"`
307
323
}
308
324
309
325
label struct {
@@ -315,25 +331,20 @@ type (
315
331
316
332
pullRequestReviewCommentHook struct {
317
333
// Action see https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent
318
- Action string `json:"action"`
319
- PullRequest pr `json:"pull_request"`
320
- Repository repository `json:"repository"`
321
- Comment reviewComment `json:"comment"`
334
+ Action string `json:"action"`
335
+ PullRequest pr `json:"pull_request"`
336
+ Repository repository `json:"repository"`
337
+ Comment reviewComment `json:"comment"`
338
+ Installation * installationRef `json:"installation"`
322
339
}
323
340
324
341
issueCommentHook struct {
325
- Action string `json:"action"`
326
- Issue issue `json:"issue"`
327
- Repository repository `json:"repository"`
328
- Comment issueComment `json:"comment"`
329
- Sender user `json:"sender"`
330
- }
331
-
332
- installationHook struct {
333
- Action string `json:"action"`
334
- Repositories []* repository `json:"repositories"`
335
- Installation * installation `json:"installation"`
336
- Sender * user `json:"sender"`
342
+ Action string `json:"action"`
343
+ Issue issue `json:"issue"`
344
+ Repository repository `json:"repository"`
345
+ Comment issueComment `json:"comment"`
346
+ Sender user `json:"sender"`
347
+ Installation * installationRef `json:"installation"`
337
348
}
338
349
339
350
// reviewComment describes a Pull Request review comment
@@ -363,13 +374,23 @@ type (
363
374
Task null.String `json:"task"`
364
375
Payload interface {} `json:"payload"`
365
376
} `json:"deployment"`
366
- Repository repository `json:"repository"`
367
- Sender user `json:"sender"`
377
+ Repository repository `json:"repository"`
378
+ Sender user `json:"sender"`
379
+ Installation * installationRef `json:"installation"`
380
+ }
381
+
382
+ // installationHook a webhook invoked when the GitHub App is installed
383
+ installationHook struct {
384
+ Action string `json:"action"`
385
+ Repositories []* repository `json:"repositories"`
386
+ Installation * installation `json:"installation"`
387
+ Sender * user `json:"sender"`
368
388
}
369
389
370
390
// github app installation
371
391
installation struct {
372
- ID int64 `json:"id"`
392
+ ID int64 `json:"id"`
393
+ NodeID string `json:"id"`
373
394
Account struct {
374
395
ID int `json:"id"`
375
396
Login string `json:"login"`
@@ -378,13 +399,22 @@ type (
378
399
RepositoriesURL string `json:"repositories_url"`
379
400
HTMLURL string `json:"html_url"`
380
401
}
402
+
403
+ // github app installation reference
404
+ installationRef struct {
405
+ ID int64 `json:"id"`
406
+ NodeID string `json:"node_id"`
407
+ }
381
408
)
382
409
383
410
//
384
411
// native data structure conversion
385
412
//
386
413
387
414
func convertInstallationHook (dst * installationHook ) * scm.InstallationHook {
415
+ if dst == nil {
416
+ return nil
417
+ }
388
418
return & scm.InstallationHook {
389
419
Action : convertAction (dst .Action ),
390
420
Repos : convertRepositoryList (dst .Repositories ),
@@ -393,9 +423,12 @@ func convertInstallationHook(dst *installationHook) *scm.InstallationHook {
393
423
}
394
424
}
395
425
396
- func convertInstallation (dst * installation ) scm.Installation {
426
+ func convertInstallation (dst * installation ) * scm.Installation {
427
+ if dst == nil {
428
+ return nil
429
+ }
397
430
acc := dst .Account
398
- return scm.Installation {
431
+ return & scm.Installation {
399
432
ID : dst .ID ,
400
433
Account : scm.Account {
401
434
ID : acc .ID ,
@@ -405,6 +438,16 @@ func convertInstallation(dst *installation) scm.Installation {
405
438
}
406
439
}
407
440
441
+ func convertInstallationRef (dst * installationRef ) * scm.InstallationRef {
442
+ if dst == nil {
443
+ return nil
444
+ }
445
+ return & scm.InstallationRef {
446
+ ID : dst .ID ,
447
+ NodeID : dst .NodeID ,
448
+ }
449
+ }
450
+
408
451
func convertPushHook (src * pushHook ) * scm.PushHook {
409
452
dst := & scm.PushHook {
410
453
Ref : src .Ref ,
@@ -444,7 +487,8 @@ func convertPushHook(src *pushHook) *scm.PushHook {
444
487
CloneSSH : src .Repository .SSHURL ,
445
488
Link : src .Repository .HTMLURL ,
446
489
},
447
- Sender : * convertUser (& src .Sender ),
490
+ Sender : * convertUser (& src .Sender ),
491
+ Installation : convertInstallationRef (src .Installation ),
448
492
}
449
493
// fix https://github.com/jenkins-x/go-scm/issues/8
450
494
if scm .IsTag (dst .Ref ) && src .Head .ID != "" {
@@ -490,7 +534,8 @@ func convertBranchHook(src *createDeleteHook) *scm.BranchHook {
490
534
CloneSSH : src .Repository .SSHURL ,
491
535
Link : src .Repository .HTMLURL ,
492
536
},
493
- Sender : * convertUser (& src .Sender ),
537
+ Sender : * convertUser (& src .Sender ),
538
+ Installation : convertInstallationRef (src .Installation ),
494
539
}
495
540
}
496
541
@@ -509,7 +554,8 @@ func convertTagHook(src *createDeleteHook) *scm.TagHook {
509
554
CloneSSH : src .Repository .SSHURL ,
510
555
Link : src .Repository .HTMLURL ,
511
556
},
512
- Sender : * convertUser (& src .Sender ),
557
+ Sender : * convertUser (& src .Sender ),
558
+ Installation : convertInstallationRef (src .Installation ),
513
559
}
514
560
}
515
561
@@ -526,10 +572,11 @@ func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook {
526
572
CloneSSH : src .Repository .SSHURL ,
527
573
Link : src .Repository .HTMLURL ,
528
574
},
529
- Label : convertLabel (src .Label ),
530
- PullRequest : * convertPullRequest (& src .PullRequest ),
531
- Sender : * convertUser (& src .Sender ),
532
- Changes : * convertPullRequestChanges (& src .Changes ),
575
+ Label : convertLabel (src .Label ),
576
+ PullRequest : * convertPullRequest (& src .PullRequest ),
577
+ Sender : * convertUser (& src .Sender ),
578
+ Changes : * convertPullRequestChanges (& src .Changes ),
579
+ Installation : convertInstallationRef (src .Installation ),
533
580
}
534
581
}
535
582
@@ -562,9 +609,10 @@ func convertPullRequestReviewCommentHook(src *pullRequestReviewCommentHook) *scm
562
609
CloneSSH : src .Repository .SSHURL ,
563
610
Link : src .Repository .HTMLURL ,
564
611
},
565
- PullRequest : * convertPullRequest (& src .PullRequest ),
566
- Comment : * convertPullRequestComment (& src .Comment ),
567
- Sender : * convertUser (& src .Comment .User ),
612
+ PullRequest : * convertPullRequest (& src .PullRequest ),
613
+ Comment : * convertPullRequestComment (& src .Comment ),
614
+ Sender : * convertUser (& src .Comment .User ),
615
+ Installation : convertInstallationRef (src .Installation ),
568
616
}
569
617
}
570
618
@@ -575,17 +623,19 @@ func convertIssueHook(dst *issueHook) *scm.IssueHook {
575
623
Issue: *convertIssue(&dst.Issue),
576
624
Repo: *convertRepository(&dst.Repository),
577
625
Sender: *convertUser(&dst.Sender),
626
+ Installation: convertInstallationRef(src.Installation),
578
627
}
579
628
}
580
629
*/
581
630
582
631
func convertIssueCommentHook (dst * issueCommentHook ) * scm.IssueCommentHook {
583
632
return & scm.IssueCommentHook {
584
- Action : convertAction (dst .Action ),
585
- Issue : * convertIssue (& dst .Issue ),
586
- Comment : * convertIssueComment (& dst .Comment ),
587
- Repo : * convertRepository (& dst .Repository ),
588
- Sender : * convertUser (& dst .Sender ),
633
+ Action : convertAction (dst .Action ),
634
+ Issue : * convertIssue (& dst .Issue ),
635
+ Comment : * convertIssueComment (& dst .Comment ),
636
+ Repo : * convertRepository (& dst .Repository ),
637
+ Sender : * convertUser (& dst .Sender ),
638
+ Installation : convertInstallationRef (dst .Installation ),
589
639
}
590
640
}
591
641
@@ -618,10 +668,11 @@ func convertDeploymentHook(src *deploymentHook) *scm.DeployHook {
618
668
CloneSSH : src .Repository .SSHURL ,
619
669
Link : src .Repository .HTMLURL ,
620
670
},
621
- Sender : * convertUser (& src .Sender ),
622
- Task : src .Deployment .Task .String ,
623
- Target : src .Deployment .Environment .String ,
624
- TargetURL : src .Deployment .EnvironmentURL .String ,
671
+ Sender : * convertUser (& src .Sender ),
672
+ Task : src .Deployment .Task .String ,
673
+ Target : src .Deployment .Environment .String ,
674
+ TargetURL : src .Deployment .EnvironmentURL .String ,
675
+ Installation : convertInstallationRef (src .Installation ),
625
676
}
626
677
if tagRE .MatchString (dst .Ref .Name ) {
627
678
dst .Ref .Path = scm .ExpandRef (dst .Ref .Path , "refs/tags/" )
0 commit comments