From ba132d68b656360ed39b19478ed40a112b9cf8d9 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 16 Oct 2026 19:42:56 +0800 Subject: [PATCH] plugin: the GitHub plugin list reads every page of the search, not the top 50 (@HoGee_xxl on X) @HoGee_xxl on X: the Plugins page's GitHub list showed 50 repositories while the opencode-plugin topic held 59. askTagged asked GitHub's search for one page of 50 and stopped, so every repository past the 50th by stars never showed. It now reads pages of 100 until a short page or total_count, up to the 1000 GitHub's search answers, and a repository that moves between two pages (starred in between) is listed once. Run: - TestTaggedReposEveryPage (new): a fake search of 7 repositories served 2 a page; all 7 listed, in order, once. Red with the page loop broken by hand (stop after page 1): only the first 2 listed. - Full suite at 63be47717 plus this change, `go test -p 2 -tags nogui -count=1 -timeout 45m ./...` (load average 230-270): 63 packages ok; internal/agent and internal/gateway timed out at 45m with no test failed; internal/gui's TestSetAnswersWhileDevinAndProvidersHang red under load (a set took 22s), the open #1375 - output and cause commented there. internal/gui and internal/plugin then run alone: both ok (343s, 416s). - gofmt -l empty; GOOS=linux and GOOS=windows go vet -tags nogui ./... exit 0. Not tried against the real GitHub API in the app. --- internal/plugin/github.go | 73 ++++++++++++++++++++++------------ internal/plugin/github_test.go | 43 ++++++++++++++++++-- 2 files changed, 88 insertions(+), 28 deletions(-) diff --git a/internal/plugin/github.go b/internal/plugin/github.go index 526b31666..6d73d97ea 100644 --- a/internal/plugin/github.go +++ b/internal/plugin/github.go @@ -8,6 +8,7 @@ import ( "os" "path" "path/filepath" + "strconv" "strings" "sync" "time" @@ -68,6 +69,9 @@ var ( taggedEach = 20 * time.Second taggedAtOnce = 12 taggedRetry = 20 * time.Second + // taggedPage is how many repositories one page of the search holds + // (GitHub's most is 100) + taggedPage = 100 ) var ( @@ -152,32 +156,51 @@ func TaggedRepos(ctx context.Context) []Tagged { func askTagged(ctx context.Context, last []Tagged) (l []Tagged, whole bool, err error) { c, cancel := context.WithTimeout(ctx, taggedWait) defer cancel() - v := url.Values{"q": {"topic:" + Topic + " archived:false fork:false"}, "sort": {"stars"}, "order": {"desc"}, "per_page": {"50"}} - b, err := fetchJSON(c, githubAPI+"/search/repositories?"+v.Encode(), 4<<20) - if err != nil { - return nil, false, err + type item struct { + FullName string `json:"full_name"` + HTMLURL string `json:"html_url"` + Description string `json:"description"` + Stars int `json:"stargazers_count"` + DefaultBranch string `json:"default_branch"` + Pushed time.Time `json:"pushed_at"` + Archived bool `json:"archived"` + Fork bool `json:"fork"` + Owner struct { + Login string `json:"login"` + AvatarURL string `json:"avatar_url"` + } `json:"owner"` + License *struct { + SPDX string `json:"spdx_id"` + } `json:"license"` } - var r struct { - Items []struct { - FullName string `json:"full_name"` - HTMLURL string `json:"html_url"` - Description string `json:"description"` - Stars int `json:"stargazers_count"` - DefaultBranch string `json:"default_branch"` - Pushed time.Time `json:"pushed_at"` - Archived bool `json:"archived"` - Fork bool `json:"fork"` - Owner struct { - Login string `json:"login"` - AvatarURL string `json:"avatar_url"` - } `json:"owner"` - License *struct { - SPDX string `json:"spdx_id"` - } `json:"license"` - } `json:"items"` - } - if err := json.Unmarshal(b, &r); err != nil { - return nil, false, err + var r struct{ Items []item } + // every page, not the first: one page left out every repository past + // it. GitHub's search answers at most 1000 results. + listed := map[string]bool{} + for page := 1; page*taggedPage <= 1000; page++ { + v := url.Values{"q": {"topic:" + Topic + " archived:false fork:false"}, "sort": {"stars"}, "order": {"desc"}, + "per_page": {strconv.Itoa(taggedPage)}, "page": {strconv.Itoa(page)}} + b, err := fetchJSON(c, githubAPI+"/search/repositories?"+v.Encode(), 4<<20) + if err != nil { + return nil, false, err + } + var p struct { + Total int `json:"total_count"` + Items []item `json:"items"` + } + if err := json.Unmarshal(b, &p); err != nil { + return nil, false, err + } + for _, it := range p.Items { + // a repository starred between two pages can show on both + if k := strings.ToLower(it.FullName); !listed[k] { + listed[k] = true + r.Items = append(r.Items, it) + } + } + if len(p.Items) < taggedPage || page*taggedPage >= p.Total { + break + } } before := map[string]Tagged{} for _, t := range last { diff --git a/internal/plugin/github_test.go b/internal/plugin/github_test.go index f88d4f151..49729024e 100644 --- a/internal/plugin/github_test.go +++ b/internal/plugin/github_test.go @@ -5,8 +5,10 @@ import ( "encoding/json" "net/http" "net/http/httptest" + "net/url" "os" "path/filepath" + "strconv" "strings" "sync" "sync/atomic" @@ -72,8 +74,17 @@ func fakeGitHub(t *testing.T, up *atomic.Bool, asked *atomic.Int32) { like("forker/opencode-claude-auth", map[string]any{"fork": true}), like("old/opencode-old", map[string]any{"archived": true}), like("magpie-community/plugins", nil)) - r["items"] = items - search, _ := json.Marshal(r) + // one page of the search, as GitHub cuts it + search := func(q url.Values) []byte { + per, _ := strconv.Atoi(q.Get("per_page")) + page, _ := strconv.Atoi(q.Get("page")) + from, to := min(max(page-1, 0)*per, len(items)), len(items) + if per > 0 { + to = min(from+per, len(items)) + } + b, _ := json.Marshal(map[string]any{"total_count": len(items), "incomplete_results": false, "items": items[from:to]}) + return b + } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, q *http.Request) { if q.URL.Path != "/search/repositories" { reads.Add(1) @@ -90,7 +101,7 @@ func fakeGitHub(t *testing.T, up *atomic.Bool, asked *atomic.Int32) { if got := q.URL.Query().Get("q"); got != "topic:magpie-plugin archived:false fork:false" { t.Errorf("searched %q", got) } - w.Write(search) + w.Write(search(q.URL.Query())) return } if name, ok := strings.CutPrefix(q.URL.Path, "/npm/"); ok { @@ -128,6 +139,32 @@ func fakeGitHub(t *testing.T, up *atomic.Bool, asked *atomic.Int32) { // Repositories tagged magpie-plugin: each with what its package.json says, // in GitHub's order; forks, archived ones and magpie-community's left out. // Asked once in six hours; with GitHub down, what it said last. +// Every page of the search is read: with a page short of what is tagged, +// the list is the same as from one page, in GitHub's order (HoGee_xxl on X: +// 59 tagged, and the 9 past GitHub's first page of 50 never showed). +func TestTaggedReposEveryPage(t *testing.T) { + var up atomic.Bool + var asked atomic.Int32 + up.Store(true) + fakeGitHub(t, &up, &asked) + was := taggedPage + taggedPage = 4 + t.Cleanup(func() { taggedPage = was }) + + var repos []string + for _, x := range TaggedRepos(context.Background()) { + repos = append(repos, x.Repo) + } + want := "rynfar/meridian griffinmartin/opencode-claude-auth slkiser/opencode-quota someone/word-guard someone/agent-aider someone/page-glance someone/flaky-entry" + if strings.Join(repos, " ") != want { + t.Fatalf("repos %v\nwant %s", repos, want) + } + // 15 items, four to a page + if n := asked.Load(); n != 4 { + t.Fatalf("asked %d pages, want 4", n) + } +} + func TestTaggedRepos(t *testing.T) { var up atomic.Bool var asked atomic.Int32