From a216e7b3a9a28c117afa8897f7ef071fcee4776a Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Wed, 4 Nov 2026 14:01:43 -0500 Subject: [PATCH] fix(automerge): never supersede a hive-authored PR for a missing sign-off The squash-attribution rule in stageDCO counted an App-signed commit on a PR by hive's own ai_author as squash-unsafe, so signoff-rehome closed the approved, green PR and re-authored it as a new one. Hive-authored PRs are now self-squash, signoff-rehome amends them in place (or leaves them) and never rehomes an operator-approved head. The App's signed re-author commit always carries a Signed-off-by trailer and the push relay refuses a hive-identity commit without one. Fixes #16647 Signed-off-by: Andrew Anderson --- .../fixed-signoff-rehome-hive-authored.md | 1 + src/cmd/hive/merge_eligibility.go | 1 + .../automerge_adoption_escape_test.go | 114 ++++++++++++++++++ .../automerge/automerge_contributor_adopt.go | 69 +++++++++-- .../automerge_contributor_adopt_test.go | 38 ++++-- .../github/automerge/automerge_stage_dco.go | 41 ++++++- .../automerge/automerge_stage_labels.go | 8 +- .../automerge/shared_ci_attribution_test.go | 2 +- src/pkg/github/pr_request_signed.go | 17 +++ src/pkg/github/pr_request_signed_test.go | 21 ++++ src/pkg/github/push_branch_request_watcher.go | 88 ++++++++++++++ .../push_branch_request_watcher_test.go | 69 ++++++++++- 12 files changed, 434 insertions(+), 35 deletions(-) create mode 100644 changelog.d/fixed-signoff-rehome-hive-authored.md diff --git a/changelog.d/fixed-signoff-rehome-hive-authored.md b/changelog.d/fixed-signoff-rehome-hive-authored.md new file mode 100644 index 0000000000..deff42eba9 --- /dev/null +++ b/changelog.d/fixed-signoff-rehome-hive-authored.md @@ -0,0 +1 @@ +- Hive no longer supersedes its own PRs for a missing sign-off: the automerge DCO stage treats commits on a PR by hive's `ai_author` or App login as self-squash (so an App-signed commit no longer counts as squash-unsafe), `signoff-rehome` keeps hive-authored and operator-approved heads in place instead of re-authoring them as a new PR, the App's signed re-author commit always carries a `Signed-off-by` trailer, and the push relay refuses a hive-identity commit that lacks one. diff --git a/src/cmd/hive/merge_eligibility.go b/src/cmd/hive/merge_eligibility.go index 992781e6d6..6883609482 100644 --- a/src/cmd/hive/merge_eligibility.go +++ b/src/cmd/hive/merge_eligibility.go @@ -130,6 +130,7 @@ func stageLabelPolicyFunc(cfg *config.Config) func() automerge.StageLabelPolicy AuthorKindLabels: cfg.Governor.Labels.Author.Names(), RelayKnown: contributorRelayKnown, OwnerAuthor: dashboardOwnerLogin(cfg), + HiveAuthor: hiveIdentity(cfg).Matches, SharedCIIncident: stageSharedCIIncident, BaseBranchRed: stageBaseBranchRed, ForkGuard: stageForkGuard, diff --git a/src/pkg/github/automerge/automerge_adoption_escape_test.go b/src/pkg/github/automerge/automerge_adoption_escape_test.go index 1a61b222c5..132e17a241 100644 --- a/src/pkg/github/automerge/automerge_adoption_escape_test.go +++ b/src/pkg/github/automerge/automerge_adoption_escape_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -639,3 +640,116 @@ func TestFormatSat(t *testing.T) { } } } + +// hivecommons/hive#16619: the hive's own clanker (project.ai_author) opened +// the PR from its fork; the hive's fixer pushed a commit signed as the App. +// The squash attributes that commit to the hive's own author, so there is +// nothing to re-home: the PR keeps its number, approval and CI, and the +// adoption stays in place. No superseder, no stall note. +func TestAdoptedInPlaceHiveAuthoredSquashUnsafeCommitStaysInPlace(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + p.DCORequired = func(string) (bool, bool) { return true, true } + p.HiveAuthor = func(login string) bool { return strings.EqualFold(login, "thatguywob") } + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + h.api.commitDetails = map[int][]stageCommitFixture{7: { + {message: "fix: preserve verdicts\n\nSigned-off-by: thatguywob ", authorLogin: "thatguywob", authorName: "thatguywob", authorEmail: "thatguywob@users.noreply.github.com"}, + {message: "fix: escape tooltip lines\n\nSigned-off-by: hive[bot] <12345+hive[bot]@users.noreply.github.com>", authorLogin: testHiveAppBotLogin, authorName: "scanner", authorEmail: "12345+hive[bot]@users.noreply.github.com"}, + }} + own := func(head string, labels ...string) *gh.PullRequest { + pr := adoptPR(7, head, labels...) + pr.User = &gh.User{Login: gh.Ptr("thatguywob")} + pr.Head.Repo = &gh.Repository{FullName: gh.Ptr("thatguywob/widget")} + return pr + } + h.adopt.allowMaintainerEdits(7, true) + h.observe(own("5980996"), contributorLaneReasonNoVerdict) + key := stageKey("acme", "widget", 7) + h.e.stageStoreOrShared().MarkAdoptedInPlace(key, "5980996", AdoptReasonGrace, h.clock.at) + h.adopt.setCommitAuthor("5980996", testHiveAppBotLogin) + h.api.reset() + + for _, advance := range []time.Duration{time.Minute, adoptedDwell + time.Hour} { + h.clock.advance(advance) + h.observe(own("5980996", adoptedLabels...), contributorLaneReasonNoVerdict) + // Past the dwell the idle head may escalate to a person (085); + // that is a note on the same PR, never a copy of it. + if len(h.adopt.pulls) != 0 || len(adoptionComments(h.api)) != 0 || h.adopt.refs["refs/heads/hive/adopt-7"] != "" { + t.Fatalf("hive-authored PR superseded for sign-off: pulls=%v refs=%v comments=%v", h.adopt.pulls, h.adopt.refs, h.api.comments) + } + rec, _ := h.e.stageStoreOrShared().Get(key) + if !rec.AdoptedInPlace() || rec.AdoptedBy != 0 || rec.AdoptReason == AdoptReasonSignoffRehome || rec.DCOSquashUnsafeHive != 0 { + t.Fatalf("record after %v: %+v", advance, rec) + } + for _, c := range stalledComments(h.api) { + if strings.Contains(c, stagelabels.LaneReasonHiveSignoffAttribution) { + t.Fatalf("hive-authored PR parked on sign-off attribution: %s", c) + } + } + } +} + +// The same head on an external contributor's fork keeps the existing +// behaviour: the App-signed commit is squash-unsafe under mallory's name, +// so the adoption re-homes onto hive/adopt-7 at once +// (TestAdoptedInPlaceSquashUnsafeHiveCommitSupersedes covers the full +// path; this pins the decision with the hive-author hook present). +func TestAdoptedInPlaceExternalForkSquashUnsafeStillSupersedes(t *testing.T) { + policy := adoptPolicy() + policy.AdoptInPlace = true + policy.HiveAuthor = func(login string) bool { return strings.EqualFold(login, "thatguywob") } + now := time.Date(2026, 10, 16, 12, 0, 0, 0, time.UTC) + inPlace := stagelabels.Record{AdoptedHead: "aaa", AdoptMode: stagelabels.AdoptModeInPlace, AdoptedAt: now, AdoptedHeadAt: now} + external := stageObservation{HeadSHA: "aaa", Author: "mallory", HeadRepo: "mallory/widget", Reason: stagelabels.LaneReasonHiveSignoffAttribution, Detail: stageDetail{DCOSquashUnsafeHive: 1}} + if got := inPlaceRehomeReason(external, inPlace, policy, now.Add(time.Minute), false); got != AdoptReasonSignoffRehome { + t.Fatalf("external fork: inPlaceRehomeReason=%q, want %q", got, AdoptReasonSignoffRehome) + } + if got := adoptionStalledReason(external, inPlace, policy, now.Add(time.Minute)); got != "adoption-stalled:hive-signoff-attribution" { + t.Fatalf("external fork stall reason=%q", got) + } + own := external + own.Author, own.HeadRepo = "thatguywob", "thatguywob/widget" + if got := inPlaceRehomeReason(own, inPlace, policy, now.Add(time.Minute), false); got != "" { + t.Fatalf("hive-authored: inPlaceRehomeReason=%q, want none", got) + } + if got := adoptionStalledReason(own, inPlace, policy, now.Add(time.Minute)); got != "" { + t.Fatalf("hive-authored stall reason=%q, want none", got) + } +} + +// An operator's approval on the adopted head is never thrown away for a +// copy: neither the sign-off, the rebase nor the stalled-finding re-home +// fires while the approval stands on that head. A new head starts over. +func TestAdoptedInPlaceApprovedHeadIsNotSuperseded(t *testing.T) { + policy := adoptPolicy() + policy.AdoptInPlace = true + now := time.Date(2026, 10, 16, 12, 0, 0, 0, time.UTC) + inPlace := stagelabels.Record{AdoptedHead: "aaa", AdoptMode: stagelabels.AdoptModeInPlace, AdoptedAt: now, AdoptedHeadAt: now} + late := now.Add(adoptedDwell + time.Hour) + approved := stageDetail{HumanApprover: "clubanderson", HumanApprovalHead: "aaa"} + cases := []struct { + name string + obs stageObservation + }{ + {"squash-unsafe sign-off", stageObservation{HeadSHA: "aaa", Author: "mallory", Reason: stagelabels.LaneReasonHiveSignoffAttribution, Detail: stageDetail{DCOSquashUnsafeHive: 1}}}, + {"needs-rebase past dwell", stageObservation{HeadSHA: "aaa", Author: "mallory", Labels: []string{"needs-rebase"}, Reason: "conflicting"}}, + {"finding past dwell", stageObservation{HeadSHA: "aaa", Author: "mallory", LaneReason: contributorLaneReasonChangesRequested, Detail: stageDetail{VerdictKnown: true, Verdict: ContributorLaneVerdict{Configured: true, Verdict: "changes_requested"}}}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := inPlaceRehomeReason(tc.obs, inPlace, policy, late, true); got == "" { + t.Fatalf("precondition: unapproved head must re-home") + } + obs := tc.obs + obs.Detail.HumanApprover, obs.Detail.HumanApprovalHead = approved.HumanApprover, approved.HumanApprovalHead + if got := inPlaceRehomeReason(obs, inPlace, policy, late, true); got != "" { + t.Fatalf("approved head re-homed: %q", got) + } + obs.Detail.HumanApprovalHead = "older" + if got := inPlaceRehomeReason(obs, inPlace, policy, late, true); got == "" { + t.Fatalf("an approval on an older head must not pin the new one") + } + }) + } +} diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index f9d81604d6..4e7c9bd876 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -288,8 +288,8 @@ func adoptionCloses(body string) []string { return out } -func adoptionNeedsDCORehome(obs stageObservation, canPushInPlace bool) bool { - return adoptionDCORepairMode(obs, canPushInPlace) == stagelabels.AdoptModeSupersede +func adoptionNeedsDCORehome(obs stageObservation, canPushInPlace, hiveAuthored bool) bool { + return adoptionDCORepairMode(obs, canPushInPlace, hiveAuthored) == stagelabels.AdoptModeSupersede } // adoptionDCORepairMode decides how a DCO-red head gets its sign-off: @@ -298,16 +298,34 @@ func adoptionNeedsDCORehome(obs stageObservation, canPushInPlace bool) bool { // base repository — a fork branch is pushable by an agent's git but not by // the App's REST token, so a fork head re-homes (AdoptModeSupersede) even // when every unsigned commit is the hive's (hivecommons/hive#14666). -func adoptionDCORepairMode(obs stageObservation, canPushInPlace bool) string { +// +// A hive-authored PR (hiveAuthored: the App bot or project.ai_author opened +// it) is never re-homed: every commit on it is the hive's to sign, so a +// pushable base-repo head is amended in place and any other head keeps its +// PR number, approvals and CI history and waits for a person (""), +// rather than restarting as a copy (hivecommons/hive#16619). +func adoptionDCORepairMode(obs stageObservation, canPushInPlace, hiveAuthored bool) string { if obs.Detail.DCO != stagelabels.DCONo || obs.Detail.DCOUnsigned <= 0 { return "" } - if canPushInPlace && headInBaseRepo(obs) && obs.Detail.DCOUnsignedHive == obs.Detail.DCOUnsigned && obs.Detail.DCOUnsignedOther == 0 { + if canPushInPlace && headInBaseRepo(obs) && (hiveAuthored || (obs.Detail.DCOUnsignedHive == obs.Detail.DCOUnsigned && obs.Detail.DCOUnsignedOther == 0)) { return stagelabels.AdoptModeInPlace } + if hiveAuthored { + return "" + } return stagelabels.AdoptModeSupersede } +// adoptionHeadApproved reports whether a trusted human's APPROVED review +// stands on the observed head. Such a head is never re-homed onto a copy: +// the copy would restart CI and drop the approval (hivecommons/hive#16619). +// The one exception is a fork branch the hive cannot push to +// (AdoptReasonPushRehome), which the caller decides. +func adoptionHeadApproved(obs stageObservation) bool { + return obs.HeadSHA != "" && strings.TrimSpace(obs.Detail.HumanApprover) != "" && strings.TrimSpace(obs.Detail.HumanApprovalHead) == obs.HeadSHA +} + // headInBaseRepo reports whether the PR head lives in the base repository // (not a fork), where the App's git-data API can rewrite it. func headInBaseRepo(obs stageObservation) bool { @@ -317,8 +335,8 @@ func headInBaseRepo(obs stageObservation) bool { // adoptedHeadNeedsDCORepair decides, for an in-place-adopted PR, whether the // unsigned commits are the hive's own on a base-repo branch it rewrites in // place with its sign-off. -func adoptedHeadNeedsDCORepair(obs stageObservation, rec stagelabels.Record, canPushInPlace bool) bool { - return adoptedHeadDCORed(obs, rec) && adoptionDCORepairMode(obs, canPushInPlace) == stagelabels.AdoptModeInPlace +func adoptedHeadNeedsDCORepair(obs stageObservation, rec stagelabels.Record, canPushInPlace, hiveAuthored bool) bool { + return adoptedHeadDCORed(obs, rec) && adoptionDCORepairMode(obs, canPushInPlace, hiveAuthored) == stagelabels.AdoptModeInPlace } // adoptedHeadDCORed reports whether an in-place-adopted PR's head carries @@ -330,8 +348,8 @@ func adoptedHeadDCORed(obs stageObservation, rec stagelabels.Record) bool { // adoptedHeadNeedsDCORehome decides, for an in-place-adopted PR, whether the // unsigned commits can be rewritten on the contributor branch (all App-linked // and the branch still pushable) or the work must be re-homed. -func adoptedHeadNeedsDCORehome(obs stageObservation, rec stagelabels.Record, canPushInPlace bool) bool { - return adoptedHeadDCORed(obs, rec) && adoptionDCORepairMode(obs, canPushInPlace) == stagelabels.AdoptModeSupersede +func adoptedHeadNeedsDCORehome(obs stageObservation, rec stagelabels.Record, canPushInPlace, hiveAuthored bool) bool { + return adoptedHeadDCORed(obs, rec) && adoptionDCORepairMode(obs, canPushInPlace, hiveAuthored) == stagelabels.AdoptModeSupersede } func adoptionRehomeCommitMessage(obs stageObservation) string { @@ -696,7 +714,7 @@ func (c *Engine) adoptContributorPR(ctx context.Context, store *stagelabels.Stor if policy.AdoptInPlace && !requested && !forced { canPushInPlace = c.canPushToPRBranch(ctx, obs) } - dcoRehome := adoptionNeedsDCORehome(obs, canPushInPlace) + dcoRehome := adoptionNeedsDCORehome(obs, canPushInPlace, hiveAuthoredPR(obs, policy, appLogin)) // Record the attempt first: whatever happens below, this head is tried once. store.MarkAdopted(key, obs.HeadSHA, 0, now) store.NoteAdoptDiffFence(key, fence) @@ -1344,10 +1362,17 @@ func (c *Engine) settleAdoption(ctx context.Context, store *stagelabels.Store, k } if adoptedHeadDCORed(obs, rec) { canPushInPlace := c.canPushToPRBranch(ctx, obs) - if adoptedHeadNeedsDCORepair(obs, rec, canPushInPlace) { + hiveAuthored := hiveAuthoredPR(obs, policy, c.transport.AppBotLogin()) + if adoptedHeadNeedsDCORepair(obs, rec, canPushInPlace, hiveAuthored) { return c.repairAdoptedDCOInPlace(ctx, store, key, obs, rec) } - if adoptedHeadNeedsDCORehome(obs, rec, canPushInPlace) { + if hiveAuthored && !adoptionHeadApproved(obs) && obs.Detail.DCOUnsigned > 0 { + // The hive's own PR on a head it cannot rewrite over REST: + // it keeps its number, approvals and CI; the unsigned head + // is the hive's agent's to re-sign on the same branch. + c.info("stage labels: hive-authored adopted PR has unsigned commits on a head the sweep cannot rewrite; keeping it in place", "repo", obs.Display, "pr", obs.Number, "author", obs.Author, "head", obs.HeadSHA, "head_repo", obs.HeadRepo, "can_push", canPushInPlace) + } + if adoptedHeadNeedsDCORehome(obs, rec, canPushInPlace, hiveAuthored) { fence, err := c.adoptionDiffRefusal(ctx, obs) if fence != "" && (err != nil || adoptionFenceDefers(fence)) { // Not a verdict: the head moved, or its file list is @@ -1954,7 +1979,18 @@ func inPlaceRehomeReason(obs stageObservation, rec stagelabels.Record, policy St if !rec.AdoptedInPlace() || obs.HeadSHA == "" || obs.HeadSHA != rec.AdoptedHead || adoptedHeadDCORed(obs, rec) || adoptionBlocked(obs.Labels) != "" || verdictNeedsHuman(obs.Detail) { return "" } + if adoptionHeadApproved(obs) { + // A copy restarts CI and drops the approval; the head stays where + // the approval is. Only a fork the hive cannot push to re-homes + // (AdoptReasonPushRehome, decided by the caller). + return "" + } if adoptedHeadSignoffUnfixable(obs, rec) { + if hiveAuthoredPR(obs, policy, "") { + // The squash attributes the hive's commits to the hive's own + // author: nothing to re-home (hivecommons/hive#16619). + return "" + } return AdoptReasonSignoffRehome } if humanConflictReasons[obs.Reason] || humanConflictReasons[obs.LaneReason] || hasLabelFold(obs.Labels, "needs-rebase") { @@ -1992,7 +2028,7 @@ func adoptionStalledReasonWith(obs stageObservation, rec stagelabels.Record, pol if rec.AdoptionAttemptedNothingOpened(obs.HeadSHA) { return stagelabels.LaneReasonAdoptionStalledPrefix + "supersede-failed:" + nonEmpty(rec.AdoptReason, obs.Reason) } - if adoptedHeadSignoffUnfixable(obs, rec) { + if adoptedHeadSignoffUnfixable(obs, rec) && !hiveAuthoredPR(obs, policy, "") { return stagelabels.LaneReasonAdoptionStalledPrefix + stagelabels.LaneReasonHiveSignoffAttribution } if adoptionOptionalCheckOnly(obs) || !adoptionStale(obs, rec, policy, now) { @@ -2020,6 +2056,15 @@ func (c *Engine) escapeStalledAdoption(ctx context.Context, store *stagelabels.S rec, _ = store.Get(key) } rehomeReason := inPlaceRehomeReason(obs, rec, policy, now, c.inPlaceRebaseExhausted(obs, rec)) + if rehomeReason == AdoptReasonSignoffRehome && hiveAuthoredPR(obs, policy, c.transport.AppBotLogin()) { + rehomeReason = "" + } + if rehomeReason != "" && c.operatorAdmission(ctx, obs.Owner, obs.Repo, obs.Number, obs.HeadSHA) != "" { + // An operator admitted this head: a copy would drop that and + // restart CI. Only a fork the hive cannot push to re-homes. + c.info("stage labels: in-place adoption keeps its approved head; not superseding", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "would_rehome", rehomeReason) + rehomeReason = "" + } if rehomeReason == "" && rec.AdoptedInPlace() && obs.HeadSHA == rec.AdoptedHead && !adoptedHeadDCORed(obs, rec) && adoptionBlocked(obs.Labels) == "" && !verdictNeedsHuman(obs.Detail) && c.pushAccessLost(ctx, obs) { rehomeReason = AdoptReasonPushRehome } diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go index 0d496bc52c..37a5043e98 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go @@ -586,45 +586,61 @@ func TestAdoptionDCORehomeDecisionAndRendering(t *testing.T) { Body: "Implements widgets.\n\nHive-Run: acme/widget#44\n", Detail: stageDetail{DCO: stagelabels.DCONo, DCOTotal: 3, DCOUnsigned: 2}, } - if !adoptionNeedsDCORehome(obs, false) { + if !adoptionNeedsDCORehome(obs, false, false) { t.Fatal("unsigned commits without branch push permission must re-home") } - if !adoptionNeedsDCORehome(obs, true) { + if !adoptionNeedsDCORehome(obs, true, false) { t.Fatal("unknown unsigned commit ownership must re-home") } obs.Detail.DCOUnsignedHive = 2 - if !adoptionNeedsDCORehome(obs, true) { + if !adoptionNeedsDCORehome(obs, true, false) { t.Fatal("a fork head must re-home even when only hive-authored commits are unsigned: the engine cannot rewrite a fork branch") } obs.Owner, obs.Repo, obs.HeadRepo = "acme", "widget", "acme/widget" - if adoptionNeedsDCORehome(obs, true) { + if adoptionNeedsDCORehome(obs, true, false) { t.Fatal("branch push permission with only hive-authored unsigned commits on a base-repo head allows in-place repair") } - if !adoptionNeedsDCORehome(obs, false) { + if !adoptionNeedsDCORehome(obs, false, false) { t.Fatal("hive-authored unsigned commits without branch push permission must still re-home") } inPlace := stagelabels.Record{AdoptedHead: "aaa", AdoptMode: stagelabels.AdoptModeInPlace} - if adoptedHeadNeedsDCORehome(obs, inPlace, true) { + if adoptedHeadNeedsDCORehome(obs, inPlace, true, false) { t.Fatal("already adopted in place with App-owned unsigned commits and push access stays in place") } - if !adoptedHeadNeedsDCORepair(obs, inPlace, true) { + if !adoptedHeadNeedsDCORepair(obs, inPlace, true, false) { t.Fatal("that head is the in-place repair's job") } obs.HeadRepo = "" - if !adoptedHeadNeedsDCORehome(obs, inPlace, true) { + if !adoptedHeadNeedsDCORehome(obs, inPlace, true, false) { t.Fatal("already adopted in place on a fork must re-home") } - if !adoptedHeadNeedsDCORehome(obs, inPlace, false) { + if !adoptedHeadNeedsDCORehome(obs, inPlace, false, false) { t.Fatal("already adopted in place without push access must re-home") } - if adoptedHeadNeedsDCORehome(obs, stagelabels.Record{AdoptedHead: "aaa", AdoptedBy: 90, AdoptMode: stagelabels.AdoptModeSupersede}, false) { + if adoptedHeadNeedsDCORehome(obs, stagelabels.Record{AdoptedHead: "aaa", AdoptedBy: 90, AdoptMode: stagelabels.AdoptModeSupersede}, false, false) { t.Fatal("a superseded adoption is never re-homed again") } obs.Detail.DCOUnsignedHive = 0 obs.Detail.DCOUnsigned = 0 - if adoptionNeedsDCORehome(obs, false) { + if adoptionNeedsDCORehome(obs, false, false) { t.Fatal("signed head does not need DCO re-home") } + // A hive-authored PR (hivecommons/hive#16619) is never re-homed for + // sign-off: a pushable base-repo head is amended in place whoever the + // unsigned commits are linked to; a fork head keeps its PR and waits. + own := obs + own.Author, own.Detail.DCOUnsigned, own.Detail.DCOUnsignedOther = "thatguywob", 2, 2 + own.Owner, own.Repo, own.HeadRepo = "acme", "widget", "acme/widget" + if got := adoptionDCORepairMode(own, true, true); got != stagelabels.AdoptModeInPlace { + t.Fatalf("hive-authored base-repo head: mode=%q, want in-place amend", got) + } + own.HeadRepo = "thatguywob/widget" + if got := adoptionDCORepairMode(own, false, true); got != "" { + t.Fatalf("hive-authored fork head: mode=%q, want no re-home", got) + } + if adoptionNeedsDCORehome(own, false, true) || adoptedHeadNeedsDCORehome(own, inPlace, false, true) { + t.Fatal("a hive-authored PR must never be superseded for sign-off") + } obs.Detail.DCOUnsigned = 2 body := AdoptionPRBody(obs, stagelabels.Record{Reason: "check-failing:dco"}, true, "") for _, want := range []string{ diff --git a/src/pkg/github/automerge/automerge_stage_dco.go b/src/pkg/github/automerge/automerge_stage_dco.go index 9729023de2..ec17c37b1b 100644 --- a/src/pkg/github/automerge/automerge_stage_dco.go +++ b/src/pkg/github/automerge/automerge_stage_dco.go @@ -7,6 +7,7 @@ import ( "strings" gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/authorkind" hgithub "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -51,6 +52,16 @@ func dcoCommitStatsDetailed(commits []*gh.RepositoryCommit, appLogin string) dco } func dcoCommitStatsDetailedForAuthor(commits []*gh.RepositoryCommit, appLogin, prAuthor string) dcoStats { + return dcoCommitStatsForSquashAuthor(commits, appLogin, prAuthor, botSquashAuthor(prAuthor)) +} + +// dcoCommitStatsForSquashAuthor is dcoCommitStatsDetailedForAuthor with the +// squash-attribution verdict on the PR author made explicit: selfSquash means +// a squash under prAuthor attributes the hive's commits to the hive itself +// (a bot author, or one of this hive's own identities: project.ai_author, +// the App bot), so no signed hive commit is squash-unsafe. Unsigned commits +// are still counted and rejected. +func dcoCommitStatsForSquashAuthor(commits []*gh.RepositoryCommit, appLogin, prAuthor string, selfSquash bool) dcoStats { var stats dcoStats for _, rc := range commits { if rc == nil || len(rc.Parents) > 1 { @@ -65,7 +76,7 @@ func dcoCommitStatsDetailedForAuthor(commits []*gh.RepositoryCommit, appLogin, p } else { stats.unsignedOther++ } - } else if !botSquashAuthor(prAuthor) && dcoCommitByHive(rc, appLogin) && !messageHasPRAuthorSignoff(msg, prAuthor) { + } else if !selfSquash && dcoCommitByHive(rc, appLogin) && !messageHasPRAuthorSignoff(msg, prAuthor) { stats.squashUnsafeHive++ } } @@ -87,10 +98,12 @@ func dcoCommitByHive(commit *gh.RepositoryCommit, appLogin string) bool { // with Store.NoteDCO once the record exists). A read // failure leaves the state unknown (labels untouched, stage unchanged) and // is reported, never fatal. -func (c *Engine) stageDCO(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation) (dcoStats, error) { +func (c *Engine) stageDCO(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, policy StageLabelPolicy) (dcoStats, error) { if obs.HeadSHA == "" { return dcoStats{}, nil } + appLogin := c.transport.AppBotLogin() + selfSquash := botSquashAuthor(obs.Author) || hiveAuthoredPR(obs, policy, appLogin) if rec, ok := store.Get(key); ok { if t, u, cached := rec.DCOFor(obs.HeadSHA); cached { // Records cached before the ownership split existed carry @@ -98,7 +111,7 @@ func (c *Engine) stageDCO(ctx context.Context, store *stagelabels.Store, key str // so read the commits once more for that case only. if (u > 0 || rec.DCOSquashUnsafeKnown) && (u == 0 || rec.DCOSplitKnown(obs.HeadSHA) || !rec.AdoptedInPlace()) { unsafe := rec.DCOSquashUnsafeHive - if botSquashAuthor(obs.Author) { + if selfSquash { unsafe = 0 } return dcoStats{total: t, unsigned: u, unsignedHive: rec.DCOUnsignedHive, unsignedOther: u - rec.DCOUnsignedHive, squashUnsafeHive: unsafe}, nil @@ -110,7 +123,27 @@ func (c *Engine) stageDCO(ctx context.Context, store *stagelabels.Store, key str if err != nil { return dcoStats{}, err } - return dcoCommitStatsDetailedForAuthor(all, c.transport.AppBotLogin(), obs.Author), nil + return dcoCommitStatsForSquashAuthor(all, appLogin, obs.Author, selfSquash), nil +} + +// hiveAuthoredPR reports whether the PR is the hive's own: author/hive by +// kind, observed under the self-authored lane, authored by the App bot, or +// authored by one of the identities the policy names as this hive's +// (project.ai_author — the clanker login the hive's agents open PRs as). +// Such a PR is never re-homed for sign-off attribution: a squash under its +// author attributes the hive's commits to the hive (hivecommons/hive#16619). +func hiveAuthoredPR(obs stageObservation, policy StageLabelPolicy, appLogin string) bool { + if obs.AuthorKind == authorkind.Hive || obs.Lane == stageLaneSelf { + return true + } + author := strings.TrimSpace(obs.Author) + if author == "" { + return false + } + if appLogin != "" && strings.EqualFold(author, appLogin) { + return true + } + return policy.HiveAuthor != nil && policy.HiveAuthor(author) } // Matches the bot-PR policy in check-squash-signoff-attribution.sh: a squash diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index 61cb4649a0..e4adeb5fa5 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -140,6 +140,12 @@ type StageLabelPolicy struct { // (dashboard.authorized_users). Their PRs, like author/human ones, are // never adopted (hivecommons/hive#15202). nil = unknown. OwnerAuthor func(login string) bool + // HiveAuthor reports whether login is one of this hive's own PR-authoring + // identities (project.ai_author, the App bot: hgithub.HiveIdentity). A + // squash of such a PR attributes the hive's commits to the hive itself, + // so a sign-off re-home never applies to it (hivecommons/hive#16619: + // the hive superseded its own clanker's approved PR). nil = unknown. + HiveAuthor func(login string) bool // SharedCIIncident returns the open [shared-ci] incident the PR // (repo "owner/name", number, head SHA) is deferred to, or 0. A red PR // deferred to one is kept out of 070/072 and never nudged @@ -1004,7 +1010,7 @@ func (c *Engine) ApplyStageLabels(ctx context.Context) (StageLabelResult, error) // zero record for the head would be served back if the // same head later stopped matching. var err error - stats, err = c.stageDCO(ctx, store, key, obs) + stats, err = c.stageDCO(ctx, store, key, obs, policy) if err != nil { c.warn("stage labels: dco commit check failed", "repo", obs.Display, "pr", obs.Number, "error", err) if firstErr == nil { diff --git a/src/pkg/github/automerge/shared_ci_attribution_test.go b/src/pkg/github/automerge/shared_ci_attribution_test.go index d3d5e83a99..1e20a6a6d3 100644 --- a/src/pkg/github/automerge/shared_ci_attribution_test.go +++ b/src/pkg/github/automerge/shared_ci_attribution_test.go @@ -45,7 +45,7 @@ func TestStageDCONormalizesCachedBotAttributionWithoutChangingSignoffs(t *testin store.NoteDCO(key, "aaa", 3, 0, 0, 1) got, err := e.stageDCO(context.Background(), store, key, stageObservation{ Owner: "acme", Repo: "widget", Number: 42, HeadSHA: "aaa", Author: "dependabot[bot]", - }) + }, StageLabelPolicy{}) if err != nil || got.total != 3 || got.unsigned != 0 || got.squashUnsafeHive != 0 { t.Fatalf("cached bot attribution: %+v, %v", got, err) } diff --git a/src/pkg/github/pr_request_signed.go b/src/pkg/github/pr_request_signed.go index 345884abb6..b46ab34604 100644 --- a/src/pkg/github/pr_request_signed.go +++ b/src/pkg/github/pr_request_signed.go @@ -150,6 +150,7 @@ func (c *Client) signBranch(ctx context.Context, owner, repo, base, head string) return "", 0, err } headline, body := signedCommitMessage(comparison.Commits) + body = ensureSignedOffBy(body, c.appBotLogin) // Scratch ref at the merge base. Delete a leftover from a crashed run // first: CreateRef 422s on an existing ref. @@ -384,6 +385,22 @@ func BotSignedOffByTrailer(login string) string { return "Signed-off-by: " + login + " <" + signedTrailerEmail(login+"@"+signedTrailerNoreplyDomain) + ">" } +// ensureSignedOffBy appends the App's own DCO trailer to a signed commit's +// body when none of the folded agent commits carried one. The commit is +// authored by the App bot, so an unsigned body would publish a hive-authored +// commit without sign-off (hivecommons/hive#16619); the trailer is the one +// BotSignedOffByTrailer derives, or nothing when the App login is unknown. +func ensureSignedOffBy(body, appLogin string) string { + if strings.TrimSpace(appLogin) == "" || signedTrailerLineRE.MatchString(body) { + return body + } + trailer := BotSignedOffByTrailer(appLogin) + if strings.TrimSpace(body) == "" { + return trailer + } + return strings.TrimRight(body, "\n") + "\n\n" + trailer +} + // signedTrailersBracketFree rewrites every Signed-off-by line in msg through // signedTrailerEmail, leaving the rest of the message byte-for-byte intact. func signedTrailersBracketFree(msg string) string { diff --git a/src/pkg/github/pr_request_signed_test.go b/src/pkg/github/pr_request_signed_test.go index ca82d8a6fc..f0fd9e42cc 100644 --- a/src/pkg/github/pr_request_signed_test.go +++ b/src/pkg/github/pr_request_signed_test.go @@ -536,6 +536,27 @@ func TestSignedCommitMessage(t *testing.T) { } } +// A signed re-author whose agent commits carried no trailer still publishes +// a signed-off commit (hivecommons/hive#16619); one that already has a +// trailer, or an unknown App login, is left as folded. +func TestEnsureSignedOffBy(t *testing.T) { + t.Setenv(signedTrailerDomainEnv, "") + want := "Signed-off-by: acme-hive[bot] " + if got := ensureSignedOffBy("body one\n\nsecond", "acme-hive[bot]"); got != "body one\n\nsecond\n\n"+want { + t.Errorf("unsigned body: got %q", got) + } + if got := ensureSignedOffBy("", "acme-hive[bot]"); got != want { + t.Errorf("empty body: got %q", got) + } + signed := "body\n\nSigned-off-by: a " + if got := ensureSignedOffBy(signed, "acme-hive[bot]"); got != signed { + t.Errorf("signed body must not gain a second trailer: got %q", got) + } + if got := ensureSignedOffBy("body", ""); got != "body" { + t.Errorf("unknown App login must not invent a trailer: got %q", got) + } +} + func TestGraphQLEndpoint(t *testing.T) { cases := map[string]string{ "https://api.github.com/": "https://api.github.com/graphql", diff --git a/src/pkg/github/push_branch_request_watcher.go b/src/pkg/github/push_branch_request_watcher.go index dd509f6929..73ec561179 100644 --- a/src/pkg/github/push_branch_request_watcher.go +++ b/src/pkg/github/push_branch_request_watcher.go @@ -405,6 +405,18 @@ func (c *Client) handleOnePushBranchRequest(ctx context.Context, path string, no } sha = strings.TrimSpace(sha) + // Every commit a hive identity authors must carry its sign-off before it + // leaves the checkout. An unsigned hive commit on a published branch is + // what later makes the automerge DCO stage re-author the PR + // (hivecommons/hive#16619); refusing it here keeps the fix at the source. + // Hooks are neutralised by pushBranchGitEnv, so the relay checks itself. + if log, err := pushBranchExec(ctx, req.Dir, env, "git", "log", "--no-merges", "--format="+unsignedHiveCommitFormat, sha, "--not", "--remotes"); err == nil { + if bad := unsignedHiveCommits(log, c.pushHiveIdentities()); len(bad) > 0 { + c.denyPushBranchRequest(path, req, fmt.Sprintf("commit %s is authored by hive identity %q without a Signed-off-by trailer; hive-authored commits must be signed off at creation (git commit -s)", bad[0].SHA, bad[0].Author), nowFn) + return + } + } + args := []string{"push"} if req.ForceWithLease { args = append(args, "--force-with-lease") @@ -493,3 +505,79 @@ func WritePushBranchRequest(dir string, req PushBranchRequest) (string, error) { } return path, nil } + +// unsignedHiveCommitFormat is the git log format unsignedHiveCommits parses: +// one record per commit, fields split by US (0x1f), records by RS (0x1e). +const unsignedHiveCommitFormat = "%H%x1f%an%x1f%ae%x1f%B%x1e" + +// unsignedHiveCommit names a commit the push relay refuses. +type unsignedHiveCommit struct { + SHA string + Author string +} + +// pushHiveIdentities lists the logins whose commits must be signed off: the +// App bot and the configured hive identity (ai_author + App login). +func (c *Client) pushHiveIdentities() []string { + var ids []string + if c.appBotLogin != "" { + ids = append(ids, c.appBotLogin) + } + id := c.getHiveIdentity() + ids = append(ids, id.AIAuthor, id.AppLogin) + return ids +} + +// unsignedHiveCommits parses `git log --format=unsignedHiveCommitFormat` +// output and returns the commits authored by one of logins (by name, or by a +// GitHub noreply / App-slug address) whose message has no Signed-off-by +// trailer. Commits by anyone else are not the relay's concern; the DCO check +// on the PR handles those. +func unsignedHiveCommits(log string, logins []string) []unsignedHiveCommit { + var bad []unsignedHiveCommit + for _, rec := range strings.Split(log, "\x1e") { + rec = strings.TrimSpace(rec) + if rec == "" { + continue + } + f := strings.SplitN(rec, "\x1f", 4) + if len(f) < 4 { + continue + } + sha, name, email, msg := strings.TrimSpace(f[0]), strings.TrimSpace(f[1]), strings.TrimSpace(f[2]), f[3] + login := hiveIdentityForCommitter(name, email, logins) + if login == "" || signedTrailerLineRE.MatchString(msg) { + continue + } + bad = append(bad, unsignedHiveCommit{SHA: sha, Author: login}) + } + return bad +} + +// hiveIdentityForCommitter returns the login in logins that name/email +// belongs to, or "" when the committer is not a hive identity. A login +// matches its own name, "@users.noreply.github.com", +// "+@users.noreply.github.com", and (for "[bot]") the +// bracket-free "@…" address signedTrailerEmail derives. +func hiveIdentityForCommitter(name, email string, logins []string) string { + email = strings.ToLower(email) + local := email + if i := strings.IndexByte(email, '@'); i >= 0 { + local = email[:i] + } + if i := strings.IndexByte(local, '+'); i >= 0 { + local = local[i+1:] + } + for _, login := range logins { + login = strings.TrimSpace(login) + if login == "" { + continue + } + slug := strings.TrimSuffix(login, "[bot]") + if strings.EqualFold(name, login) || strings.EqualFold(name, slug) || + strings.EqualFold(local, login) || strings.EqualFold(local, slug) { + return login + } + } + return "" +} diff --git a/src/pkg/github/push_branch_request_watcher_test.go b/src/pkg/github/push_branch_request_watcher_test.go index d8d2bd8898..2ca68f8ff8 100644 --- a/src/pkg/github/push_branch_request_watcher_test.go +++ b/src/pkg/github/push_branch_request_watcher_test.go @@ -99,10 +99,10 @@ func TestPushBranchRequestWatcher_PushesAndConsumes(t *testing.T) { if !resp.OK || resp.Branch != "scanner/fix-1" || resp.SHA != "cafe1234" { t.Errorf("result = %+v, want ok with branch and sha", resp) } - if len(*calls) != 2 { - t.Fatalf("git calls = %d, want rev-parse + push", len(*calls)) + if len(*calls) != 3 { + t.Fatalf("git calls = %d, want rev-parse + sign-off log + push", len(*calls)) } - push := (*calls)[1] + push := (*calls)[2] joined := strings.Join(push.args, " ") if push.dir != checkout || !strings.Contains(joined, "https://github.com/o/r.git") || @@ -136,10 +136,10 @@ func TestPushBranchRequestWatcher_ForceWithLease(t *testing.T) { } c.ProcessPushBranchRequestsOnce(context.Background()) - if len(*calls) != 2 { - t.Fatalf("git calls = %d, want 2", len(*calls)) + if len(*calls) != 3 { + t.Fatalf("git calls = %d, want 3", len(*calls)) } - if joined := strings.Join((*calls)[1].args, " "); !strings.Contains(joined, "--force-with-lease") || strings.Contains(joined, " --force ") { + if joined := strings.Join((*calls)[2].args, " "); !strings.Contains(joined, "--force-with-lease") || strings.Contains(joined, " --force ") { t.Errorf("push args = %q, want --force-with-lease and never a plain --force", joined) } } @@ -392,3 +392,60 @@ func TestPushBranchGitEnv_NoHeader(t *testing.T) { t.Errorf("env missing core.hooksPath override: %q", joined) } } + +// A branch carrying a hive-authored commit without a Signed-off-by trailer +// is refused before anything reaches GitHub (hivecommons/hive#16619); the +// same commit by an outside contributor is the PR DCO check's business. +func TestPushBranchRequestWatcher_RefusesUnsignedHiveCommit(t *testing.T) { + c := pushTestClient(t, "http://127.0.0.1:1") + c.SetHiveIdentity(HiveIdentity{AIAuthor: "thatguywob", AppLogin: "hivecommons-hive[bot]"}) + dir := withPushDir(t) + var calls []pushExecCall + old := pushBranchExec + pushBranchExec = func(_ context.Context, _ string, _ []string, name string, args ...string) (string, error) { + calls = append(calls, pushExecCall{args: append([]string{name}, args...)}) + switch args[0] { + case "rev-parse": + return "cafe1234\n", nil + case "log": + return "cafe1234\x1fthatguywob\x1fthatguywob@users.noreply.github.com\x1ffix: thing\n\nbody\n\x1e" + + "beef5678\x1fscanner\x1f280983584+hivecommons-hive[bot]@users.noreply.github.com\x1ffix: other\n\nSigned-off-by: scanner \n\x1e", nil + } + return "", nil + } + t.Cleanup(func() { pushBranchExec = old }) + + reqPath, err := WritePushBranchRequest(dir, PushBranchRequest{Repo: "o/r", Branch: "scanner/fix-3", Dir: t.TempDir(), Agent: "scanner"}) + if err != nil { + t.Fatal(err) + } + c.ProcessPushBranchRequestsOnce(context.Background()) + + resp := readPushBranchResult(t, reqPath) + if resp.OK || !strings.Contains(resp.Error, "cafe1234") || !strings.Contains(resp.Error, "thatguywob") || !strings.Contains(resp.Error, "Signed-off-by") { + t.Errorf("result = %+v, want a denial naming the unsigned hive commit", resp) + } + for _, call := range calls { + if call.args[1] == "push" { + t.Fatalf("unsigned hive commit was pushed: %v", call.args) + } + } + if _, err := os.Stat(reqPath + ".denied"); err != nil { + t.Errorf("denied request not parked: %v", err) + } +} + +func TestUnsignedHiveCommits(t *testing.T) { + logins := []string{"hivecommons-hive[bot]", "thatguywob"} + log := "a1\x1fthatguywob\x1fthatguywob@users.noreply.github.com\x1fsubject\n\x1e" + + "a2\x1fscanner\x1f280983584+hivecommons-hive[bot]@users.noreply.github.com\x1fsubject\n\x1e" + + "a3\x1fhivecommons-hive[bot]\x1fhivecommons-hive@hive.kubestellar.io\x1fsubject\n\nSigned-off-by: hivecommons-hive[bot] \n\x1e" + + "a4\x1fMallory\x1fmallory@example.com\x1fsubject\n\x1e" + got := unsignedHiveCommits(log, logins) + if len(got) != 2 || got[0].SHA != "a1" || got[0].Author != "thatguywob" || got[1].SHA != "a2" || got[1].Author != "hivecommons-hive[bot]" { + t.Errorf("unsignedHiveCommits = %+v, want a1 (ai_author) and a2 (App by noreply address) only", got) + } + if got := unsignedHiveCommits("", logins); len(got) != 0 { + t.Errorf("empty log flagged %+v", got) + } +}