-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Host enumerable type for hostnames
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
Showing
6 changed files
with
135 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters