Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"strings"
"time"
)

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

if !strings.HasSuffix(base.Path, "/") {
base.Path += "/"
}

if !strings.HasSuffix(base.Path, "/api/v3/") &&
!strings.HasPrefix(base.Host, "api.") &&
!strings.Contains(base.Host, ".api.") {
base.Path += "api/v3/"
}

c.baseURL = base

return c, nil
Expand Down
45 changes: 30 additions & 15 deletions github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,48 @@ import (

func Test_githubClient_withEnterpriseURL(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add test cases for the remaining branches?

https://github.example.com/api/v3/ — already fully suffixed, should stay as-is
https://ghes.api.example.com — .api. in host, should skip /api/v3/ append
https://github.example.com/ — trailing slash already present, should not double it

tests := []struct {
name string
baseURL string
wantErr bool
name string
baseURL string
expectedBaseURL interface{}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer keeping the original wantErr bool + a separate expectedURL string field instead of using interface{} to represent both. This is more idiomatic Go and gives type safety at compile time.

}{
{
name: "valid URL",
baseURL: "https://github.example.com",
wantErr: false,
name: "valid URL with subdomain",
baseURL: "https://api.github.example.com",
expectedBaseURL: "https://api.github.example.com/",
},
{
name: "valid URL with path",
baseURL: "https://github.example.com/api/v3",
expectedBaseURL: "https://github.example.com/api/v3/",
},
{
name: "valid URL without path",
baseURL: "https://github.example.com",
expectedBaseURL: "https://github.example.com/api/v3/",
},
{
name: "invalid URL with control characters",
baseURL: "ht\ntp://invalid",
wantErr: true,
name: "invalid URL with control characters",
baseURL: "ht\ntp://invalid",
expectedBaseURL: nil,
},
{
name: "URL with spaces",
baseURL: "http://invalid url with spaces",
wantErr: true,
name: "URL with spaces",
baseURL: "http://invalid url with spaces",
expectedBaseURL: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := newGitHubClient(&http.Client{})
_, err := client.withEnterpriseURL(tt.baseURL)
if (err != nil) != tt.wantErr {
t.Errorf("withEnterpriseURL() error = %v, wantErr %v", err, tt.wantErr)
githubClient, err := client.withEnterpriseURL(tt.baseURL)

if err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This silently passes when an error occurs and expectedBaseURL is nil, but doesn't assert that an error was expected. The original if (err != nil) != tt.wantErr pattern is stricter — it catches both unexpected errors and missing expected errors.

if tt.expectedBaseURL != nil {
t.Errorf("withEnterpriseURL(%v) error = %v", tt.baseURL, err)
}
} else if githubClient.baseURL.String() != tt.expectedBaseURL {
t.Errorf("withEnterpriseURL(%v) expected = %v, received = %v", tt.baseURL, tt.expectedBaseURL, githubClient.baseURL)
}
})
}
Expand Down