Support github.localhost as a non-enterprise host

This commit is contained in:
Josh Gross 2021-10-14 17:40:28 -04:00 committed by Mislav Marohnić
parent f6b33572fd
commit e43777ebd2
2 changed files with 36 additions and 3 deletions

View file

@ -8,6 +8,9 @@ import (
const defaultHostname = "github.com"
// localhost is the domain name of a local GitHub instance
const localhost = "github.localhost"
// Default returns the host name of the default GitHub instance
func Default() string {
return defaultHostname
@ -15,7 +18,8 @@ func Default() string {
// IsEnterprise reports whether a non-normalized host name looks like a GHE instance
func IsEnterprise(h string) bool {
return NormalizeHostname(h) != defaultHostname
normalizedHostName := NormalizeHostname(h)
return normalizedHostName != defaultHostname && normalizedHostName != localhost
}
// NormalizeHostname returns the canonical host name of a GitHub instance
@ -24,6 +28,11 @@ func NormalizeHostname(h string) string {
if strings.HasSuffix(hostname, "."+defaultHostname) {
return defaultHostname
}
if strings.HasSuffix(hostname, "."+localhost) {
return localhost
}
return hostname
}
@ -46,14 +55,14 @@ func GraphQLEndpoint(hostname string) string {
if IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/api/graphql", hostname)
}
return "https://api.github.com/graphql"
return fmt.Sprintf("https://api.%s/graphql", hostname)
}
func RESTPrefix(hostname string) string {
if IsEnterprise(hostname) {
return fmt.Sprintf("https://%s/api/v3/", hostname)
}
return "https://api.github.com/"
return fmt.Sprintf("https://api.%s/", hostname)
}
func GistPrefix(hostname string) string {

View file

@ -19,6 +19,14 @@ func TestIsEnterprise(t *testing.T) {
host: "api.github.com",
want: false,
},
{
host: "github.localhost",
want: false,
},
{
host: "api.github.localhost",
want: false,
},
{
host: "ghe.io",
want: true,
@ -58,6 +66,14 @@ func TestNormalizeHostname(t *testing.T) {
host: "upload.github.com",
want: "github.com",
},
{
host: "GitHub.localhost",
want: "github.localhost",
},
{
host: "api.github.localhost",
want: "github.localhost",
},
{
host: "GHE.IO",
want: "ghe.io",
@ -129,6 +145,10 @@ func TestGraphQLEndpoint(t *testing.T) {
host: "github.com",
want: "https://api.github.com/graphql",
},
{
host: "github.localhost",
want: "https://api.github.localhost/graphql",
},
{
host: "ghe.io",
want: "https://ghe.io/api/graphql",
@ -152,6 +172,10 @@ func TestRESTPrefix(t *testing.T) {
host: "github.com",
want: "https://api.github.com/",
},
{
host: "github.localhost",
want: "https://api.github.localhost/",
},
{
host: "ghe.io",
want: "https://ghe.io/api/v3/",