diff --git a/changelog.d/fixed-15088-promotion-search.md b/changelog.d/fixed-15088-promotion-search.md new file mode 100644 index 0000000000..256dfc3dff --- /dev/null +++ b/changelog.d/fixed-15088-promotion-search.md @@ -0,0 +1 @@ +- Avoid repeated contributor auto-promotion searches within a sweep and skip bot authors ([#15088](https://github.com/hivecommons/hive/issues/15088)). diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index 4991610b2f..c449b84afb 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -398,6 +398,7 @@ func (c *Engine) SweepContributorLaneAutoMerges(ctx context.Context, opts AutoMe } ctx = contextWithExpectedCheckCache(ctx, newExpectedCheckCache()) ctx = contextWithLaneMergedBranches(ctx) + ctx = context.WithValue(ctx, contributorRepoSearchCacheKey{}, make(map[string][]contributorRepoRef)) for _, displayRepo := range c.activeRepos() { if len(result.Merged) >= maxMerges { break @@ -626,7 +627,7 @@ func (c *Engine) admitContributor(ctx context.Context, owner, repo string, pr *g if match.ObservedTier == "" { match.ObservedTier = c.observedRegistryTier(author) } - if err == nil && match.Reason != "" && policy.AutoPromoteAfterMergedPR > 0 && !contributorRegistryTierRevoked(c.observedRegistryTier(author)) { + if err == nil && match.Reason != "" && policy.AutoPromoteAfterMergedPR > 0 && !contributorRegistryTierRevoked(c.observedRegistryTier(author)) && !strings.HasSuffix(strings.ToLower(author), "[bot]") && (c.transport == nil || !strings.EqualFold(author, c.transport.AppBotLogin())) { history, promoteErr := c.contributorCleanMergedHistory(ctx, owner, repo, author, policy.AutoPromoteAfterMergedPR, policy.hiveWideAutoPromotion()) if promoteErr != nil { return contributorClaimMatch{Reason: "contributor-lane:auto-promote-history"}, promoteErr @@ -797,6 +798,8 @@ type contributorRepoRef struct { // contributorSearchQueryMaxLen is GitHub's limit on a search `q` parameter. const contributorSearchQueryMaxLen = 256 +type contributorRepoSearchCacheKey struct{} + // contributorMergedRepos returns the hive's other active repositories that // hold at least one merged PR by author, in the order the transport lists // them, excluding the PR's own (owner, repo). It asks search/issues @@ -810,6 +813,17 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author return nil } own := strings.ToLower(owner + "/" + repo) + cache, _ := ctx.Value(contributorRepoSearchCacheKey{}).(map[string][]contributorRepoRef) + key := strings.ToLower(strings.TrimSpace(author)) + if refs, ok := cache[key]; ok { + var others []contributorRepoRef + for _, ref := range refs { + if !strings.EqualFold(ref.Owner+"/"+ref.Repo, own) { + others = append(others, ref) + } + } + return others + } var candidates []contributorRepoRef seen := map[string]bool{} for _, display := range c.activeRepos() { @@ -818,7 +832,7 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author continue } full := strings.ToLower(o + "/" + r) - if full == own || seen[full] { + if (cache == nil && full == own) || seen[full] { continue } seen[full] = true @@ -854,6 +868,9 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author if len(query)+len(qualifier) > contributorSearchQueryMaxLen { if err := flush(); err != nil { c.warn("contributor-lane auto-promotion repo search failed; counting the PR's repository only", "repo", owner+"/"+repo, "author", author, "error", err) + if cache != nil { + cache[key] = nil + } return nil } } @@ -861,6 +878,9 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author } if err := flush(); err != nil { c.warn("contributor-lane auto-promotion repo search failed; counting the PR's repository only", "repo", owner+"/"+repo, "author", author, "error", err) + if cache != nil { + cache[key] = nil + } return nil } out := make([]contributorRepoRef, 0, len(found)) @@ -869,6 +889,16 @@ func (c *Engine) contributorMergedRepos(ctx context.Context, owner, repo, author out = append(out, ref) } } + if cache != nil { + cache[key] = out + var others []contributorRepoRef + for _, ref := range out { + if !strings.EqualFold(ref.Owner+"/"+ref.Repo, own) { + others = append(others, ref) + } + } + return others + } return out } diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index f48318722e..81bc734429 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -1666,3 +1666,52 @@ func TestContributorLaneAssociationTrusted(t *testing.T) { t.Fatal("nil PR must not be trusted") } } + +func TestContributorPromotionSearchCachePerSweep(t *testing.T) { + fx := validContributorFixture() + fx.issueComments[0]["body"] = contributorClaimMarkerTier(contributorLaneTestAuthor, "contributor", contributorLaneTestHive, " tier=contributor verified=true") + api := newContributorLaneAPI(t, &fx) + defer api.Close() + policy := defaultContributorLanePolicy() + policy.AutoPromoteAfterMergedPR = 5 + clock := &contributorLaneClock{at: time.Date(2026, 10, 15, 13, 0, 0, 0, time.UTC)} + engine := newContributorLaneEngineRepos(api.URL, []string{"widget", "hive"}, policy, clock, Options{}) + ctx := context.WithValue(context.Background(), contributorRepoSearchCacheKey{}, make(map[string][]contributorRepoRef)) + for _, author := range []string{"alice", "alice", "alice", "bob"} { + _, err := engine.contributorCleanMergedHistory(ctx, "acme", "widget", author, 5, true) + if err != nil { + t.Fatal(err) + } + } + if fx.searches != 2 { + t.Fatalf("searches = %d, want 2", fx.searches) + } + ctx = context.WithValue(context.Background(), contributorRepoSearchCacheKey{}, make(map[string][]contributorRepoRef)) + _, err := engine.contributorCleanMergedHistory(ctx, "acme", "widget", "alice", 5, true) + if err != nil { + t.Fatal(err) + } + if fx.searches != 3 { + t.Fatalf("new sweep searches = %d, want 3", fx.searches) + } +} + +func TestContributorPromotionSkipsBots(t *testing.T) { + for _, author := range []string{"github-actions[bot]", "mergeraptor[bot]", "other[bot]"} { + t.Run(author, func(t *testing.T) { + fx := validContributorFixture() + fx.author = author + fx.issueComments[0]["body"] = contributorClaimMarkerTier(author, "contributor", contributorLaneTestHive, " tier=contributor verified=true") + api := newContributorLaneAPI(t, &fx) + defer api.Close() + policy := defaultContributorLanePolicy() + policy.AutoPromoteAfterMergedPR = 5 + clock := &contributorLaneClock{at: time.Date(2026, 10, 15, 13, 0, 0, 0, time.UTC)} + engine := newContributorLaneEngineRepos(api.URL, []string{"widget", "hive"}, policy, clock, Options{}) + _, _, _ = engine.trySweepContributorLanePR(context.Background(), "widget", "acme", "widget", 7, policy) + if fx.searches != 0 { + t.Fatalf("bot triggered %d searches", fx.searches) + } + }) + } +}