What changed: a plugin installed from GitHub (github:owner/repo) is
checked by asking GitHub's API for its branch's commit (githubHead,
through fetchJSON). That request went without a token, so a token saved
in Settings → Network and sharing did nothing for it: the check stayed
at GitHub's 60 requests an hour for the whole address, which a shared
proxy exit uses up, and the row said only "<url>: 403 Forbidden".
- The token lookup, where a token may go and how a rate limit is told
move from internal/library into a new internal/ghtoken, shared by the
library and the plugin package (plugin can't import library).
The order is unchanged: Settings, then GITHUB_TOKEN, then GH_TOKEN.
- plugin's fetchJSON has a request to GitHub's API's host (githubAPI)
carry it, which covers the git plugins' check and the Plugins page's
topic search. Raw files, npm and every other host never get it, and
source.Do already sends nothing with Authorization to a mirror.
- An answer from GitHub's API that is a rate limit (429, or 403 with
X-RateLimit-Remaining 0, Retry-After or a rate-limit message) is a
ghtoken.Limited: the check's row has Why "limited" and Limited {until,
token} for the page, as the library's skill check already had. A 401
to a request with a token names which token was refused, never the
token.
What was run:
- TestGitPluginCheckCarriesGitHubToken (new, internal/plugin): asks with
no token / GH_TOKEN / GITHUB_TOKEN / Settings in that order of
precedence, a 403 rate limit with and without a token, a 429 with
Retry-After, a refused GH_TOKEN, and a raw-file host that must get no
Authorization. Broken by hand twice: with fetchJSON's Authorize removed
it fails "GH_TOKEN: GitHub's API was asked with Authorization "", want
"Bearer gh-env"" (and the other two sources); with the limit check
removed it fails with Why "registry", Limited nil, "403 Forbidden".
- go test -tags nogui -count=1 ./internal/library ./internal/plugin
(temp HOME, pinned caches): ok, the library's TestGitHubToken*,
TestGitHubRateLimitSaid among them.
- gofmt -l clean; go vet -tags nogui on darwin, GOOS=linux and
GOOS=windows for internal/plugin, library, ghtoken, gui: clean.
- Not tried against the real api.github.com (tests stay off the network).
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package library
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
|
|
"github.com/yetone/magpie/internal/ghtoken"
|
|
)
|
|
|
|
// ---- asking GitHub's API with the user's token ----------------------------
|
|
//
|
|
// The token, where it may go and what a rate limit says are ghtoken's,
|
|
// shared with the Plugins page's checks of git plugins (#1942).
|
|
|
|
// GitHubTokenEnv names the environment variables a token is taken from when
|
|
// Settings has none, in the order they are looked at.
|
|
var GitHubTokenEnv = ghtoken.Env
|
|
|
|
// GitHubToken is the token requests to GitHub's API carry, and where it
|
|
// came from: "settings", the environment variable's name, or "" for none.
|
|
func GitHubToken() (token, from string) { return ghtoken.Token() }
|
|
|
|
// withGitHubToken has req carry the token, if there is one and req goes to
|
|
// GitHub's API (githubAPI's host); it says whether it did.
|
|
func withGitHubToken(req *http.Request) bool { return ghtoken.Authorize(req, githubAPI) }
|
|
|
|
// errLimited is GitHub turning requests away for a while.
|
|
type errLimited = ghtoken.Limited
|
|
|
|
var limitedBy = ghtoken.LimitedBy
|
|
|
|
// etags are GitHub's answers kept by URL, for the next request for one to
|
|
// ask if it changed: a 304 doesn't count against the rate limit.
|
|
var etags = struct {
|
|
sync.Mutex
|
|
m map[string]etagged
|
|
}{m: map[string]etagged{}}
|
|
|
|
type etagged struct {
|
|
etag string
|
|
body []byte
|
|
}
|