diff --git a/changelog.d/changed-18452-automerge-note-status-card.md b/changelog.d/changed-18452-automerge-note-status-card.md new file mode 100644 index 0000000000..903a148d9c --- /dev/null +++ b/changelog.d/changed-18452-automerge-note-status-card.md @@ -0,0 +1 @@ +- Notifications: in `github.notifications.mode: quiet` (the default) the auto-merge note ("merged by Hive trusted-author auto-merge …" / "merged by Hive on behalf of …") is now the `auto-merge` section of the PR's `hive:status` card, edited in place with its @-mentions neutralised, instead of a fresh comment on every merged PR (about 32 notifying comments a day on hivecommons/hive). `mode: normal` keeps the standalone comment, and a merge that waits on a person still gets its own 085 / 090 entry comment (hivecommons/hive#18452). diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 53427fe798..100db6170e 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -760,8 +760,9 @@ merges use merge commits; other PRs squash). It refuses excluded labels (`hold`, `do-not-merge`, and `needs-human` by default), existing hold/exempt labels, fork PRs whose author is not a repo member, and any PR with an outstanding `CHANGES_REQUESTED` review. Each merge records the tier as -`trusted-author` and leaves an audit comment naming the author role, GitHub -permission, and green head SHA. +`trusted-author` and records an audit note naming the author role, GitHub +permission, and green head SHA: an `auto-merge` section of the PR's `hive:status` +card in `quiet` notification mode (the default), a standalone comment in `normal`. ```yaml auto_merge: diff --git a/src/docs/notifications.md b/src/docs/notifications.md index b8eef9e64f..722270d30c 100644 --- a/src/docs/notifications.md +++ b/src/docs/notifications.md @@ -575,7 +575,10 @@ github: ``` Quiet mode (`github.notifications.mode: quiet`) folds the hive's PR -bookkeeping into one edited `hive:status` comment but leaves issue notes as +bookkeeping into one edited `hive:status` comment (including the auto-merge +note, under the `auto-merge` section: in `normal` mode it stays its own +comment, and a merge blocked on a person still gets its 085 / 090 entry +comment, hivecommons/hive#18452) but leaves issue notes as comments of their own, so one issue lifecycle (partial merge, remainder, close) could still email its watchers several times. With the pilot on, the issue gets the same card, written through the same single status writer diff --git a/src/docs/operator-reference/sweeps-and-operations.md b/src/docs/operator-reference/sweeps-and-operations.md index e60fabb31f..212906fa32 100644 --- a/src/docs/operator-reference/sweeps-and-operations.md +++ b/src/docs/operator-reference/sweeps-and-operations.md @@ -548,7 +548,13 @@ not what it records: stage-regressed notes, the trusted-human 071 note and the duplicate-PR redirect are sections of a single comment marked ``. The comment is created the first time a note is due and PATCHed for every - later note; only the creation sends email. + later note; only the creation sends email. The auto-merge note ("merged by + Hive trusted-author auto-merge …" or "merged by Hive on behalf of …") is + the card's `auto-merge` section too, with its @-mentions neutralised, so + a merge no longer emails the author and every watcher; in `normal` it is + still its own comment. A merge that waits on a person (the 085 + needs-decision and 090 ready-for-human-merge entry comments) still posts + a comment of its own. - **No "no findings" reviews.** A reviewer whose whole body is the clean acknowledgement — or whose body says it has no findings while its verdict report approves with an empty findings list, as the plain `reviewer` diff --git a/src/pkg/github/automerge/automerge_merge_note_status_test.go b/src/pkg/github/automerge/automerge_merge_note_status_test.go new file mode 100644 index 0000000000..1ef744e8c9 --- /dev/null +++ b/src/pkg/github/automerge/automerge_merge_note_status_test.go @@ -0,0 +1,146 @@ +package automerge + +// Tests for the auto-merge note's delivery (hivecommons/hive#18452): in +// github.notifications.mode: quiet it is a section of the PR's hive:status +// card, edited in place; in mode normal it stays its own comment; and a +// merge that waits on a person still gets its own notifying comment. + +import ( + "context" + "net/http" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/prstatus" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +func quietStagePolicy(quiet bool) func() StageLabelPolicy { + return func() StageLabelPolicy { return StageLabelPolicy{QuietNotifications: quiet} } +} + +func runTrustedMerge(t *testing.T, fx *trustedSweepFixture, opts Options) AutoMergeSweepEvent { + t.Helper() + api := newTrustedSweepAPI(t, fx) + t.Cleanup(api.Close) + policy := TrustedAuthorPolicy{Enabled: true} + engine := newTrustedSweepEngine(api.URL, policy, opts) + event, reason, err := engine.trySweepTrustedAuthorPR(context.Background(), "widget", "acme", "widget", 7, policy) + if err != nil || reason != "" { + t.Fatalf("reason=%q err=%v, want a merge", reason, err) + } + if fx.merges != 1 || event.MergeSHA != "merge7" { + t.Fatalf("merges=%d event=%+v, want one merge", fx.merges, event) + } + return event +} + +func TestAutoMergeNoteQuietCreatesStatusCard(t *testing.T) { + fx := trustedSweepFixture{} + runTrustedMerge(t, &fx, Options{StageLabelPolicy: quietStagePolicy(true)}) + if fx.comments != 1 { + t.Fatalf("comments=%d, want the one status card created", fx.comments) + } + if !prstatus.IsStatusComment(fx.lastComment) { + t.Fatalf("quiet note must be the hive:status card, got %q", fx.lastComment) + } + section, ok := prstatus.Get(fx.lastComment, prstatus.KeyAutoMerge) + if !ok || !strings.Contains(section, "merged by Hive trusted-author auto-merge") || !strings.Contains(section, "CI green at sha7") { + t.Fatalf("auto-merge section = %q (found %v)", section, ok) + } + if strings.Contains(section, "@alice") { + t.Fatalf("folded note must not mention the author live: %q", section) + } +} + +func TestAutoMergeNoteQuietEditsExistingCardInPlace(t *testing.T) { + existing := prstatus.Upsert("", prstatus.KeyStageStalled, " waiting") + fx := trustedSweepFixture{issueComments: []map[string]any{ + {"id": 501, "body": "a person's comment", "user": map[string]string{"login": "bob"}}, + {"id": 502, "body": existing, "user": map[string]string{"login": testHiveAppBotLogin}}, + }} + runTrustedMerge(t, &fx, Options{StageLabelPolicy: quietStagePolicy(true)}) + if fx.comments != 0 || fx.edits != 1 { + t.Fatalf("comments=%d edits=%d, want the card edited in place and no new comment", fx.comments, fx.edits) + } + if _, ok := prstatus.Get(fx.lastEdit, prstatus.KeyStageStalled); !ok { + t.Fatalf("existing section lost: %q", fx.lastEdit) + } + if section, ok := prstatus.Get(fx.lastEdit, prstatus.KeyAutoMerge); !ok || !strings.Contains(section, "merged by Hive trusted-author auto-merge") { + t.Fatalf("auto-merge section = %q (found %v)", section, ok) + } +} + +func TestAutoMergeNoteQuietOperatorApprovalFoldsWithoutMention(t *testing.T) { + fx := trustedSweepFixture{author: "mallory"} + api := newTrustedSweepAPI(t, &fx) + defer api.Close() + policy := TrustedAuthorPolicy{Enabled: true, RequireRole: "merger"} + store := stagelabels.NewStore("", nil) + key := stageKey("acme", "widget", 7) + store.Observe(key, stagelabels.ReadyForHumanMerge, "untrusted-author-role") + if !store.NoteAdmission(key, "sha7", stagelabels.OperatorApprovalAdmission("clubanderson"), time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC)) { + t.Fatal("NoteAdmission refused the stage record") + } + engine := newTrustedSweepEngine(api.URL, policy, Options{StageStore: store, StageLabelPolicy: quietStagePolicy(true)}) + if _, reason, err := engine.trySweepTrustedAuthorPR(context.Background(), "widget", "acme", "widget", 7, policy); err != nil || reason != "" || fx.merges != 1 { + t.Fatalf("reason=%q err=%v merges=%d, want a merge", reason, err, fx.merges) + } + section, ok := prstatus.Get(fx.lastComment, prstatus.KeyAutoMerge) + if fx.comments != 1 || !ok || !strings.Contains(section, "on behalf of") || !strings.Contains(section, "clubanderson") { + t.Fatalf("comments=%d section=%q", fx.comments, section) + } + if strings.Contains(section, "@clubanderson") || strings.Contains(section, "@mallory") { + t.Fatalf("folded operator note must not mention anyone live: %q", section) + } +} + +func TestAutoMergeNoteNormalModeKeepsStandaloneComment(t *testing.T) { + fx := trustedSweepFixture{} + runTrustedMerge(t, &fx, Options{StageLabelPolicy: quietStagePolicy(false)}) + if fx.comments != 1 || fx.edits != 0 { + t.Fatalf("comments=%d edits=%d, want one standalone comment", fx.comments, fx.edits) + } + if prstatus.IsStatusComment(fx.lastComment) || !strings.HasPrefix(fx.lastComment, "merged by Hive trusted-author auto-merge: author @alice") { + t.Fatalf("normal-mode note changed: %q", fx.lastComment) + } +} + +func TestAutoMergeNoteQuietWriteFailureDoesNotFailMerge(t *testing.T) { + fx := trustedSweepFixture{listCommentsStatus: http.StatusInternalServerError} + runTrustedMerge(t, &fx, Options{StageLabelPolicy: quietStagePolicy(true)}) + if fx.comments != 0 || fx.edits != 0 { + t.Fatalf("comments=%d edits=%d, want no write when the card cannot be read", fx.comments, fx.edits) + } +} + +// TestNeedsHumanEntryStillCommentsInQuietMode: folding the merge note must +// not fold the notice a person acts on. A PR the hive cannot merge without +// a person (085 needs-decision) still gets its own notifying comment in +// quiet mode. +func TestNeedsHumanEntryStillCommentsInQuietMode(t *testing.T) { + api, closeAPI := seededStageAPI(t) + defer closeAPI() + clock := &contributorLaneClock{at: time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC)} + verdict := &ContributorLaneVerdict{Configured: true, Verdict: "requires_human"} + e, store := humanStageEngine(t, api.url, clock, verdict, 0) + e.stageLabelPolicy = func() StageLabelPolicy { return StageLabelPolicy{Enabled: true, QuietNotifications: true} } + api.reset() + pr := stagePR(13, "role/contributor", "author/clanker", "stage/050-review-pending") + pr.Body = strPtr("Fixes the widget.\n\n— hive: backend=pi model=gpt-6\n") + e.recordStage(stageLaneContributor, "acme/widget", "acme", "widget", pr, contributorLaneReasonNoVerdict, false, nil) + e.noteStageVerdict("acme", "widget", 13, *verdict) + e.noteStageRole("acme", "widget", 13, stagelabels.RoleContributor) + if _, err := e.ApplyStageLabels(context.Background()); err != nil { + t.Fatal(err) + } + if got := stageOf(t, store, 13); got != stagelabels.NeedsDecision { + t.Fatalf("stage=%v want 085", got) + } + if len(api.comments) != 1 || !strings.HasPrefix(api.comments[0], stagelabels.DecisionCommentMarker) || prstatus.IsStatusComment(api.comments[0]) { + t.Fatalf("needs-human entry must stay its own comment in quiet mode: %q", api.comments) + } +} + +func strPtr(s string) *string { return &s } diff --git a/src/pkg/github/automerge/automerge_sweep_trusted.go b/src/pkg/github/automerge/automerge_sweep_trusted.go index 45b2146d6f..435f59f2ec 100644 --- a/src/pkg/github/automerge/automerge_sweep_trusted.go +++ b/src/pkg/github/automerge/automerge_sweep_trusted.go @@ -12,6 +12,7 @@ import ( gh "github.com/google/go-github/v72/github" "github.com/hivecommons/hive/pkg/effects" hgithub "github.com/hivecommons/hive/pkg/github" + "github.com/hivecommons/hive/pkg/prstatus" "github.com/hivecommons/hive/pkg/stageengine/prstage" "github.com/hivecommons/hive/pkg/stagelabels" ) @@ -1018,6 +1019,23 @@ func (c *Engine) commentTrustedAuthorMerge(ctx context.Context, owner, repo stri if login := strings.TrimPrefix(role, trustedAuthorRoleOperatorApprovedPrefix); login != role { body = fmt.Sprintf("merged by Hive on behalf of @%s, who approved this head (%s) with \"Approve and let hive merge\": author @%s holds no trusted-author role; CI green at %s.", login, stagelabels.OperatorApprovalAdmission(login), author, sha) } + return c.postAutoMergeNote(ctx, owner, repo, number, body) +} + +// postAutoMergeNote records that the hive merged the PR. Nobody has to act +// on it, so in github.notifications.mode: quiet (the default) it becomes the +// auto-merge section of the PR's hive:status card, edited in place: an edit +// emails nobody, where a fresh comment on every merge emailed the author and +// every watcher (~32 a day, hivecommons/hive#18452). prstatus.Write +// neutralises the @logins in it, so the card never notifies either. Mode +// normal keeps today's standalone comment. A merge that waits on a person +// is not this note: the 085 / 090 entry comments still post as their own +// comment. +func (c *Engine) postAutoMergeNote(ctx context.Context, owner, repo string, number int, body string) error { + if c.stagePolicy().QuietNotifications { + _, err := c.writeStatusSectionWith(ctx, owner, repo, number, prstatus.KeyAutoMerge, body, prstatus.Write) + return err + } _, _, err := c.gh.Issues.CreateComment(ctx, owner, repo, number, &gh.IssueComment{Body: gh.Ptr(body)}) return err } diff --git a/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go b/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go index 2dcdc8d301..dcd76aea42 100644 --- a/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go +++ b/src/pkg/github/automerge/automerge_sweep_trusted_author_test.go @@ -7,6 +7,7 @@ package automerge import ( "context" "encoding/json" + "fmt" "net/http" "net/http/httptest" "strings" @@ -40,6 +41,12 @@ type trustedSweepFixture struct { updateStatus int labels []string title string + // issueComments is the PR's comment thread, as GET .../comments serves + // it; a POST appends to it. listCommentsStatus fails that GET. + issueComments []map[string]any + listCommentsStatus int + edits int + lastEdit string } func newTrustedSweepAPI(t *testing.T, fx *trustedSweepFixture) *httptest.Server { @@ -140,6 +147,30 @@ func newTrustedSweepAPI(t *testing.T, fx *trustedSweepFixture) *httptest.Server w.WriteHeader(fx.updateStatus) } json.NewEncoder(w).Encode(map[string]any{"message": "Updating pull request branch."}) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/issues/7/comments": + if fx.listCommentsStatus != 0 { + http.Error(w, "list comments error", fx.listCommentsStatus) + return + } + out := fx.issueComments + if out == nil { + out = []map[string]any{} + } + json.NewEncoder(w).Encode(out) + case r.Method == http.MethodPatch && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/issues/comments/"): + fx.edits++ + var comment struct { + Body string `json:"body"` + } + _ = json.NewDecoder(r.Body).Decode(&comment) + fx.lastEdit = comment.Body + id := strings.TrimPrefix(r.URL.Path, "/repos/acme/widget/issues/comments/") + for _, c := range fx.issueComments { + if fmt.Sprint(c["id"]) == id { + c["body"] = comment.Body + } + } + json.NewEncoder(w).Encode(map[string]any{"id": id, "body": comment.Body}) case r.Method == http.MethodPost && r.URL.Path == "/repos/acme/widget/issues/7/comments": fx.comments++ var comment struct { @@ -151,7 +182,9 @@ func newTrustedSweepAPI(t *testing.T, fx *trustedSweepFixture) *httptest.Server http.Error(w, "comment error", fx.commentStatus) return } - json.NewEncoder(w).Encode(map[string]any{"id": 1}) + id := 1000 + len(fx.issueComments) + fx.issueComments = append(fx.issueComments, map[string]any{"id": id, "body": comment.Body, "user": map[string]string{"login": testHiveAppBotLogin}}) + json.NewEncoder(w).Encode(map[string]any{"id": id}) default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) } diff --git a/src/pkg/prstatus/prstatus.go b/src/pkg/prstatus/prstatus.go index f2e35cf5e3..166adb6e3b 100644 --- a/src/pkg/prstatus/prstatus.go +++ b/src/pkg/prstatus/prstatus.go @@ -169,4 +169,8 @@ const ( // `hive/awaiting-ci:` deferral, a stage/045 parking note, an // "Already fixed" verdict) that used to be its own comment. KeyAgentNotePrefix = "agent-note:" + // KeyAutoMerge records that the hive merged the PR (the trusted-author + // or operator-approved auto-merge note, "merged by Hive …"), which used + // to be a fresh comment on every merge (hivecommons/hive#18452). + KeyAutoMerge = "auto-merge" )