diff --git a/changelog.d/fixed-17148.md b/changelog.d/fixed-17148.md new file mode 100644 index 0000000000..4d2925b944 --- /dev/null +++ b/changelog.d/fixed-17148.md @@ -0,0 +1 @@ +- **automerge:** the contributor lane's hive-wide auto-promotion search no longer drops every other repository when GitHub refuses to search one of them (`422 Validation Failed` for a renamed, deleted or invisible repo, or an unsearchable author): the chunk is re-asked one repository at a time, and each refused (author, repository) pair warns once and is left out of the search for an hour instead of repeating the 422 and the `repo search failed` warning on every sweep (#17148). diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index b9c9613238..b23018fcb3 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -876,9 +876,16 @@ A PR merges through this tier only when all of the following hold: walked first; only when it is short does one `search/issues` query (`is:pr is:merged author: repo:… repo:…`) find which other hive repos hold the author's merges, and only those are walked for revert - evidence — a revert or sentinel hit in any repository blocks. Promotion - is a property of the author in the hive, not of a PR, so the sweep - announces it **once per (hive, author)**: the first PR that earns it + evidence — a revert or sentinel hit in any repository blocks. When + GitHub refuses that query with `422 Validation Failed` (a listed + repository was renamed, deleted or is invisible to the App, or the + author cannot be searched), its repositories are re-asked one at a time + so the searchable ones still count; each refused (author, repository) + pair warns once (`contributor-lane auto-promotion search cannot see + repository for this author`) and is left out of the search for an hour + (#17148). Any other search failure counts the PR's own repository only. + Promotion is a property of the author in the hive, not of a PR, so the + sweep announces it **once per (hive, author)**: the first PR that earns it gets a `` comment ("@login is now trusted for contributor-lane merging in this hive … Applies to their open and diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 80338d77ec..2e2d23e65e 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -878,11 +878,17 @@ const contributorSearchQueryMaxLen = 256 // 256-character query limit, so a ten-repo hive costs one or two search // calls instead of ten closed-PR listings. A search failure degrades to the // PR's own repository with a warning rather than blocking the sweep: the -// result is then exactly the pre-#14517 per-repo count. +// result is then exactly the pre-#14517 per-repo count. A 422 — GitHub +// refusing to search a listed repository or user — is not a failure of the +// whole search: the chunk's repositories are re-asked one at a time so one +// renamed or invisible repository does not hide the others, and each refused +// (author, repository) pair is left out of later searches for +// contributorUnsearchableTTL (hivecommons/hive#17148). func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author string) []contributorRepoRef { if c == nil || c.gh == nil || c.transport == nil { return nil } + author = strings.TrimPrefix(strings.TrimSpace(author), "@") own := strings.ToLower(owner + "/" + repo) var candidates []contributorRepoRef seen := map[string]bool{} @@ -896,20 +902,23 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author continue } seen[full] = true - candidates = append(candidates, contributorRepoRef{Owner: o, Repo: r}) + ref := contributorRepoRef{Owner: o, Repo: r} + if c.contributorSearchSkips(author, ref) { + continue + } + candidates = append(candidates, ref) } if len(candidates) == 0 { return nil } - base := "is:pr is:merged author:" + strings.TrimPrefix(strings.TrimSpace(author), "@") + base := "is:pr is:merged author:" + author found := map[string]bool{} - query := base - flush := func() error { - if query == base { - return nil + search := func(refs []contributorRepoRef) error { + query := base + for _, ref := range refs { + query += " repo:" + ref.Owner + "/" + ref.Repo } result, _, err := c.gh.Search.Issues(ctx, query, &gh.SearchOptions{Sort: "updated", Order: "desc", ListOptions: gh.ListOptions{PerPage: 100}}) - query = base if err != nil { return err } @@ -923,17 +932,42 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author } return nil } + flush := func(chunk []contributorRepoRef) error { + err := search(chunk) + if !searchQueryUnsearchable(err) { + return err + } + if len(chunk) == 1 { + c.noteContributorUnsearchable(owner, repo, author, chunk[0], err) + return nil + } + for _, ref := range chunk { + err := search([]contributorRepoRef{ref}) + if searchQueryUnsearchable(err) { + c.noteContributorUnsearchable(owner, repo, author, ref, err) + continue + } + if err != nil { + return err + } + } + return nil + } + var chunk []contributorRepoRef + length := len(base) for _, ref := range candidates { - qualifier := " repo:" + ref.Owner + "/" + ref.Repo - if len(query)+len(qualifier) > contributorSearchQueryMaxLen { - if err := flush(); err != nil { + qualifier := len(" repo:") + len(ref.Owner) + len("/") + len(ref.Repo) + if len(chunk) > 0 && length+qualifier > contributorSearchQueryMaxLen { + if err := flush(chunk); err != nil { c.warn("contributor-lane auto-promotion repo search failed; counting the PR's repository only", "repo", owner+"/"+repo, "author", author, "error", err) return nil } + chunk, length = nil, len(base) } - query += qualifier + chunk = append(chunk, ref) + length += qualifier } - if err := flush(); err != nil { + if err := flush(chunk); err != nil { c.warn("contributor-lane auto-promotion repo search failed; counting the PR's repository only", "repo", owner+"/"+repo, "author", author, "error", err) return nil } diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index c60ed61224..ea8fb4f6c8 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -75,10 +75,15 @@ type contributorLaneFixture struct { // (acme/), served to the hive-wide auto-promotion walk; the fake // search/issues endpoint reports a repo only when it holds a merged PR // by the searched author. - otherHistory map[string][]map[string]any - searchStatus int - searches int - searchQuery string + otherHistory map[string][]map[string]any + searchStatus int + searches int + searchQuery string + searchQueries []string + // unsearchable lists the owner/repo names the fake search/issues + // endpoint refuses with GitHub's 422 Validation Failed whenever a query + // names them, as it does for a renamed or invisible repository. + unsearchable map[string]bool otherLists []string issueCommentsSt int reviews []map[string]any @@ -233,6 +238,7 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S case r.Method == http.MethodGet && r.URL.Path == "/search/issues": fx.searches++ fx.searchQuery = r.URL.Query().Get("q") + fx.searchQueries = append(fx.searchQueries, fx.searchQuery) if fx.searchStatus != 0 { http.Error(w, "search error", fx.searchStatus) return @@ -247,6 +253,14 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S searchedRepos = append(searchedRepos, strings.TrimPrefix(term, "repo:")) } } + for _, full := range searchedRepos { + if fx.unsearchable[full] { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusUnprocessableEntity) + json.NewEncoder(w).Encode(map[string]any{"message": "Validation Failed", "errors": []map[string]string{{"resource": "Search", "field": "q", "code": "invalid", "message": "The listed users and repositories cannot be searched either because the resources do not exist or you do not have permission to view them."}}}) + return + } + } items := []map[string]any{} for _, full := range searchedRepos { name := strings.TrimPrefix(full, "acme/") @@ -1245,6 +1259,7 @@ func TestTrySweepContributorLanePRAutoPromotionScope(t *testing.T) { own []map[string]any other map[string][]map[string]any searchStatus int + unsearchable []string wantReason string wantMerges int wantSearches int @@ -1320,10 +1335,30 @@ func TestTrySweepContributorLanePRAutoPromotionScope(t *testing.T) { name: "search failure degrades to the PR's repository", own: cleanHistory(1), other: map[string][]map[string]any{"hive": cleanHistory(9)}, - searchStatus: http.StatusUnprocessableEntity, + searchStatus: http.StatusInternalServerError, wantReason: contributorLaneReasonClaimTier, wantSearches: 1, }, + // hivecommons/hive#17148: GitHub refuses the whole query when one + // listed repository cannot be searched; the others still count. + { + name: "an unsearchable repository does not hide the others", + own: cleanHistory(1), + other: map[string][]map[string]any{"hive": cleanHistory(9)}, + unsearchable: []string{"acme/docs"}, + wantMerges: 1, + wantSearches: 4, + wantLists: []string{"hive"}, + wantComment: "merged=5 scope=hive repos=widget:1,hive:4 -->", + }, + { + name: "an unsearchable author degrades to the PR's repository", + own: cleanHistory(1), + other: map[string][]map[string]any{"hive": cleanHistory(9)}, + unsearchable: []string{"acme/hive", "acme/docs", "acme/homebrew-hive"}, + wantReason: contributorLaneReasonClaimTier, + wantSearches: 4, + }, } { t.Run(tc.name, func(t *testing.T) { fx := validContributorFixture() @@ -1331,6 +1366,10 @@ func TestTrySweepContributorLanePRAutoPromotionScope(t *testing.T) { fx.mergedHistory = tc.own fx.otherHistory = tc.other fx.searchStatus = tc.searchStatus + fx.unsearchable = map[string]bool{} + for _, full := range tc.unsearchable { + fx.unsearchable[full] = true + } api := newContributorLaneAPI(t, &fx) defer api.Close() policy := defaultContributorLanePolicy() @@ -1352,13 +1391,14 @@ func TestTrySweepContributorLanePRAutoPromotionScope(t *testing.T) { t.Fatalf("searches = %d, want %d (q=%q)", fx.searches, tc.wantSearches, fx.searchQuery) } if fx.searches > 0 { + first := fx.searchQueries[0] for _, want := range []string{"is:pr", "is:merged", "author:" + contributorLaneTestAuthor, "repo:acme/hive", "repo:acme/docs", "repo:acme/homebrew-hive"} { - if !strings.Contains(fx.searchQuery, want) { - t.Fatalf("search query %q missing %q", fx.searchQuery, want) + if !strings.Contains(first, want) { + t.Fatalf("search query %q missing %q", first, want) } } - if strings.Contains(fx.searchQuery, "repo:acme/widget") { - t.Fatalf("search query %q must not include the PR's own repository", fx.searchQuery) + if strings.Contains(first, "repo:acme/widget") { + t.Fatalf("search query %q must not include the PR's own repository", first) } } if strings.Join(fx.otherLists, ",") != strings.Join(tc.wantLists, ",") { diff --git a/src/pkg/github/automerge/automerge_contributor_unsearchable.go b/src/pkg/github/automerge/automerge_contributor_unsearchable.go new file mode 100644 index 0000000000..ee9714616b --- /dev/null +++ b/src/pkg/github/automerge/automerge_contributor_unsearchable.go @@ -0,0 +1,66 @@ +package automerge + +import ( + "errors" + "net/http" + "strings" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// contributorUnsearchableTTL is how long contributorMergedRepos leaves an +// (author, repository) pair out of its search after GitHub refused to search +// it. The refusal is deterministic — the repository was renamed, deleted or +// made private to the App, or the author's account cannot be searched — so +// re-asking every sweep only repeats the 422 (hivecommons/hive#17148); the +// TTL lets a repaired repository or account come back without a restart. +const contributorUnsearchableTTL = time.Hour + +// contributorUnsearchableMessage is logged once per (author, repository) +// pair per process at WARN, then at Debug. +const contributorUnsearchableMessage = "contributor-lane auto-promotion search cannot see repository for this author; leaving it out of the hive-wide count" + +// searchQueryUnsearchable reports whether err is GitHub's 422 Validation +// Failed for a search whose listed users or repositories "cannot be searched +// either because the resources do not exist or you do not have permission to +// view them". It does not say which qualifier it refused. +func searchQueryUnsearchable(err error) bool { + var ghErr *gh.ErrorResponse + return errors.As(err, &ghErr) && ghErr.Response != nil && ghErr.Response.StatusCode == http.StatusUnprocessableEntity +} + +func contributorUnsearchableKey(author string, ref contributorRepoRef) string { + return strings.ToLower(author + "|" + ref.Owner + "/" + ref.Repo) +} + +// contributorSearchSkips reports whether ref is still inside the window in +// which author's search leaves it out. +func (c *Engine) contributorSearchSkips(author string, ref contributorRepoRef) bool { + key := contributorUnsearchableKey(author, ref) + c.contributorUnsearchableMu.Lock() + until, ok := c.contributorUnsearchable[key] + c.contributorUnsearchableMu.Unlock() + return ok && c.clock().Before(until) +} + +// noteContributorUnsearchable records that GitHub refused to search ref for +// author, so the next contributorUnsearchableTTL of sweeps leave it out. The +// first refusal for a pair logs at WARN; a refusal again after the TTL logs +// at Debug, so a permanently unsearchable pair is not a repeated warning. +func (c *Engine) noteContributorUnsearchable(owner, repo, author string, ref contributorRepoRef, err error) { + key := contributorUnsearchableKey(author, ref) + c.contributorUnsearchableMu.Lock() + if c.contributorUnsearchable == nil || len(c.contributorUnsearchable) > 4096 { + c.contributorUnsearchable = map[string]time.Time{} + } + _, seen := c.contributorUnsearchable[key] + c.contributorUnsearchable[key] = c.clock().Add(contributorUnsearchableTTL) + c.contributorUnsearchableMu.Unlock() + args := []any{"repo", owner + "/" + repo, "author", author, "searched_repo", ref.Owner + "/" + ref.Repo, "retry_after", contributorUnsearchableTTL, "error", err} + if seen { + c.debug(contributorUnsearchableMessage, args...) + return + } + c.warn(contributorUnsearchableMessage, args...) +} diff --git a/src/pkg/github/automerge/automerge_contributor_unsearchable_test.go b/src/pkg/github/automerge/automerge_contributor_unsearchable_test.go new file mode 100644 index 0000000000..99d84bb503 --- /dev/null +++ b/src/pkg/github/automerge/automerge_contributor_unsearchable_test.go @@ -0,0 +1,74 @@ +package automerge + +import ( + "bytes" + "context" + "log/slog" + "strings" + "testing" + "time" +) + +// Regression for hivecommons/hive#17148: a repository GitHub refuses to +// search for an author is re-asked once per contributorUnsearchableTTL, not +// on every sweep, the searchable repositories keep counting meanwhile, and +// the refusal warns once per process instead of on every PR evaluation. +func TestContributorMergedReposRemembersUnsearchableRepository(t *testing.T) { + fx := validContributorFixture() + fx.otherHistory = map[string][]map[string]any{"hive": {mergedHistoryPR(contributorLaneTestAuthor, "fix: clean change")}} + fx.unsearchable = map[string]bool{"acme/docs": true} + api := newContributorLaneAPI(t, &fx) + defer api.Close() + var logs bytes.Buffer + clock := &contributorLaneClock{at: time.Date(2026, 11, 8, 2, 0, 0, 0, time.UTC)} + engine := newContributorLaneEngineRepos(api.URL, []string{"widget", "hive", "docs", "homebrew-hive"}, defaultContributorLanePolicy(), clock, Options{ + Logger: slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelWarn})), + }) + ask := func(step string, wantQueries ...string) { + t.Helper() + before := len(fx.searchQueries) + var got []string + for _, ref := range engine.contributorMergedRepos(context.Background(), "acme", "widget", contributorLaneTestAuthor) { + got = append(got, ref.Owner+"/"+ref.Repo) + } + if strings.Join(got, ",") != "acme/hive" { + t.Fatalf("%s: repos = %v, want [acme/hive]", step, got) + } + base := "is:pr is:merged author:" + contributorLaneTestAuthor + var want []string + for _, repos := range wantQueries { + want = append(want, base+repos) + } + if gotQueries := fx.searchQueries[before:]; strings.Join(gotQueries, "\n") != strings.Join(want, "\n") { + t.Fatalf("%s: queries =\n%s\nwant\n%s", step, strings.Join(gotQueries, "\n"), strings.Join(want, "\n")) + } + } + + ask("first refusal isolates the repository", + " repo:acme/hive repo:acme/docs repo:acme/homebrew-hive", + " repo:acme/hive", + " repo:acme/docs", + " repo:acme/homebrew-hive", + ) + ask("next sweep leaves the refused pair out", " repo:acme/hive repo:acme/homebrew-hive") + clock.at = clock.at.Add(contributorUnsearchableTTL - time.Minute) + ask("still inside the TTL", " repo:acme/hive repo:acme/homebrew-hive") + clock.at = clock.at.Add(time.Minute) + ask("TTL expiry re-asks GitHub", + " repo:acme/hive repo:acme/docs repo:acme/homebrew-hive", + " repo:acme/hive", + " repo:acme/docs", + " repo:acme/homebrew-hive", + ) + + out := logs.String() + if n := strings.Count(out, contributorUnsearchableMessage); n != 1 { + t.Fatalf("unsearchable warning logged %d times, want 1:\n%s", n, out) + } + if strings.Contains(out, "repo search failed") { + t.Fatalf("a refused repository must not degrade the whole search:\n%s", out) + } + if !strings.Contains(out, "searched_repo=acme/docs") { + t.Fatalf("warning should name the refused repository:\n%s", out) + } +} diff --git a/src/pkg/github/automerge/automerge_sweep.go b/src/pkg/github/automerge/automerge_sweep.go index 33aafbfe90..bd02d619f6 100644 --- a/src/pkg/github/automerge/automerge_sweep.go +++ b/src/pkg/github/automerge/automerge_sweep.go @@ -290,6 +290,12 @@ type Engine struct { selfSkipWarnedMu sync.Mutex selfSkipWarned map[string]struct{} + // contributorUnsearchable maps author|owner/repo to the time until which + // contributorMergedRepos leaves that pair out of its search, after + // GitHub refused to search it (see automerge_contributor_unsearchable.go). + contributorUnsearchableMu sync.Mutex + contributorUnsearchable map[string]time.Time + mergerAuthzMu sync.RWMutex // humanReturnChecked remembers, per PR key, the head whose worn // 071/072 label was already attributed (see foldHumanReturnLabel), so