@@ -42,6 +42,7 @@ const (
42
42
// githubAccessTokenUsername is a username that is used to with the github access token
43
43
githubAccessTokenUsername = "x-access-token"
44
44
forceBasicAuthHeaderEnv = "ARGOCD_GIT_AUTH_HEADER"
45
+ defaultGithubApiUrl = "https://api.github.com"
45
46
)
46
47
47
48
func init () {
@@ -443,12 +444,7 @@ func (g GitHubAppCreds) getAccessToken() (string, error) {
443
444
return itr .Token (ctx )
444
445
}
445
446
446
- // GitHub API url
447
- baseUrl := "https://api.github.com"
448
- if g .baseURL != "" {
449
- baseUrl = strings .TrimSuffix (g .baseURL , "/" )
450
- }
451
-
447
+ baseUrl := g .getBaseURL ()
452
448
// Create a new GitHub transport
453
449
c := GetRepoHTTPClient (baseUrl , g .insecure , g , g .proxy )
454
450
itr , err := ghinstallation .New (c .Transport ,
@@ -468,6 +464,27 @@ func (g GitHubAppCreds) getAccessToken() (string, error) {
468
464
return itr .Token (ctx )
469
465
}
470
466
467
+ func (g GitHubAppCreds ) getBaseURL () string {
468
+ if g .baseURL != "" {
469
+ return strings .TrimSuffix (g .baseURL , "/" )
470
+ }
471
+ if g .repoURL == "" {
472
+ return defaultGithubApiUrl
473
+ }
474
+
475
+ repoUrl , err := url .Parse (g .repoURL )
476
+ if err != nil || repoUrl .Hostname () == "github.com" {
477
+ return defaultGithubApiUrl
478
+ }
479
+
480
+ // GitHub Enterprise
481
+ scheme := repoUrl .Scheme
482
+ if scheme == "" {
483
+ scheme = "https"
484
+ }
485
+ return fmt .Sprintf ("%s://%s/api/v3" , scheme , repoUrl .Host )
486
+ }
487
+
471
488
func (g GitHubAppCreds ) HasClientCert () bool {
472
489
return g .clientCertData != "" && g .clientCertKey != ""
473
490
}
0 commit comments