Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the version of go-github in User-Agent headers sent to the GitHub API #2403

Merged
merged 7 commits into from Aug 13, 2022
9 changes: 5 additions & 4 deletions github/github.go
Expand Up @@ -28,9 +28,10 @@ import (
)

const (
defaultBaseURL = "https://api.github.com/"
uploadBaseURL = "https://uploads.github.com/"
userAgent = "go-github"
CurrentVersion = "45.2.0"
timrogers marked this conversation as resolved.
Show resolved Hide resolved
defaultBaseURL = "https://api.github.com/"
defaultUserAgent = "go-github" + "/" + CurrentVersion
uploadBaseURL = "https://uploads.github.com/"

headerRateLimit = "X-RateLimit-Limit"
headerRateRemaining = "X-RateLimit-Remaining"
Expand Down Expand Up @@ -301,7 +302,7 @@ func NewClient(httpClient *http.Client) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
uploadURL, _ := url.Parse(uploadBaseURL)

c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL}
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: defaultUserAgent, UploadURL: uploadURL}
c.common.client = c
c.Actions = (*ActionsService)(&c.common)
c.Activity = (*ActivityService)(&c.common)
Expand Down
10 changes: 8 additions & 2 deletions github/github_test.go
Expand Up @@ -248,7 +248,7 @@ func TestNewClient(t *testing.T) {
if got, want := c.BaseURL.String(), defaultBaseURL; got != want {
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
}
if got, want := c.UserAgent, userAgent; got != want {
if got, want := c.UserAgent, defaultUserAgent; got != want {
t.Errorf("NewClient UserAgent is %v, want %v", got, want)
}

Expand Down Expand Up @@ -507,10 +507,16 @@ func TestNewRequest(t *testing.T) {
t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want)
}

userAgent := req.Header.Get("User-Agent")

// test that default user-agent is attached to the request
if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want {
if got, want := userAgent, c.UserAgent; got != want {
t.Errorf("NewRequest() User-Agent is %v, want %v", got, want)
}

if !strings.Contains(userAgent, CurrentVersion) {
t.Errorf("NewRequest() User-Agent should contain %v, found %v", CurrentVersion, userAgent)
}
}

func TestNewRequest_invalidJSON(t *testing.T) {
Expand Down