diff --git a/changelog.d/fixed-15155-closed-issue-blockers.md b/changelog.d/fixed-15155-closed-issue-blockers.md new file mode 100644 index 0000000000..8209207071 --- /dev/null +++ b/changelog.d/fixed-15155-closed-issue-blockers.md @@ -0,0 +1 @@ +- Clear blockers when issues close through GitHub or the hive, so closed issues no longer inflate the capabilities blocked-item count ([#15155](https://github.com/hivecommons/hive/issues/15155)). diff --git a/src/pkg/github/issue_blocker_test.go b/src/pkg/github/issue_blocker_test.go index 44b74cee01..209b0231ce 100644 --- a/src/pkg/github/issue_blocker_test.go +++ b/src/pkg/github/issue_blocker_test.go @@ -102,3 +102,53 @@ func TestIssueStageFactsCarryBlocker(t *testing.T) { t.Fatalf("derived = %+v", d) } } + +// Ordinary GitHub closes must clear the same ledger used by capabilitiesStatus. +func TestWebhookClosedIssueClearsBlocker(t *testing.T) { + for _, tt := range []struct { + name, event, action, state, repo string + pr, wantBlocked bool + }{ + {"closed", "issues", "closed", "closed", "org/repo", false, false}, + {"closed comment", "issue_comment", "created", "closed", "org/repo", false, false}, + {"edited closed comment", "issue_comment", "edited", "closed", "org/repo", false, false}, + {"closed label event", "issues", "labeled", "closed", "ORG/REPO", false, false}, + {"open issue", "issues", "labeled", "open", "org/repo", false, true}, + {"reopened issue", "issues", "reopened", "open", "org/repo", false, true}, + {"unknown repository", "issues", "closed", "closed", "other/repo", false, true}, + {"PR comment", "issue_comment", "created", "closed", "org/repo", true, true}, + } { + t.Run(tt.name, func(t *testing.T) { + l := swapSharedLedger(t) + c, _ := setupWebhookCacheTest(t) + l.Observe("org/repo", 7, workflowsReport, time.Now()) + l.Observe("org/repo", 8, workflowsReport, time.Now()) + l.Observe("repo", 7, workflowsReport, time.Now()) + pr := "" + if tt.pr { + pr = `,"pull_request":{"url":"x"}` + } + payload := `{"action":` + jsonString(tt.action) + `,"issue":{"number":7,"state":` + jsonString(tt.state) + pr + `},"comment":{"body":` + jsonString(workflowsReport) + `},"repository":{"full_name":` + jsonString(tt.repo) + `}}` + res, err := c.HandleWebhookEvent(tt.event, []byte(payload)) + if err != nil { + t.Fatal(err) + } + if _, blocked := l.Blocking("org/repo", 7); blocked != tt.wantBlocked { + t.Fatalf("blocked = %v, want %v", blocked, tt.wantBlocked) + } + if _, blocked := l.Blocking("repo", 7); blocked != tt.wantBlocked { + t.Fatalf("short-name blocked = %v, want %v", blocked, tt.wantBlocked) + } + wantCount := 1 + if tt.wantBlocked { + wantCount += 2 + } + if caps := l.Capabilities(); caps.BlockedItems != wantCount || caps.ByPermission["workflows"] != wantCount { + t.Fatalf("capabilities = %+v, want %d blocked items", caps, wantCount) + } + if !tt.wantBlocked && res.BlockerKind != "" { + t.Fatalf("closed issue recorded blocker %q", res.BlockerKind) + } + }) + } +} diff --git a/src/pkg/github/issue_close.go b/src/pkg/github/issue_close.go index d2aa40ed71..4f2e53099c 100644 --- a/src/pkg/github/issue_close.go +++ b/src/pkg/github/issue_close.go @@ -9,6 +9,7 @@ import ( gh "github.com/google/go-github/v72/github" + itemblocker "github.com/hivecommons/hive/pkg/blocker" "github.com/hivecommons/hive/pkg/forgestate" ) @@ -128,6 +129,13 @@ func (c *Client) closeIssue(ctx context.Context, repo string, number int, opts I if _, _, err := c.client.Issues.Edit(ctx, owner, repoName, number, req); err != nil { return false, fmt.Errorf("closing issue %s/%s#%d: %w", owner, repoName, number, err) } + if !isPR { + l := itemblocker.Shared() + l.Clear(owner+"/"+repoName, number) + if repo != owner+"/"+repoName { + l.Clear(repo, number) + } + } closedKind := forgestate.KindIssue if isPR { closedKind = forgestate.KindPR diff --git a/src/pkg/github/issue_close_error_paths_test.go b/src/pkg/github/issue_close_error_paths_test.go index 280c592f65..343467f5c0 100644 --- a/src/pkg/github/issue_close_error_paths_test.go +++ b/src/pkg/github/issue_close_error_paths_test.go @@ -9,6 +9,7 @@ import ( "net/http/httptest" "strings" "testing" + "time" gh "github.com/google/go-github/v72/github" ) @@ -132,6 +133,8 @@ func TestCloseIssueOverrideCommentFailureAbortsClose(t *testing.T) { } func TestCloseIssueEditFailure(t *testing.T) { + l := swapSharedLedger(t) + l.Observe("o/r", 7, workflowsReport, time.Now()) issue := closeGateIssue("hive-app[bot]", "Bot", "Bug: bot finding", "automated", []string{"bug"}) srv := closeGateMux(t, issue, emptyCommentList, failHandler, failHandler) defer srv.Close() @@ -141,6 +144,9 @@ func TestCloseIssueEditFailure(t *testing.T) { if err == nil || !strings.Contains(err.Error(), "closing issue") { t.Fatalf("err = %v, want close-edit failure", err) } + if _, blocked := l.Blocking("o/r", 7); !blocked { + t.Fatal("failed close cleared the blocker") + } } // TestHasReporterConfirmationRequestPaginates pins the pagination arm: a diff --git a/src/pkg/github/issue_close_test.go b/src/pkg/github/issue_close_test.go index fdec6d282b..7116c29349 100644 --- a/src/pkg/github/issue_close_test.go +++ b/src/pkg/github/issue_close_test.go @@ -8,6 +8,7 @@ import ( "net/http/httptest" "strings" "testing" + "time" gh "github.com/google/go-github/v72/github" ) @@ -77,6 +78,8 @@ func TestCloseIssueReporterConfirmationGate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + l := swapSharedLedger(t) + l.Observe("o/r", 7, workflowsReport, time.Now()) var closed bool var comments []string var labels []string @@ -137,6 +140,9 @@ func TestCloseIssueReporterConfirmationGate(t *testing.T) { if closed != tt.wantClosed { t.Fatalf("closed = %v, want %v", closed, tt.wantClosed) } + if _, blocked := l.Blocking("o/r", 7); blocked == tt.wantClosed { + t.Fatalf("blocked = %v after close, want %v", blocked, !tt.wantClosed) + } if tt.wantComment == "" { if len(comments) != 0 { t.Fatalf("comments = %q, want none", comments) diff --git a/src/pkg/github/webhook_cache.go b/src/pkg/github/webhook_cache.go index 23db230b4a..86045cfe1b 100644 --- a/src/pkg/github/webhook_cache.go +++ b/src/pkg/github/webhook_cache.go @@ -490,9 +490,21 @@ func (c *Client) HandleWebhookEvent(event string, payload []byte) (WebhookEventR aliases := webhookRepoAliases(configured, p.Repository.FullName) sharedWebhookTracker.recordEvent(aliases) + res.Item = webhookItemHint(event, p) if n, ok := webhookTouchedIssue(event, p); ok { res.TouchedIssues = []int{n} - if body, at, ok := webhookIssueCommentBody(event, p); ok { + if res.Item.Closed() { + // Closed issues no longer contribute to blocked_items. Comments + // delivered after closure must not recreate their blocker. + l := itemblocker.Shared() + l.Clear(configured, n) + // Comment webhooks use the configured name; sweep reports use + // owner/repo. Clear both when configuration uses a short name. + owner, repo := c.splitRepo(configured) + if full := owner + "/" + repo; full != configured { + l.Clear(full, n) + } + } else if body, at, ok := webhookIssueCommentBody(event, p); ok { // A needs-human report that names a capability the App lacks // becomes a structured blocker the moment it lands, so the // tracker and "Let hive decide" read it before the next @@ -502,7 +514,6 @@ func (c *Client) HandleWebhookEvent(event string, payload []byte) (WebhookEventR } } } - res.Item = webhookItemHint(event, p) if reviewRequestWakeEvent(event, p) { c.reviewWake.wake() res.WokeReviewWatcher = true