X Tutup
Skip to content

Commit e43777e

Browse files
joshmgrossmislav
authored andcommitted
Support github.localhost as a non-enterprise host
1 parent f6b3357 commit e43777e

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

internal/ghinstance/host.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ import (
88

99
const defaultHostname = "github.com"
1010

11+
// localhost is the domain name of a local GitHub instance
12+
const localhost = "github.localhost"
13+
1114
// Default returns the host name of the default GitHub instance
1215
func Default() string {
1316
return defaultHostname
1417
}
1518

1619
// IsEnterprise reports whether a non-normalized host name looks like a GHE instance
1720
func IsEnterprise(h string) bool {
18-
return NormalizeHostname(h) != defaultHostname
21+
normalizedHostName := NormalizeHostname(h)
22+
return normalizedHostName != defaultHostname && normalizedHostName != localhost
1923
}
2024

2125
// NormalizeHostname returns the canonical host name of a GitHub instance
@@ -24,6 +28,11 @@ func NormalizeHostname(h string) string {
2428
if strings.HasSuffix(hostname, "."+defaultHostname) {
2529
return defaultHostname
2630
}
31+
32+
if strings.HasSuffix(hostname, "."+localhost) {
33+
return localhost
34+
}
35+
2736
return hostname
2837
}
2938

@@ -46,14 +55,14 @@ func GraphQLEndpoint(hostname string) string {
4655
if IsEnterprise(hostname) {
4756
return fmt.Sprintf("https://%s/api/graphql", hostname)
4857
}
49-
return "https://api.github.com/graphql"
58+
return fmt.Sprintf("https://api.%s/graphql", hostname)
5059
}
5160

5261
func RESTPrefix(hostname string) string {
5362
if IsEnterprise(hostname) {
5463
return fmt.Sprintf("https://%s/api/v3/", hostname)
5564
}
56-
return "https://api.github.com/"
65+
return fmt.Sprintf("https://api.%s/", hostname)
5766
}
5867

5968
func GistPrefix(hostname string) string {

internal/ghinstance/host_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ func TestIsEnterprise(t *testing.T) {
1919
host: "api.github.com",
2020
want: false,
2121
},
22+
{
23+
host: "github.localhost",
24+
want: false,
25+
},
26+
{
27+
host: "api.github.localhost",
28+
want: false,
29+
},
2230
{
2331
host: "ghe.io",
2432
want: true,
@@ -58,6 +66,14 @@ func TestNormalizeHostname(t *testing.T) {
5866
host: "upload.github.com",
5967
want: "github.com",
6068
},
69+
{
70+
host: "GitHub.localhost",
71+
want: "github.localhost",
72+
},
73+
{
74+
host: "api.github.localhost",
75+
want: "github.localhost",
76+
},
6177
{
6278
host: "GHE.IO",
6379
want: "ghe.io",
@@ -129,6 +145,10 @@ func TestGraphQLEndpoint(t *testing.T) {
129145
host: "github.com",
130146
want: "https://api.github.com/graphql",
131147
},
148+
{
149+
host: "github.localhost",
150+
want: "https://api.github.localhost/graphql",
151+
},
132152
{
133153
host: "ghe.io",
134154
want: "https://ghe.io/api/graphql",
@@ -152,6 +172,10 @@ func TestRESTPrefix(t *testing.T) {
152172
host: "github.com",
153173
want: "https://api.github.com/",
154174
},
175+
{
176+
host: "github.localhost",
177+
want: "https://api.github.localhost/",
178+
},
155179
{
156180
host: "ghe.io",
157181
want: "https://ghe.io/api/v3/",

0 commit comments

Comments
 (0)
X Tutup