Skip to content

Commit

Permalink
Merge pull request #172 from cli/wm/tenant-api
Browse files Browse the repository at this point in the history
Use api subdomain for REST and GQL clients when host is tenant
  • Loading branch information
andyfeller authored Sep 16, 2024
2 parents 25db6b9 + af31cb3 commit 267e1ca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/api/graphql_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ func TestGraphQLEndpoint(t *testing.T) {
host: "enterprise.com",
wantEndpoint: "https://enterprise.com/api/graphql",
},
{
name: "tenant",
host: "tenant.ghe.com",
wantEndpoint: "https://api.tenant.ghe.com/graphql",
},
}

for _, tt := range tests {
Expand Down
17 changes: 16 additions & 1 deletion pkg/api/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,14 @@ func isGarage(host string) bool {
}

func isEnterprise(host string) bool {
return host != github && host != localhost
return host != github && host != localhost && !isTenancy(host)
}

// tenancyHost is the domain name of a tenancy GitHub instance.
const tenancyHost = "ghe.com"

func isTenancy(host string) bool {
return strings.HasSuffix(host, "."+tenancyHost)
}

func normalizeHostname(hostname string) string {
Expand All @@ -146,6 +153,14 @@ func normalizeHostname(hostname string) string {
if strings.HasSuffix(hostname, "."+localhost) {
return localhost
}
// 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, "."+tenancyHost); found {
idx := strings.LastIndex(before, ".")
return fmt.Sprintf("%s.%s", before[idx+1:], tenancyHost)
}
return hostname
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/api/rest_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ func TestRestPrefix(t *testing.T) {
host: "enterprise.com",
wantEndpoint: "https://enterprise.com/api/v3/",
},
{
name: "tenant",
host: "tenant.ghe.com",
wantEndpoint: "https://api.tenant.ghe.com/",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 267e1ca

Please sign in to comment.