diff --git a/changelog.d/fixed-15689-already-landed-issue-note-after-close.md b/changelog.d/fixed-15689-already-landed-issue-note-after-close.md new file mode 100644 index 0000000000..34a7f81624 --- /dev/null +++ b/changelog.d/fixed-15689-already-landed-issue-note-after-close.md @@ -0,0 +1 @@ +- Already-landed auto-close now posts its "this issue is done" note only after the issue close goes through, so an issue that CloseIssue's tracking-issue or reporter-confirmation gate keeps open no longer carries a misleading note ([#15689](https://github.com/hivecommons/hive/issues/15689)) diff --git a/src/pkg/github/pr_empty_diff_hive_autoclose.go b/src/pkg/github/pr_empty_diff_hive_autoclose.go index 0b0db89aff..fa8264ba28 100644 --- a/src/pkg/github/pr_empty_diff_hive_autoclose.go +++ b/src/pkg/github/pr_empty_diff_hive_autoclose.go @@ -377,8 +377,10 @@ func (c *Client) closeHiveRedundantPR(ctx context.Context, repo string, pr *gh.P // closeIssuesAlreadyLanded closes the issues pr's closing references name // once the PR itself is closed as already landed: the work is on the base, // so the issue gets the usual close (CloseIssue's tracking-issue and -// reporter-confirmation gates included) with a note naming the PR that -// landed it. Failures are logged, never fatal: the PR close stands. +// reporter-confirmation gates included), then a note naming the PR that +// landed it — only once the close went through, so a refused close leaves +// no "done" note on an open issue. Failures are logged, never fatal: the +// PR close stands. func (c *Client) closeIssuesAlreadyLanded(ctx context.Context, owner, name string, pr *gh.PullRequest, redundantOf int) []int { repo := owner + "/" + name var closed []int @@ -391,13 +393,8 @@ func (c *Client) closeIssuesAlreadyLanded(ctx context.Context, owner, name strin if d := c.WritebackAllowed(repo, item, writeback.KindIssueClose, "close issue already landed"); !d.Allowed { continue } - note := alreadyLandedIssueNote(pr.GetNumber(), redundantOf, strings.TrimSpace(pr.GetBase().GetRef())) - if _, _, err := c.client.Issues.CreateComment(ctx, owner, name, number, &gh.IssueComment{Body: gh.Ptr(note)}); err != nil { // writeback:gated - if c.logger != nil { - c.logger.Warn("already-landed auto-close: issue note failed", slog.String("repo", repo), slog.Int("issue", number), slog.String("error", err.Error())) - } - continue - } + // Close first: CloseIssue's gates may refuse, and a "this issue is + // done" note must never sit on an issue that stayed open. if err := c.CloseIssue(ctx, repo, number, IssueCloseOptions{StateReason: IssueStateReasonCompleted}); err != nil { // writeback:gated if c.logger != nil { c.logger.Warn("already-landed auto-close: issue left open", slog.String("repo", repo), slog.Int("issue", number), slog.String("error", err.Error())) @@ -405,6 +402,10 @@ func (c *Client) closeIssuesAlreadyLanded(ctx context.Context, owner, name strin continue } closed = append(closed, number) + note := alreadyLandedIssueNote(pr.GetNumber(), redundantOf, strings.TrimSpace(pr.GetBase().GetRef())) + if _, _, err := c.client.Issues.CreateComment(ctx, owner, name, number, &gh.IssueComment{Body: gh.Ptr(note)}); err != nil && c.logger != nil { // writeback:gated + c.logger.Warn("already-landed auto-close: issue note failed", slog.String("repo", repo), slog.Int("issue", number), slog.String("error", err.Error())) + } } return closed } diff --git a/src/pkg/github/pr_empty_diff_hive_autoclose_test.go b/src/pkg/github/pr_empty_diff_hive_autoclose_test.go index de30f2c02a..52c5b858fd 100644 --- a/src/pkg/github/pr_empty_diff_hive_autoclose_test.go +++ b/src/pkg/github/pr_empty_diff_hive_autoclose_test.go @@ -1,7 +1,13 @@ package github import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "reflect" "strings" + "sync" "testing" "time" @@ -109,3 +115,43 @@ func TestReviewRedundantOfNumber(t *testing.T) { } } } + +// hivecommons/hive#15689: CloseIssue's gates (here the tracking-issue +// refusal) may keep an issue open; the "this issue is done" note must then +// not be posted. A close that goes through still gets its note. +func TestCloseIssuesAlreadyLandedNotesOnlyClosedIssues(t *testing.T) { + var mu sync.Mutex + var comments, closes []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + switch { + case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/issues/1": + _ = json.NewEncoder(w).Encode(trackingTestIssue(1, nil, trackingTestBody)) + case r.Method == http.MethodGet && r.URL.Path == "/repos/o/r/issues/2": + _ = json.NewEncoder(w).Encode(&gh.Issue{Number: gh.Ptr(2), State: gh.Ptr("open"), Title: gh.Ptr("Fix the sweep"), User: &gh.User{Login: gh.Ptr("owner")}}) + case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/comments"): + comments = append(comments, r.URL.Path) + _ = json.NewEncoder(w).Encode(map[string]any{"id": 1}) + case r.Method == http.MethodPatch: + closes = append(closes, r.URL.Path) + _ = json.NewEncoder(w).Encode(map[string]any{"state": "closed"}) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + defer srv.Close() + c := testClient(t, srv.URL) + pr := &gh.PullRequest{Number: gh.Ptr(7), Body: gh.Ptr("Closes #1\nCloses #2"), Base: &gh.PullRequestBranch{Ref: gh.Ptr("v5")}} + + got := c.closeIssuesAlreadyLanded(context.Background(), "o", "r", pr, 5) + if !reflect.DeepEqual(got, []int{2}) { + t.Fatalf("closed = %v, want [2]", got) + } + if want := []string{"/repos/o/r/issues/2"}; !reflect.DeepEqual(closes, want) { + t.Fatalf("closes = %v, want %v", closes, want) + } + if want := []string{"/repos/o/r/issues/2/comments"}; !reflect.DeepEqual(comments, want) { + t.Fatalf("done notes = %v, want only on the closed issue %v", comments, want) + } +}