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-16393-stage-note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Route normal-mode PR stage notes through the canary gate and effects journal used by other issue comments.
2 changes: 1 addition & 1 deletion src/docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` | |
Expand Down
6 changes: 6 additions & 0 deletions src/pkg/github/automerge/automerge_stage_human_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/pkg/github/automerge/automerge_stage_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions src/pkg/github/automerge/automerge_stage_note_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion src/pkg/writeback/testdata/direct-writes-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading