Skip to content

Commit f47be43

Browse files
committed
fix: determine the suitable github api base url from repo url
Signed-off-by: Cheng Fang <cfang@redhat.com>
1 parent 99a0cb2 commit f47be43

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

ext/git/creds.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const (
4242
// githubAccessTokenUsername is a username that is used to with the github access token
4343
githubAccessTokenUsername = "x-access-token"
4444
forceBasicAuthHeaderEnv = "ARGOCD_GIT_AUTH_HEADER"
45+
defaultGithubApiUrl = "https://api.github.com"
4546
)
4647

4748
func init() {
@@ -443,12 +444,7 @@ func (g GitHubAppCreds) getAccessToken() (string, error) {
443444
return itr.Token(ctx)
444445
}
445446

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()
452448
// Create a new GitHub transport
453449
c := GetRepoHTTPClient(baseUrl, g.insecure, g, g.proxy)
454450
itr, err := ghinstallation.New(c.Transport,
@@ -468,6 +464,27 @@ func (g GitHubAppCreds) getAccessToken() (string, error) {
468464
return itr.Token(ctx)
469465
}
470466

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+
471488
func (g GitHubAppCreds) HasClientCert() bool {
472489
return g.clientCertData != "" && g.clientCertKey != ""
473490
}

ext/git/creds_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,27 @@ func TestGoogleCloudCreds_Environ_cleanup(t *testing.T) {
367367
io.Close(closer)
368368
assert.NotContains(t, store.creds, nonce)
369369
}
370+
371+
func TestGitHubAppCreds_getBaseURL(t *testing.T) {
372+
g := GitHubAppCreds{
373+
appID: 123,
374+
appInstallId: 1234,
375+
}
376+
assert.Equal(t, defaultGithubApiUrl, g.getBaseURL())
377+
378+
g.baseURL = "https://example.com/api"
379+
assert.Equal(t, g.baseURL, g.getBaseURL())
380+
381+
g.baseURL = ""
382+
g.repoURL = "https://github.com/org/repo"
383+
assert.Equal(t, defaultGithubApiUrl, g.getBaseURL())
384+
385+
g.repoURL = "http://github.com/org/repo"
386+
assert.Equal(t, defaultGithubApiUrl, g.getBaseURL())
387+
388+
g.repoURL = "https://example.com/org/repo"
389+
assert.Equal(t, "https://example.com/api/v3", g.getBaseURL())
390+
391+
g.repoURL = "http://example.com/org/repo"
392+
assert.Equal(t, "http://example.com/api/v3", g.getBaseURL())
393+
}

0 commit comments

Comments
 (0)