Skip to content

Commit

Permalink
Allow reuse of host checks for extension authors
Browse files Browse the repository at this point in the history
Following on the heels of #151, this commit exports the internal logic used to
determine if a host is GHES deployment or a tenant.

Exporting these functions allow extension authors like `github/gh-copilot` to
provide tailored experiences consistently with `gh`.
  • Loading branch information
andyfeller committed Mar 8, 2024
1 parent 45fa8a4 commit 6e9fb7c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pkg/auth/auth.go
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,11 +152,11 @@ 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)
func IsEnterprise(host string) bool {
return host != github && host != localhost && !IsTenancy(host)
}

func isTenancy(host string) bool {
func IsTenancy(host string) bool {
return strings.HasSuffix(host, "."+tenancyHost)
}

Expand Down
38 changes: 37 additions & 1 deletion pkg/auth/auth_test.go
Expand Up @@ -256,7 +256,43 @@ func TestIsEnterprise(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := isEnterprise(tt.host)
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: "localhost",
host: "github.localhost",
wantOut: false,
},
{
name: "enterprise",
host: "mygithub.com",
wantOut: false,
},
{
name: "tenant",
host: "tenant.ghe.com",
wantOut: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := IsTenancy(tt.host)
assert.Equal(t, tt.wantOut, out)
})
}
Expand Down

0 comments on commit 6e9fb7c

Please sign in to comment.