@@ -53,7 +53,11 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
53
53
gh := d .GithubClientProvider
54
54
log .Printf ("GithubAppWebHook" )
55
55
56
- payload , err := github .ValidatePayload (c .Request , []byte (os .Getenv ("GITHUB_WEBHOOK_SECRET" )))
56
+ appID := c .GetHeader ("X-GitHub-Hook-Installation-Target-ID" )
57
+ log .Printf ("app id from header is: %v" , appID )
58
+ _ , _ , webhookSecret , _ , err := d .GithubClientProvider .FetchCredentials (appID )
59
+
60
+ payload , err := github .ValidatePayload (c .Request , []byte (webhookSecret ))
57
61
if err != nil {
58
62
log .Printf ("Error validating github app webhook's payload: %v" , err )
59
63
c .String (http .StatusBadRequest , "Error validating github app webhook's payload" )
@@ -70,12 +74,17 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
70
74
71
75
log .Printf ("github event type: %v\n " , reflect .TypeOf (event ))
72
76
77
+ appId64 , err := strconv .ParseInt (appID , 10 , 64 )
78
+ if err != nil {
79
+ log .Printf ("Error converting appId string to int64: %v" , err )
80
+ return
81
+ }
82
+
73
83
switch event := event .(type ) {
74
84
case * github.InstallationEvent :
75
85
log .Printf ("InstallationEvent, action: %v\n " , * event .Action )
76
-
77
86
if * event .Action == "deleted" {
78
- err := handleInstallationDeletedEvent (event )
87
+ err := handleInstallationDeletedEvent (event , appId64 )
79
88
if err != nil {
80
89
c .String (http .StatusAccepted , "Failed to handle webhook event." )
81
90
return
@@ -87,7 +96,7 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
87
96
c .String (http .StatusOK , "OK" )
88
97
return
89
98
}
90
- err : = handleIssueCommentEvent (gh , event , d .CiBackendProvider )
99
+ err = handleIssueCommentEvent (gh , event , d .CiBackendProvider , appId64 )
91
100
if err != nil {
92
101
log .Printf ("handleIssueCommentEvent error: %v" , err )
93
102
c .String (http .StatusAccepted , err .Error ())
@@ -105,7 +114,7 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
105
114
}
106
115
case * github.PullRequestEvent :
107
116
log .Printf ("Got pull request event for %d" , * event .PullRequest .ID )
108
- err : = handlePullRequestEvent (gh , event , d .CiBackendProvider )
117
+ err = handlePullRequestEvent (gh , event , d .CiBackendProvider , appId64 )
109
118
if err != nil {
110
119
log .Printf ("handlePullRequestEvent error: %v" , err )
111
120
c .String (http .StatusAccepted , err .Error ())
@@ -231,11 +240,6 @@ func (d DiggerController) GithubSetupExchangeCode(c *gin.Context) {
231
240
}
232
241
log .Printf ("Found credentials for GitHub app %v with id %d" , * cfg .Name , cfg .GetID ())
233
242
234
- _ , err = models .DB .CreateGithubApp (cfg .GetName (), cfg .GetID (), cfg .GetHTMLURL ())
235
- if err != nil {
236
- c .Error (fmt .Errorf ("Failed to create github app record on callback" ))
237
- }
238
-
239
243
PEM := cfg .GetPEM ()
240
244
PemBase64 := base64 .StdEncoding .EncodeToString ([]byte (PEM ))
241
245
c .HTML (http .StatusOK , "github_setup.tmpl" , gin.H {
@@ -299,10 +303,8 @@ generate_projects:
299
303
return repo , org , nil
300
304
}
301
305
302
- func handleInstallationDeletedEvent (installation * github.InstallationEvent ) error {
306
+ func handleInstallationDeletedEvent (installation * github.InstallationEvent , appId int64 ) error {
303
307
installationId := * installation .Installation .ID
304
- appId := * installation .Installation .AppID
305
-
306
308
link , err := models .DB .GetGithubInstallationLinkForInstallationId (installationId )
307
309
if err != nil {
308
310
return err
@@ -323,13 +325,7 @@ func handleInstallationDeletedEvent(installation *github.InstallationEvent) erro
323
325
return nil
324
326
}
325
327
326
- func handlePullRequestEvent (gh utils.GithubClientProvider , payload * github.PullRequestEvent , ciBackendProvider ci_backends.CiBackendProvider ) error {
327
- appId , err := strconv .ParseInt (os .Getenv ("GITHUB_APP_ID" ), 10 , 64 )
328
- if err != nil {
329
- log .Printf ("error getting github app installation id: %v" , err )
330
- return fmt .Errorf ("error getting github app installation id" )
331
- }
332
-
328
+ func handlePullRequestEvent (gh utils.GithubClientProvider , payload * github.PullRequestEvent , ciBackendProvider ci_backends.CiBackendProvider , appId int64 ) error {
333
329
installationId := * payload .Installation .ID
334
330
repoName := * payload .Repo .Name
335
331
repoOwner := * payload .Repo .Owner .Login
@@ -645,13 +641,7 @@ func getBatchType(jobs []orchestrator_scheduler.Job) orchestrator_scheduler.Digg
645
641
}
646
642
}
647
643
648
- func handleIssueCommentEvent (gh utils.GithubClientProvider , payload * github.IssueCommentEvent , ciBackendProvider ci_backends.CiBackendProvider ) error {
649
- appId , err := strconv .ParseInt (os .Getenv ("GITHUB_APP_ID" ), 10 , 64 )
650
- if err != nil {
651
- log .Printf ("error getting github app installation id: %v" , err )
652
- return fmt .Errorf ("error getting github app installation id" )
653
- }
654
-
644
+ func handleIssueCommentEvent (gh utils.GithubClientProvider , payload * github.IssueCommentEvent , ciBackendProvider ci_backends.CiBackendProvider , appId int64 ) error {
655
645
installationId := * payload .Installation .ID
656
646
repoName := * payload .Repo .Name
657
647
repoOwner := * payload .Repo .Owner .Login
@@ -1028,8 +1018,14 @@ func (d DiggerController) GithubAppCallbackPage(c *gin.Context) {
1028
1018
installationId := c .Request .URL .Query ()["installation_id" ][0 ]
1029
1019
//setupAction := c.Request.URL.Query()["setup_action"][0]
1030
1020
code := c .Request .URL .Query ()["code" ][0 ]
1031
- clientId := os .Getenv ("GITHUB_APP_CLIENT_ID" )
1032
- clientSecret := os .Getenv ("GITHUB_APP_CLIENT_SECRET" )
1021
+ appId := c .Request .URL .Query ().Get ("state" )
1022
+
1023
+ clientId , clientSecret , _ , _ , err := d .GithubClientProvider .FetchCredentials (appId )
1024
+ if err != nil {
1025
+ log .Printf ("could not fetch credentials for the app: %v" , err )
1026
+ c .String (500 , "could not find credentials for github app" )
1027
+ return
1028
+ }
1033
1029
1034
1030
installationId64 , err := strconv .ParseInt (installationId , 10 , 64 )
1035
1031
if err != nil {
0 commit comments