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
Original file line number Diff line number Diff line change
@@ -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))
19 changes: 10 additions & 9 deletions src/pkg/github/pr_empty_diff_hive_autoclose.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package github

import (
Expand Down Expand Up @@ -377,8 +377,10 @@
// 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
Expand All @@ -391,20 +393,19 @@
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()))
}
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
}
Expand Down
46 changes: 46 additions & 0 deletions src/pkg/github/pr_empty_diff_hive_autoclose_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package github

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -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)
}
}
Loading