diff --git a/changelog.d/fixed-role-label-author-standing.md b/changelog.d/fixed-role-label-author-standing.md new file mode 100644 index 0000000000..67bea4a753 --- /dev/null +++ b/changelog.d/fixed-role-label-author-standing.md @@ -0,0 +1 @@ +- A maintainer's PR keeps `role/trusted` when the hive merges main into it. A person's Approve (or the hive's in-place adoption) admits the PR's head, and that route returned before the clean-merged-PR auto-promotion check, so the role label fell back to the author's raw registry tier. On projectbluefin/utah#1132 the hive's merge commit carried hanthor's own approval onto the new head, and the PR went from `role/trusted` to `role/newcomer` three minutes later and stayed there; projectbluefin/utah-packages#893 flipped the same way and back once the approval no longer matched the head. The role now comes from the author's own standing on every admission route: the registry tier, or trusted when their clean merged history promotes them. Who pushed or approved the head no longer changes it. diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index eefb42ad21..851b072aa7 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -579,9 +579,11 @@ type contributorClaimMatch struct { // OperatorLogin is set when a person's Approve admitted the head // (hivecommons/hive#15136, operatorAdmission). OperatorLogin string - // ObservedTier is the raw tier the lane saw for the author (registry - // profile or this hive's claim marker), admitted or not. It feeds the - // author/* stage label; admission itself still goes through Reason. + // ObservedTier is the tier the lane saw for the author (registry + // profile, this hive's claim marker, or "trusted" when the clean + // merged-history ratchet promotes them), admitted or not, and whatever + // route admitted the head. It feeds the role/* stage label; admission + // itself still goes through Reason. ObservedTier string } @@ -663,11 +665,17 @@ func (c *Engine) admittedRegistryTier(author string, policy ContributorLanePolic // its skip reasons exactly as before. func (c *Engine) admitContributor(ctx context.Context, owner, repo string, pr *gh.PullRequest, policy ContributorLanePolicy) (contributorClaimMatch, error) { author := hgithub.SafeGetLogin(pr.GetUser()) + // The adoption and operator routes admit a HEAD (the hive's own push, a + // person's Approve), not the author, so they return before the + // merged-history ratchet runs. The role label still shows the author's + // own standing, never the raw registry tier: a hive merge of main that a + // person's approval was carried onto flipped an auto-promoted maintainer + // from role/trusted to role/newcomer (projectbluefin/utah#1132). if headSHA := pr.GetHead().GetSHA(); c.adoptedHeadOwnedByHive(owner, repo, pr.GetNumber(), headSHA) { - return contributorClaimMatch{AdoptedHead: headSHA, ObservedTier: c.observedRegistryTier(author)}, nil + return contributorClaimMatch{AdoptedHead: headSHA, ObservedTier: c.authorStandingTier(ctx, owner, repo, author, policy)}, nil } if login := c.operatorAdmission(ctx, owner, repo, pr.GetNumber(), pr.GetHead().GetSHA()); login != "" { - return contributorClaimMatch{OperatorLogin: login, ObservedTier: c.observedRegistryTier(author)}, nil + return contributorClaimMatch{OperatorLogin: login, ObservedTier: c.authorStandingTier(ctx, owner, repo, author, policy)}, nil } if tier := c.admittedRegistryTier(author, policy); tier != "" { return contributorClaimMatch{RegistryTier: tier, ObservedTier: tier}, nil @@ -676,7 +684,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)) && !strings.HasSuffix(strings.ToLower(author), "[bot]") && (c.transport == nil || !strings.EqualFold(author, c.transport.AppBotLogin())) { + if err == nil && match.Reason != "" && c.autoPromotionEligible(author, policy) { history, promoteErr := c.contributorCleanMergedHistory(ctx, owner, repo, author, policy.AutoPromoteAfterMergedPR, policy.hiveWideAutoPromotion()) if promoteErr != nil { return contributorClaimMatch{Reason: "contributor-lane:auto-promote-history"}, promoteErr @@ -738,6 +746,42 @@ func (c *Engine) observedRegistryTier(author string) string { return strings.ToLower(strings.TrimSpace(tier)) } +// autoPromotionEligible reports whether the clean-merged-PR ratchet may run +// for author: it is configured, the operator has not revoked the author, +// and the author is neither a bot nor this hive's App. +func (c *Engine) autoPromotionEligible(author string, policy ContributorLanePolicy) bool { + if policy.AutoPromoteAfterMergedPR <= 0 || strings.TrimSpace(author) == "" { + return false + } + if contributorRegistryTierRevoked(c.observedRegistryTier(author)) || strings.HasSuffix(strings.ToLower(author), "[bot]") { + return false + } + return c.transport == nil || !strings.EqualFold(author, c.transport.AppBotLogin()) +} + +// authorStandingTier is the tier the role label shows for a PR the lane +// admitted by a head-scoped route (in-place adoption, a person's Approve). +// It is the author's registry tier, or "trusted" when that tier is below +// require_tiers and the author's clean merged history would auto-promote +// them, which is what the author's own route would have recorded. Nothing +// about the head (who pushed it, who approved it) enters. A history read +// that fails keeps the registry tier and never fails the admission. +func (c *Engine) authorStandingTier(ctx context.Context, owner, repo, author string, policy ContributorLanePolicy) string { + tier := c.observedRegistryTier(author) + if (tier != "" && policy.RequireTiers[tier]) || !c.autoPromotionEligible(author, policy) { + return tier + } + history, err := c.contributorCleanMergedHistory(ctx, owner, repo, author, policy.AutoPromoteAfterMergedPR, policy.hiveWideAutoPromotion()) + if err != nil { + c.debug("contributor-lane: author standing read failed; role keeps the registry tier", "repo", owner+"/"+repo, "author", author, "error", err) + return tier + } + if history.Clean { + return "trusted" + } + return tier +} + // contributorRegistryTierRevoked reports whether the registry tier is the // operator's explicit `revoked` decision. Revocation is a statement that the // contributor must not be trusted again without a human reinstating them, so diff --git a/src/pkg/github/automerge/automerge_contributor_role_standing_test.go b/src/pkg/github/automerge/automerge_contributor_role_standing_test.go new file mode 100644 index 0000000000..7e7d7f5bf9 --- /dev/null +++ b/src/pkg/github/automerge/automerge_contributor_role_standing_test.go @@ -0,0 +1,95 @@ +package automerge + +import ( + "context" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +// A maintainer whose registry profile still says newcomer is trusted through +// the clean-merged-PR ratchet. When the hive merges main into their branch +// and carries their own Approve onto the merge commit (or owns the head +// through an in-place adoption), the lane admits that head by the +// head-scoped route and returns before the ratchet runs. The role label must +// still show the author's standing, not the raw registry tier: the head's +// author (the App) and the admission route say nothing about the PR author +// (projectbluefin/utah#1132 went role/trusted -> role/newcomer three minutes +// after the hive's merge commit and stayed there). +func TestAdmitContributorHeadRoutesKeepAuthorStanding(t *testing.T) { + const mergeHead = "sha7" // the hive's "Merge branch 'main' into …" commit + cleanHistory := func(n int) []map[string]any { + out := make([]map[string]any, 0, n) + for i := 0; i < n; i++ { + out = append(out, mergedHistoryPR(contributorLaneTestAuthor, "fix: clean change")) + } + return out + } + for _, tc := range []struct { + name string + route string // "operator" or "adopted" + registryTier string + history []map[string]any + wantTier string + wantRole stagelabels.Role + }{ + {name: "approval carried onto the hive merge commit keeps trusted", route: "operator", registryTier: "newcomer", history: cleanHistory(5), wantTier: "trusted", wantRole: stagelabels.RoleTrusted}, + {name: "hive-owned adopted head keeps trusted", route: "adopted", registryTier: "newcomer", history: cleanHistory(5), wantTier: "trusted", wantRole: stagelabels.RoleTrusted}, + {name: "short history still shows the registry tier", route: "operator", registryTier: "newcomer", history: cleanHistory(4), wantTier: "newcomer", wantRole: stagelabels.RoleNewcomer}, + {name: "revoked author is never promoted by history", route: "operator", registryTier: "revoked", history: cleanHistory(5), wantTier: "revoked", wantRole: stagelabels.RoleUntrusted}, + {name: "registry tier in require_tiers needs no history", route: "operator", registryTier: "merger", wantTier: "merger", wantRole: stagelabels.RoleTrusted}, + } { + t.Run(tc.name, func(t *testing.T) { + fx := validContributorFixture() + fx.mergedHistory = tc.history + api := newContributorLaneAPI(t, &fx) + defer api.Close() + policy := defaultContributorLanePolicy() + policy.AutoPromoteAfterMergedPR = 5 + clock := &contributorLaneClock{at: time.Date(2026, 11, 7, 5, 11, 35, 0, time.UTC)} + store := stagelabels.NewStore("", clock.now) + engine := newContributorLaneEngine(api.URL, policy, clock, Options{ + StageStore: store, + ContributorRegistryTier: registryTiers(map[string]string{contributorLaneTestAuthor: tc.registryTier}), + }) + key := stageKey("acme", "widget", 7) + store.Observe(key, stagelabels.ReviewPending, "contributor-lane:workflow-path") + switch tc.route { + case "operator": + if !store.NoteAdmission(key, mergeHead, stagelabels.OperatorApprovalAdmission(contributorLaneTestAuthor), clock.now()) { + t.Fatal("NoteAdmission refused") + } + case "adopted": + store.MarkAdoptedInPlace(key, mergeHead, "check-failing", clock.now()) + } + pr := &gh.PullRequest{Number: gh.Ptr(7), Body: gh.Ptr("Fixes #42"), User: &gh.User{Login: gh.Ptr(contributorLaneTestAuthor)}, Head: &gh.PullRequestBranch{SHA: gh.Ptr(mergeHead)}} + match, err := engine.admitContributor(context.Background(), "acme", "widget", pr, policy) + if err != nil { + t.Fatalf("admit err = %v", err) + } + switch tc.route { + case "operator": + if !match.admittedByOperator() { + t.Fatalf("not admitted by operator: %+v", match) + } + case "adopted": + if !match.admittedByAdoption() { + t.Fatalf("not admitted by adoption: %+v", match) + } + } + if match.ObservedTier != tc.wantTier { + t.Fatalf("ObservedTier = %q, want %q", match.ObservedTier, tc.wantTier) + } + if got := stagelabels.RoleFromTier(match.ObservedTier, policy.RequireTiers); got != tc.wantRole { + t.Fatalf("role = %s, want %s", got.Label(), tc.wantRole.Label()) + } + // Labelling never announces a promotion; only the author's own + // admission route does. + if fx.comments != 0 { + t.Fatalf("comments = %d, want 0 (last %q)", fx.comments, fx.lastComment) + } + }) + } +}