Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reuse of host checks for extension authors #152

Merged
merged 6 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
68 changes: 67 additions & 1 deletion pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
})
}
Expand Down