diff --git a/internal/plugin/bun.go b/internal/plugin/bun.go index f3e8757a9..269ec1fcc 100644 --- a/internal/plugin/bun.go +++ b/internal/plugin/bun.go @@ -23,6 +23,7 @@ import ( "time" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/ghtoken" "github.com/yetone/magpie/internal/libc" "github.com/yetone/magpie/internal/netproxy" "github.com/yetone/magpie/internal/source" @@ -283,6 +284,9 @@ func getURLFrom(ctx context.Context, url string, limit int64, mirror bool) ([]by if err != nil { return nil, err } + // Bun's newest release is asked of GitHub's API: with the user's + // GitHub token, as every request magpie makes to it is (#1942) + ghtoken.Authorize(req, githubAPI) var res *http.Response if mirror { res, err = source.Do(http.DefaultClient, req) diff --git a/internal/plugin/bunupdate.go b/internal/plugin/bunupdate.go index 2740c94cb..649afe73d 100644 --- a/internal/plugin/bunupdate.go +++ b/internal/plugin/bunupdate.go @@ -145,7 +145,7 @@ func bunReported(exe string) string { // bunLatest is Bun's newest release and when it was published (tests stub it). var bunLatest = func(ctx context.Context) (string, time.Time, error) { - b, err := getURL(ctx, "https://api.github.com/repos/oven-sh/bun/releases/latest", 4<<20) + b, err := getURL(ctx, githubAPI+"/repos/oven-sh/bun/releases/latest", 4<<20) if err != nil { return "", time.Time{}, err } diff --git a/internal/plugin/github_token_test.go b/internal/plugin/github_token_test.go index 298f0b65d..02a39b0d5 100644 --- a/internal/plugin/github_token_test.go +++ b/internal/plugin/github_token_test.go @@ -169,3 +169,39 @@ func TestGitPluginCheckCarriesGitHubToken(t *testing.T) { t.Errorf("a host other than GitHub's API was sent Authorization %q", rawAuth) } } + +// Bun's newest release, asked of GitHub's API, carries the token too; a +// Bun download from another host never does (#1942). +func TestBunReleaseCheckCarriesGitHubToken(t *testing.T) { + dir := t.TempDir() + t.Setenv("HOME", dir) + t.Setenv("USERPROFILE", dir) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config")) + t.Setenv("GITHUB_TOKEN", "github-env") + var apiAuth, dlAuth string + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + apiAuth = r.Header.Get("Authorization") + fmt.Fprint(w, `{"tag_name":"bun-v1.4.2","published_at":"2026-10-01T00:00:00Z"}`) + })) + t.Cleanup(api.Close) + dl := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + dlAuth = r.Header.Get("Authorization") + fmt.Fprint(w, "zip") + })) + t.Cleanup(dl.Close) + was := githubAPI + githubAPI = api.URL + t.Cleanup(func() { githubAPI = was }) + if v, _, err := bunLatest(context.Background()); err != nil || v != "1.4.2" { + t.Fatalf("bunLatest = %q, %v", v, err) + } + if apiAuth != "Bearer github-env" { + t.Errorf("Bun's release was asked of GitHub's API with Authorization %q", apiAuth) + } + if _, err := getURL(context.Background(), dl.URL+"/bun-darwin-aarch64.zip", 1<<10); err != nil { + t.Fatal(err) + } + if dlAuth != "" { + t.Errorf("a download from another host was sent Authorization %q", dlAuth) + } +}