Include the version of go-github in User-Agent headers sent to the GitHub API#2403
Conversation
This exposes the current version of the `go-github` package (e.g. `"45.2.0"`) as a field on the `Client` struct. This will allow users of `go-github` to access the current version of the package, for example if they want to construct a custom `User-Agent` header including it. This change means that, before releasing tagging and releasing a new version of the package, this string constant will have to be updated.
… header This updates the default request header sent by the module to include the current version, e.g. `go-github/45.2.0`. This makes it easier for GitHub to track usage of this SDK and how users are upgrading between versions. Users can continue to overwrite this default user agent with their own string by setting the `UserAgent` property on the `Client`.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
github/github.go
Outdated
| ) | ||
|
|
||
| const ( | ||
| packageVersion = "45.2.0" |
There was a problem hiding this comment.
I couldn't find this defined anywhere else in the package code, so I added it here. I'm happy to move it elsewhere, or access it in some other way if there are good options out there.
github/github.go
Outdated
| defaultBaseURL = "https://api.github.com/" | ||
| uploadBaseURL = "https://uploads.github.com/" | ||
| userAgent = "go-github" | ||
| userAgent = "go-github" + "/" + packageVersion |
There was a problem hiding this comment.
What do you think of renaming this to defaultUserAgent, which is more representative of what it is?
There was a problem hiding this comment.
Sure, that sounds good, @timrogers - let's also please sort these contant names within this group of 4 if you don't mind.
github/github.go
Outdated
| // User agent used when communicating with the GitHub API. | ||
| UserAgent string | ||
|
|
||
| PackageVersion string |
There was a problem hiding this comment.
I thought that exposing this on the Client would help with (a) people constructing customer User-Agent headers and (b) me testing the default User-Agent!
There was a problem hiding this comment.
Actually, you can kill two birds with one stone just by exporting the packageVersion constant above by making it PackageVersion and problem solved.
github/github_test.go
Outdated
| t.Errorf("NewRequest() User-Agent is %v, want %v", got, want) | ||
| } | ||
|
|
||
| if !strings.Contains(userAgent, c.PackageVersion) { |
There was a problem hiding this comment.
Is there somewhere else I should add a test to help confirm that this is being added to all requests? Or do you think this is sufficient?
There was a problem hiding this comment.
I think this is sufficient, thanks.
Codecov Report
@@ Coverage Diff @@
## master #2403 +/- ##
=======================================
Coverage 98.06% 98.06%
=======================================
Files 119 119
Lines 10546 10546
=======================================
Hits 10342 10342
Misses 140 140
Partials 64 64
Continue to review full report at Codecov.
|
gmlewis
left a comment
There was a problem hiding this comment.
Great idea, @timrogers !
Just a couple minor tweaks, please, then we should be ready for a second LGTM+Approval before merging.
github/github.go
Outdated
| defaultBaseURL = "https://api.github.com/" | ||
| uploadBaseURL = "https://uploads.github.com/" | ||
| userAgent = "go-github" | ||
| userAgent = "go-github" + "/" + packageVersion |
There was a problem hiding this comment.
Sure, that sounds good, @timrogers - let's also please sort these contant names within this group of 4 if you don't mind.
github/github.go
Outdated
| // User agent used when communicating with the GitHub API. | ||
| UserAgent string | ||
|
|
||
| PackageVersion string |
There was a problem hiding this comment.
Actually, you can kill two birds with one stone just by exporting the packageVersion constant above by making it PackageVersion and problem solved.
github/github.go
Outdated
| uploadURL, _ := url.Parse(uploadBaseURL) | ||
|
|
||
| c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL} | ||
| c := &Client{client: httpClient, BaseURL: baseURL, PackageVersion: packageVersion, UserAgent: userAgent, UploadURL: uploadURL} |
There was a problem hiding this comment.
So this would no longer be needed since the PackageVersion constant is now exported and available.
github/github_test.go
Outdated
| t.Errorf("NewRequest() User-Agent is %v, want %v", got, want) | ||
| } | ||
|
|
||
| if !strings.Contains(userAgent, c.PackageVersion) { |
There was a problem hiding this comment.
I think this is sufficient, thanks.
timrogers
left a comment
There was a problem hiding this comment.
Thanks for the feedback! I've addressed those comments and I think it's looking better now. Plus I learned about exported constants, which is great 🧠 ⬆️
gmlewis
left a comment
There was a problem hiding this comment.
Thank you, @timrogers !
LGTM.
Awaiting second LGTM+Approval from any other contributor to this report before merging.
| [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. |
There was a problem hiding this comment.
This could be automated. Since the version constant is currently only used for the user agent, an error should not be a big problem, but if somebody depends on the constant this could lead to problems along the way.
Unfortunatly the release process in this project is not automated yet. If it gets automated in the future we need to include this constant in there.
|
Thank you, @raynigon ! |
👋🏻 Hi there! I'm Tim, and I'm a Product Manager at GitHub. To allow us to better understand how people are using our API, it's super helpful to know what versions of SDKs like
go-githubthey are using.At the moment, this package sends the
User-Agent"go-github" in requests, but it doesn't say what version is being used.This updates the
User-Agentheader to include that information, whilst still maintaining the ability for people to choose their own user agent by setting theUserAgentfield onClient.It also exports a
Versionconstant, so people can use that information - for example to construct customUser-Agentheaders.This will mean that someone has to update the
Versionconstant with the new version when it is incremented.This is the first ever Go I've written, so I won't be upset if you tell me that I've done this in a terrible way 😉