diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 88e02c2..6ab996f 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -62,7 +62,7 @@ func TokenFromEnvOrConfig(host string) (string, string) { func tokenForHost(cfg *config.Config, host string) (string, string) { host = normalizeHostname(host) - if isEnterprise(host) { + if IsEnterprise(host) { if token := os.Getenv(ghEnterpriseToken); token != "" { return token, ghEnterpriseToken } @@ -152,12 +152,18 @@ func defaultHost(cfg *config.Config) (string, string) { // TenancyHost is the domain name of a tenancy GitHub instance. const tenancyHost = "ghe.com" -func isEnterprise(host string) bool { - return host != github && host != localhost && !isTenancy(host) +// IsEnterprise determines if a provided host is a GitHub Enterprise Server instance, +// rather than GitHub.com or a tenancy GitHub instance. +func IsEnterprise(host string) bool { + normalizedHost := normalizeHostname(host) + return normalizedHost != github && normalizedHost != localhost && !IsTenancy(normalizedHost) } -func isTenancy(host string) bool { - return strings.HasSuffix(host, "."+tenancyHost) +// IsTenancy determines if a provided host is a tenancy GitHub instance, +// rather than GitHub.com or a GitHub Enterprise Server instance. +func IsTenancy(host string) bool { + normalizedHost := normalizeHostname(host) + return strings.HasSuffix(normalizedHost, "."+tenancyHost) } func normalizeHostname(host string) string { diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index a0bb59d..0588a1c 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -237,11 +237,21 @@ func TestIsEnterprise(t *testing.T) { host: "github.com", wantOut: false, }, + { + name: "github API", + host: "api.github.com", + wantOut: false, + }, { name: "localhost", host: "github.localhost", wantOut: false, }, + { + name: "localhost API", + host: "api.github.localhost", + wantOut: false, + }, { name: "enterprise", host: "mygithub.com", @@ -252,11 +262,67 @@ func TestIsEnterprise(t *testing.T) { host: "tenant.ghe.com", wantOut: false, }, + { + name: "tenant API", + host: "api.tenant.ghe.com", + wantOut: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + out := IsEnterprise(tt.host) + assert.Equal(t, tt.wantOut, out) + }) + } +} + +func TestIsTenancy(t *testing.T) { + tests := []struct { + name string + host string + wantOut bool + }{ + { + name: "github", + host: "github.com", + wantOut: false, + }, + { + name: "github API", + host: "api.github.com", + wantOut: false, + }, + { + name: "localhost", + host: "github.localhost", + wantOut: false, + }, + { + name: "localhost API", + host: "api.github.localhost", + wantOut: false, + }, + { + name: "enterprise", + host: "mygithub.com", + wantOut: false, + }, + { + name: "tenant", + host: "tenant.ghe.com", + wantOut: true, + }, + { + name: "tenant API", + host: "api.tenant.ghe.com", + wantOut: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - out := isEnterprise(tt.host) + out := IsTenancy(tt.host) assert.Equal(t, tt.wantOut, out) }) }