Skip to content

Commit

Permalink
Replace duplicate isEnterprise and isTenancy fn in api pkg with auth pkg
Browse files Browse the repository at this point in the history
There were duplicated, unexported isEnterprise and isTenancy functions in
the api package. The exported IsEnterprise and IsTenancy functions exist
in the auth package. This removes the duplicated functions from api and
replaces them with the exported packages from auth. It doesn't attempt to
clean up any more logic than that.
  • Loading branch information
jtmcg committed Oct 11, 2024
1 parent 267e1ca commit 877a8e3
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 106 deletions.
5 changes: 3 additions & 2 deletions pkg/api/graphql_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"strings"

"github.com/cli/go-gh/v2/pkg/auth"
graphql "github.com/cli/shurcooL-graphql"
)

Expand Down Expand Up @@ -171,8 +172,8 @@ func graphQLEndpoint(host string) string {
if isGarage(host) {
return fmt.Sprintf("https://%s/api/graphql", host)
}
host = normalizeHostname(host)
if isEnterprise(host) {
host = auth.NormalizeHostname(host)
if auth.IsEnterprise(host) {
return fmt.Sprintf("https://%s/api/graphql", host)
}
if strings.EqualFold(host, localhost) {
Expand Down
32 changes: 2 additions & 30 deletions pkg/api/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
modulePath = "github.com/cli/go-gh"
timeZone = "Time-Zone"
userAgent = "User-Agent"
// tenancyHost is the domain name of a tenancy GitHub instance.
tenancyHost = "ghe.com"
)

var jsonTypeRE = regexp.MustCompile(`[/+]json($|;)`)
Expand Down Expand Up @@ -134,36 +136,6 @@ func isGarage(host string) bool {
return strings.EqualFold(host, "garage.github.com")
}

func isEnterprise(host string) bool {
return host != github && host != localhost && !isTenancy(host)
}

// tenancyHost is the domain name of a tenancy GitHub instance.
const tenancyHost = "ghe.com"

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

func normalizeHostname(hostname string) string {
hostname = strings.ToLower(hostname)
if strings.HasSuffix(hostname, "."+github) {
return github
}
if strings.HasSuffix(hostname, "."+localhost) {
return localhost
}
// This has been copied over from the cli/cli NormalizeHostname function
// to ensure compatible behaviour but we don't fully understand when or
// why it would be useful here. We can't see what harm will come of
// duplicating the logic.
if before, found := strings.CutSuffix(hostname, "."+tenancyHost); found {
idx := strings.LastIndex(before, ".")
return fmt.Sprintf("%s.%s", before[idx+1:], tenancyHost)
}
return hostname
}

type headerRoundTripper struct {
headers map[string]string
host string
Expand Down
67 changes: 0 additions & 67 deletions pkg/api/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,73 +157,6 @@ func TestNewHTTPClient(t *testing.T) {
}
}

func TestIsEnterprise(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: true,
},
}

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

func TestNormalizeHostname(t *testing.T) {
tests := []struct {
name string
host string
wantHost string
}{
{
name: "github domain",
host: "test.github.com",
wantHost: "github.com",
},
{
name: "capitalized",
host: "GitHub.com",
wantHost: "github.com",
},
{
name: "localhost domain",
host: "test.github.localhost",
wantHost: "github.localhost",
},
{
name: "enterprise domain",
host: "mygithub.com",
wantHost: "mygithub.com",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
normalized := normalizeHostname(tt.host)
assert.Equal(t, tt.wantHost, normalized)
})
}
}

type tripper struct {
roundTrip func(*http.Request) (*http.Response, error)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/api/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net/http"
"strings"

"github.com/cli/go-gh/v2/pkg/auth"
)

// RESTClient wraps methods for the different types of
Expand Down Expand Up @@ -159,8 +161,8 @@ func restPrefix(hostname string) string {
if isGarage(hostname) {
return fmt.Sprintf("https://%s/api/v3/", hostname)
}
hostname = normalizeHostname(hostname)
if isEnterprise(hostname) {
hostname = auth.NormalizeHostname(hostname)
if auth.IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/api/v3/", hostname)
}
if strings.EqualFold(hostname, localhost) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TokenFromEnvOrConfig(host string) (string, string) {
}

func tokenForHost(cfg *config.Config, host string) (string, string) {
host = normalizeHostname(host)
host = NormalizeHostname(host)
if IsEnterprise(host) {
if token := os.Getenv(ghEnterpriseToken); token != "" {
return token, ghEnterpriseToken
Expand Down Expand Up @@ -155,18 +155,18 @@ const tenancyHost = "ghe.com"
// 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)
normalizedHost := NormalizeHostname(host)
return normalizedHost != github && normalizedHost != localhost && !IsTenancy(normalizedHost)
}

// 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)
normalizedHost := NormalizeHostname(host)
return strings.HasSuffix(normalizedHost, "."+tenancyHost)
}

func normalizeHostname(host string) string {
func NormalizeHostname(host string) string {
hostname := strings.ToLower(host)
if strings.HasSuffix(hostname, "."+github) {
return github
Expand Down
2 changes: 1 addition & 1 deletion pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func TestNormalizeHostname(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
normalized := normalizeHostname(tt.host)
normalized := NormalizeHostname(tt.host)
assert.Equal(t, tt.wantHost, normalized)
})
}
Expand Down

0 comments on commit 877a8e3

Please sign in to comment.