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-16263-single-status-writer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- All hive:status comment writers go through one serialised writer with compare-and-retry and duplicate reconciliation, so concurrent writers no longer lose sections or create duplicate status comments (#16263).
73 changes: 51 additions & 22 deletions src/pkg/github/automerge/automerge_stage_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,41 +1476,70 @@ func regressionNotable(reason string) bool {
// as a comment posted. Returns the number of comments created.
func (c *Engine) postStageNote(ctx context.Context, policy StageLabelPolicy, obs stageObservation, key, body string) (int, error) {
if policy.QuietNotifications {
done, fresh, err := c.editStatusSection(ctx, obs.Owner, obs.Repo, obs.Number, key, body)
if err != nil || done {
return 0, err
}
body = fresh
return c.writeStatusSection(ctx, obs.Owner, obs.Repo, obs.Number, key, body)
}
if _, _, err := c.gh.Issues.CreateComment(ctx, obs.Owner, obs.Repo, obs.Number, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil {
return 0, err
}
return 1, nil
}

// editStatusSection rewrites section key of the PR's hive status comment
// (prstatus.Marker) to text when that comment exists, and reports done. When
// the PR has no status comment yet nothing is written: the caller creates
// one from the returned body, so the create stays a single call site.
func (c *Engine) editStatusSection(ctx context.Context, owner, repo string, number int, key, text string) (done bool, fresh string, err error) {
comments, err := c.listIssueComments(ctx, owner, repo, number)
// writeStatusSection rewrites section key of the PR's hive status comment
// to text through prstatus.Write, the one status writer
// the GitHub client shares: it is serialised per PR, the edit is retried
// when the comment changed underneath, and the comment is created only when
// the PR has none. Returns the number of comments created.
func (c *Engine) writeStatusSection(ctx context.Context, owner, repo string, number int, key, text string) (int, error) {
f := &engineStatusForge{e: c, owner: owner, repo: repo, number: number}
res, err := prstatus.Write(ctx, f, prstatus.ItemKey(owner+"/"+repo, number), key, text)
if res.Created {
return 1, err
}
return 0, err
}

// engineStatusForge adapts the engine's comment calls on one PR to
// prstatus.Forge.
type engineStatusForge struct {
e *Engine
owner string
repo string
number int
}

func (f *engineStatusForge) ListComments(ctx context.Context) ([]prstatus.Comment, error) {
comments, err := f.e.listIssueComments(ctx, f.owner, f.repo, f.number)
if err != nil {
return false, "", err
return nil, err
}
out := make([]prstatus.Comment, 0, len(comments))
for _, cm := range comments {
if cm == nil || !prstatus.IsStatusComment(cm.GetBody()) {
if cm == nil {
continue
}
next := prstatus.Upsert(cm.GetBody(), key, text)
if next == cm.GetBody() {
return true, "", nil
}
if _, _, err := c.gh.Issues.EditComment(ctx, owner, repo, cm.GetID(), &gh.IssueComment{Body: gh.Ptr(next)}); err != nil {
return true, "", fmt.Errorf("editing hive status comment on %s/%s#%d: %w", owner, repo, number, err)
}
return true, "", nil
own := strings.TrimSpace(cm.GetUser().GetLogin()) != "" && !f.e.isPersonLogin(cm.GetUser())
out = append(out, prstatus.Comment{ID: cm.GetID(), Body: cm.GetBody(), Own: own})
}
return out, nil
}

func (f *engineStatusForge) EditComment(ctx context.Context, id int64, body string) error {
if _, _, err := f.e.gh.Issues.EditComment(ctx, f.owner, f.repo, id, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil {
return fmt.Errorf("editing hive status comment on %s/%s#%d: %w", f.owner, f.repo, f.number, err)
}
return false, prstatus.Upsert("", key, text), nil
return nil
}

func (f *engineStatusForge) CreateComment(ctx context.Context, body string) error {
_, _, err := f.e.gh.Issues.CreateComment(ctx, f.owner, f.repo, f.number, &gh.IssueComment{Body: gh.Ptr(body)})
return err
}

func (f *engineStatusForge) DeleteComment(ctx context.Context, id int64) error {
if _, err := f.e.gh.Issues.DeleteComment(ctx, f.owner, f.repo, id); err != nil {
return fmt.Errorf("deleting hive status comment on %s/%s#%d: %w", f.owner, f.repo, f.number, err)
}
return nil
}

// ensureStageLabels provisions (or recolours) the family's labels in a repo
Expand Down
64 changes: 51 additions & 13 deletions src/pkg/github/pr_status_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,75 @@ package github

import (
"context"
"strings"
"fmt"

gh "github.com/google/go-github/v72/github"
"github.com/hivecommons/hive/pkg/prstatus"
)

// UpsertPRStatusSection rewrites section key of repo#number's single hive
// status comment (prstatus.Marker) to text, creating the comment only when
// status comment to text, creating the comment only when
// the PR has none. Editing a comment notifies nobody; creating one emails
// the author and every watcher, so this is the quiet-mode delivery for
// every note the hive used to post as its own comment. An empty text
// removes the section. Reports whether a comment was created.
//
// The write goes through prstatus.Write, the one status writer the
// auto-merge engine shares, so it is serialised per item, retried when the
// comment changed between read and edit, and duplicate status comments are
// merged into the oldest.
func (c *Client) UpsertPRStatusSection(ctx context.Context, repo string, number int, key, text string) (bool, error) {
if c == nil {
return false, ErrNoGitHubClient
}
current, err := c.UpsertIssueMarkerComment(ctx, repo, number, prstatus.Marker, "", false)
owner, repoName := c.splitRepo(repo)
f := &clientStatusForge{c: c, owner: owner, repo: repoName, number: number}
res, err := prstatus.Write(ctx, f, prstatus.ItemKey(owner+"/"+repoName, number), key, text)
return res.Created, err
}

// clientStatusForge adapts the client's comment calls on one item to
// prstatus.Forge. Creates go through CreateIssueComment (canary gate,
// effects journal, created tally); edits are tallied as edited.
type clientStatusForge struct {
c *Client
owner string
repo string
number int
}

func (f *clientStatusForge) ListComments(ctx context.Context) ([]prstatus.Comment, error) {
comments, err := f.c.listIssueComments(ctx, f.owner, f.repo, f.number)
if err != nil {
return false, err
return nil, err
}
if !current.Found && strings.TrimSpace(text) == "" {
return false, nil
out := make([]prstatus.Comment, 0, len(comments))
for _, cm := range comments {
if cm == nil {
continue
}
out = append(out, prstatus.Comment{ID: cm.GetID(), Body: cm.GetBody(), Own: f.c.hiveAuthoredLogin(cm.GetUser().GetLogin())})
}
next := prstatus.Upsert(current.Previous, key, text)
if current.Found && next == current.Previous {
return false, nil
return out, nil
}

func (f *clientStatusForge) EditComment(ctx context.Context, id int64, body string) error {
if _, _, err := f.c.client.Issues.EditComment(ctx, f.owner, f.repo, id, &gh.IssueComment{Body: gh.Ptr(body)}); err != nil {
return fmt.Errorf("editing hive status comment on %s/%s#%d: %w", f.owner, f.repo, f.number, err)
}
res, err := c.UpsertIssueMarkerComment(ctx, repo, number, prstatus.Marker, next, true)
if err != nil {
return false, err
f.c.noteCommentEdited()
return nil
}

func (f *clientStatusForge) CreateComment(ctx context.Context, body string) error {
return f.c.CreateIssueComment(ctx, f.owner+"/"+f.repo, f.number, body)
}

func (f *clientStatusForge) DeleteComment(ctx context.Context, id int64) error {
if _, err := f.c.client.Issues.DeleteComment(ctx, f.owner, f.repo, id); err != nil {
return fmt.Errorf("deleting hive status comment on %s/%s#%d: %w", f.owner, f.repo, f.number, err)
}
return res.Created, nil
return nil
}

// PostNoteOrFold delivers a hive note on repo#number. In quiet mode
Expand Down
47 changes: 47 additions & 0 deletions src/pkg/github/pr_status_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type statusCommentServer struct {
comments []map[string]any
creates int
edits int
deletes []string
}

func (s *statusCommentServer) start(t *testing.T) *httptest.Server {
Expand Down Expand Up @@ -52,6 +53,17 @@ func (s *statusCommentServer) start(t *testing.T) *httptest.Server {
}
}
_ = json.NewEncoder(w).Encode(map[string]any{"id": 1})
case r.Method == http.MethodDelete && strings.Contains(r.URL.Path, "/issues/comments/"):
id := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
s.deletes = append(s.deletes, id)
kept := s.comments[:0]
for _, cm := range s.comments {
if jsonNumber(cm["id"]) != id {
kept = append(kept, cm)
}
}
s.comments = kept
w.WriteHeader(http.StatusNoContent)
default:
w.WriteHeader(http.StatusNotFound)
}
Expand Down Expand Up @@ -107,6 +119,41 @@ func TestUpsertPRStatusSectionCreatesOnceThenEdits(t *testing.T) {
}
}

// A PR carrying two hive status comments (two writers both created one)
// is reconciled on the next write: every section lands in the oldest, the
// newer hive-authored copy is deleted, and the edit is tallied as an edit,
// not a created comment.
func TestUpsertPRStatusSectionMergesDuplicateStatusComments(t *testing.T) {
bot := map[string]any{"login": "hive-app[bot]", "type": "Bot"}
s := &statusCommentServer{comments: []map[string]any{
{"id": 11, "body": prstatus.Upsert("", prstatus.KeyStageStalled, "stalled"), "user": bot},
{"id": 12, "body": prstatus.Upsert("", prstatus.KeyStageRegressed, "regressed"), "user": bot},
}}
c := testClient(t, s.start(t).URL)
created, err := c.UpsertPRStatusSection(context.Background(), "o/r", 5, prstatus.KeyEvidence, "evidence")
if err != nil || created {
t.Fatalf("created=%v err=%v", created, err)
}
if s.creates != 0 || len(s.deletes) != 1 || s.deletes[0] != "12" || len(s.comments) != 1 {
t.Fatalf("creates=%d deletes=%v comments=%d, want the newer status comment deleted", s.creates, s.deletes, len(s.comments))
}
body, _ := s.comments[0]["body"].(string)
for _, key := range []string{prstatus.KeyStageStalled, prstatus.KeyStageRegressed, prstatus.KeyEvidence} {
n := 0
for _, sec := range prstatus.Parse(body) {
if sec.Key == key {
n++
}
}
if n != 1 {
t.Fatalf("section %q appears %d times in %q", key, n, body)
}
}
if created, edited, _ := c.NotificationCounts(); created != 0 || edited != 2 {
t.Fatalf("tally created=%d edited=%d, want 0 created and 2 edits", created, edited)
}
}

// Quiet mode: the duplicate-guard redirect lands in the status comment of
// the PR it points at and a retry edits it rather than stacking comments.
func TestCreatePR_DuplicateGuardQuietModeUsesStatusComment(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions src/pkg/prstatus/prstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// nothing. So instead of one comment per note (stage stalled, stage
// regressed, the trusted-human 071 note, the duplicate-PR redirect, …) the
// hive keeps a single comment tagged Marker and rewrites one keyed section
// of it in place. The package is pure string handling; the GitHub client
// and the auto-merge engine each own the list/edit/create calls around it.
// of it in place. Write (writer.go) is the one path every writer goes
// through: the GitHub client and the auto-merge engine each adapt their
// list/edit/create/delete calls to its Forge, and Write serialises them
// per item, retries an edit whose base changed underneath, and merges
// duplicate status comments into the oldest.
package prstatus

import (
Expand Down
Loading
Loading