ignore stale merged/closed PRs for reused branch names#49
ignore stale merged/closed PRs for reused branch names#49skarim wants to merge 2 commits intoskarim/preflight-stacksfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens PR metadata discovery so reused branch names don’t accidentally pick up stale merged/closed PRs, and it updates sync logic to prefer authoritative sources (remote stack API when available, otherwise OPEN-only branch discovery).
Changes:
- Update TUI branch node loading to only adopt PR details when the PR is tracked for that branch or is currently OPEN.
- Rework
syncStackPRsto (a) use the stack API as source of truth whenStack.IDis set and (b) otherwise refresh tracked PRs by number and only adopt OPEN PRs by branch name. - Add/adjust tests across TUI + cmd packages and refine GitHub client behavior for “PR not found”.
Show a summary per file
| File | Description |
|---|---|
| internal/tui/stackview/data.go | Filter adopted PR details to avoid showing stale merged/closed PRs for reused branch names. |
| internal/tui/stackview/data_test.go | Add coverage for ignoring stale merged PR details and still showing tracked merged + OPEN PRs. |
| internal/github/github.go | Make FindPRByNumber return nil for “not found” results. |
| cmd/utils.go | Rework PR sync: remote stack API path + local OPEN-only adoption path. |
| cmd/utils_test.go | Add extensive tests for new PR sync behavior (tracked/untracked, merged/closed, remote stack). |
| cmd/view_test.go | Update mocks to use PR-by-number lookups consistent with new sync behavior. |
| cmd/submit_test.go | Update mocks/expectations for new sync behavior; add a missing pipe close. |
| cmd/merge_test.go | Ensure merge tests provide a GitHub client override since merge now always syncs PR state. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 8/8 changed files
- Comments generated: 1
89d0a64 to
db8f91b
Compare
9c58a54 to
1537762
Compare
db8f91b to
260a40b
Compare
958ce78 to
dadb187
Compare
260a40b to
d4c7e0c
Compare
|
|
||
| // When the stack has a remote ID, the stack API is the source of truth. | ||
| if s.ID != "" { | ||
| if syncStackPRsFromRemote(client, s) { |
There was a problem hiding this comment.
Should we return if this evaluates to falsy too?
There was a problem hiding this comment.
if syncStackPRsFromRemote returns falsy it likely means the stack was deleted on remote, so we need to fallback to the loop below.
d4c7e0c to
08df626
Compare
Lukeghenco
left a comment
There was a problem hiding this comment.
This code seems correct based on my limited knowledge of some of the API callers.
Ignore stale merged/closed PRs for reused branch names
When a branch name previously used in a merged or closed PR is reused in a new stack,
syncStackPRswould adopt the old PR and incorrectly mark the branch as "MERGED". This causedgh stack viewto show the wrong status,gh stack submitto skip PR creation, andgh stack pushto skip the branch entirely.Root cause:
syncStackPRscalledFindAnyPRForBranch(returns the most recent PR regardless of state) for every branch, including ones with no locally tracked PR. A reused branch name would pick up the old merged/closed PR from a previous stack.Fix: Rewrite
syncStackPRswith two sync modes:1. Remote stack exists (
s.IDset) — stack API is source of truth:ListStacks, looks up each PR by number, and matches to local branches by head ref name.2. No remote stack — local branch-name discovery:
FindPRByNumber. If the PR was closed (not merged), clear the association and fall through to open-PR discovery.FindPRForBranch(OPEN state only), preventing adoption of stale merged/closed PRs.Additional fixes:
data.go):FindPRDetailsForBranchalso returned stale merged PRs vialast: 1. Added a filter so PR details are only shown if the PR matches the tracked number or is OPEN.FindPRByNumber(github.go): Now returnsnilfor zero-value GraphQL responses.GitHubClientOverrideand silently hitting the real API. Fixed with proper mocks. Updated mock functions fromFindAnyPRForBranchFntoFindPRByNumberFnwhere branches have tracked PRs.Stack created with GitHub Stacks CLI • Give Feedback 💬