diff --git a/changelog.d/fixed-gfi-welcome-auto-merge-promise.md b/changelog.d/fixed-gfi-welcome-auto-merge-promise.md new file mode 100644 index 0000000000..800ed5f3ea --- /dev/null +++ b/changelog.d/fixed-gfi-welcome-auto-merge-promise.md @@ -0,0 +1 @@ +- The good-first-issue welcome comment and its paste-to-agent prompt no longer promise that the hive merges green contributor PRs automatically in a repository whose auto-merge is off (below L6, or `project.repo_policies[].auto_merge: false`); there they say the hive reviews the PR and a maintainer merges it. Repositories the hive does merge keep the same text. Custom `good_first_issue.comment_template` overrides can branch on the new `{{ .AutoMerge }}` variable. diff --git a/src/cmd/hive/gfi_wire.go b/src/cmd/hive/gfi_wire.go index 2e8791dfcb..d14378618d 100644 --- a/src/cmd/hive/gfi_wire.go +++ b/src/cmd/hive/gfi_wire.go @@ -106,6 +106,9 @@ func gfiSweepOptions(cfg *config.Config, dashSrv *dashboard.Server, logger *slog Links: gfi.Links{Origin: gfiPublicOrigin(cfg), HiveID: cfg.HiveID, DocsBase: gc.DocsURL}, Template: gc.CommentTemplate, Logger: logger, + // The invite promises an automatic merge only where the contributor + // lane would merge (repo_policies[].auto_merge, L6). + RepoAutoMerge: cfg.RepoAutoMergeEnabled, } if dashSrv != nil { opts.Audit = func(e gfi.Event) { diff --git a/src/cmd/hive/gfi_wire_test.go b/src/cmd/hive/gfi_wire_test.go index 6b5c6a5120..19e00caad7 100644 --- a/src/cmd/hive/gfi_wire_test.go +++ b/src/cmd/hive/gfi_wire_test.go @@ -44,6 +44,30 @@ func TestGFIPublicOrigin(t *testing.T) { } } +// gfiSweepOptions hands the sweep the config's per-repo auto-merge switch, +// so the welcome comment promises an automatic merge only where the +// contributor lane merges (projectbluefin/common#1688). +func TestGFISweepOptionsWireRepoAutoMerge(t *testing.T) { + cfg := gfiTestConfig() + level, off := 6, false + cfg.ACMMLevel = &level + cfg.Project.RepoPolicies = []config.RepoPolicy{{Repo: "acme/common", AutoMerge: &off}} + opts := gfiSweepOptions(cfg, nil, slog.New(slog.DiscardHandler)) + if opts.RepoAutoMerge == nil { + t.Fatal("RepoAutoMerge not wired") + } + if opts.RepoAutoMerge("acme/common") { + t.Error("acme/common has auto_merge: false; the invite must not promise an automatic merge") + } + if !opts.RepoAutoMerge("acme/widgets") { + t.Error("acme/widgets has no override at L6; the contributor lane merges there") + } + level = 5 + if gfiSweepOptions(cfg, nil, slog.New(slog.DiscardHandler)).RepoAutoMerge("acme/widgets") { + t.Error("below L6 no repository auto-merges") + } +} + func TestGFICandidatesProjection(t *testing.T) { now := time.Now() res := &github.ActionableResult{Issues: github.IssueResult{Items: []github.Issue{ diff --git a/src/docs/good-first-issue.md b/src/docs/good-first-issue.md index 2beb94d7e8..540f9f5bc7 100644 --- a/src/docs/good-first-issue.md +++ b/src/docs/good-first-issue.md @@ -53,7 +53,11 @@ the pitch, end to end: maintainer. 4. **Hive merges it.** CI runs, the hive review runs, and a green PR from a claimed contributor whose tier is sufficient (`require_tiers` or - `registry_tiers_sufficient`) merges on its own. + `registry_tiers_sufficient`) merges on its own. That needs the + repository's auto-merge on (`acmm_level` 6 and no + `project.repo_policies[].auto_merge: false`); where it is off the hive + still reviews, a maintainer merges, and the comment says so instead of + promising an automatic merge. The comment also says that hive may pick the issue up itself if nobody claims it, and that claiming first prevents that. Note the interplay with the @@ -160,10 +164,19 @@ Fix # in github.com// (). First open and If nobody claims it, hive may pick it up itself — claiming first prevents that. Docs: [contributor relay](/contributor-relay.md) · [contributor lane auto-merge](/hive-merge.md#contributor-lane-auto-merge) · [trust tiers](/contributor-trust-and-roles.md) · [AGENTS.md](). ```` +With the repository's auto-merge off, the heading reads **How to land it:**, +step 4 reads "Hive runs CI and reviews the PR; **a maintainer of this +repository merges it**.", the footer link is labelled *contributor lane*, and +the agent prompt ends "so the hive's contributor lane links it to your claim; +the hive reviews it and a maintainer merges it." The next selecting pass edits +an invite already posted, so turning a repository's auto-merge off or on +corrects its open invites after the next restart. + Template variables: `.Repo`, `.Number`, `.Title`, `.IssueURL`, `.ClaimURL`, `.RelayURL`, `.Docs`, `.Hive`, `.Reasons` / `.TopReasons` (first two), -`.AgentsMDURL` (empty when the repository has no `AGENTS.md`), `.AgentPrompt` -and `.Marker`. An override set in `good_first_issue.comment_template` must +`.AgentsMDURL` (empty when the repository has no `AGENTS.md`), `.AutoMerge` +(whether the hive merges this repository's contributor PRs itself; use +`{{ if .AutoMerge }}` to word an override), `.AgentPrompt` and `.Marker`. An override set in `good_first_issue.comment_template` must contain `{{ .Marker }}` (or the literal marker); otherwise the config is rejected, because without the marker the sweep could not find its own comment. diff --git a/src/pkg/gfi/automerge_test.go b/src/pkg/gfi/automerge_test.go new file mode 100644 index 0000000000..33e4d06c61 --- /dev/null +++ b/src/pkg/gfi/automerge_test.go @@ -0,0 +1,129 @@ +package gfi + +import ( + "context" + "strings" + "testing" +) + +// The welcome comment promises an automatic merge only where the hive's +// contributor lane merges: in a repository whose auto-merge is off a +// maintainer merges, so the invite says so (projectbluefin/common#1688). +func TestRenderCommentAutoMerge(t *testing.T) { + tmpl, err := ParseTemplate("") + if err != nil { + t.Fatal(err) + } + v := Vars{Candidate: good(), Links: Links{Origin: "https://hive.example", HiveID: "acme-hive"}, + Reasons: []string{"scoped to one package (pkg/paginate)"}, AutoMerge: true} + on, err := RenderComment(tmpl, v) + if err != nil { + t.Fatal(err) + } + // On: byte-identical to the template before the switch existed, i.e. + // the same text with each conditional spelled out as its true branch. + legacy := strings.NewReplacer( + "{{ if .AutoMerge }} (no waiting on a human){{ end }}", " (no waiting on a human)", + "{{ if .AutoMerge }}Hive runs CI and **merges green PRs from claimed contributors automatically**.{{ else }}Hive runs CI and reviews the PR; **a maintainer of this repository merges it**.{{ end }}", + "Hive runs CI and **merges green PRs from claimed contributors automatically**.", + "[contributor lane{{ if .AutoMerge }} auto-merge{{ end }}]", "[contributor lane auto-merge]", + ).Replace(DefaultTemplate) + if strings.Contains(legacy, "AutoMerge") { + t.Fatalf("legacy template still branches:\n%s", legacy) + } + legacyTmpl, err := ParseTemplate(legacy) + if err != nil { + t.Fatal(err) + } + want, err := RenderComment(legacyTmpl, v) + if err != nil { + t.Fatal(err) + } + if on != want { + t.Fatalf("auto-merge on changed the comment:\n got %q\nwant %q", on, want) + } + for _, s := range []string{"**How to land it (no waiting on a human):**", "4. Hive runs CI and **merges green PRs from claimed contributors automatically**."} { + if !strings.Contains(on, s) { + t.Errorf("auto-merge on: comment lacks %q", s) + } + } + if p := v.AgentPrompt(); !strings.HasSuffix(p, "so the hive's contributor lane can auto-merge it once CI is green.") { + t.Errorf("auto-merge on: agent prompt changed: %s", p) + } + + v.AutoMerge = false + off, err := RenderComment(tmpl, v) + if err != nil { + t.Fatal(err) + } + for _, s := range []string{"**How to land it:**", "4. Hive runs CI and reviews the PR; **a maintainer of this repository merges it**.", "Fixes #42", "[contributor lane](" + DefaultDocsBase + "/hive-merge.md#contributor-lane-auto-merge)"} { + if !strings.Contains(off, s) { + t.Errorf("auto-merge off: comment lacks %q:\n%s", s, off) + } + } + for _, s := range []string{"automatically", "no waiting on a human", "[contributor lane auto-merge]", "can auto-merge"} { + if strings.Contains(off, s) { + t.Errorf("auto-merge off: comment still promises %q:\n%s", s, off) + } + } + p := v.AgentPrompt() + if strings.Contains(p, "auto-merge") || !strings.Contains(p, "links it to your claim; the hive reviews it and a maintainer merges it.") || !strings.Contains(p, "'Fixes #42'") { + t.Errorf("auto-merge off: agent prompt = %s", p) + } +} + +func TestCustomTemplateCanBranchOnAutoMerge(t *testing.T) { + tmpl, err := ParseTemplate(Marker + "\n{{ if .AutoMerge }}merged for you{{ else }}a maintainer merges{{ end }}") + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + on bool + want string + }{{true, "merged for you"}, {false, "a maintainer merges"}} { + out, err := RenderComment(tmpl, Vars{Candidate: good(), AutoMerge: tc.on}) + if err != nil || !strings.Contains(out, tc.want) { + t.Fatalf("AutoMerge=%v: %q, %v", tc.on, out, err) + } + } +} + +// The sweep asks per repository, keeps the promise when nothing is wired, +// and corrects an invite it already posted on the next selecting pass. +func TestSweepRendersPerRepoAutoMerge(t *testing.T) { + forge := newFakeForge() + off := good() + on := good() + on.Repo = "acme/gadgets" + opts := quietOpts() + opts.RepoAutoMerge = func(repo string) bool { return repo != off.Repo } + if res := NewSweeper().Sweep(context.Background(), forge, []Candidate{off, on}, opts); len(res.Selected) != 2 { + t.Fatalf("result %+v", res) + } + if body := forge.comments[off.Key()][0]; strings.Contains(body, "automatically") || !strings.Contains(body, "a maintainer of this repository merges it") { + t.Fatalf("auto-merge-off repo: %s", body) + } + if body := forge.comments[on.Key()][0]; !strings.Contains(body, "merges green PRs from claimed contributors automatically") { + t.Fatalf("auto-merge-on repo: %s", body) + } + + // Unwired (nil) keeps today's promise. + plain := newFakeForge() + NewSweeper().Sweep(context.Background(), plain, []Candidate{off}, quietOpts()) + if body := plain.comments[off.Key()][0]; !strings.Contains(body, "automatically") { + t.Fatalf("nil RepoAutoMerge: %s", body) + } + + // An invite posted with the old promise is edited in place by the next + // process's pass once the repo's auto-merge is off. + labelled := off + labelled.Labels = append(labelled.Labels, Label, HelpWantedLabel) + opts.RepoAutoMerge = func(string) bool { return false } + NewSweeper().Sweep(context.Background(), plain, []Candidate{labelled}, opts) + if n := plain.commentCount(off.Repo, off.Number); n != 1 { + t.Fatalf("comments = %d, want the one edited in place", n) + } + if body := plain.comments[off.Key()][0]; strings.Contains(body, "automatically") { + t.Fatalf("invite not corrected: %s", body) + } +} diff --git a/src/pkg/gfi/comment.go b/src/pkg/gfi/comment.go index a0d4f6d02c..e654f2ebef 100644 --- a/src/pkg/gfi/comment.go +++ b/src/pkg/gfi/comment.go @@ -17,6 +17,11 @@ type Vars struct { Reasons []string // AgentsMDURL is the repository's AGENTS.md when it has one. AgentsMDURL string + // AutoMerge is whether the hive merges this repository's contributor + // PRs itself. False where its auto-merge is off (below L6, or + // project.repo_policies[].auto_merge: false): there a maintainer + // merges, so the comment must not promise an automatic merge. + AutoMerge bool } // Marker returns the comment marker so a template can place it. @@ -60,7 +65,11 @@ func (v Vars) AgentPrompt() string { fmt.Fprintf(&b, "Fix %s in github.com/%s (%s). First open %s and claim the issue through the hive relay (sign in with GitHub). ", "#"+strconv.Itoa(v.Number), v.Repo, v.IssueURL(), v.ClaimURL()) b.WriteString("Work on a fork/branch; keep the change scoped to the issue; sign every commit with DCO (git commit -s); ") - fmt.Fprintf(&b, "open a PR whose description contains the line 'Fixes #%d' so the hive's contributor lane can auto-merge it once CI is green.", v.Number) + if v.AutoMerge { + fmt.Fprintf(&b, "open a PR whose description contains the line 'Fixes #%d' so the hive's contributor lane can auto-merge it once CI is green.", v.Number) + } else { + fmt.Fprintf(&b, "open a PR whose description contains the line 'Fixes #%d' so the hive's contributor lane links it to your claim; the hive reviews it and a maintainer merges it.", v.Number) + } if v.AgentsMDURL != "" { fmt.Fprintf(&b, " Read and follow %s before changing code.", v.AgentsMDURL) } @@ -77,12 +86,12 @@ Hi! This repository is maintained with [hive]({{ .Docs }}/README.md) and welcome {{ range .TopReasons }} - {{ . }}{{ end }} -**How to land it (no waiting on a human):** +**How to land it{{ if .AutoMerge }} (no waiting on a human){{ end }}:** 1. **Claim it through the relay:** open {{ .ClaimURL }} and sign in with GitHub. Claiming stamps the issue so hive leaves it to you. 2. **Fork or branch**, make the change with your agent, and sign each commit with DCO (` + "`git commit -s`" + `). 3. **Open a PR whose description says ` + "`Fixes #{{ .Number }}`" + `** — that closing keyword is how hive's contributor lane links the PR to your claim. -4. Hive runs CI and **merges green PRs from claimed contributors automatically**. +4. {{ if .AutoMerge }}Hive runs CI and **merges green PRs from claimed contributors automatically**.{{ else }}Hive runs CI and reviews the PR; **a maintainer of this repository merges it**.{{ end }}
Paste this to your agent @@ -94,7 +103,7 @@ Hi! This repository is maintained with [hive]({{ .Docs }}/README.md) and welcome Prefer not to drive the loop by hand? Run your own hive (we call them clankers) — a relay on your machine that claims, opens the PR and handles review fixes for you, and earns you a contributor profile and a leaderboard place: see {{ .RelayURL }}. -If nobody claims it, hive may pick it up itself — claiming first prevents that. Docs: [contributor relay]({{ .Docs }}/contributor-relay.md) · [contributor lane auto-merge]({{ .Docs }}/hive-merge.md#contributor-lane-auto-merge) · [trust tiers]({{ .Docs }}/contributor-trust-and-roles.md){{ if .AgentsMDURL }} · [AGENTS.md]({{ .AgentsMDURL }}){{ end }}. +If nobody claims it, hive may pick it up itself — claiming first prevents that. Docs: [contributor relay]({{ .Docs }}/contributor-relay.md) · [contributor lane{{ if .AutoMerge }} auto-merge{{ end }}]({{ .Docs }}/hive-merge.md#contributor-lane-auto-merge) · [trust tiers]({{ .Docs }}/contributor-trust-and-roles.md){{ if .AgentsMDURL }} · [AGENTS.md]({{ .AgentsMDURL }}){{ end }}. ` // ParseTemplate parses an operator override, or DefaultTemplate when text is diff --git a/src/pkg/gfi/gfi.go b/src/pkg/gfi/gfi.go index a75440a015..c7de405211 100644 --- a/src/pkg/gfi/gfi.go +++ b/src/pkg/gfi/gfi.go @@ -607,6 +607,10 @@ type SweepOptions struct { // AgentsMD, when set, returns the URL of the repository's AGENTS.md (or // "" when it has none); the template points agents at it. AgentsMD func(ctx context.Context, repo string) string + // RepoAutoMerge, when set, reports whether the hive auto-merges the + // repository's contributor PRs; the welcome comment promises an + // automatic merge only where it does. Unset keeps that promise. + RepoAutoMerge func(repo string) bool // Audit, when set, receives one event per label applied or released. Audit func(Event) } @@ -758,8 +762,12 @@ func (s *Sweeper) Sweep(ctx context.Context, forge Forge, candidates []Candidate if opts.AgentsMD != nil { agentsMD = opts.AgentsMD(ctx, repo) } + autoMerge := true + if opts.RepoAutoMerge != nil { + autoMerge = opts.RepoAutoMerge(repo) + } body, err := RenderComment(tmpl, Vars{ - Candidate: sc.c, Links: opts.Links, Reasons: sc.d.Reasons, AgentsMDURL: agentsMD, + Candidate: sc.c, Links: opts.Links, Reasons: sc.d.Reasons, AgentsMDURL: agentsMD, AutoMerge: autoMerge, }) if err != nil { logger.Warn("gfi: rendering comment failed", "issue", sc.c.Key(), "error", err)