diff --git a/changelog.d/fixed-16393-stage-note.md b/changelog.d/fixed-16393-stage-note.md new file mode 100644 index 0000000000..20b9d2f75e --- /dev/null +++ b/changelog.d/fixed-16393-stage-note.md @@ -0,0 +1 @@ +- Route normal-mode PR stage notes through the canary gate and effects journal used by other issue comments. diff --git a/src/docs/notifications.md b/src/docs/notifications.md index b6a086b54a..83134418a5 100644 --- a/src/docs/notifications.md +++ b/src/docs/notifications.md @@ -457,7 +457,7 @@ without a class. Review submissions and review requests are always counted as | `github.(*Client).PostNoteOrFold` | `status` | passthrough wrapper; attributed to its caller | | `github.(*Client).SweepLegacyFleetReportIssues` | `status` | | | `github.(*Client).UpsertIssueMarkerComment` | `status` | passthrough wrapper; attributed to its caller | -| `automerge.(*engineStatusForge).CreateComment` | `status` | passthrough wrapper (status-comment forge under `prstatus.Write`); attributed to its caller | +| `automerge.(*engineStatusForge).CreateComment` | `status` | passthrough wrapper shared by normal-mode stage notes and the quiet-mode status-comment forge under `prstatus.Write`; attributed to its caller | | `github.(*clientStatusForge).CreateComment` | `status` | passthrough wrapper (status-comment forge under `prstatus.Write`); attributed to its caller | | `github.(*Client).PostIssueNote` | `status` | passthrough wrapper (issue status card, `attention_pilot`); attributed to its caller | | `github.(*Client).closeDecomposedParent` | `status` | | diff --git a/src/pkg/github/automerge/automerge_stage_human_test.go b/src/pkg/github/automerge/automerge_stage_human_test.go index f5ae34e3a5..a886e33e7b 100644 --- a/src/pkg/github/automerge/automerge_stage_human_test.go +++ b/src/pkg/github/automerge/automerge_stage_human_test.go @@ -692,6 +692,12 @@ func (s *stageSupersededCloserSpy) StageReconciler(store *stagelabels.Store, opt return s.stage.StageReconciler(store, opts, logger) } +// Preserve the underlying transport's optional comment primitive as well +// as its stage reconciler when wrapping it to observe superseded closes. +func (s *stageSupersededCloserSpy) CreateIssueComment(ctx context.Context, repo string, number int, body string) error { + return s.Transport.(issueCommenter).CreateIssueComment(ctx, repo, number, body) +} + func newStageSupersededCloserSpy(t *testing.T, e *Engine, result hgithub.HiveSupersededClose) *stageSupersededCloserSpy { t.Helper() stage, ok := e.transport.(stageReconcilerTransport) diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index 7df7f9a741..ca50023273 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -1490,7 +1490,10 @@ func (c *Engine) postStageNoteWith(ctx context.Context, policy StageLabelPolicy, if policy.QuietNotifications { return c.writeStatusSectionWith(ctx, obs.Owner, obs.Repo, obs.Number, key, body, write) } - if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, obs.Number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil { + // Share the quiet-mode creation adapter so both modes use the same + // transport primitive and fail closed when it is unavailable. + f := &engineStatusForge{e: c, owner: obs.Owner, repo: obs.Repo, number: obs.Number} + if err := f.CreateComment(ctx, body); err != nil { return 0, err } return 1, nil diff --git a/src/pkg/github/automerge/automerge_stage_note_test.go b/src/pkg/github/automerge/automerge_stage_note_test.go new file mode 100644 index 0000000000..13cd678b4a --- /dev/null +++ b/src/pkg/github/automerge/automerge_stage_note_test.go @@ -0,0 +1,81 @@ +package automerge + +import ( + "context" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "sync/atomic" + "testing" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/prstatus" +) + +type stageNoteCommenter struct { + Transport + calls int + repo string + number int + body string + err error +} + +func (c *stageNoteCommenter) CreateIssueComment(_ context.Context, repo string, number int, body string) error { + c.calls++ + c.repo, c.number, c.body = repo, number, body + return c.err +} + +func TestNormalStageNoteUsesCommentPrimitive(t *testing.T) { + denied := errors.New("comment denied by write gate") + for _, mentioning := range []bool{false, true} { + for _, writeErr := range []error{nil, denied} { + t.Run(fmt.Sprintf("mentioning=%t/denied=%t", mentioning, writeErr != nil), func(t *testing.T) { + var rawWrites atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + rawWrites.Add(1) + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(`{}`)) + })) + defer srv.Close() + commenter := &stageNoteCommenter{err: writeErr} + client := gh.NewClient(nil) + client.BaseURL, _ = url.Parse(srv.URL + "/") + e := &Engine{transport: commenter, gh: client} + post := e.postStageNote + if mentioning { + post = e.postStageNoteMentioning + } + const body = "Stage regressed: @author please review" + n, err := post(context.Background(), StageLabelPolicy{}, stageObservation{Owner: "acme", Repo: "widget", Number: 42}, prstatus.KeyStageRegressed, body) + wantCount := 1 + if writeErr != nil { + wantCount = 0 + } + if !errors.Is(err, writeErr) || n != wantCount { + t.Fatalf("post = (%d, %v), want (%d, %v)", n, err, wantCount, writeErr) + } + if commenter.calls != 1 || commenter.repo != "acme/widget" || commenter.number != 42 || commenter.body != body { + t.Fatalf("comment primitive received %+v", commenter) + } + if rawWrites.Load() != 0 { + t.Fatalf("stage note bypassed primitive: %d raw writes", rawWrites.Load()) + } + }) + } + } +} + +// A transport without the gate must fail closed, rather than falling back to +// the engine's raw GitHub client (which is intentionally absent here). +func TestNormalStageNoteRequiresCommentPrimitive(t *testing.T) { + e := &Engine{transport: struct{ Transport }{}} + n, err := e.postStageNote(context.Background(), StageLabelPolicy{}, stageObservation{Owner: "acme", Repo: "widget", Number: 42}, prstatus.KeyStageRegressed, "regressed") + if n != 0 || err == nil || !strings.Contains(err.Error(), "transport has no comment primitive") { + t.Fatalf("post = (%d, %v), want missing-primitive error and no comment", n, err) + } +} diff --git a/src/pkg/writeback/testdata/direct-writes-baseline.txt b/src/pkg/writeback/testdata/direct-writes-baseline.txt index 08557c5b10..7b66f292e9 100644 --- a/src/pkg/writeback/testdata/direct-writes-baseline.txt +++ b/src/pkg/writeback/testdata/direct-writes-baseline.txt @@ -15,7 +15,7 @@ raw pkg/github/automerge/automerge_held_runs.go 3 raw pkg/github/automerge/automerge_merge_commit.go 1 raw pkg/github/automerge/automerge_merge_forward.go 1 raw pkg/github/automerge/automerge_stage_human.go 1 -raw pkg/github/automerge/automerge_stage_labels.go 4 +raw pkg/github/automerge/automerge_stage_labels.go 3 raw pkg/github/automerge/automerge_sweep_queued.go 3 raw pkg/github/automerge/automerge_sweep_trusted.go 4 raw pkg/github/automerge/automerge_unauthorized.go 3