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-16345.md
Original file line number Diff line number Diff line change
@@ -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)).
2 changes: 1 addition & 1 deletion src/pkg/github/pr_duplicate_guard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/"):
Expand Down
3 changes: 2 additions & 1 deletion src/pkg/github/pr_status_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/github/review_evidence_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/"):
Expand Down
54 changes: 41 additions & 13 deletions src/pkg/prstatus/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -93,20 +100,21 @@ 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)
})
res.Edited = res.Edited || edited
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)
Expand All @@ -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 {
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/pkg/prstatus/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", " ")
Expand Down
Loading