@@ -36,13 +36,14 @@ import (
36
36
"golang.org/x/oauth2"
37
37
)
38
38
39
- type GithubController struct {
40
- CiBackendProvider ci_backends.CiBackendProvider
39
+ type DiggerController struct {
40
+ CiBackendProvider ci_backends.CiBackendProvider
41
+ GithubClientProvider utils.GithubClientProvider
41
42
}
42
43
43
- func (g GithubController ) GithubAppWebHook (c * gin.Context ) {
44
+ func (d DiggerController ) GithubAppWebHook (c * gin.Context ) {
44
45
c .Header ("Content-Type" , "application/json" )
45
- gh := & utils. DiggerGithubRealClientProvider {}
46
+ gh := d . GithubClientProvider
46
47
log .Printf ("GithubAppWebHook" )
47
48
48
49
payload , err := github .ValidatePayload (c .Request , []byte (os .Getenv ("GITHUB_WEBHOOK_SECRET" )))
@@ -100,15 +101,15 @@ func (g GithubController) GithubAppWebHook(c *gin.Context) {
100
101
c .String (http .StatusOK , "OK" )
101
102
return
102
103
}
103
- err := handleIssueCommentEvent (gh , event , g .CiBackendProvider )
104
+ err := handleIssueCommentEvent (gh , event , d .CiBackendProvider )
104
105
if err != nil {
105
106
log .Printf ("handleIssueCommentEvent error: %v" , err )
106
107
c .String (http .StatusInternalServerError , err .Error ())
107
108
return
108
109
}
109
110
case * github.PullRequestEvent :
110
111
log .Printf ("Got pull request event for %d" , * event .PullRequest .ID )
111
- err := handlePullRequestEvent (gh , event , g .CiBackendProvider )
112
+ err := handlePullRequestEvent (gh , event , d .CiBackendProvider )
112
113
if err != nil {
113
114
log .Printf ("handlePullRequestEvent error: %v" , err )
114
115
c .String (http .StatusInternalServerError , err .Error ())
@@ -191,9 +192,10 @@ func GithubAppSetup(c *gin.Context) {
191
192
},
192
193
}
193
194
195
+ githubHostname := getGithubHostname ()
194
196
url := & url.URL {
195
197
Scheme : "https" ,
196
- Host : "github.com" ,
198
+ Host : githubHostname ,
197
199
Path : "/settings/apps/new" ,
198
200
}
199
201
@@ -212,16 +214,36 @@ func GithubAppSetup(c *gin.Context) {
212
214
c .HTML (http .StatusOK , "github_setup.tmpl" , gin.H {"Target" : url .String (), "Manifest" : string (jsonManifest )})
213
215
}
214
216
217
+ func getGithubHostname () string {
218
+ githubHostname := os .Getenv ("DIGGER_GITHUB_HOSTNAME" )
219
+ if githubHostname == "" {
220
+ githubHostname = "github.com"
221
+ }
222
+ return githubHostname
223
+ }
224
+
215
225
// GithubSetupExchangeCode handles the user coming back from creating their app
216
226
// A code query parameter is exchanged for this app's ID, key, and webhook_secret
217
227
// Implements https://developer.github.com/apps/building-github-apps/creating-github-apps-from-a-manifest/#implementing-the-github-app-manifest-flow
218
- func GithubSetupExchangeCode (c * gin.Context ) {
228
+ func ( d DiggerController ) GithubSetupExchangeCode (c * gin.Context ) {
219
229
code := c .Query ("code" )
220
230
if code == "" {
221
231
c .Error (fmt .Errorf ("Ignoring callback, missing code query parameter" ))
222
232
}
223
233
224
- client := github .NewClient (nil )
234
+ // TODO: to make tls verification configurable for debug purposes
235
+ //var transport *http.Transport = nil
236
+ //_, exists := os.LookupEnv("DIGGER_GITHUB_SKIP_TLS")
237
+ //if exists {
238
+ // transport = &http.Transport{
239
+ // TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
240
+ // }
241
+ //}
242
+
243
+ client , err := d .GithubClientProvider .NewClient (nil )
244
+ if err != nil {
245
+ c .Error (fmt .Errorf ("could not create github client: %v" , err ))
246
+ }
225
247
cfg , _ , err := client .Apps .CompleteAppManifest (context .Background (), code )
226
248
if err != nil {
227
249
c .Error (fmt .Errorf ("Failed to exchange code for github app: %s" , err ))
@@ -584,6 +606,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
584
606
585
607
ciBackend , err := ciBackendProvider .GetCiBackend (
586
608
ci_backends.CiBackendOptions {
609
+ GithubClientProvider : gh ,
587
610
GithubInstallationId : installationId ,
588
611
RepoName : repoName ,
589
612
RepoOwner : repoOwner ,
@@ -870,6 +893,7 @@ func handleIssueCommentEvent(gh utils.GithubClientProvider, payload *github.Issu
870
893
871
894
ciBackend , err := ciBackendProvider .GetCiBackend (
872
895
ci_backends.CiBackendOptions {
896
+ GithubClientProvider : gh ,
873
897
GithubInstallationId : installationId ,
874
898
RepoName : repoName ,
875
899
RepoOwner : repoOwner ,
@@ -1057,7 +1081,7 @@ jobs:
1057
1081
return nil
1058
1082
}
1059
1083
1060
- func GithubAppCallbackPage (c * gin.Context ) {
1084
+ func ( d DiggerController ) GithubAppCallbackPage (c * gin.Context ) {
1061
1085
installationId := c .Request .URL .Query ()["installation_id" ][0 ]
1062
1086
//setupAction := c.Request.URL.Query()["setup_action"][0]
1063
1087
code := c .Request .URL .Query ()["code" ][0 ]
@@ -1077,7 +1101,7 @@ func GithubAppCallbackPage(c *gin.Context) {
1077
1101
return
1078
1102
}
1079
1103
1080
- result , err := validateGithubCallback (clientId , clientSecret , code , installationId64 )
1104
+ result , err := validateGithubCallback (d . GithubClientProvider , clientId , clientSecret , code , installationId64 )
1081
1105
if ! result {
1082
1106
log .Printf ("Failed to validated installation id, %v\n " , err )
1083
1107
c .String (http .StatusInternalServerError , "Failed to validate installation_id." )
@@ -1100,7 +1124,7 @@ func GithubAppCallbackPage(c *gin.Context) {
1100
1124
c .HTML (http .StatusOK , "github_success.tmpl" , gin.H {})
1101
1125
}
1102
1126
1103
- func GithubReposPage (c * gin.Context ) {
1127
+ func ( d DiggerController ) GithubReposPage (c * gin.Context ) {
1104
1128
orgId , exists := c .Get (middleware .ORGANISATION_ID_KEY )
1105
1129
if ! exists {
1106
1130
log .Printf ("Organisation ID not found in context" )
@@ -1127,7 +1151,7 @@ func GithubReposPage(c *gin.Context) {
1127
1151
return
1128
1152
}
1129
1153
1130
- gh := & utils. DiggerGithubRealClientProvider {}
1154
+ gh := d . GithubClientProvider
1131
1155
client , _ , err := gh .Get (installations [0 ].GithubAppId , installations [0 ].GithubInstallationId )
1132
1156
if err != nil {
1133
1157
log .Printf ("failed to create github client, %v" , err )
@@ -1147,14 +1171,15 @@ func GithubReposPage(c *gin.Context) {
1147
1171
1148
1172
// why this validation is needed: https://roadie.io/blog/avoid-leaking-github-org-data/
1149
1173
// validation based on https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app , step 3
1150
- func validateGithubCallback (clientId string , clientSecret string , code string , installationId int64 ) (bool , error ) {
1174
+ func validateGithubCallback (githubClientProvider utils. GithubClientProvider , clientId string , clientSecret string , code string , installationId int64 ) (bool , error ) {
1151
1175
ctx := context .Background ()
1152
1176
type OAuthAccessResponse struct {
1153
1177
AccessToken string `json:"access_token"`
1154
1178
}
1155
1179
httpClient := http.Client {}
1156
1180
1157
- reqURL := fmt .Sprintf ("https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s" , clientId , clientSecret , code )
1181
+ githubHostname := getGithubHostname ()
1182
+ reqURL := fmt .Sprintf ("https://%v/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s" , githubHostname , clientId , clientSecret , code )
1158
1183
req , err := http .NewRequest (http .MethodPost , reqURL , nil )
1159
1184
if err != nil {
1160
1185
return false , fmt .Errorf ("could not create HTTP request: %v\n " , err )
@@ -1179,11 +1204,27 @@ func validateGithubCallback(clientId string, clientSecret string, code string, i
1179
1204
& oauth2.Token {AccessToken : t .AccessToken },
1180
1205
)
1181
1206
tc := oauth2 .NewClient (ctx , ts )
1182
- client := github .NewClient (tc )
1207
+ //tc := &http.Client{
1208
+ // Transport: &oauth2.Transport{
1209
+ // Base: httpClient.Transport,
1210
+ // Source: oauth2.ReuseTokenSource(nil, ts),
1211
+ // },
1212
+ //}
1213
+
1214
+ client , err := githubClientProvider .NewClient (tc )
1215
+ if err != nil {
1216
+ log .Printf ("could create github client: %v" , err )
1217
+ return false , fmt .Errorf ("could not create github client: %v" , err )
1218
+ }
1183
1219
1184
1220
installationIdMatch := false
1185
1221
// list all installations for the user
1186
1222
installations , _ , err := client .Apps .ListUserInstallations (ctx , nil )
1223
+ if err != nil {
1224
+ log .Printf ("could not retrieve installations: %v" , err )
1225
+ return false , fmt .Errorf ("could not retrieve installations: %v" , installationId )
1226
+ }
1227
+ log .Printf ("installations %v" , installations )
1187
1228
for _ , v := range installations {
1188
1229
log .Printf ("installation id: %v\n " , * v .ID )
1189
1230
if * v .ID == installationId {
0 commit comments