diff --git a/changelog.d/fixed-16345.md b/changelog.d/fixed-16345.md new file mode 100644 index 0000000000..ca886e609e --- /dev/null +++ b/changelog.d/fixed-16345.md @@ -0,0 +1 @@ +- `prstatus.Write` now edits only hive-authored status comments: a status marker pasted by a person is merged from but left in place, and a new hive-owned comment is created when none exists ([#16345](https://github.com/hivecommons/hive/issues/16345)). diff --git a/src/pkg/github/pr_duplicate_guard_test.go b/src/pkg/github/pr_duplicate_guard_test.go index c1bb331f01..ab3fc17e24 100644 --- a/src/pkg/github/pr_duplicate_guard_test.go +++ b/src/pkg/github/pr_duplicate_guard_test.go @@ -162,7 +162,7 @@ func (g *guardServer) start(t *testing.T) *httptest.Server { case r.Method == "GET" && strings.HasSuffix(path, "/comments"): out := []map[string]any{} for i, body := range g.existingComments { - out = append(out, map[string]any{"id": i + 1, "body": body}) + out = append(out, map[string]any{"id": i + 1, "body": body, "user": map[string]any{"login": "hive-app[bot]", "type": "Bot"}}) } enc(w, out) case r.Method == "GET" && strings.Contains(path, "/issues/"): diff --git a/src/pkg/github/pr_status_comment_test.go b/src/pkg/github/pr_status_comment_test.go index 947a98d03e..3bccbbd9ef 100644 --- a/src/pkg/github/pr_status_comment_test.go +++ b/src/pkg/github/pr_status_comment_test.go @@ -39,7 +39,8 @@ func (s *statusCommentServer) start(t *testing.T) *httptest.Server { var body map[string]string _ = json.NewDecoder(r.Body).Decode(&body) s.creates++ - cm := map[string]any{"id": 100 + s.creates, "body": body["body"]} + // GitHub stamps the authenticated App as the comment's user. + cm := map[string]any{"id": 100 + s.creates, "body": body["body"], "user": map[string]any{"login": "hive-app[bot]", "type": "Bot"}} s.comments = append(s.comments, cm) w.WriteHeader(http.StatusCreated) _ = json.NewEncoder(w).Encode(cm) diff --git a/src/pkg/github/review_evidence_read_test.go b/src/pkg/github/review_evidence_read_test.go index 4794505f05..4bd06cb43b 100644 --- a/src/pkg/github/review_evidence_read_test.go +++ b/src/pkg/github/review_evidence_read_test.go @@ -255,7 +255,7 @@ func newEvidenceMergeServer(t *testing.T, f *evidenceMergeFixture) *httptest.Ser } out := []map[string]any{} for i, c := range f.comments { - out = append(out, map[string]any{"id": i + 1, "body": c}) + out = append(out, map[string]any{"id": i + 1, "body": c, "user": map[string]any{"login": "hive-app[bot]", "type": "Bot"}}) } _ = json.NewEncoder(w).Encode(out) case r.Method == http.MethodPatch && strings.HasPrefix(r.URL.Path, "/repos/o/r/issues/comments/"): diff --git a/src/pkg/prstatus/writer.go b/src/pkg/prstatus/writer.go index fc50450a1b..d525fc0266 100644 --- a/src/pkg/prstatus/writer.go +++ b/src/pkg/prstatus/writer.go @@ -65,8 +65,9 @@ var ( // computed from the section is recomputed on the fresh body, so a section // another writer (or another hive) added in between survives. After a // create, and whenever more than one status comment is found, the -// duplicates are reconciled: their sections are merged into the oldest and -// the newer hive-authored copies deleted. +// duplicates are reconciled: their sections are merged into the oldest +// hive-authored comment and the other hive-authored copies deleted. A +// comment the hive did not author is never edited or deleted. func Write(ctx context.Context, f Forge, item, key, text string) (WriteResult, error) { unlock := lockItem(item) defer unlock() @@ -75,11 +76,17 @@ func Write(ctx context.Context, f Forge, item, key, text string) (WriteResult, e if err != nil { return res, err } - if len(status) == 0 { + if len(ownStatus(status)) == 0 { if strings.TrimSpace(text) == "" { return res, nil } - if err := f.CreateComment(ctx, Upsert("", key, text)); err != nil { + // Foreign status comments are merged from but never edited, so a + // hive-owned comment is created to carry the merged sections. + bodies := make([]string, 0, len(status)) + for _, c := range status { + bodies = append(bodies, c.Body) + } + if err := f.CreateComment(ctx, Upsert(Merge(bodies...), key, text)); err != nil { return res, err } res.Created = true @@ -93,11 +100,12 @@ func Write(ctx context.Context, f Forge, item, key, text string) (WriteResult, e if status, err = listStatusComments(ctx, f); err != nil { return res, err } - if len(status) == 0 { - return res, ErrStatusCommentGone - } } - target := status[0] + own := ownStatus(status) + if len(own) == 0 { + return res, ErrStatusCommentGone + } + target := own[0] edited, err := editCompareAndRetry(ctx, f, target.ID, target.Body, func(cur string) string { return Upsert(cur, key, text) }) @@ -105,8 +113,8 @@ func Write(ctx context.Context, f Forge, item, key, text string) (WriteResult, e return res, err } -// Reconcile merges every status comment on the item into the oldest and -// deletes the newer hive-authored copies, under the same per-item lock as +// Reconcile merges every status comment on the item into the oldest +// hive-authored one and deletes the other hive-authored copies, under the same per-item lock as // Write. It is a no-op when the item has at most one status comment. func Reconcile(ctx context.Context, f Forge, item string) (WriteResult, error) { unlock := lockItem(item) @@ -121,9 +129,18 @@ func reconcile(ctx context.Context, f Forge, res *WriteResult) error { if err != nil || len(status) < 2 { return err } - oldest, newer := status[0], status[1:] - others := make([]string, 0, len(newer)) - for _, c := range newer { + own := ownStatus(status) + if len(own) == 0 { + return nil + } + oldest := own[0] + var newer []Comment + others := make([]string, 0, len(status)-1) + for _, c := range status { + if c.ID == oldest.ID { + continue + } + newer = append(newer, c) others = append(others, c.Body) } edited, err := editCompareAndRetry(ctx, f, oldest.ID, oldest.Body, func(cur string) string { @@ -145,6 +162,17 @@ func reconcile(ctx context.Context, f Forge, res *WriteResult) error { return nil } +// ownStatus returns the hive-authored comments of status, keeping order. +func ownStatus(status []Comment) []Comment { + var out []Comment + for _, c := range status { + if c.Own { + out = append(out, c) + } + } + return out +} + // editCompareAndRetry edits comment id to compute(body), where body is the // comment as last read. Immediately before each edit the comment is read // again; if it changed, compute runs on the fresh body and the edit is diff --git a/src/pkg/prstatus/writer_test.go b/src/pkg/prstatus/writer_test.go index e440e637e3..00abe0bbe9 100644 --- a/src/pkg/prstatus/writer_test.go +++ b/src/pkg/prstatus/writer_test.go @@ -312,6 +312,50 @@ func TestWriteNeverDeletesForeignStatusComment(t *testing.T) { } } +// A status marker a person pasted is never the edit target, even when it is +// the oldest status comment: the oldest hive-owned one is edited instead. +func TestWriteEditsOldestOwnedNotForeignStatusComment(t *testing.T) { + f := &fakeForge{} + foreignBody := Upsert("", "pasted", "by a person") + foreign := f.add(foreignBody, false) + own := f.add(Upsert("", "a", "a"), true) + res, err := Write(context.Background(), f, ItemKey("o/r", 9), "b", "b") + if err != nil || res.Created || !res.Edited || res.Deleted != 0 { + t.Fatalf("Write = %+v, %v", res, err) + } + for _, c := range f.comments { + switch c.ID { + case foreign: + if c.Body != foreignBody { + t.Fatalf("foreign comment was rewritten:\n%s", c.Body) + } + case own: + if sectionCount(c.Body, "b") != 1 || sectionCount(c.Body, "a") != 1 { + t.Fatalf("owned comment not updated:\n%s", c.Body) + } + } + } +} + +// With only a foreign status comment the writer creates its own, merging +// from the foreign one and leaving it in place. +func TestWriteCreatesOwnedCommentWhenOnlyForeignStatusExists(t *testing.T) { + f := &fakeForge{} + foreignBody := Upsert("", "pasted", "by a person") + f.add(foreignBody, false) + res, err := Write(context.Background(), f, ItemKey("o/r", 10), "b", "b") + if err != nil || !res.Created || res.Edited || res.Deleted != 0 { + t.Fatalf("Write = %+v, %v", res, err) + } + if len(f.comments) != 2 || f.comments[0].Body != foreignBody { + t.Fatalf("foreign comment must be left untouched: %+v", f.comments) + } + got := f.comments[1].Body + if !f.comments[1].Own || sectionCount(got, "b") != 1 || sectionCount(got, "pasted") != 1 { + t.Fatalf("new owned comment wrong: %+v", f.comments[1]) + } +} + func TestWriteEmptyTextWithoutCommentWritesNothing(t *testing.T) { f := &fakeForge{} res, err := Write(context.Background(), f, ItemKey("o/r", 8), "a", " ")