From a6ae9783785a19264770d01a71e3d92fe606ecf4 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Thu, 29 Oct 2026 15:26:37 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20tracker:=20name=20the=20merged?= =?UTF-8?q?=20PR=20that=20closed=20an=20issue=20under=20a=20follow-up=20PR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A PR row whose linked issue is already closed drew a bare `closed` chip when the hive's merged ledger did not know the closer (a PR the owner merged by hand; a merge lost across a roll), which read as the issue having been closed before its fix merged — hive's #15422 under follow-up #15425 and #15058 under #15431. The issues were in fact closed by GitHub the second #15426 / #15068 merged. The soft-link enrichment now asks GitHub which MERGED pull request closed the issue (closedByPullRequestsReferences; an open PR with Fixes #N is listed there too and is never named) and carries state_reason and closed_by on the link, so the chip reads `closed by #N`. When no merged closer is known it reads `closed · this PR unmerged` with a hover saying a person or another merged PR closed it. Docs state the rule: the hive closes an issue only after its PR has merged. Fixes #15479 Signed-off-by: Andrew Anderson --- ...racker-closed-issue-names-merged-closer.md | 1 + src/docs/dashboard-sections.md | 2 + src/docs/hive-merge.md | 28 ++++++ src/pkg/dashboard/stage_tracker.go | 21 ++++- src/pkg/dashboard/stage_tracker_test.go | 23 +++-- src/pkg/dashboard/stage_tracker_ui_test.go | 20 +++-- .../dashboard/static-src/js/tracker-filter.js | 20 +++-- src/pkg/dashboard/static/index.html | 20 +++-- src/pkg/github/advisory_work.go | 44 ++++++++++ src/pkg/github/issue_link_state_test.go | 87 +++++++++++++++++++ 10 files changed, 233 insertions(+), 33 deletions(-) create mode 100644 changelog.d/fixed-tracker-closed-issue-names-merged-closer.md create mode 100644 src/pkg/github/issue_link_state_test.go diff --git a/changelog.d/fixed-tracker-closed-issue-names-merged-closer.md b/changelog.d/fixed-tracker-closed-issue-names-merged-closer.md new file mode 100644 index 0000000000..fb675b7210 --- /dev/null +++ b/changelog.d/fixed-tracker-closed-issue-names-merged-closer.md @@ -0,0 +1 @@ +- Activity tracker: a follow-up or duplicate PR opened against an issue an earlier merge already delivered now reads `closed by #N` (the merged PR GitHub records as the closer, never an open PR with `Fixes #N`), or `closed · this PR unmerged` when no merged closer is known, instead of a bare `closed` chip that read as if the issue had been closed before its fix merged; docs state the rule that the hive closes an issue only after its PR merges diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 1d09f03f6e..623c3de13c 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -1037,6 +1037,8 @@ When the blocked sweep or the review-backlog block reconciler lifts `blocked` it A pull request whose `Fixes #N` issue has already closed (auto-closed by a bot, or closed by hand) while the PR is still open is live work, not a stale line: the row is anchored on the PR ([#14655](https://github.com/hivecommons/hive/issues/14655)). The PR's own icon, number and title lead the row at full width, its stage drives the strip and its timestamps sort it — closing the issue is not a regression of the PR — and the closed issue shrinks to a muted `closes #N · closed` chip linking to it. +The chip names the closer when it can. The server asks GitHub which *merged* pull request closed the issue (`closedByPullRequestsReferences`; the hive's own merged ledger is the fallback) and the slot reads `closed by #N`, linking to that PR — the open PR on the row is then a follow-up or a duplicate, and merging it will not close anything. An open PR whose body says `Fixes #N` is listed under the same GitHub relation but is never named: it has closed nothing. When no merged closer is known the chip reads `closed · this PR unmerged`, with the hover spelling out that a person or another merged PR closed the issue; it never reads as a bare `closed`, which looked like the issue had been closed before its fix merged (hive's #15422 under follow-up #15425, #15058 under #15431). The hive itself closes an issue only after its PR has merged (see [Issues close only after the merge](hive-merge.md#issues-close-only-after-the-merge)). + An issue closed as *not planned* adds an `issue closed (not planned)` chip so the reader can tell the PR may be moot. At 090 such a row still counts under *Needs attention*: the human has a merge to click. When the PR closes or merges too, the row leaves the list as before (a merged PR shows in *Recently merged* for its retention window). #### Row gutter diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index eea1248866..927bda097b 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -624,6 +624,34 @@ report them. If the branch's commits cannot be read at merge time, the hive falls back to GitHub's default message rather than risk dropping a sign-off. Switch it off with `auto_merge.credit_issue_authors: false` (#14581). +### Issues close only after the merge + +The hive never closes an issue for a pull request that has not merged. The +only PR-driven close path is close-on-merge (`CloseOnMergeLedgerSweep`, +`close_on_merge.go`), which reads the PR back from the forge and returns +`pr_not_merged` without touching the issue unless `merged_at` is set (or +`merged` is true); opening a PR, admitting it to the merge train or the +pre-merge soak, and a `Fixes #N` in its body change nothing on the issue. On +GitHub's default branch GitHub itself closes the issue in the same instant +the PR merges, attributed to whoever pressed merge; on a release line the +hive's sweep does it on the next pass, with a "Fixed by #N (merged …)" +comment. Every such close goes through the central `CloseIssue` (which +refuses tracking issues and honours the reporter-confirmation gate) and is +counted by the direct-write ratchet, so no other code path can grow a close +of its own. + +The tracker shows the consequence honestly. A follow-up or duplicate PR whose +body names an issue an *earlier* PR already delivered — `Refs #15422` on +#15425 after #15426 merged, `extending #15058` on #15431 after #15068 merged — +sits under the struck-through issue with the chip **closed by #N**, linking +to the merged PR GitHub records as the closer +(`closedByPullRequestsReferences`, with the hive's merged ledger as the +fallback). Only a merged PR is ever named: an open PR with `Fixes #N` is +listed under the same relation and has closed nothing. When no merged closer +can be found the chip reads **closed · this PR unmerged**, never a bare +`closed` that could be read as the issue having been closed before its fix +merged. + ### Stage labels Every sweep cycle also writes the hive-owned `stage/NNN-*` label for each PR diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 23d32a2d4d..fa4948ce73 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -2886,8 +2886,13 @@ const ( // or pr when the number names a pull request; ok is false for a failed GET. type stageTrackerLinkInfo struct { title, state string - pr, ok bool - at time.Time + // stateReason and closedBy are set on a closed issue when the forge + // reports them: GitHub's stateReason and the merged PR that closed it + // (never an open PR, whatever its body says). + stateReason string + closedBy int + pr, ok bool + at time.Time } // stageTrackerLinkMemo memoises link lookups keyed by owner/repo#number. @@ -2934,6 +2939,11 @@ func (in stageTrackerLinkInfo) ttl() time.Duration { // reference titled (soft refs / title / mention links, and closing keywords // hive read from the PR text) a title and state, from one issues GET per // distinct same-repo number, memoised across rebuilds (stageTrackerLinkMemo). +// A closed issue also gets its stateReason and the merged PR that closed it +// (ClosedBy, from GitHub's closedByPullRequestsReferences), so a follow-up +// PR opened against an issue an earlier merge already delivered reads +// `closed by #N` rather than a bare `closed` that looks like the issue was +// closed before any PR merged (hivecommons/hive#15232 for the ledger path). // A number that turns out to be a pull request, open or closed (`Supersedes // #N`, an adoption's "close #N"), is dropped: it is not an issue the PR // covers (hivecommons/hive#14667). A failed or skipped lookup leaves the link @@ -2961,8 +2971,8 @@ func (s *Server) stageTrackerEnrichSoftLinks(items []StageTrackerItem, qualify f in, hit := s.stageTrackerLinks.get(key, now) if !hit && lookups < stageTrackerLinkLookups && ctx.Err() == nil { lookups++ - title, state, isPR, err := s.deps.GHClient.IssueTitleState(ctx, owner, name, li.Number) - in = stageTrackerLinkInfo{title: title, state: state, pr: isPR, ok: err == nil} + facts, err := s.deps.GHClient.IssueLinkState(ctx, owner, name, li.Number, pr.Number) + in = stageTrackerLinkInfo{title: facts.Title, state: facts.State, stateReason: facts.StateReason, closedBy: facts.ClosedBy, pr: facts.IsPR, ok: err == nil} // Running out of this rebuild's budget says nothing // about the issue: leave it for the next rebuild. if err == nil || ctx.Err() == nil { @@ -2974,6 +2984,9 @@ func (s *Server) stageTrackerEnrichSoftLinks(items []StageTrackerItem, qualify f } if in.ok { li.Title, li.State = in.title, in.state + if in.state == "closed" { + li.StateReason, li.ClosedBy = in.stateReason, in.closedBy + } } } kept = append(kept, li) diff --git a/src/pkg/dashboard/stage_tracker_test.go b/src/pkg/dashboard/stage_tracker_test.go index 598098c32a..924895903a 100644 --- a/src/pkg/dashboard/stage_tracker_test.go +++ b/src/pkg/dashboard/stage_tracker_test.go @@ -1061,12 +1061,20 @@ func TestStageTrackerClosedIssueAnchor(t *testing.T) { // (`Supersedes #14592`, an adoption's "close #14592"), is dropped rather than // drawn as an issue "(title unavailable)". func TestStageTrackerEnrichSoftLinks(t *testing.T) { - var gets atomic.Int32 + var gets, closerReads atomic.Int32 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/graphql") { + // The closer lookup a closed issue costs: #14576 was closed by + // the merge of #14570; #14590 (open, body says Fixes #14576) + // is listed under the same relation and must not be named. + closerReads.Add(1) + fmt.Fprint(w, `{"data":{"repository":{"issue":{"closedByPullRequestsReferences":{"nodes":[{"number":14590,"merged":false},{"number":14570,"url":"https://github.com/hivecommons/hive/pull/14570","merged":true,"mergedAt":"2026-10-27T12:00:00Z"}]}}}}}`) + return + } gets.Add(1) switch r.URL.Path { case "/repos/hivecommons/hive/issues/14576": - fmt.Fprint(w, `{"number":14576,"title":"Runner stalls","state":"closed"}`) + fmt.Fprint(w, `{"number":14576,"title":"Runner stalls","state":"closed","state_reason":"completed"}`) case "/repos/hivecommons/hive/issues/14577": fmt.Fprint(w, `{"number":14577,"title":"A PR","state":"open","pull_request":{}}`) case "/repos/hivecommons/hive/issues/14592": @@ -1100,11 +1108,14 @@ func TestStageTrackerEnrichSoftLinks(t *testing.T) { if len(links) != 3 { t.Fatalf("links = %+v, want the PR numbers 14577 and 14592 dropped", links) } - if got := links[0]; got.Number != 14576 || got.Title != "Runner stalls" || got.State != "closed" { - t.Fatalf("refs link = %+v, want titled and closed", got) + if got := links[0]; got.Number != 14576 || got.Title != "Runner stalls" || got.State != "closed" || got.StateReason != "completed" || got.ClosedBy != 14570 { + t.Fatalf("refs link = %+v, want titled, closed (completed) by merged #14570", got) + } + if got := items[1].LinkedIssues[0]; got.Title != "Runner stalls" || got.State != "closed" || got.ClosedBy != 14570 { + t.Fatalf("second PR's refs link = %+v, want closed by #14570 (never the open #14590)", got) } - if got := items[1].LinkedIssues[0]; got.Title != "Runner stalls" || got.State != "closed" { - t.Fatalf("second PR's refs link = %+v", got) + if n := closerReads.Load(); n != 1 { + t.Fatalf("closer reads = %d, want 1 (closed issues only, memoised)", n) } if got := links[1]; got.Number != 14578 || got.Title != "" || got.State != "" { t.Fatalf("failed lookup = %+v, want untouched", got) diff --git a/src/pkg/dashboard/stage_tracker_ui_test.go b/src/pkg/dashboard/stage_tracker_ui_test.go index 7c82279e35..f8caa76cab 100644 --- a/src/pkg/dashboard/stage_tracker_ui_test.go +++ b/src/pkg/dashboard/stage_tracker_ui_test.go @@ -641,14 +641,14 @@ assert.strictEqual(titles(row, 'Fix sort'), 1); // body still says Fixes #N): the issue slot keeps "#N title" as a link, // struck through under a "closed" chip, so the pairing reads at a glance. row = stageTrackerRowHtml({ repo: 'acme/console', number: 111, kind: 'pr', title: 'Docs', stage: 95, linked_issues: [{ number: 13409, state: 'closed', title: 'Usage on --help', url: 'https://x/13409' }] }, stages, ctx); -assert.strictEqual(lead(row), '#13409 Usage on --help closed', lead(row)); +assert.strictEqual(lead(row), '#13409 Usage on --help closed · this PR unmerged', lead(row)); assert.ok(/
/.test(row) && /]*>#13409<\/a>/.test(row), 'closed issue stays a link in a struck-through slot: ' + row); -assert.ok(row.includes('Usage on --help closed'), 'title then closed chip: ' + row); +assert.ok(/Usage on --help<\/span> closed · this PR unmerged<\/span>/.test(row), 'title then closed chip naming the unmerged PR: ' + row); assert.ok(!row.includes('ham-no-issue-t') && !/]*title=/.test(row) && prCol(row).length === 1 && prCol(row)[0].includes('#111'), row); // The same with no title resolvable: a muted "(closed issue)" placeholder, never a blank. row = stageTrackerRowHtml({ repo: 'acme/console', number: 111, kind: 'pr', title: 'Docs', stage: 95, linked_issues: [{ number: 13409, state: 'closed', url: 'https://x/13409' }] }, stages, ctx); -assert.strictEqual(lead(row), '#13409 (closed issue) closed', lead(row)); -assert.ok(row.includes('(closed issue) closed'), row); +assert.strictEqual(lead(row), '#13409 (closed issue) closed · this PR unmerged', lead(row)); +assert.ok(/\(closed issue\)<\/span> closed · this PR unmerged<\/span>/.test(row), row); // hivecommons/hive#15232: a PR whose closing link names a closed issue // (closed_issue set by the server, hivecommons/hive#14655) still puts the // ISSUE in the left slot — struck through under its chip — and itself in the @@ -664,14 +664,16 @@ assert.ok(/closed closed'); +assert.strictEqual(lead(row), '#15173 Sweep closed · this PR unmerged issue closed (not planned)', lead(row)); +assert.ok(/closed · this PR unmerged<\/span> closed · this PR unmerged<\/span>$/.test(stageTrackerClosedChipHtml({ repo: 'acme/console', number: 500 }, { number: 496, closed_by: 500, title: 'T' }))); assert.deepStrictEqual(stageTrackerPRIssueSlot({ number: 500, linked_issues: [{ number: 496, state: 'closed', title: 'C', url: 'u', closed_by: 501 }] }), { number: 496, title: 'C', url: 'u', closed: true, placeholder: '', closed_by: 501 }, 'closed_by rides the slot'); assert.strictEqual(stageTrackerItemURL('acme/console', 501, 'pull', 'https://ghe.example/acme/console/pull/500'), 'https://ghe.example/acme/console/pull/501', 'closer URL follows the sibling host'); assert.strictEqual(stageTrackerItemURL('acme/console', 501, 'pull', ''), 'https://github.com/acme/console/pull/501'); diff --git a/src/pkg/dashboard/static-src/js/tracker-filter.js b/src/pkg/dashboard/static-src/js/tracker-filter.js index 8ea99f87c3..740cf4fc0a 100644 --- a/src/pkg/dashboard/static-src/js/tracker-filter.js +++ b/src/pkg/dashboard/static-src/js/tracker-filter.js @@ -38,20 +38,26 @@ return { number: 0, text: 'no linked issue' }; } // Pure: the chip(s) beside a closed issue in a PR row's issue slot. - // `closed` when nothing more is known; `closed by #N` linking to the - // merged PR that closed it (the server's merged ledger) when another PR - // delivered the issue — the open PR on this row is then a duplicate - // (hivecommons/hive#15232). A not-planned close adds an "issue closed - // (not planned)" chip: the PR may be moot (hivecommons/hive#14655). + // `closed by #N` linking to the merged PR that closed it (GitHub's + // closedByPullRequestsReferences, else the server's merged ledger) when + // another PR delivered the issue — the open PR on this row is then a + // follow-up or a duplicate (hivecommons/hive#15232). When no merged + // closer is known the chip still says the issue closed while THIS PR is + // unmerged, so the row cannot read as the issue having been closed by + // an unmerged PR: the hive only ever closes an issue after its PR has + // merged. A not-planned close adds an "issue closed (not planned)" + // chip: the PR may be moot (hivecommons/hive#14655). function stageTrackerClosedChipHtml(item, slot) { + // Inline (not a module const): the UI test lifts this function alone. + const unmergedTip = 'Issue #' + slot.number + ' is closed, but this PR is still open and unmerged: it did not close the issue. A person or another merged PR did (the hive closes an issue only after its PR merges); this hive could not name the closer.'; const notPlanned = String(slot.state_reason || '') === 'not_planned'; const by = Number(slot.closed_by) || 0; let chip; if (by > 0 && by !== Number(item.number)) { const url = stageTrackerItemURL(item.repo, by, 'pull', item.url); - chip = `closed by #${by}`; + chip = `closed by #${by}`; } else { - chip = 'closed'; + chip = `closed · this PR unmerged`; } return chip + (notPlanned ? ' issue closed (not planned)' : ''); } diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 0aa2513327..95ef66468d 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -17413,20 +17413,26 @@

