diff --git a/internal/ghinstance/host.go b/internal/ghinstance/host.go index 709d71255..339444804 100644 --- a/internal/ghinstance/host.go +++ b/internal/ghinstance/host.go @@ -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 { diff --git a/internal/ghinstance/host_test.go b/internal/ghinstance/host_test.go index 45bac3800..20b4f8973 100644 --- a/internal/ghinstance/host_test.go +++ b/internal/ghinstance/host_test.go @@ -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/",