Authenticate the GitHub API call to avoid rate limiting - #134
Merged
Conversation
Resolve the GitHub token once in the constructor and layer `auth_bearer` onto the injected client with `ScopingHttpClient`, instead of rebuilding the request options and re-reading the token on every paginated `tags()` call. Merge the token coverage into `TailwindVersionFinderTest`.
Contributor
|
Thanks @Amoifr, great addition! I made a few minor modifications. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #60
TailwindVersionFinderqueries the GitHub API to resolve the latest Tailwind release, but the request is anonymous, so it hits the low unauthenticated rate limit (60 requests/hour, shared per IP). That is what bit @nicolas-grekas during a workshop where everyone shared the same IP. The test suite already works around this by readingGITHUB_TOKEN, but the runtime code did not.This authenticates the call with
auth_bearerwhenever a GitHub token can be found, looked up in this order:GITHUB_TOKEN/GH_TOKENenvironment variables (same convention as the existing test);COMPOSER_AUTHenvironment variable;auth.json(COMPOSER_HOME/auth.json, otherwise~/.composer/auth.jsonand~/.config/composer/auth.json), reading thegithub-oauthtoken developers usually already have configured (@nicolas-grekas' first idea).Without any token the behavior is unchanged (still anonymous, just rate limited as before), so nothing breaks for existing setups.
Tests use a
MockHttpClientto assert theAuthorizationheader is sent for each source and absent otherwise, with the environment isolated so the existing live test keeps readingGITHUB_TOKEN.A couple of deliberate scope choices, happy to adjust:
auth.json, not a project-level one, since thegithub-oauthtoken is almost always global. Easy to add if you'd prefer.Thanks a lot for the bundle, and @bocharsky-bw for keeping this one alive! 🙏