till's Plugins page said "无法连接 GitHub: GitHub refused GITHUB_TOKEN (no GitHub token is set in Settings → Network and sharing)". ghtoken.Token falls back to the environment's GITHUB_TOKEN / GH_TOKEN, and a stale one there made every GitHub read fail, though GitHub serves the same reads without a token. ghtoken.Do now sends a GitHub API request with the token. When the token came from the environment and GitHub answers 401, it remembers that token as refused for the rest of the process (Token() skips it, so GH_TOKEN is still tried after a refused GITHUB_TOKEN, and Settings no longer shows it) and asks once more without Authorization, returning that answer. A token set in Settings is never dropped: its 401 is still Refused()'s error. An anonymous rate limit after the drop is said by LimitedBy as before. Every caller goes through it: plugin fetchJSONFrom (tagged-repo search, git plugin checks, githubHead) and getURLFrom (Bun), and library apiGet, skills fetch, skilltree fetchBlob and latestRTK via doGitHub. internal/davsync/github.go stays strict: its token is the one the user gave sync to write a private repo, outside ghtoken, and an anonymous retry can't write there, so it would only hide why sync failed. Tests (httptest only): ghtoken_test covers an env token refused then asked again without, not sent again (and GH_TOKEN tried next), a Settings token refused stays an error, an env token refused then an anonymous rate limit is reported, and another host untouched. plugin TestTaggedReposStaleEnvTokenDropped is till's case: TaggedRepos with GITHUB_TOKEN=stale-till lists 7 repos, no error. github_token_test and library TestGitHubRateLimitSaid now expect the env token dropped and the Settings token still refused. Broken by hand, each red: (a) no anonymous retry: every new env test, the plugin and library tests red, and TestTaggedReposStaleEnvTokenDropped shows till's exact error; (b) no memory of the refusal: NotSentAgain, the plugin and library tests red; (c) a Settings token also retried: SettingsTokenRefusedStaysAnError, the plugin and library tests red. Run on the commit before the rebase (base9b04c4e20): - gofmt -l: clean. go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows: clean. - set -o pipefail; go test -timeout 45m ./... at load ~30: 62 packages ok. agent, gateway and library ran past 45m while still progressing; gui failed TestSetAnswersWhileDevinAndProvidersHang (set took 10.26s, #1375) and TestMainWindowCorners (measuring killed, #1936/#2645); provider failed TestPluginPicksComeBackWithTheirList (#3051, fixed on main inc922225f1). - Each rerun alone at load 21-56: library ok 809.7s, provider ok 308.8s, gui ok 159.5s, agent ok 1673.3s, gateway ok 780.4s. After rebasing ontoa3ebed202(main moved 8 commits): gofmt -l clean; go vet darwin, linux -tags nogui, windows clean; go test -count=1 at load ~57: ghtoken ok 2.9s, plugin ok 277.6s, library ok 144.2s.
51 lines
1.7 KiB
Go
51 lines
1.7 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) }
|
|
|
|
// doGitHub sends req with send, carrying the token as withGitHubToken
|
|
// does, and says whether the answer is to a request that carried it: an
|
|
// environment token GitHub refuses is dropped and req sent again without
|
|
// it (ghtoken.Do).
|
|
func doGitHub(req *http.Request, send func(*http.Request) (*http.Response, error)) (*http.Response, bool, error) {
|
|
return ghtoken.Do(req, githubAPI, send)
|
|
}
|
|
|
|
// 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
|
|
}
|