Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-squash-unsigned-dco-gate.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions src/cmd/hive/main_sweeps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions src/docs/labels-and-control-signals/stage-labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,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
Expand Down
17 changes: 12 additions & 5 deletions src/pkg/github/automerge/automerge_merge_credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,16 +38,22 @@ 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
}
return message, opts, ""
}

// 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"
45 changes: 45 additions & 0 deletions src/pkg/github/automerge/automerge_unsigned_squash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
})
}
}
3 changes: 3 additions & 0 deletions src/pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,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
Expand Down
21 changes: 20 additions & 1 deletion src/pkg/github/merge_credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
122 changes: 116 additions & 6 deletions src/pkg/github/merge_signoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"errors"
"fmt"
"log/slog"
"regexp"
"strings"
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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:
Expand Down
Loading
Loading