diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ef94c683a8a..25d455b8f77 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/github/github.go b/github/github.go index 08b7db8e55b..fe346f5ab72 100644 --- a/github/github.go +++ b/github/github.go @@ -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" @@ -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) diff --git a/github/github_test.go b/github/github_test.go index 0a28ced93c6..3fa5b4412a4 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -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) } @@ -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) {