Skip to content

Commit 5840c44

Browse files
authored
Merge pull request #152 from cli/andyfeller/export-auth-is-tenancy
Allow reuse of host checks for extension authors
2 parents ecc5db7 + 8a3c02a commit 5840c44

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

pkg/auth/auth.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TokenFromEnvOrConfig(host string) (string, string) {
6262

6363
func tokenForHost(cfg *config.Config, host string) (string, string) {
6464
host = normalizeHostname(host)
65-
if isEnterprise(host) {
65+
if IsEnterprise(host) {
6666
if token := os.Getenv(ghEnterpriseToken); token != "" {
6767
return token, ghEnterpriseToken
6868
}
@@ -152,12 +152,18 @@ func defaultHost(cfg *config.Config) (string, string) {
152152
// TenancyHost is the domain name of a tenancy GitHub instance.
153153
const tenancyHost = "ghe.com"
154154

155-
func isEnterprise(host string) bool {
156-
return host != github && host != localhost && !isTenancy(host)
155+
// IsEnterprise determines if a provided host is a GitHub Enterprise Server instance,
156+
// rather than GitHub.com or a tenancy GitHub instance.
157+
func IsEnterprise(host string) bool {
158+
normalizedHost := normalizeHostname(host)
159+
return normalizedHost != github && normalizedHost != localhost && !IsTenancy(normalizedHost)
157160
}
158161

159-
func isTenancy(host string) bool {
160-
return strings.HasSuffix(host, "."+tenancyHost)
162+
// IsTenancy determines if a provided host is a tenancy GitHub instance,
163+
// rather than GitHub.com or a GitHub Enterprise Server instance.
164+
func IsTenancy(host string) bool {
165+
normalizedHost := normalizeHostname(host)
166+
return strings.HasSuffix(normalizedHost, "."+tenancyHost)
161167
}
162168

163169
func normalizeHostname(host string) string {

pkg/auth/auth_test.go

+67-1
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,21 @@ func TestIsEnterprise(t *testing.T) {
237237
host: "github.com",
238238
wantOut: false,
239239
},
240+
{
241+
name: "github API",
242+
host: "api.github.com",
243+
wantOut: false,
244+
},
240245
{
241246
name: "localhost",
242247
host: "github.localhost",
243248
wantOut: false,
244249
},
250+
{
251+
name: "localhost API",
252+
host: "api.github.localhost",
253+
wantOut: false,
254+
},
245255
{
246256
name: "enterprise",
247257
host: "mygithub.com",
@@ -252,11 +262,67 @@ func TestIsEnterprise(t *testing.T) {
252262
host: "tenant.ghe.com",
253263
wantOut: false,
254264
},
265+
{
266+
name: "tenant API",
267+
host: "api.tenant.ghe.com",
268+
wantOut: false,
269+
},
270+
}
271+
272+
for _, tt := range tests {
273+
t.Run(tt.name, func(t *testing.T) {
274+
out := IsEnterprise(tt.host)
275+
assert.Equal(t, tt.wantOut, out)
276+
})
277+
}
278+
}
279+
280+
func TestIsTenancy(t *testing.T) {
281+
tests := []struct {
282+
name string
283+
host string
284+
wantOut bool
285+
}{
286+
{
287+
name: "github",
288+
host: "github.com",
289+
wantOut: false,
290+
},
291+
{
292+
name: "github API",
293+
host: "api.github.com",
294+
wantOut: false,
295+
},
296+
{
297+
name: "localhost",
298+
host: "github.localhost",
299+
wantOut: false,
300+
},
301+
{
302+
name: "localhost API",
303+
host: "api.github.localhost",
304+
wantOut: false,
305+
},
306+
{
307+
name: "enterprise",
308+
host: "mygithub.com",
309+
wantOut: false,
310+
},
311+
{
312+
name: "tenant",
313+
host: "tenant.ghe.com",
314+
wantOut: true,
315+
},
316+
{
317+
name: "tenant API",
318+
host: "api.tenant.ghe.com",
319+
wantOut: true,
320+
},
255321
}
256322

257323
for _, tt := range tests {
258324
t.Run(tt.name, func(t *testing.T) {
259-
out := isEnterprise(tt.host)
325+
out := IsTenancy(tt.host)
260326
assert.Equal(t, tt.wantOut, out)
261327
})
262328
}

0 commit comments

Comments
 (0)