Skip to content

Commit

Permalink
Add Host enumerable type for hostnames
Browse files Browse the repository at this point in the history
github, localhost, and tenancyhost were floating around different parts of
the code, here, all defining consts for our expected hosts. This attempts
to leverage a type to define what hosts we expect, and add a ToHostType
method that converts a given host string to its respective Host type.

In theory, this makes the logic easier to read, in that all the host types
are in one place and follow the same patterns for dealing with them.
There's probably more that can be done, here, to improve upon this
pattern.

In fact, there may be an opportunity to refactor all the logic
that deals with hosts (things like normalizeHostName and isSameDomain) to
its own struct to pass around and clean up the code. I wanted to get
feedback on this initial refactor, though, before diving any further into
exploring that design direction.
  • Loading branch information
jtmcg committed Oct 10, 2024
1 parent 74d9847 commit df2be3f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 105 deletions.
2 changes: 1 addition & 1 deletion pkg/api/graphql_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func graphQLEndpoint(host string) string {
if IsEnterprise(host) {
return fmt.Sprintf("https://%s/api/graphql", host)
}
if strings.EqualFold(host, localhost) {
if strings.EqualFold(host, Localhost.String()) {
return fmt.Sprintf("http://api.%s/graphql", host)
}
return fmt.Sprintf("https://api.%s/graphql", host)
Expand Down
59 changes: 59 additions & 0 deletions pkg/api/hosts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package api

import (
"fmt"
"strings"
)

type Host string

const (
GitHub Host = "github.com"
Localhost Host = "github.localhost"
Tenancy Host = "ghe.com"
Garage Host = "garage.github.com"
)

func (h Host) String() string {
return string(h)
}

func ToHost(h string) Host {
if strings.HasSuffix(h, "."+Tenancy.String()) {
return Tenancy
}

return Host(h)
}

func isGarage(host string) bool {
return ToHost(host) == Garage
}

func IsEnterprise(host string) bool {
typedHost := ToHost(host)
return typedHost != GitHub && typedHost != Tenancy && typedHost != Garage || typedHost != Localhost
}

func IsTenancy(host string) bool {
return ToHost(host) == Tenancy
}

func normalizeHostname(hostname string) string {
hostname = strings.ToLower(hostname)
if strings.HasSuffix(hostname, "."+GitHub.String()) {
return GitHub.String()
}
if strings.HasSuffix(hostname, "."+Localhost.String()) {
return Localhost.String()
}
// 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, "."+Tenancy.String()); found {
idx := strings.LastIndex(before, ".")
return fmt.Sprintf("%s.%s", before[idx+1:], Tenancy.String())
}
return hostname
}
74 changes: 74 additions & 0 deletions pkg/api/hosts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package api

import (
"testing"

"github.com/stretchr/testify/assert"
)

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)
})
}
}
36 changes: 0 additions & 36 deletions pkg/api/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const (
accept = "Accept"
authorization = "Authorization"
contentType = "Content-Type"
github = "github.com"
jsonContentType = "application/json; charset=utf-8"
localhost = "github.localhost"
modulePath = "github.com/cli/go-gh"
timeZone = "Time-Zone"
userAgent = "User-Agent"
Expand Down Expand Up @@ -130,40 +128,6 @@ func isSameDomain(requestHost, domain string) bool {
return (requestHost == domain) || strings.HasSuffix(requestHost, "."+domain)
}

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
2 changes: 1 addition & 1 deletion pkg/api/rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func restPrefix(hostname string) string {
if IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/api/v3/", hostname)
}
if strings.EqualFold(hostname, localhost) {
if strings.EqualFold(hostname, Localhost.String()) {
return fmt.Sprintf("http://api.%s/", hostname)
}
return fmt.Sprintf("https://api.%s/", hostname)
Expand Down

0 comments on commit df2be3f

Please sign in to comment.