From ac459c3c32874dda456ef23de3c02d44b57845c0 Mon Sep 17 00:00:00 2001 From: Danathar <6772335+Danathar@users.noreply.github.com> Date: Tue, 17 Nov 2026 05:35:05 -0500 Subject: [PATCH 1/2] automerge: refuse an unsigned squash only where the repo requires DCO Since #18204 the hive refused to squash any PR whose branch carries no Signed-off-by, without asking whether the repo requires DCO at all. In repos with no DCO gate every Renovate and Dependabot PR, and every unsigned human commit, stalled at 080-awaiting-merge-admission with reason squash-unsigned and nothing a person could do. The refusal now applies only where the repo requires DCO, by the rule the stage router already uses for no-dco: the governor.labels.stage.dco.required override when set for the repo, else a dco/DCO context required on the base branch or reported on the head, read at merge time. A head whose checks cannot be read keeps the refusal. Elsewhere the squash goes out with the message as built, as before #18204, and any branch sign-offs that exist are still carried. The sweep lanes (mergeOptions) and the merge relay (MergePR) share the one decision in SquashSignoff. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> --- changelog.d/fixed-squash-unsigned-dco-gate.md | 1 + src/cmd/hive/main_sweeps.go | 4 + .../stage-labels.md | 12 ++ .../automerge/automerge_merge_credit.go | 17 +- .../automerge_unsigned_squash_test.go | 45 ++++ src/pkg/github/client.go | 3 + src/pkg/github/merge_credit.go | 21 +- src/pkg/github/merge_signoff.go | 122 ++++++++++- src/pkg/github/merge_signoff_test.go | 195 +++++++++++++++++- src/pkg/github/pullrequest.go | 9 +- 10 files changed, 408 insertions(+), 21 deletions(-) create mode 100644 changelog.d/fixed-squash-unsigned-dco-gate.md diff --git a/changelog.d/fixed-squash-unsigned-dco-gate.md b/changelog.d/fixed-squash-unsigned-dco-gate.md new file mode 100644 index 0000000000..8dbc2cd107 --- /dev/null +++ b/changelog.d/fixed-squash-unsigned-dco-gate.md @@ -0,0 +1 @@ +- An unsigned squash merge is refused only in repos that require DCO. Since [#18204](https://github.com/hivecommons/hive/pull/18204) the hive refused to squash any PR with no `Signed-off-by` on its branch, even in repos with no DCO gate, so every Renovate and Dependabot PR and every unsigned human commit there stalled at `080-awaiting-merge-admission` with `squash-unsigned` and nothing a person could do. The refusal now applies only where the repo requires DCO: the existing `governor.labels.stage.dco.required` override, else a `dco` / `DCO` check required on the base branch or reported on the PR head, the same rule the stage router uses for `no-dco`. Repos with a DCO gate, hivecommons/hive included, keep the refusal from [#18203](https://github.com/hivecommons/hive/issues/18203). Applies to the sweep lanes and the merge relay. diff --git a/src/cmd/hive/main_sweeps.go b/src/cmd/hive/main_sweeps.go index 2509393e92..a47158479e 100644 --- a/src/cmd/hive/main_sweeps.go +++ b/src/cmd/hive/main_sweeps.go @@ -2108,6 +2108,10 @@ func installReviewRelaySettings(client *github.Client, cfg *config.Config, logge // hivecommons/hive#14581: issue-author credit on every merge path, read // live so a reload of auto_merge.credit_issue_authors applies. client.SetCreditIssueAuthors(func(string) bool { return cfg.AutoMerge.CreditIssueAuthorsEnabled() }) + // hivecommons/hive#18203: a squash with no branch sign-off is refused + // only where the repo requires DCO; the per-repo override is the same + // governor.labels.stage.dco.required the stage router reads. + client.SetDCORequired(func(repo string) (bool, bool) { return cfg.Governor.Labels.Stage.DCO.RequiredFor(repo) }) // hivecommons/hive#13241: an issue a person reopens after its closing PR // merged is a change failure on that PR's evidence record, from both the // issues.reopened webhook and the enumeration reopen check. diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index 8a3c3aa063..7deda7aa69 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -551,6 +551,18 @@ governor: On an ungated repo an unsigned head keeps the lane's stage, carries `no-dco` as information only, and the nudge does not ask for a rebase. +The same rule decides the hive's squash merges. A squash body is rebuilt from +the branch commits so every branch `Signed-off-by` lands in the merged commit +([#18203](https://github.com/hivecommons/hive/issues/18203)). When no branch +commit carries a sign-off at all, the hive refuses the squash (skip reason +`squash-unsigned`) only where the repo requires DCO: the `dco.required` +override above, else a `dco` / `DCO` context required on the base branch or +reported on the head, read at merge time. A head whose checks cannot be read +keeps the refusal. On an ungated repo the unsigned squash merges with the +message as built, so Renovate and Dependabot PRs and unsigned human commits +are not held at `080` with nothing a person can do. The sweep lanes and the +merge relay (`MergePR`) apply the same rule. + Prow's `dco-signoff: yes` / `dco-signoff: no` (the Prow `dco` plugin's hard-coded names, also written by the repo's DCO workflow) are **foreign**: the hive never adds or removes them. #12544 tried to retire them from the diff --git a/src/pkg/github/automerge/automerge_merge_credit.go b/src/pkg/github/automerge/automerge_merge_credit.go index 6266d16e29..31f1f2fa2f 100644 --- a/src/pkg/github/automerge/automerge_merge_credit.go +++ b/src/pkg/github/automerge/automerge_merge_credit.go @@ -24,7 +24,8 @@ type mergeCreditTransport interface { // the repo credits issue authors and the PR has one to credit, or a squash // needs the body rebuilt to keep the branch's Signed-off-by trailers // (hivecommons/hive#18203). A non-empty refused is the skip reason when the -// squash would land with no sign-off at all; the caller must not merge. +// squash would land with no sign-off at all in a repo that requires DCO; the +// caller must not merge. func (c *Engine) mergeOptions(ctx context.Context, owner, repo string, pr *gh.PullRequest, headSHA, method string) (message string, opts *gh.PullRequestOptions, refused string) { opts = &gh.PullRequestOptions{SHA: headSHA, MergeMethod: method} mt, ok := c.transport.(mergeCreditTransport) @@ -37,10 +38,15 @@ func (c *Engine) mergeOptions(ctx context.Context, owner, repo string, pr *gh.Pu mt.NoteMergeCredit(owner+"/"+repo, pr.GetNumber(), credit) message = credit.CommitMessage } - message, err := creditor.SquashSignoff(ctx, owner, repo, pr.GetNumber(), method, message) + head := headSHA + if head == "" { + head = pr.GetHead().GetSHA() + } + message, err := creditor.SquashSignoff(ctx, owner, repo, pr.GetNumber(), pr.GetBase().GetRef(), head, method, message) if err != nil { - // An unsigned branch needs its author to sign off; this is an - // expected merge refusal, not a runtime failure. + // An unsigned branch in a repo that requires DCO needs its author + // to sign off; this is an expected merge refusal, not a runtime + // failure. c.info("automerge refused squash without Signed-off-by", "repo", owner+"/"+repo, "pr", pr.GetNumber(), "error", err) return "", opts, autoMergeReasonSquashUnsigned } @@ -48,5 +54,6 @@ func (c *Engine) mergeOptions(ctx context.Context, owner, repo string, pr *gh.Pu } // autoMergeReasonSquashUnsigned is the skip reason when no branch commit -// carries a Signed-off-by, so the squash would land a commit that fails DCO. +// carries a Signed-off-by in a repo that requires DCO, so the squash would +// land a commit that fails DCO. const autoMergeReasonSquashUnsigned = "squash-unsigned" diff --git a/src/pkg/github/automerge/automerge_unsigned_squash_test.go b/src/pkg/github/automerge/automerge_unsigned_squash_test.go index 01a3999aa0..d9b2904db2 100644 --- a/src/pkg/github/automerge/automerge_unsigned_squash_test.go +++ b/src/pkg/github/automerge/automerge_unsigned_squash_test.go @@ -32,6 +32,7 @@ func TestUnsignedSquashRefusalIsNotSelfReported(t *testing.T) { logger := slog.New(slogfeed.NewWithObserver(slog.NewTextHandler(&logs, nil), func(_, _, _, _ string) { observations++ })) client := hgithub.NewClient("token", "acme", []string{"widget"}, logger, api.URL) client.SetCreditIssueAuthors(func(string) bool { return false }) + client.SetDCORequired(func(string) (bool, bool) { return true, true }) engine := New(client, Options{Logger: logger}) pr := &gh.PullRequest{Number: gh.Ptr(7)} for range 5 { @@ -50,3 +51,47 @@ func TestUnsignedSquashRefusalIsNotSelfReported(t *testing.T) { t.Fatalf("signed squash message = %q, refusal = %q", got, refused) } } + +// In a repo with no DCO gate (no dco.required entry, no dco/DCO context on +// the head) an unsigned squash is not refused: Renovate PRs and unsigned +// human commits merge as they did before #18203 +// (projectbluefin/bluefin-report#91, projectbluefin/utah-packages#1109). +func TestUnsignedSquashMergesWithoutDCOGate(t *testing.T) { + for _, tc := range []struct { + name string + override func(string) (bool, bool) + headContext string + wantRefused string + }{ + {name: "no gate merges", headContext: "Tests Passed"}, + {name: "a dco status on the head refuses", headContext: "dco", wantRefused: autoMergeReasonSquashUnsigned}, + {name: "dco.required false overrides the detected gate", override: func(string) (bool, bool) { return false, true }, headContext: "dco"}, + } { + t.Run(tc.name, func(t *testing.T) { + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/repos/acme/widget/pulls/7/commits": + fmt.Fprint(w, `[{"sha":"c0","commit":{"message":"chore(deps): update widget"},"parents":[{"sha":"p"}]}]`) + case "/repos/acme/widget/commits/head1/status": + fmt.Fprintf(w, `{"sha":"head1","total_count":1,"statuses":[{"context":%q,"state":"success"}]}`, tc.headContext) + case "/repos/acme/widget/commits/head1/check-runs": + fmt.Fprint(w, `{"total_count":0,"check_runs":[]}`) + default: + t.Errorf("unexpected request: %s", r.URL.Path) + http.NotFound(w, r) + } + })) + defer api.Close() + logger := slog.New(slog.NewTextHandler(&bytes.Buffer{}, nil)) + client := hgithub.NewClient("token", "acme", []string{"widget"}, logger, api.URL) + client.SetCreditIssueAuthors(func(string) bool { return false }) + client.SetDCORequired(tc.override) + engine := New(client, Options{Logger: logger}) + pr := &gh.PullRequest{Number: gh.Ptr(7)} + message, _, refused := engine.mergeOptions(context.Background(), "acme", "widget", pr, "head1", "squash") + if refused != tc.wantRefused || message != "" { + t.Fatalf("mergeOptions = message %q, refused %q; want refused %q", message, refused, tc.wantRefused) + } + }) + } +} diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 2439875b38..120c4df20e 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -247,9 +247,12 @@ type Client struct { // (merge_credit.go, hivecommons/hive#14581); creditIssueAuthors is the // live auto_merge.credit_issue_authors switch and mergeCredits holds the // credit decided for a PR until its pr_merged audit consumes it. + // dcoRequired is the live governor.labels.stage.dco.required override + // the squash sign-off refusal consults (merge_signoff.go). mergeCreditMu sync.Mutex mergeCredit *MergeCreditor creditIssueAuthors func(repo string) bool + dcoRequired func(repo string) (required, ok bool) mergeCredits map[string]MergeCredit // issueClaims reports whether issue claims are recognised at enumeration // time and the TTL an assignee-inferred claim runs for (#8380). Nil or diff --git a/src/pkg/github/merge_credit.go b/src/pkg/github/merge_credit.go index c1201a69de..f21ee55d54 100644 --- a/src/pkg/github/merge_credit.go +++ b/src/pkg/github/merge_credit.go @@ -127,7 +127,10 @@ type issueAuthorEntry struct { type MergeCreditor struct { gh *gh.Client enabled func(repo string) bool - logger *slog.Logger + // dcoRequired is the per-repo dco.required override SquashSignoff + // consults before refusing an unsigned squash (nil: detection only). + dcoRequired func(repo string) (required, ok bool) + logger *slog.Logger mu sync.Mutex authors map[string]issueAuthorEntry @@ -416,6 +419,21 @@ func (c *Client) SetCreditIssueAuthors(fn func(repo string) bool) { } } +// SetDCORequired installs the live governor.labels.stage.dco.required +// override the squash sign-off refusal consults (nil: detection only). +// Nil-receiver safe like the other setters. +func (c *Client) SetDCORequired(fn func(repo string) (required, ok bool)) { + if c == nil { + return + } + c.mergeCreditMu.Lock() + defer c.mergeCreditMu.Unlock() + c.dcoRequired = fn + if c.mergeCredit != nil { + c.mergeCredit.dcoRequired = fn + } +} + // MergeCreditor returns the client's shared creditor (one issue-author cache // for every merge path), or nil without an API client. func (c *Client) MergeCreditor() *MergeCreditor { @@ -426,6 +444,7 @@ func (c *Client) MergeCreditor() *MergeCreditor { defer c.mergeCreditMu.Unlock() if c.mergeCredit == nil { c.mergeCredit = NewMergeCreditor(c.client, c.creditIssueAuthors, c.logger) + c.mergeCredit.dcoRequired = c.dcoRequired } return c.mergeCredit } diff --git a/src/pkg/github/merge_signoff.go b/src/pkg/github/merge_signoff.go index 75a4cfecf2..71264f3f8c 100644 --- a/src/pkg/github/merge_signoff.go +++ b/src/pkg/github/merge_signoff.go @@ -3,6 +3,7 @@ package github import ( "context" "errors" + "fmt" "log/slog" "regexp" "strings" @@ -19,10 +20,19 @@ import ( // place the trailer can be lost after the PR-time DCO check passed, so the // hive no longer leaves the body to GitHub: it rebuilds it from the branch // commits, makes sure every branch sign-off is in it, and refuses to squash -// when the branch carries none at all. It never signs off on anyone's behalf. +// when the branch carries none at all in a repo that requires DCO. It never +// signs off on anyone's behalf. +// +// A repo requires DCO by the same rule the stage router routes a missing +// sign-off on (dcoRequired in pkg/github/automerge): the per-repo +// governor.labels.stage.dco.required override, else a detected `dco`/`DCO` +// gate. Refusing everywhere stalled every unsigned PR in repos with no DCO +// gate at all, Renovate and Dependabot PRs included, at 080 with nothing a +// person could do (projectbluefin/bluefin-report#91). // ErrSquashUnsigned is returned when a squash merge would land a commit with -// no Signed-off-by: none of the branch commits carries one. +// no Signed-off-by in a repo that requires DCO: none of the branch commits +// carries one. var ErrSquashUnsigned = errors.New("squash refused: no branch commit carries a Signed-off-by trailer, the landing commit would fail DCO") // signedOffByRE matches one Signed-off-by trailer line anywhere in a body @@ -38,9 +48,11 @@ var signedOffByRE = regexp.MustCompile(`(?mi)^Signed-off-by:\s*(\S.*?)\s*$`) // // It applies whether or not issue-author credit is on for the repo. If the // branch commits cannot be read the message is returned unchanged (the -// PR-time DCO gate already ran); a branch read with no sign-off at all -// returns ErrSquashUnsigned and the caller must not merge. -func (m *MergeCreditor) SquashSignoff(ctx context.Context, owner, repo string, number int, method, message string) (string, error) { +// PR-time DCO gate already ran). A branch read with no sign-off at all +// returns ErrSquashUnsigned, and the caller must not merge, only when the +// repo requires DCO (requiresDCO, for base and head); elsewhere the message +// goes out as built, as it did before #18203. +func (m *MergeCreditor) SquashSignoff(ctx context.Context, owner, repo string, number int, base, head, method, message string) (string, error) { if m == nil || m.gh == nil || number <= 0 { return message, nil } @@ -57,7 +69,105 @@ func (m *MergeCreditor) SquashSignoff(ctx context.Context, owner, repo string, n slog.String("repo", owner+"/"+repo), slog.Int("number", number), slog.String("error", err.Error())) return message, nil } - return squashSignoffMessage(commits, message) + out, err := squashSignoffMessage(commits, message) + if !errors.Is(err, ErrSquashUnsigned) { + return out, err + } + if m.requiresDCO(ctx, owner, repo, base, head) { + return "", err + } + m.logger.Info("squash sign-off: no branch sign-off and the repo does not require DCO, merging as built", + slog.String("repo", owner+"/"+repo), slog.Int("number", number)) + return message, nil +} + +// dcoEvidenceMaxPages bounds the head's status and check-run reads. +const dcoEvidenceMaxPages = 3 + +// requiresDCO reports whether owner/repo requires a sign-off on a squash of +// head onto base: the dco.required override when one is set for the repo, +// else a `dco`/`DCO` context in base's required checks or reported on head +// (the DCO App, Prow's dco plugin). A head it cannot read counts as +// required, so a failed read keeps the refusal. +func (m *MergeCreditor) requiresDCO(ctx context.Context, owner, repo, base, head string) bool { + if m.dcoRequired != nil { + if required, ok := m.dcoRequired(owner + "/" + repo); ok { + return required + } + } + ctx = WithRESTCaller(ctx, mergeCreditCaller) + if base != "" { + if required, known := RequiredStatusCheckContexts(ctx, m.gh, owner, repo, base, nil, false); known { + for name := range required { + if IsDCOContext(name) { + return true + } + } + } + } + if head == "" { + return true + } + reported, err := headContextNames(ctx, m.gh, owner, repo, head) + if err != nil { + m.logger.Warn("squash sign-off: could not read the head's checks, treating the repo as requiring DCO", + slog.String("repo", owner+"/"+repo), slog.String("head", head), slog.String("error", err.Error())) + return true + } + for _, name := range reported { + if IsDCOContext(name) { + return true + } + } + return false +} + +// headContextNames lists the commit-status contexts and check-run names +// reported on ref. More than dcoEvidenceMaxPages pages of either is an +// error: the list would be incomplete. +func headContextNames(ctx context.Context, client *gh.Client, owner, repo, ref string) ([]string, error) { + var names []string + statusOpts := &gh.ListOptions{PerPage: 100} + for page := 0; ; page++ { + status, resp, err := client.Repositories.GetCombinedStatus(ctx, owner, repo, ref, statusOpts) + if err != nil { + return nil, err + } + if status == nil { + return nil, fmt.Errorf("no combined status for %s", ref) + } + for _, s := range status.Statuses { + names = append(names, s.GetContext()) + } + if resp == nil || resp.NextPage == 0 { + break + } + if page+1 >= dcoEvidenceMaxPages { + return nil, fmt.Errorf("more than %d pages of statuses on %s", dcoEvidenceMaxPages, ref) + } + statusOpts.Page = resp.NextPage + } + checkOpts := &gh.ListCheckRunsOptions{ListOptions: gh.ListOptions{PerPage: 100}} + for page := 0; ; page++ { + runs, resp, err := client.Checks.ListCheckRunsForRef(ctx, owner, repo, ref, checkOpts) + if err != nil { + return nil, err + } + if runs == nil { + return nil, fmt.Errorf("no check runs answer for %s", ref) + } + for _, run := range runs.CheckRuns { + names = append(names, run.GetName()) + } + if resp == nil || resp.NextPage == 0 { + break + } + if page+1 >= dcoEvidenceMaxPages { + return nil, fmt.Errorf("more than %d pages of check runs on %s", dcoEvidenceMaxPages, ref) + } + checkOpts.Page = resp.NextPage + } + return names, nil } // squashSignoffMessage is SquashSignoff's decision over commits already read: diff --git a/src/pkg/github/merge_signoff_test.go b/src/pkg/github/merge_signoff_test.go index 18617d45d4..105a6fa59f 100644 --- a/src/pkg/github/merge_signoff_test.go +++ b/src/pkg/github/merge_signoff_test.go @@ -4,10 +4,13 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" + "log/slog" "net/http" "net/http/httptest" "strings" + "sync/atomic" "testing" gh "github.com/google/go-github/v72/github" @@ -81,7 +84,7 @@ func TestMergeCreditorSquashSignoff(t *testing.T) { t.Run("applies with issue-author credit off", func(t *testing.T) { api := &mergeCreditAPI{commits: []string{"Fix\n\nSigned-off-by: K "}} m, _ := newMergeCreditorForTest(t, api, off) - got, err := m.SquashSignoff(ctx, "acme", "widget", 7, "squash", "") + got, err := m.SquashSignoff(ctx, "acme", "widget", 7, "", "", "squash", "") if err != nil || got != "Signed-off-by: K " { t.Fatalf("SquashSignoff = %q, %v", got, err) } @@ -90,7 +93,7 @@ func TestMergeCreditorSquashSignoff(t *testing.T) { api := &mergeCreditAPI{} m, _ := newMergeCreditorForTest(t, api, off) msg := "Body\n\nSigned-off-by: K " - if got, err := m.SquashSignoff(ctx, "acme", "widget", 7, "squash", msg); err != nil || got != msg || api.listGets.Load() != 0 { + if got, err := m.SquashSignoff(ctx, "acme", "widget", 7, "", "", "squash", msg); err != nil || got != msg || api.listGets.Load() != 0 { t.Fatalf("SquashSignoff = %q, %v after %d commit lists", got, err, api.listGets.Load()) } }) @@ -98,7 +101,7 @@ func TestMergeCreditorSquashSignoff(t *testing.T) { api := &mergeCreditAPI{} m, _ := newMergeCreditorForTest(t, api, off) for _, method := range []string{"merge", "rebase"} { - if got, err := m.SquashSignoff(ctx, "acme", "widget", 7, method, ""); err != nil || got != "" { + if got, err := m.SquashSignoff(ctx, "acme", "widget", 7, "", "", method, ""); err != nil || got != "" { t.Fatalf("%s: SquashSignoff = %q, %v", method, got, err) } } @@ -106,24 +109,27 @@ func TestMergeCreditorSquashSignoff(t *testing.T) { t.Fatalf("merge/rebase read the commits %d times", api.listGets.Load()) } }) - t.Run("unsigned branch is refused", func(t *testing.T) { + t.Run("unsigned branch is refused where DCO is required", func(t *testing.T) { api := &mergeCreditAPI{commits: []string{"Fix"}} m, _ := newMergeCreditorForTest(t, api, off) - if _, err := m.SquashSignoff(ctx, "acme", "widget", 7, "squash", ""); !errors.Is(err, ErrSquashUnsigned) { + m.dcoRequired = func(string) (bool, bool) { return true, true } + if _, err := m.SquashSignoff(ctx, "acme", "widget", 7, "", "", "squash", ""); !errors.Is(err, ErrSquashUnsigned) { t.Fatalf("err = %v, want ErrSquashUnsigned", err) } }) t.Run("nil creditor passes the message through", func(t *testing.T) { var m *MergeCreditor - if got, err := m.SquashSignoff(ctx, "acme", "widget", 7, "squash", "x"); err != nil || got != "x" { + if got, err := m.SquashSignoff(ctx, "acme", "widget", 7, "", "", "squash", "x"); err != nil || got != "x" { t.Fatalf("nil: %q, %v", got, err) } }) } -// MergePRVia refuses to squash a branch with no sign-off and sends no merge. +// MergePRVia refuses to squash a branch with no sign-off in a repo whose +// head carries Prow's dco status, and sends no merge. func TestMergePRViaRefusesUnsignedSquash(t *testing.T) { green := greenFixture() + green.statuses = []ciStatus{{context: "dco", state: "failure"}} merges := 0 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { @@ -156,3 +162,178 @@ func TestMergePRViaRefusesUnsignedSquash(t *testing.T) { t.Fatalf("merges = %d, want 0", merges) } } + +// MergePRVia squashes a branch with no sign-off in a repo with no DCO gate +// (no dco.required entry, no dco/DCO context required or reported) and sends +// the message as built, as before #18203 (projectbluefin/bluefin-report#91). +func TestMergePRViaMergesUnsignedSquashWithoutDCOGate(t *testing.T) { + green := greenFixture() + merges := 0 + var mergeBody map[string]any + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/pulls/42": + json.NewEncoder(w).Encode(map[string]any{ + "number": 42, "state": "open", "title": "chore(deps): update widget", "body": "", + "user": map[string]any{"login": "renovate[bot]"}, + "head": map[string]any{"sha": "abc123", "ref": "renovate/widget"}, + "base": map[string]any{"ref": "main"}, + }) + case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/pulls/42/commits": + json.NewEncoder(w).Encode([]map[string]any{{"sha": "c0", "commit": map[string]any{"message": "chore(deps): update widget"}, "parents": []map[string]any{{"sha": "p"}}}}) + case r.Method == http.MethodPut && r.URL.Path == "/repos/o/r/pulls/42/merge": + merges++ + mergeBody = map[string]any{} + _ = json.NewDecoder(r.Body).Decode(&mergeBody) + _, _ = io.WriteString(w, `{"sha":"deadbeef","merged":true}`) + default: + if green.serveCI(w, r) { + return + } + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + c := testMergeClient(t, srv.URL) + c.SetCreditIssueAuthors(func(string) bool { return false }) + res, err := c.MergePRVia(context.Background(), "o/r", 42, "squash", "", PRAuditPathForge) + if err != nil || !res.Merged || merges != 1 { + t.Fatalf("MergePRVia = %+v, %v after %d merges, want one merge", res, err, merges) + } + if msg, _ := mergeBody["commit_message"].(string); msg != "" { + t.Fatalf("commit_message = %q, want GitHub's default (empty)", msg) + } +} + +// dcoGateAPI is a fake GitHub for the DCO gate behind the unsigned-squash +// refusal: the PR's commits, base main's required checks and head1's +// statuses and check runs. reads counts the gate's requests. +type dcoGateAPI struct { + commits []string + required []string // nil: main is not protected and has no rulesets + statuses []string + checks []string + headHTTP int // non-zero: head1's status read fails with it + reads atomic.Int32 +} + +func (a *dcoGateAPI) creditor(t *testing.T, override func(string) (bool, bool)) *MergeCreditor { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + enc := func(v any) { w.Header().Set("Content-Type", "application/json"); _ = json.NewEncoder(w).Encode(v) } + switch r.URL.Path { + case "/repos/acme/widget/pulls/7/commits": + out := make([]map[string]any, 0, len(a.commits)) + for i, msg := range a.commits { + out = append(out, map[string]any{"sha": fmt.Sprintf("c%d", i), "commit": map[string]any{"message": msg}, "parents": []map[string]any{{"sha": "p"}}}) + } + enc(out) + case "/repos/acme/widget/branches/main/protection/required_status_checks": + a.reads.Add(1) + if a.required == nil { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(w, `{"message":"Branch not protected"}`) + return + } + enc(map[string]any{"contexts": a.required}) + case "/repos/acme/widget/rules/branches/main": + a.reads.Add(1) + enc([]any{}) + case "/repos/acme/widget/commits/head1/status": + a.reads.Add(1) + if a.headHTTP != 0 { + http.Error(w, `{"message":"boom"}`, a.headHTTP) + return + } + statuses := []map[string]any{} + for _, name := range a.statuses { + statuses = append(statuses, map[string]any{"context": name, "state": "success"}) + } + enc(map[string]any{"sha": "head1", "total_count": len(statuses), "statuses": statuses}) + case "/repos/acme/widget/commits/head1/check-runs": + a.reads.Add(1) + runs := []map[string]any{} + for i, name := range a.checks { + runs = append(runs, map[string]any{"id": i + 1, "name": name, "status": "completed", "conclusion": "success"}) + } + enc(map[string]any{"total_count": len(runs), "check_runs": runs}) + default: + t.Errorf("unexpected request: %s %s", r.Method, r.URL.String()) + http.Error(w, "unexpected", http.StatusTeapot) + } + })) + t.Cleanup(srv.Close) + c := NewClientForTest(srv.URL, "acme", []string{"widget"}, slog.New(slog.NewTextHandler(io.Discard, nil))) + m := NewMergeCreditor(c.client, func(string) bool { return false }, c.logger) + m.dcoRequired = override + return m +} + +// The unsigned-squash refusal applies only where the repo requires DCO: the +// dco.required override when set, else a dco/DCO context required on the +// base or reported on the head. +func TestSquashSignoffDCOGate(t *testing.T) { + ctx := context.Background() + unset := func(string) (bool, bool) { return false, false } + required := func(string) (bool, bool) { return true, true } + notRequired := func(string) (bool, bool) { return false, true } + squash := func(m *MergeCreditor, message string) (string, error) { + return m.SquashSignoff(ctx, "acme", "widget", 7, "main", "head1", "squash", message) + } + t.Run("no gate: an unsigned squash merges with the message as built", func(t *testing.T) { + api := &dcoGateAPI{commits: []string{"chore(deps): update widget"}, statuses: []string{"tide"}, checks: []string{"Tests Passed"}} + m := api.creditor(t, unset) + for _, message := range []string{"", "Co-authored-by: x "} { + got, err := squash(m, message) + if err != nil || got != message { + t.Fatalf("SquashSignoff(%q) = %q, %v; want the message unchanged", message, got, err) + } + } + }) + t.Run("no gate: the sign-offs that exist are still carried", func(t *testing.T) { + api := &dcoGateAPI{commits: []string{"Fix", "Review\n\nSigned-off-by: K "}} + m := api.creditor(t, unset) + got, err := squash(m, "Co-authored-by: x ") + if err != nil || got != "Co-authored-by: x \n\nSigned-off-by: K " { + t.Fatalf("SquashSignoff = %q, %v", got, err) + } + if n := api.reads.Load(); n != 0 { + t.Fatalf("a signed branch read the DCO gate %d times", n) + } + }) + t.Run("dco.required true refuses without reading the repo", func(t *testing.T) { + api := &dcoGateAPI{commits: []string{"Fix"}} + if _, err := squash(api.creditor(t, required), ""); !errors.Is(err, ErrSquashUnsigned) { + t.Fatalf("err = %v, want ErrSquashUnsigned", err) + } + if n := api.reads.Load(); n != 0 { + t.Fatalf("override read the DCO gate %d times", n) + } + }) + t.Run("dco.required false wins over a detected gate", func(t *testing.T) { + api := &dcoGateAPI{commits: []string{"Fix"}, required: []string{"DCO"}, statuses: []string{"dco"}, checks: []string{"DCO"}} + got, err := squash(api.creditor(t, notRequired), "") + if err != nil || got != "" { + t.Fatalf("SquashSignoff = %q, %v; want a merge", got, err) + } + if n := api.reads.Load(); n != 0 { + t.Fatalf("override read the DCO gate %d times", n) + } + }) + for _, tc := range []struct { + name string + api *dcoGateAPI + }{ + {name: "DCO required on the base refuses", api: &dcoGateAPI{commits: []string{"Fix"}, required: []string{"build", "DCO"}}}, + {name: "Prow's dco status on the head refuses", api: &dcoGateAPI{commits: []string{"Fix"}, statuses: []string{"tide", "dco"}}}, + {name: "the DCO App's check run on the head refuses", api: &dcoGateAPI{commits: []string{"Fix"}, required: []string{"build"}, checks: []string{"build", "DCO"}}}, + {name: "a head that cannot be read keeps the refusal", api: &dcoGateAPI{commits: []string{"Fix"}, headHTTP: http.StatusNotFound}}, + } { + t.Run(tc.name, func(t *testing.T) { + if _, err := squash(tc.api.creditor(t, unset), ""); !errors.Is(err, ErrSquashUnsigned) { + t.Fatalf("err = %v, want ErrSquashUnsigned", err) + } + }) + } +} diff --git a/src/pkg/github/pullrequest.go b/src/pkg/github/pullrequest.go index 10a17d48c9..d432d307b9 100644 --- a/src/pkg/github/pullrequest.go +++ b/src/pkg/github/pullrequest.go @@ -499,8 +499,13 @@ func (c *Client) MergePRVia(ctx context.Context, repo string, number int, mergeM c.NoteMergeCredit(owner+"/"+repo, number, credit) } // A squash carries the branch's Signed-off-by trailers into the landing - // commit, or does not happen (hivecommons/hive#18203). - commitMessage, err = c.MergeCreditor().SquashSignoff(ctx, owner, repo, number, mergeMethod, commitMessage) + // commit, or, in a repo that requires DCO, does not happen + // (hivecommons/hive#18203). + squashHead := expectSHA + if squashHead == "" { + squashHead = fetched.GetHead().GetSHA() + } + commitMessage, err = c.MergeCreditor().SquashSignoff(ctx, owner, repo, number, fetched.GetBase().GetRef(), squashHead, mergeMethod, commitMessage) if err != nil { return MergePRResult{}, fmt.Errorf("merging PR %s/%s#%d (%s): %w", owner, repo, number, mergeMethod, err) } From f53bf6b8d23ecbcc2989e0f4e24b2bc2896a9ff1 Mon Sep 17 00:00:00 2001 From: Danathar <6772335+Danathar@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:14:47 -0500 Subject: [PATCH 2/2] docs: re-cite RankActionableIssues for this branch's client.go lines This branch adds three lines to client.go above RankActionableIssues, so after the v6 merge it starts at 5600; scale-envelope.md now cites 5600-5610 and the docs citation check finds AgeMinutes again. Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 --- src/docs/scale-envelope.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/docs/scale-envelope.md b/src/docs/scale-envelope.md index 1f9da47b4e..c0ba535a7c 100644 --- a/src/docs/scale-envelope.md +++ b/src/docs/scale-envelope.md @@ -34,7 +34,7 @@ ones to: 2. **Truncation must preserve the prioritization signal, not cut an arbitrary slice.** This holds today and is easy to assume backwards: - Issues are sorted by actionable rank tier, then **oldest first** within each tier before any cap is applied - (`RankActionableIssues`, `src/pkg/github/client.go:5593-5603`, descending `AgeMinutes` within each tier). + (`RankActionableIssues`, `src/pkg/github/client.go:5600-5610`, descending `AgeMinutes` within each tier). - PRs are sorted by **review class then oldest-first within class** (`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`; fixes → refactors/docs → tests).