Skip to content

Commit f2d99f1

Browse files
authored
Include the version of go-github in User-Agent headers sent to the GitHub API (#2403)
1 parent ce1f6b9 commit f2d99f1

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ this][modified-comment].
127127
[git-aliases]: https://github.com/willnorris/dotfiles/blob/d640d010c23b1116bdb3d4dc12088ed26120d87d/git/.gitconfig#L13-L15
128128
[rebase-comment]: https://github.com/google/go-github/pull/277#issuecomment-183035491
129129
[modified-comment]: https://github.com/google/go-github/pull/280#issuecomment-184859046
130+
131+
**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.

github/github.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ import (
2828
)
2929

3030
const (
31-
defaultBaseURL = "https://api.github.com/"
32-
uploadBaseURL = "https://uploads.github.com/"
33-
userAgent = "go-github"
31+
Version = "45.2.0"
32+
33+
defaultBaseURL = "https://api.github.com/"
34+
defaultUserAgent = "go-github" + "/" + Version
35+
uploadBaseURL = "https://uploads.github.com/"
3436

3537
headerRateLimit = "X-RateLimit-Limit"
3638
headerRateRemaining = "X-RateLimit-Remaining"
@@ -301,7 +303,7 @@ func NewClient(httpClient *http.Client) *Client {
301303
baseURL, _ := url.Parse(defaultBaseURL)
302304
uploadURL, _ := url.Parse(uploadBaseURL)
303305

304-
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL}
306+
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: defaultUserAgent, UploadURL: uploadURL}
305307
c.common.client = c
306308
c.Actions = (*ActionsService)(&c.common)
307309
c.Activity = (*ActivityService)(&c.common)

github/github_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func TestNewClient(t *testing.T) {
248248
if got, want := c.BaseURL.String(), defaultBaseURL; got != want {
249249
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
250250
}
251-
if got, want := c.UserAgent, userAgent; got != want {
251+
if got, want := c.UserAgent, defaultUserAgent; got != want {
252252
t.Errorf("NewClient UserAgent is %v, want %v", got, want)
253253
}
254254

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

510+
userAgent := req.Header.Get("User-Agent")
511+
510512
// test that default user-agent is attached to the request
511-
if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want {
513+
if got, want := userAgent, c.UserAgent; got != want {
512514
t.Errorf("NewRequest() User-Agent is %v, want %v", got, want)
513515
}
516+
517+
if !strings.Contains(userAgent, Version) {
518+
t.Errorf("NewRequest() User-Agent should contain %v, found %v", Version, userAgent)
519+
}
514520
}
515521

516522
func TestNewRequest_invalidJSON(t *testing.T) {

0 commit comments

Comments
 (0)