Skip to content

Commit dd3658e

Browse files
Merge pull request drone#38 from jstrachan/stuff
fix: add support for passing the GitHub App installation header
2 parents 64e8b26 + f881d8b commit dd3658e

File tree

5 files changed

+206
-149
lines changed

5 files changed

+206
-149
lines changed
Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1 @@
1-
{
2-
"Action": "deleted",
3-
"Repos": [
4-
{
5-
"ID": "1296269",
6-
"Namespace": "",
7-
"Name": "Hello-World",
8-
"FullName": "octocat/Hello-World",
9-
"Perm": {
10-
"Pull": false,
11-
"Push": false,
12-
"Admin": false
13-
},
14-
"Branch": "",
15-
"Private": false,
16-
"Clone": "",
17-
"CloneSSH": "",
18-
"Link": "",
19-
"Created": "0001-01-01T00:00:00Z",
20-
"Updated": "0001-01-01T00:00:00Z"
21-
}
22-
],
23-
"Installation": {
24-
"ID": 2,
25-
"Account": {
26-
"ID": 1,
27-
"Login": "octocat"
28-
},
29-
"AccessTokensLink": "https://api.github.com/installations/2/access_tokens"
30-
},
31-
"Sender": {
32-
"Login": "octocat",
33-
"Name": "",
34-
"Email": "",
35-
"Avatar": "https://github.com/images/error/octocat_happy.gif",
36-
"Link": "https://github.com/octocat",
37-
"Created": "0001-01-01T00:00:00Z",
38-
"Updated": "0001-01-01T00:00:00Z"
39-
}
40-
}
1+
{"Action":"deleted","Repos":[{"ID":"1296269","Namespace":"","Name":"Hello-World","FullName":"octocat/Hello-World","Perm":{"Pull":false,"Push":false,"Admin":false},"Branch":"","Private":false,"Clone":"","CloneSSH":"","Link":"","Created":"0001-01-01T00:00:00Z","Updated":"0001-01-01T00:00:00Z"}],"Sender":{"Login":"octocat","Name":"","Email":"","Avatar":"https://github.com/images/error/octocat_happy.gif","Link":"https://github.com/octocat","Created":"0001-01-01T00:00:00Z","Updated":"0001-01-01T00:00:00Z"},"Installation":{"ID":0,"Account":{"ID":1,"Login":"octocat"},"AccessTokensLink":"https://api.github.com/installations/2/access_tokens"}}

scm/driver/github/testdata/webhooks/issue_comment.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,5 +273,9 @@
273273
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
274274
"type": "User",
275275
"site_admin": false
276+
},
277+
"installation": {
278+
"id": 456789,
279+
"node_id": "SomeNode"
276280
}
277281
}

scm/driver/github/testdata/webhooks/issue_comment.json.golden

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,9 @@
7070
"Link": "https://github.com/Codertocat",
7171
"Created": "0001-01-01T00:00:00Z",
7272
"Updated": "0001-01-01T00:00:00Z"
73+
},
74+
"Installation": {
75+
"ID": 456789,
76+
"NodeID": "SomeNode"
7377
}
7478
}

scm/driver/github/webhook.go

Lines changed: 105 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ import (
1010
"io"
1111
"io/ioutil"
1212
"net/http"
13+
"os"
1314
"regexp"
1415
"time"
1516

1617
"github.com/jenkins-x/go-scm/scm"
1718
"github.com/jenkins-x/go-scm/scm/driver/internal/hmac"
1819
"github.com/jenkins-x/go-scm/scm/driver/internal/null"
20+
"github.com/sirupsen/logrus"
1921
)
2022

