diff --git a/changelog.d/fixed-maintainer-push-takeover.md b/changelog.d/fixed-maintainer-push-takeover.md new file mode 100644 index 0000000000..da1ae4a32b --- /dev/null +++ b/changelog.d/fixed-maintainer-push-takeover.md @@ -0,0 +1 @@ +- A new head on an in-place-adopted PR pushed by someone other than the author with write or higher on the repository, or by another App, no longer hands the PR back to the offline author or nudges them: the hive keeps the adoption. A push by the author, or by a read-only third party, still yields as before (projectbluefin/utah#919, projectbluefin/utah-packages#804, #812, #822). diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index d722629aa0..a45e0365a3 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -1016,7 +1016,15 @@ When a contributor-agent PR goes red the hive does not just relabel it (a human- hive-controlled evidence means the contributor is back: the hive yields — ONE `` note, the adoption is forgotten and the PR is back on the ordinary 072 flow (a fresh nudge for the new - head; it may be adopted again under the same rules). + head; it may be adopted again under the same rules). The exception is a + head pushed by someone other than the author who may take the PR over: + the GitHub-linked committer (or the author, when the committer is + `web-flow` or unlinked) is an App bot, or a person whose repository + permission is write, maintain or admin. That is a maintainer takeover, + not the contributor returning: the adoption stands on the new head, no + yield note and no nudge to the author. A read-only third party's push + yields as above; an unreadable permission keeps the adoption and re-reads + on the next sweep. A review verdict of `changes_requested` on an in-place adoption is the hive's work: the review-fix dispatcher may push signed fixer commits to that adopted branch and keeps the PR in the fixer's work list every cycle diff --git a/src/docs/operator-reference/sweeps-and-operations.md b/src/docs/operator-reference/sweeps-and-operations.md index 590123e87b..984ee983bc 100644 --- a/src/docs/operator-reference/sweeps-and-operations.md +++ b/src/docs/operator-reference/sweeps-and-operations.md @@ -175,7 +175,10 @@ ownership: the hive follows the head only when GitHub links the commit to the App bot (the stage store only remembers SHAs that already passed that check); otherwise it hands the PR back as a contributor push (`governor.labels.stage.adoption.strict_attribution`, default on; first deploys -without any repo evidence log and keep adoption). Push-capable App-authored +without any repo evidence log and keep adoption) — unless an App bot or a +person with write or higher on the repository, not the author, pushed it: +that is a maintainer takeover and the adoption stands, with no nudge to the +author. Push-capable App-authored agents use the App bot noreply email (keyed by the bot's GitHub user id, not the App id) for git commits so fixer pushes normally carry that GitHub linkage while their commit name/sign-off still names the lane. Once the hive's in-place head is green, reviewer-approved, DCO-clean diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index 4840918aa2..bf23814f71 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -1781,7 +1781,8 @@ func (c *Engine) baseSyncTouchesWorkflows(ctx context.Context, obs stageObservat // loops. The first deploy after this rule also keeps adoption until this repo // has recorded any App-linked head; after that, missing hive evidence means // the contributor is back, so the hive yields once and the ordinary 072 flow -// re-arms. +// re-arms — unless the head was pushed by someone other than the author who +// may take the PR over (adoptionTakeoverPusher): then the adoption stands. // Returns the number of comments posted. func (c *Engine) settleInPlaceAdoption(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, prev stagelabels.Record, policy StageLabelPolicy, now time.Time) (int, error) { if prev.AdoptedInPlace() { @@ -1831,6 +1832,14 @@ func (c *Engine) settleInPlaceAdoption(ctx context.Context, store *stagelabels.S c.warn("stage labels: adopted head changed without hive evidence; no repo evidence recorded yet, keeping adoption", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "pusher", commitIdentity(commit)) return 0, nil } + if pusher, deferred := c.adoptionTakeoverPusher(ctx, obs, commit); deferred { + // Pusher's role unknown: keep the adoption, re-read next sweep. + return 0, nil + } else if pusher != "" { + store.MarkAdoptedHead(key, obs.HeadSHA) + c.info("stage labels: adopted head pushed by a maintainer or App, not the author; keeping adoption", "repo", obs.Display, "pr", obs.Number, "author", obs.Author, "head", obs.HeadSHA, "pusher", pusher) + return 0, nil + } store.MarkYielded(key, obs.HeadSHA) c.info("stage labels: head changed without hive push evidence; yielding", "repo", obs.Display, "pr", obs.Number, "author", obs.Author, "head", obs.HeadSHA, "pusher", commitIdentity(commit)) if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, obs.Number, &gh.IssueComment{Body: gh.Ptr(AdoptionYieldComment(obs))}); err != nil { @@ -1840,6 +1849,50 @@ func (c *Engine) settleInPlaceAdoption(ctx context.Context, store *stagelabels.S return 1, nil } +// adoptionTakeoverPusher returns the GitHub login that pushed an adopted +// head when it is not the PR's author and its push does not mean the author +// is back: an App bot (another hive, a CI bot) or a person with write or +// higher on the base repo. A maintainer rebasing or fixing an offline +// contributor's PR was read as the contributor returning, so the hive +// handed the PR back to the absent author and nudged them +// (projectbluefin/utah#919, projectbluefin/utah-packages#804, #812, #822). +// "" keeps the yield: the author, an unlinked identity, or a read-only +// third party. deferred is true when the pusher's permission could not be +// read. +func (c *Engine) adoptionTakeoverPusher(ctx context.Context, obs stageObservation, commit *gh.RepositoryCommit) (login string, deferred bool) { + pusher := commitPusher(commit) + login = strings.TrimSpace(pusher.GetLogin()) + if login == "" || normalizeBotLogin(login) == normalizeBotLogin(obs.Author) { + return "", false + } + if strings.EqualFold(pusher.GetType(), "Bot") || strings.HasSuffix(strings.ToLower(login), "[bot]") { + return login, false + } + perm, err := c.authorRepoPermission(ctx, obs.Owner, obs.Repo, login) + if err != nil { + c.warn("stage labels: could not read the pusher's repository permission; keeping adoption this sweep", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "pusher", login, "error", err) + return "", true + } + if !perm.Allowed { + return "", false + } + return login, false +} + +// commitPusher is the GitHub account closest to the push: the committer +// (a maintainer's rebase keeps the contributor as author), or the author +// when GitHub itself committed (web-flow: web edits, Update branch) or the +// committer is not linked to an account. nil when neither is linked. +func commitPusher(commit *gh.RepositoryCommit) *gh.User { + if u := commit.GetCommitter(); u.GetLogin() != "" && !strings.EqualFold(u.GetLogin(), "web-flow") { + return u + } + if u := commit.GetAuthor(); u.GetLogin() != "" { + return u + } + return nil +} + // commitIdentity renders the commit's pusher for logs: the GitHub login when // linked, else the git author name/email. func commitIdentity(commit *gh.RepositoryCommit) string { diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go index ccaa9b655a..55a7183be9 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go @@ -89,8 +89,9 @@ type adoptAPI struct { } type adoptCommit struct { - login string // GitHub login of author/committer, or git: for an unlinked identity - verified bool + login string // GitHub login of author/committer, or git: for an unlinked identity + committer string // GitHub committer login when it differs from login ("" = login) + verified bool } func newAdoptAPI() *adoptAPI { @@ -338,7 +339,7 @@ func (a *adoptAPI) handle(w http.ResponseWriter, r *http.Request) bool { _ = json.NewEncoder(w).Encode(map[string]any{"sha": sha, "author": nil, "committer": nil, "commit": map[string]any{"author": map[string]any{"name": name}, "committer": map[string]any{"name": name}, "verification": verification}}) return true } - _ = json.NewEncoder(w).Encode(map[string]any{"sha": sha, "author": map[string]any{"login": commit.login}, "committer": map[string]any{"login": commit.login}, "commit": map[string]any{"verification": verification}}) + _ = json.NewEncoder(w).Encode(map[string]any{"sha": sha, "author": map[string]any{"login": commit.login}, "committer": map[string]any{"login": nonEmpty(commit.committer, commit.login)}, "commit": map[string]any{"verification": verification}}) return true case r.Method == http.MethodPatch && strings.HasPrefix(path, "/repos/acme/widget/pulls/"): var body map[string]any @@ -386,6 +387,14 @@ func (a *adoptAPI) setVerifiedCommitAuthor(sha, login string) { a.commits[sha] = adoptCommit{login: login, verified: true} } +// setCommitPushedBy serves sha with distinct GitHub author and committer +// logins (a rebase, or web-flow committing a web edit). +func (a *adoptAPI) setCommitPushedBy(sha, author, committer string) { + a.mu.Lock() + defer a.mu.Unlock() + a.commits[sha] = adoptCommit{login: author, committer: committer} +} + func (a *adoptAPI) setSuperseder(n int, state string, merged bool) { a.mu.Lock() defer a.mu.Unlock() @@ -1577,6 +1586,105 @@ func TestAdoptionKeepsWhenHeadCommitLookupFails(t *testing.T) { } } +// A new head without hive evidence yields only when the author (or a +// read-only third party) pushed it. A push by someone with write or higher, +// or by an App, is a takeover: the adoption stands and the absent author is +// neither handed the PR nor nudged (projectbluefin/utah#919: Danathar's push +// handed the PR back to its offline author; utah-packages#804/#812/#822). +func TestAdoptionNewHeadPusherDecidesYield(t *testing.T) { + for _, tc := range []struct { + name string + setup func(*adoptAPI) + want string // yield, keep, or defer (adoption kept, head not moved) + permissions int // permission reads the settle makes + }{ + {name: "author push", setup: func(a *adoptAPI) { a.setCommitAuthor("bbb", "mallory") }, want: "yield"}, + {name: "author web edit", setup: func(a *adoptAPI) { a.setCommitPushedBy("bbb", "mallory", "web-flow") }, want: "yield"}, + {name: "write collaborator push", setup: func(a *adoptAPI) { + a.permissions = map[string]string{"danathar": "write"} + a.setCommitAuthor("bbb", "danathar") + }, want: "keep", permissions: 1}, + {name: "maintain collaborator push", setup: func(a *adoptAPI) { + a.permissions = map[string]string{"hanthor": "maintain"} + a.setCommitAuthor("bbb", "hanthor") + }, want: "keep", permissions: 1}, + {name: "admin collaborator push", setup: func(a *adoptAPI) { + a.permissions = map[string]string{"castrojo": "admin"} + a.setCommitAuthor("bbb", "castrojo") + }, want: "keep", permissions: 1}, + {name: "maintainer rebase keeps author", setup: func(a *adoptAPI) { + a.permissions = map[string]string{"hanthor": "maintain"} + a.setCommitPushedBy("bbb", "mallory", "hanthor") + }, want: "keep", permissions: 1}, + {name: "maintainer update branch via web", setup: func(a *adoptAPI) { + a.permissions = map[string]string{"danathar": "write"} + a.setCommitPushedBy("bbb", "danathar", "web-flow") + }, want: "keep", permissions: 1}, + {name: "other hive app push", setup: func(a *adoptAPI) { a.setCommitAuthor("bbb", "other-hive[bot]") }, want: "keep"}, + {name: "read-only third party push", setup: func(a *adoptAPI) { + a.permissions = map[string]string{"drive-by": "read"} + a.setCommitAuthor("bbb", "drive-by") + }, want: "yield", permissions: 1}, + {name: "triage third party push", setup: func(a *adoptAPI) { + a.permissions = map[string]string{"drive-by": "triage"} + a.setCommitAuthor("bbb", "drive-by") + }, want: "yield", permissions: 1}, + {name: "pusher permission unreadable", setup: func(a *adoptAPI) { + a.failPermission = true + a.setCommitAuthor("bbb", "danathar") + }, want: "defer", permissions: 1}, + } { + t.Run(tc.name, func(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + h.adopt.allowMaintainerEdits(7, true) + h.observe(adoptPR(7, "aaa"), "check-failing:unit") + h.clock.advance(30 * time.Minute) + h.observe(adoptPR(7, "aaa", "stage/072-returned-to-contributor", "role/contributor", "author/clanker"), "check-failing:unit") + key := stageKey("acme", "widget", 7) + if rec, _ := h.e.stageStoreOrShared().Get(key); !rec.AdoptedInPlace() || rec.AdoptedHead != "aaa" { + t.Fatalf("not adopted in place: %+v", rec) + } + h.e.stageStoreOrShared().MarkHivePushedHead(key, "prior-hive-head") + tc.setup(h.adopt) + reads := h.adopt.permissionReads + + h.api.reset() + h.clock.advance(time.Minute) + h.observe(adoptPR(7, "bbb", "stage/074-adopted-by-hive", "role/contributor", "author/clanker"), "check-failing:unit") + if got := h.adopt.permissionReads - reads; got != tc.permissions { + t.Fatalf("permission reads=%d, want %d", got, tc.permissions) + } + var yields int + for _, c := range h.api.comments { + if strings.HasPrefix(c, AdoptionYieldMarker) { + yields++ + } + } + rec, _ := h.e.stageStoreOrShared().Get(key) + switch tc.want { + case "yield": + if rec.AdoptedInPlace() || rec.YieldedHead != "bbb" || yields != 1 { + t.Fatalf("want yield: yields=%d record=%+v comments=%v", yields, rec, h.api.comments) + } + case "keep", "defer": + wantHead := "bbb" + if tc.want == "defer" { + wantHead = "aaa" + } + if !rec.AdoptedInPlace() || rec.AdoptedHead != wantHead || rec.YieldedHead != "" { + t.Fatalf("want adoption kept at %s: %+v", wantHead, rec) + } + if h.api.writes() != 0 || yields != 0 || len(nudgeComments(h.api)) != 0 { + t.Fatalf("takeover handed back or nudged: added=%v removed=%v comments=%v", h.api.added, h.api.removed, h.api.comments) + } + } + }) + } +} + func TestAdoptionStrictAttributionSafetyValveBeforeRepoEvidence(t *testing.T) { p := adoptPolicy() p.AdoptInPlace = true