Skip to content

Commit 03856d2

Browse files
fix: regression in github enterprise url handling
1 parent c454a71 commit 03856d2

2 files changed

Lines changed: 41 additions & 15 deletions

File tree

github.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11+
"strings"
1112
"time"
1213
)
1314

@@ -101,6 +102,16 @@ func (c *githubClient) withEnterpriseURL(baseURL string) (*githubClient, error)
101102
return nil, fmt.Errorf("failed to parse base URL: %w", err)
102103
}
103104

105+
if !strings.HasSuffix(base.Path, "/") {
106+
base.Path += "/"
107+
}
108+
109+
if !strings.HasSuffix(base.Path, "/api/v3/") &&
110+
!strings.HasPrefix(base.Host, "api.") &&
111+
!strings.Contains(base.Host, ".api.") {
112+
base.Path += "api/v3/"
113+
}
114+
104115
c.baseURL = base
105116

106117
return c, nil

github_test.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,48 @@ import (
1212

1313
func Test_githubClient_withEnterpriseURL(t *testing.T) {
1414
tests := []struct {
15-
name string
16-
baseURL string
17-
wantErr bool
15+
name string
16+
baseURL string
17+
expectedBaseURL interface{}
1818
}{
1919
{
20-
name: "valid URL",
21-
baseURL: "https://github.example.com",
22-
wantErr: false,
20+
name: "valid URL with subdomain",
21+
baseURL: "https://api.github.example.com",
22+
expectedBaseURL: "https://api.github.example.com/",
23+
},
24+
{
25+
name: "valid URL with path",
26+
baseURL: "https://github.example.com/api/v3",
27+
expectedBaseURL: "https://github.example.com/api/v3/",
28+
},
29+
{
30+
name: "valid URL without path",
31+
baseURL: "https://github.example.com",
32+
expectedBaseURL: "https://github.example.com/api/v3/",
2333
},
2434
{
25-
name: "invalid URL with control characters",
26-
baseURL: "ht\ntp://invalid",
27-
wantErr: true,
35+
name: "invalid URL with control characters",
36+
baseURL: "ht\ntp://invalid",
37+
expectedBaseURL: nil,
2838
},
2939
{
30-
name: "URL with spaces",
31-
baseURL: "http://invalid url with spaces",
32-
wantErr: true,
40+
name: "URL with spaces",
41+
baseURL: "http://invalid url with spaces",
42+
expectedBaseURL: nil,
3343
},
3444
}
3545

3646
for _, tt := range tests {
3747
t.Run(tt.name, func(t *testing.T) {
3848
client := newGitHubClient(&http.Client{})
39-
_, err := client.withEnterpriseURL(tt.baseURL)
40-
if (err != nil) != tt.wantErr {
41-
t.Errorf("withEnterpriseURL() error = %v, wantErr %v", err, tt.wantErr)
49+
githubClient, err := client.withEnterpriseURL(tt.baseURL)
50+
51+
if err != nil {
52+
if tt.expectedBaseURL != nil {
53+
t.Errorf("withEnterpriseURL(%v) error = %v", tt.baseURL, err)
54+
}
55+
} else if githubClient.baseURL.String() != tt.expectedBaseURL {
56+
t.Errorf("withEnterpriseURL(%v) expected = %v, received = %v", tt.baseURL, tt.expectedBaseURL, githubClient.baseURL)
4257
}
4358
})
4459
}

0 commit comments

Comments
 (0)