23+
var logWebHooks = os.Getenv("GO_SCM_LOG_WEBHOOKS") == "true"
24+
2125
type webhookService struct {
2226
client *wrapper
2327
}
@@ -30,6 +34,15 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
3034
return nil, err
3135
}
3236

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+
3346
guid := req.Header.Get("X-GitHub-Delivery")
3447
if guid == "" {
3548
return nil, scm.MissingHeader{"X-GitHub-Delivery"}
@@ -53,7 +66,7 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
5366
// case "issues":
5467
case "issue_comment":
5568
hook, err = s.parseIssueCommentHook(data)
56-
case "installation":
69+
case "installation", "integration_installation":
5770
hook, err = s.parseInstallationHook(data)
5871
default:
5972
return nil, scm.UnknownWebhook{event}
@@ -206,10 +219,11 @@ func (s *webhookService) parseInstallationHook(data []byte) (*scm.InstallationHo
206219
type (
207220
// github create webhook payload
208221
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"`
213227
}
214228

215229
pushCommit struct {
@@ -281,8 +295,9 @@ type (
281295
CloneURL string `json:"clone_url"`
282296
DefaultBranch string `json:"default_branch"`
283297
} `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"`
286301
}
287302

288303
pullRequestHookChanges struct {
@@ -297,13 +312,14 @@ type (
297312
}
298313

299314
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"`
307323
}
308324

309325
label struct {
@@ -315,25 +331,20 @@ type (
315331

316332
pullRequestReviewCommentHook struct {
317333
// 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"`
322339
}
323340

324341
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"`
337348
}
338349

339350
// reviewComment describes a Pull Request review comment
@@ -363,13 +374,23 @@ type (
363374
Task null.String `json:"task"`
364375
Payload interface{} `json:"payload"`
365376
} `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"`
368388
}
369389

370390
// github app installation
371391
installation struct {
372-
ID int64 `json:"id"`
392+
ID int64 `json:"id"`
393+
NodeID string `json:"id"`
373394
Account struct {
374395
ID int `json:"id"`
375396
Login string `json:"login"`
@@ -378,13 +399,22 @@ type (
378399
RepositoriesURL string `json:"repositories_url"`
379400
HTMLURL string `json:"html_url"`
380401
}
402+
403+
// github app installation reference
404+
installationRef struct {
405+
ID int64 `json:"id"`
406+
NodeID string `json:"node_id"`
407+
}
381408
)
382409

383410
//
384411
// native data structure conversion
385412
//
386413

387414
func convertInstallationHook(dst *installationHook) *scm.InstallationHook {
415+
if dst == nil {
416+
return nil
417+
}
388418
return &scm.InstallationHook{
389419
Action: convertAction(dst.Action),
390420
Repos: convertRepositoryList(dst.Repositories),
@@ -393,9 +423,12 @@ func convertInstallationHook(dst *installationHook) *scm.InstallationHook {
393423
}
394424
}
395425

396-
func convertInstallation(dst *installation) scm.Installation {
426+
func convertInstallation(dst *installation) *scm.Installation {
427+
if dst == nil {
428+
return nil
429+
}
397430
acc := dst.Account
398-
return scm.Installation{
431+
return &scm.Installation{
399432
ID: dst.ID,
400433
Account: scm.Account{
401434
ID: acc.ID,
@@ -405,6 +438,16 @@ func convertInstallation(dst *installation) scm.Installation {
405438
}
406439
}
407440

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+
408451
func convertPushHook(src *pushHook) *scm.PushHook {
409452
dst := &scm.PushHook{
410453
Ref: src.Ref,
@@ -444,7 +487,8 @@ func convertPushHook(src *pushHook) *scm.PushHook {
444487
CloneSSH: src.Repository.SSHURL,
445488
Link: src.Repository.HTMLURL,
446489
},
447-
Sender: *convertUser(&src.Sender),
490+
Sender: *convertUser(&src.Sender),
491+
Installation: convertInstallationRef(src.Installation),
448492
}
449493
// fix https://github.com/jenkins-x/go-scm/issues/8
450494
if scm.IsTag(dst.Ref) && src.Head.ID != "" {
@@ -490,7 +534,8 @@ func convertBranchHook(src *createDeleteHook) *scm.BranchHook {
490534
CloneSSH: src.Repository.SSHURL,
491535
Link: src.Repository.HTMLURL,
492536
},
493-
Sender: *convertUser(&src.Sender),
537+
Sender: *convertUser(&src.Sender),
538+
Installation: convertInstallationRef(src.Installation),
494539
}
495540
}
496541

@@ -509,7 +554,8 @@ func convertTagHook(src *createDeleteHook) *scm.TagHook {
509554
CloneSSH: src.Repository.SSHURL,
510555
Link: src.Repository.HTMLURL,
511556
},
512-
Sender: *convertUser(&src.Sender),
557+
Sender: *convertUser(&src.Sender),
558+
Installation: convertInstallationRef(src.Installation),
513559
}
514560
}
515561

@@ -526,10 +572,11 @@ func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook {
526572
CloneSSH: src.Repository.SSHURL,
527573
Link: src.Repository.HTMLURL,
528574
},
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),
533580
}
534581
}
535582

@@ -562,9 +609,10 @@ func convertPullRequestReviewCommentHook(src *pullRequestReviewCommentHook) *scm
562609
CloneSSH: src.Repository.SSHURL,
563610
Link: src.Repository.HTMLURL,
564611
},
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),
568616
}
569617
}
570618

@@ -575,17 +623,19 @@ func convertIssueHook(dst *issueHook) *scm.IssueHook {
575623
Issue: *convertIssue(&dst.Issue),
576624
Repo: *convertRepository(&dst.Repository),
577625
Sender: *convertUser(&dst.Sender),
626+
Installation: convertInstallationRef(src.Installation),
578627
}
579628
}
580629
*/
581630

582631
func convertIssueCommentHook(dst *issueCommentHook) *scm.IssueCommentHook {
583632
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),
589639
}
590640
}
591641

@@ -618,10 +668,11 @@ func convertDeploymentHook(src *deploymentHook) *scm.DeployHook {
618668
CloneSSH: src.Repository.SSHURL,
619669
Link: src.Repository.HTMLURL,
620670
},
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),
625676
}
626677
if tagRE.MatchString(dst.Ref.Name) {
627678
dst.Ref.Path = scm.ExpandRef(dst.Ref.Path, "refs/tags/")

0 commit comments

Comments
 (0)