Skip to content

Commit

Permalink
Include the version of go-github in User-Agent headers sent to th…
Browse files Browse the repository at this point in the history
…e GitHub API (#2403)
  • Loading branch information
timrogers committed Aug 13, 2022
1 parent ce1f6b9 commit f2d99f1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -127,3 +127,5 @@ this][modified-comment].
[git-aliases]: https://github.com/willnorris/dotfiles/blob/d640d010c23b1116bdb3d4dc12088ed26120d87d/git/.gitconfig#L13-L15
[rebase-comment]: https://github.com/google/go-github/pull/277#issuecomment-183035491
[modified-comment]: https://github.com/google/go-github/pull/280#issuecomment-184859046

**When creating a release, don't forget to update the `Version` constant in `github.go`.** This is used to send the version in the `User-Agent` header to identify clients to the GitHub API.
10 changes: 6 additions & 4 deletions github/github.go
Expand Up @@ -28,9 +28,11 @@ import (
)

const (
defaultBaseURL = "https://api.github.com/"
uploadBaseURL = "https://uploads.github.com/"
userAgent = "go-github"
Version = "45.2.0"

defaultBaseURL = "https://api.github.com/"
defaultUserAgent = "go-github" + "/" + Version
uploadBaseURL = "https://uploads.github.com/"

headerRateLimit = "X-RateLimit-Limit"
headerRateRemaining = "X-RateLimit-Remaining"
Expand Down Expand Up @@ -301,7 +303,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, Version) {
t.Errorf("NewRequest() User-Agent should contain %v, found %v", Version, userAgent)
}
}

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

0 comments on commit f2d99f1

Please sign in to comment.