diff --git a/changelog.d/fixed-14666.md b/changelog.d/fixed-14666.md new file mode 100644 index 0000000000..4a5af28a60 --- /dev/null +++ b/changelog.d/fixed-14666.md @@ -0,0 +1 @@ +- Re-sign the contributor's commits when adopting a dco-missing PR, and make the fix kick for a no-dco adopted PR name the DCO re-sign first (#14666) diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 31cbbe4abb..608281d827 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -14,6 +14,7 @@ import ( "os" "path/filepath" "regexp" + "slices" "sort" "strconv" "strings" @@ -78,6 +79,7 @@ import ( "github.com/hivecommons/hive/pkg/snapshot" "github.com/hivecommons/hive/pkg/spokealerts" "github.com/hivecommons/hive/pkg/stageengine" + "github.com/hivecommons/hive/pkg/stagelabels" "github.com/hivecommons/hive/pkg/timeline" "github.com/hivecommons/hive/pkg/tokens" "github.com/hivecommons/hive/pkg/tracing" @@ -8735,6 +8737,10 @@ func redPRFixKick(o escalation.Observation, rerouted bool) string { fmt.Fprintf(&b, "Its owning agent (%s) is not reachable right now, so this repair is routed to you.\n\n", redPRFixOwner(o.Agent)) } b.WriteString("Repair it BEFORE claiming new issues or opening any new PR:\n") + if slices.Contains(o.Labels, stagelabels.NoDCOLabel) { + fmt.Fprintf(&b, " FIRST (required): the PR is %s — it inherited unsigned commits. Re-sign them: gh pr checkout %d --repo %s → git rebase --signoff (original authors preserved) → git push --force-with-lease\n", stagelabels.NoDCOLabel, o.Number, o.Repo) + b.WriteString(" THEN fix any failing CI the same way.\n") + } fmt.Fprintf(&b, " gh pr checkout %d --repo %s → fix → git commit -s → git push\n", o.Number, o.Repo) b.WriteString("Push to the SAME branch. Do NOT open a replacement PR.\n") b.WriteString("After the push, STOP: do not watch, poll, or sleep on CI (no gh run watch/view loops).\n") diff --git a/src/cmd/hive/redpr_fix_kick_no_ci_wait_test.go b/src/cmd/hive/redpr_fix_kick_no_ci_wait_test.go index 38bab694d2..d744b54888 100644 --- a/src/cmd/hive/redpr_fix_kick_no_ci_wait_test.go +++ b/src/cmd/hive/redpr_fix_kick_no_ci_wait_test.go @@ -23,3 +23,20 @@ func TestRedPRFixKickForbidsCIPolling(t *testing.T) { } } } + +// An adopted PR that inherited unsigned commits is labelled no-dco; its fix +// kick must name the DCO re-sign first, not only the CI failures (#14666). +func TestRedPRFixKickNamesDCORehomeFirstForNoDCO(t *testing.T) { + got := redPRFixKick(escalation.Observation{Repo: "hivecommons/hive", Number: 14618, Labels: []string{"no-dco"}, FailingChecks: []string{"DCO"}}, false) + first := strings.Index(got, "FIRST (required)") + if first < 0 || !strings.Contains(got, "git rebase --signoff") { + t.Fatalf("no-dco kick lacks the DCO re-sign action:\n%s", got) + } + if first > strings.Index(got, "→ fix → git commit -s") { + t.Errorf("DCO re-sign must come before the generic fix step:\n%s", got) + } + plain := redPRFixKick(escalation.Observation{Repo: "hivecommons/hive", Number: 1}, false) + if strings.Contains(plain, "git rebase --signoff") { + t.Errorf("DCO action leaked into a signed PR's kick:\n%s", plain) + } +} diff --git a/src/pkg/github/automerge/automerge_adoption_fence_test.go b/src/pkg/github/automerge/automerge_adoption_fence_test.go index 821024ef63..020929d351 100644 --- a/src/pkg/github/automerge/automerge_adoption_fence_test.go +++ b/src/pkg/github/automerge/automerge_adoption_fence_test.go @@ -90,5 +90,5 @@ func TestSelfAuthoredSweepCarriedFenceHoldsMerge(t *testing.T) { // supersedeAdoptedPR would for the adopted contributor PR #7. func adoptionPRBodyWithFence(fence string) string { obs := stageObservation{Owner: "acme", Repo: "widget", Display: "acme/widget", Number: 7, HeadSHA: "aaa", Author: "mallory", BaseRef: "v5", Title: "Fix the widget"} - return AdoptionPRBody(obs, stagelabels.Record{AdoptReason: AdoptReasonOffline, AdoptDiffFence: fence}, false) + return AdoptionPRBody(obs, stagelabels.Record{AdoptReason: AdoptReasonOffline, AdoptDiffFence: fence}, false, "") } diff --git a/src/pkg/github/automerge/automerge_contributor_adopt.go b/src/pkg/github/automerge/automerge_contributor_adopt.go index 39c8a57174..d08635f814 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt.go @@ -329,8 +329,10 @@ func AdoptionDCORehomeComment(obs stageObservation, hiveID string, superseding i AdoptionMarkerPrefix, nonEmpty(hiveID, "hive"), AdoptReasonDCORehome, stagelabels.AdoptModeSupersede, superseding, obs.HeadSHA, obs.Author, superseding) } -// AdoptionPRBody is the superseding App PR's body. -func AdoptionPRBody(obs stageObservation, rec stagelabels.Record, dcoRehome bool) string { +// AdoptionPRBody is the superseding App PR's body. resignedTip is the head of +// the re-signed replay the branch points at, or "" when the branch carries the +// contributor's head unchanged. +func AdoptionPRBody(obs stageObservation, rec stagelabels.Record, dcoRehome bool, resignedTip string) string { var b strings.Builder if rec.AdoptDiffFence != "" { // Read back by carriedFenceGate: the App lanes never merge this @@ -350,7 +352,11 @@ func AdoptionPRBody(obs stageObservation, rec stagelabels.Record, dcoRehome bool if obs.HeadRepo != "" && !strings.EqualFold(obs.HeadRepo, obs.Display) { fmt.Fprintf(&b, " (fork `%s`)", obs.HeadRepo) } - b.WriteString(", unchanged. Last lane reason on the original: `") + if resignedTip != "" { + fmt.Fprintf(&b, ", re-signed as `%s` (content unchanged, `Signed-off-by` added). Last lane reason on the original: `", shortSHA(resignedTip)) + } else { + b.WriteString(", unchanged. Last lane reason on the original: `") + } b.WriteString(nonEmpty(rec.Reason, obs.Reason)) b.WriteString("`.\n\n") b.WriteString("**Hive fix agent:** rebase this branch on `") @@ -369,7 +375,7 @@ func AdoptionPRBody(obs stageObservation, rec stagelabels.Record, dcoRehome bool if rec.AdoptReason == AdoptReasonSignoffRehome { b.WriteString("\n**Sign-off attribution:** the original PR would squash the App's signed fixes as the contributor. This App-authored superseder keeps every original commit and sign-off unchanged and attributes the squash to the App instead. DCO and all merge gates still apply here.\n") } - if dcoRehome { + if dcoRehome && resignedTip == "" { b.WriteString("\n**DCO repair required:** this adoption has unsigned non-merge commits and the hive cannot repair them by amending the contributor branch. Re-home the work onto a hive-owned branch from `") b.WriteString(nonEmpty(obs.BaseRef, "the base branch")) b.WriteString("`: cherry-pick each original commit with `git cherry-pick -s` (or squash with `git commit --author` preserving the original author), ensure every replacement commit has `Signed-off-by`, push the DCO-clean branch, open/keep one superseding PR with the same title, then comment on and close #") @@ -673,25 +679,37 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor } branch := adoptBranchPrefix + strconv.Itoa(number) fullRepo := owner + "/" + repo + // resignedTip stays "" when the branch falls back to the unsigned + // contributor head; the body then keeps the manual DCO repair steps. + var resignedTip string _, err = effects.Execute(ctx, c.mutation, effects.Claim{ Repo: fullRepo, Kind: effects.KindBranchPush, Target: branch, Actor: "stage_labels", Inputs: map[string]string{"pr": strconv.Itoa(number), "head_sha": obs.HeadSHA, "adopt": "true"}, }, func(ctx context.Context) (effects.Result, error) { + tip := obs.HeadSHA + if dcoRehome { + if signed, signErr := c.resignAdoptionCommits(ctx, obs); signErr != nil { + c.warn("stage labels: re-signing adopted commits failed; branching from the unsigned head", "repo", obs.Display, "pr", number, "error", signErr) + } else if signed != "" { + tip = signed + resignedTip = signed + } + } _, _, apiErr := c.gh.Git.CreateRef(ctx, owner, repo, &gh.Reference{ Ref: gh.Ptr("refs/heads/" + branch), - Object: &gh.GitObject{SHA: gh.Ptr(obs.HeadSHA)}, + Object: &gh.GitObject{SHA: gh.Ptr(tip)}, }) if apiErr != nil && isGitHubStatus(apiErr, http.StatusUnprocessableEntity) { // The branch exists from an earlier attempt: point it at this head. _, _, apiErr = c.gh.Git.UpdateRef(ctx, owner, repo, &gh.Reference{ Ref: gh.Ptr("refs/heads/" + branch), - Object: &gh.GitObject{SHA: gh.Ptr(obs.HeadSHA)}, + Object: &gh.GitObject{SHA: gh.Ptr(tip)}, }, true) } if apiErr != nil { return effects.Result{}, effects.NotApplied(apiErr) } - return effects.Result{Provenance: obs.HeadSHA}, nil + return effects.Result{Provenance: tip}, nil }) if err != nil { c.warn("stage labels: adoption failed to push branch", "repo", obs.Display, "pr", number, "branch", branch, "error", err) @@ -708,7 +726,7 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor Title: gh.Ptr(nonEmpty(strings.TrimSpace(obs.Title), fmt.Sprintf("Adopt #%d", number))), Head: gh.Ptr(branch), Base: gh.Ptr(obs.BaseRef), - Body: gh.Ptr(AdoptionPRBody(obs, rec, dcoRehome)), + Body: gh.Ptr(AdoptionPRBody(obs, rec, dcoRehome, resignedTip)), }) if apiErr != nil { return effects.Result{}, effects.NotApplied(apiErr) @@ -741,6 +759,55 @@ func (c *Engine) supersedeAdoptedPR(ctx context.Context, store *stagelabels.Stor return 1, nil } +// resignAdoptionCommits replays the contributor's commits as a linear chain +// with the App's Signed-off-by added where missing (the API equivalent of +// `git rebase --signoff`), keeping each commit's tree, author and message, +// and returns the new tip. It returns "" when the commits cannot be replayed +// without changing content (a merge commit, or no commits), so the caller +// keeps the contributor head. +func (c *Engine) resignAdoptionCommits(ctx context.Context, obs stageObservation) (string, error) { + opts := &gh.ListOptions{PerPage: 100} + var all []*gh.RepositoryCommit + for page := 0; page < stageDCOMaxPages; page++ { + commits, resp, err := c.gh.PullRequests.ListCommits(ctx, obs.Owner, obs.Repo, obs.Number, opts) + if err != nil { + return "", fmt.Errorf("listing commits for %s#%d: %w", obs.Display, obs.Number, err) + } + all = append(all, commits...) + if resp == nil || resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage + } + if len(all) == 0 || all[len(all)-1].GetSHA() != obs.HeadSHA { + return "", nil + } + for _, rc := range all { + if len(rc.Parents) != 1 { + return "", nil + } + } + trailer := hgithub.BotSignedOffByTrailer(c.transport.AppBotLogin()) + parent := all[0].Parents[0].GetSHA() + for _, rc := range all { + msg := rc.GetCommit().GetMessage() + if !signedOffByRE.MatchString(msg) { + msg = strings.TrimRight(msg, "\n") + "\n\n" + trailer + "\n" + } + created, _, err := c.gh.Git.CreateCommit(hgithub.WithRESTCaller(ctx, "hive:automerge_sweep"), obs.Owner, obs.Repo, &gh.Commit{ + Message: gh.Ptr(msg), + Tree: &gh.Tree{SHA: gh.Ptr(rc.GetCommit().GetTree().GetSHA())}, + Parents: []*gh.Commit{{SHA: gh.Ptr(parent)}}, + Author: rc.GetCommit().GetAuthor(), + }, nil) + if err != nil { + return "", fmt.Errorf("re-signing %s: %w", shortSHA(rc.GetSHA()), err) + } + parent = created.GetSHA() + } + return parent, nil +} + // settleAdoption polls the superseding App PR for a PR in stage 074 (at // most every adoptCheckInterval): merged → close the original with a note; // closed unmerged → forget the adoption so the original returns to 072. diff --git a/src/pkg/github/automerge/automerge_contributor_adopt_test.go b/src/pkg/github/automerge/automerge_contributor_adopt_test.go index 83491b25e2..967bcd0244 100644 --- a/src/pkg/github/automerge/automerge_contributor_adopt_test.go +++ b/src/pkg/github/automerge/automerge_contributor_adopt_test.go @@ -3,6 +3,7 @@ package automerge import ( "context" "encoding/json" + "fmt" "net/http" "slices" "strings" @@ -25,6 +26,7 @@ type adoptAPI struct { nextPR int superseders map[int]map[string]any // number → GET payload prEdits []string + newCommits []map[string]any // POST git/commits bodies commits map[string]adoptCommit // failRefs makes POST git/refs answer 500; refAttempts counts them. failRefs bool @@ -117,6 +119,13 @@ func (a *adoptAPI) handle(w http.ResponseWriter, r *http.Request) bool { } _ = json.NewEncoder(w).Encode(files) return true + case r.Method == http.MethodPost && path == "/repos/acme/widget/git/commits": + var body map[string]any + _ = json.NewDecoder(r.Body).Decode(&body) + a.newCommits = append(a.newCommits, body) + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(map[string]any{"sha": fmt.Sprintf("signed%d", len(a.newCommits))}) + return true case r.Method == http.MethodPost && path == "/repos/acme/widget/git/refs": var body struct { Ref string `json:"ref"` @@ -524,7 +533,7 @@ func TestAdoptionDCORehomeDecisionAndRendering(t *testing.T) { t.Fatal("signed head does not need DCO re-home") } obs.Detail.DCOUnsigned = 2 - body := AdoptionPRBody(obs, stagelabels.Record{Reason: "check-failing:dco"}, true) + body := AdoptionPRBody(obs, stagelabels.Record{Reason: "check-failing:dco"}, true, "") for _, want := range []string{ "**DCO repair required:**", "cherry-pick each original commit with `git cherry-pick -s`", @@ -540,6 +549,17 @@ func TestAdoptionDCORehomeDecisionAndRendering(t *testing.T) { t.Fatalf("body missing %q:\n%s", want, body) } } + // A successful re-sign replay names the new tip and drops the manual + // re-home steps the hive already performed. + body = AdoptionPRBody(obs, stagelabels.Record{Reason: "check-failing:dco"}, true, "5e5e5e5e5e5e5e5e") + if !strings.Contains(body, "re-signed as `"+shortSHA("5e5e5e5e5e5e5e5e")+"`") || strings.Contains(body, ", unchanged.") { + t.Fatalf("re-signed body must name the re-signed tip:\n%s", body) + } + for _, unwanted := range []string{"**DCO repair required:**", "git cherry-pick -s"} { + if strings.Contains(body, unwanted) { + t.Fatalf("re-signed body still carries %q:\n%s", unwanted, body) + } + } } // Online author: nudge + 2h grace; nothing is adopted before the grace, then @@ -1026,6 +1046,43 @@ func TestAlreadyAdoptedInPlaceDCOFailureRehomesAndClosesOriginal(t *testing.T) { } } +// The dco-rehome branch must not inherit the contributor's unsigned commits +// (#14666): each is replayed with a Signed-off-by and the branch points at the +// new tip. +func TestDCORehomeBranchCarriesResignedCommits(t *testing.T) { + p := adoptPolicy() + p.AdoptInPlace = true + p.DCORequired = func(string) (bool, bool) { return true, true } + h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p) + defer done() + h.adopt.allowMaintainerEdits(7, true) + h.api.commits = map[int][]string{7: {"fix: unsigned contributor commit"}} + h.observe(adoptPR(7, "c0"), "check-failing:DCO") + key := stageKey("acme", "widget", 7) + h.e.stageStoreOrShared().MarkAdoptedInPlace(key, "c0", AdoptReasonOffline, h.clock.at) + + h.api.reset() + h.clock.advance(time.Minute) + h.observe(adoptPR(7, "c0", "stage/074-adopted-by-hive", "role/contributor", "author/clanker", stagelabels.NoDCOLabel), "check-failing:DCO") + if len(h.adopt.newCommits) != 1 { + t.Fatalf("re-signed commits=%v", h.adopt.newCommits) + } + msg, _ := h.adopt.newCommits[0]["message"].(string) + if !strings.Contains(msg, "fix: unsigned contributor commit") || !signedOffByRE.MatchString(msg) { + t.Fatalf("re-signed message lacks sign-off or original text: %q", msg) + } + if got := h.adopt.refs["refs/heads/hive/adopt-7"]; got != "signed1" { + t.Fatalf("rehome branch sha=%q, want the re-signed tip; refs=%v", got, h.adopt.refs) + } + if len(h.adopt.pulls) != 1 { + t.Fatalf("pulls=%v", h.adopt.pulls) + } + body, _ := h.adopt.pulls[0]["body"].(string) + if !strings.Contains(body, "re-signed as `signed1`") || strings.Contains(body, "**DCO repair required:**") || strings.Contains(body, "git cherry-pick -s") { + t.Fatalf("re-signed rehome body must name the new tip and omit the manual DCO repair block:\n%s", body) + } +} + // The ownership split is cached on the store record, so the decision does // not depend on the stage label being present on the PR. func TestAlreadyAdoptedInPlaceDCOFailureRehomesFromCachedSplitWithoutStageLabel(t *testing.T) { @@ -1048,8 +1105,10 @@ func TestAlreadyAdoptedInPlaceDCOFailureRehomesFromCachedSplitWithoutStageLabel( h.clock.advance(time.Minute) reads := h.api.commitReads h.observe(adoptPR(7, "aaa", "role/contributor", "author/clanker", stagelabels.NoDCOLabel), "check-failing:DCO") - if h.api.commitReads != reads { - t.Fatalf("commit reads=%d, want cached decision (%d)", h.api.commitReads, reads) + // The cached split spares the DCO decision a commit read; the re-sign + // replay needs the commit trees and messages, so it lists them exactly once. + if h.api.commitReads != reads+1 { + t.Fatalf("commit reads=%d, want cached decision plus one re-sign listing (%d)", h.api.commitReads, reads+1) } if len(h.adopt.pulls) != 1 || len(h.adopt.prEdits) != 1 { t.Fatalf("expected re-home from cached split: pulls=%v edits=%v", h.adopt.pulls, h.adopt.prEdits)