Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-15088-promotion-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Avoid repeated contributor auto-promotion searches within a sweep and skip bot authors ([#15088](https://github.com/hivecommons/hive/issues/15088)).
34 changes: 32 additions & 2 deletions src/pkg/github/automerge/automerge_contributor_lane.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package automerge

// Contributor-lane auto-merge tier.
Expand Down Expand Up @@ -398,6 +398,7 @@
}
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
Expand Down Expand Up @@ -626,7 +627,7 @@
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
Expand Down Expand Up @@ -797,6 +798,8 @@
// 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
Expand All @@ -810,6 +813,17 @@
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() {
Expand All @@ -818,7 +832,7 @@
continue
}
full := strings.ToLower(o + "/" + r)
if full == own || seen[full] {
if (cache == nil && full == own) || seen[full] {
continue
}
seen[full] = true
Expand Down Expand Up @@ -854,13 +868,19 @@
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
}
}
query += qualifier
}
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))
Expand All @@ -869,6 +889,16 @@
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
}

Expand Down
49 changes: 49 additions & 0 deletions src/pkg/github/automerge/automerge_contributor_lane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
Loading