Where to get help

return { number: 0, text: 'no linked issue' }; } // Pure: the chip(s) beside a closed issue in a PR row's issue slot. - // `closed` when nothing more is known; `closed by #N` linking to the - // merged PR that closed it (the server's merged ledger) when another PR - // delivered the issue — the open PR on this row is then a duplicate - // (hivecommons/hive#15232). A not-planned close adds an "issue closed - // (not planned)" chip: the PR may be moot (hivecommons/hive#14655). + // `closed by #N` linking to the merged PR that closed it (GitHub's + // closedByPullRequestsReferences, else the server's merged ledger) when + // another PR delivered the issue — the open PR on this row is then a + // follow-up or a duplicate (hivecommons/hive#15232). When no merged + // closer is known the chip still says the issue closed while THIS PR is + // unmerged, so the row cannot read as the issue having been closed by + // an unmerged PR: the hive only ever closes an issue after its PR has + // merged. A not-planned close adds an "issue closed (not planned)" + // chip: the PR may be moot (hivecommons/hive#14655). function stageTrackerClosedChipHtml(item, slot) { + // Inline (not a module const): the UI test lifts this function alone. + const unmergedTip = 'Issue #' + slot.number + ' is closed, but this PR is still open and unmerged: it did not close the issue. A person or another merged PR did (the hive closes an issue only after its PR merges); this hive could not name the closer.'; const notPlanned = String(slot.state_reason || '') === 'not_planned'; const by = Number(slot.closed_by) || 0; let chip; if (by > 0 && by !== Number(item.number)) { const url = stageTrackerItemURL(item.repo, by, 'pull', item.url); - chip = `closed by #${by}`; + chip = `closed by #${by}`; } else { - chip = 'closed'; + chip = `closed · this PR unmerged`; } return chip + (notPlanned ? ' issue closed (not planned)' : ''); } diff --git a/src/pkg/github/advisory_work.go b/src/pkg/github/advisory_work.go index 0a7a84bd31..3a6b6ce87f 100644 --- a/src/pkg/github/advisory_work.go +++ b/src/pkg/github/advisory_work.go @@ -64,3 +64,47 @@ func (c *Client) IssueTitleState(ctx context.Context, owner, repo string, number } return issue.GetTitle(), strings.ToLower(issue.GetState()), false, nil } + +// IssueLinkFacts is what the activity tracker's issue slot needs for a +// linked issue it has no row for: title, lower-cased state and stateReason, +// and — on a closed issue — ClosedBy, the number of the MERGED pull request +// GitHub records as having closed it (closedByPullRequestsReferences), 0 +// when none is known. A merged-only reading is deliberate: an open PR with +// `Fixes #N` is listed under the same relation and has closed nothing. +type IssueLinkFacts struct { + Title string + State string + StateReason string + IsPR bool + ClosedBy int +} + +// IssueLinkState is IssueTitleState plus the closed issue's stateReason and +// the merged PR that closed it (IssueLinkFacts.ClosedBy). The closer lookup +// is one GraphQL read, made only for a closed issue, and best effort: a +// forge that cannot answer it leaves ClosedBy at 0 and the rest intact, so +// the slot still draws the title and the closed chip. exclude is a PR +// number never reported as the closer (the PR carrying the link). +func (c *Client) IssueLinkState(ctx context.Context, owner, repo string, number, exclude int) (IssueLinkFacts, error) { + if c == nil || c.client == nil { + return IssueLinkFacts{}, ErrNoGitHubClient + } + issue, _, err := c.client.Issues.Get(ctx, owner, repo, number) + if err != nil { + return IssueLinkFacts{}, err + } + if issue.IsPullRequest() { + return IssueLinkFacts{IsPR: true}, nil + } + out := IssueLinkFacts{ + Title: issue.GetTitle(), + State: strings.ToLower(issue.GetState()), + StateReason: strings.ToLower(strings.TrimSpace(issue.GetStateReason())), + } + if out.State == "closed" { + if closer, ok := c.closedByPullRequestReference(ctx, owner, repo, number, exclude); ok { + out.ClosedBy = closer.Number + } + } + return out, nil +} diff --git a/src/pkg/github/issue_link_state_test.go b/src/pkg/github/issue_link_state_test.go new file mode 100644 index 0000000000..fbc567c958 --- /dev/null +++ b/src/pkg/github/issue_link_state_test.go @@ -0,0 +1,87 @@ +package github + +import ( + "context" + "fmt" + "log/slog" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// An issue closed while a PR that says "Fixes #N" is still open was closed +// by something else. The slot names only a MERGED closer: GitHub lists the +// open PR under closedByPullRequestsReferences too, and naming it would read +// as if the open PR had already delivered the issue. +func TestIssueLinkStateNamesOnlyMergedCloser(t *testing.T) { + graphql := 0 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/graphql") { + graphql++ + fmt.Fprint(w, `{"data":{"repository":{"issue":{"closedByPullRequestsReferences":{"nodes":[`+ + `{"number":101,"merged":false},`+ + `{"number":99,"url":"https://github.com/o/r/pull/99","merged":true,"mergedAt":"2026-10-29T16:38:39Z"}]}}}}}`) + return + } + switch r.URL.Path { + case "/repos/o/r/issues/7": + fmt.Fprint(w, `{"number":7,"title":"Closed one","state":"closed","state_reason":"completed"}`) + case "/repos/o/r/issues/8": + fmt.Fprint(w, `{"number":8,"title":"Open one","state":"open"}`) + case "/repos/o/r/issues/9": + fmt.Fprint(w, `{"number":9,"title":"A PR","state":"open","pull_request":{}}`) + default: + http.NotFound(w, r) + } + })) + defer ts.Close() + c := NewClientForTest(ts.URL, "o", []string{"r"}, slog.Default()) + ctx := context.Background() + + got, err := c.IssueLinkState(ctx, "o", "r", 7, 101) + if err != nil { + t.Fatal(err) + } + if got.Title != "Closed one" || got.State != "closed" || got.StateReason != "completed" || got.ClosedBy != 99 || got.IsPR { + t.Fatalf("closed issue = %+v, want closed by merged #99, never the open #101", got) + } + + got, err = c.IssueLinkState(ctx, "o", "r", 8, 0) + if err != nil { + t.Fatal(err) + } + if got.State != "open" || got.ClosedBy != 0 { + t.Fatalf("open issue = %+v, want open with no closer", got) + } + if graphql != 1 { + t.Fatalf("graphql reads = %d, want 1 (closed issues only)", graphql) + } + + got, err = c.IssueLinkState(ctx, "o", "r", 9, 0) + if err != nil || !got.IsPR || got.Title != "" { + t.Fatalf("pull request = %+v, %v; want IsPR and nothing else", got, err) + } +} + +// A forge that cannot answer the closer query (older GHE, a token without +// the scope) still yields the title and state: the chip reads `closed` +// rather than the row losing its issue. +func TestIssueLinkStateCloserLookupBestEffort(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost { + http.Error(w, `{"message":"nope"}`, http.StatusForbidden) + return + } + fmt.Fprint(w, `{"number":7,"title":"Closed one","state":"closed","state_reason":"not_planned"}`) + })) + defer ts.Close() + c := NewClientForTest(ts.URL, "o", []string{"r"}, slog.Default()) + got, err := c.IssueLinkState(context.Background(), "o", "r", 7, 0) + if err != nil { + t.Fatal(err) + } + if got.Title != "Closed one" || got.State != "closed" || got.StateReason != "not_planned" || got.ClosedBy != 0 { + t.Fatalf("got %+v, want title/state kept and ClosedBy 0", got) + } +}