Skip to content

ignore stale merged/closed PRs for reused branch names#49

Open
skarim wants to merge 2 commits intoskarim/preflight-stacksfrom
skarim/ignore-merged-prs
Open

ignore stale merged/closed PRs for reused branch names#49
skarim wants to merge 2 commits intoskarim/preflight-stacksfrom
skarim/ignore-merged-prs

Conversation

@skarim
Copy link
Copy Markdown
Collaborator

@skarim skarim commented Apr 17, 2026

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, syncStackPRs would adopt the old PR and incorrectly mark the branch as "MERGED". This caused gh stack view to show the wrong status, gh stack submit to skip PR creation, and gh stack push to skip the branch entirely.

Root cause: syncStackPRs called FindAnyPRForBranch (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 syncStackPRs with two sync modes:

1. Remote stack exists (s.ID set) — stack API is source of truth:

  • Fetches the remote stack's PR list via ListStacks, looks up each PR by number, and matches to local branches by head ref name.
  • PRs stay associated regardless of state (open, closed, merged) — the stack API owns the relationship.
  • Falls back to local discovery if the API fails or the stack isn't found.

2. No remote stack — local branch-name discovery:

  • Merged PR → skip. The merged state is final and doesn't need refreshing.
  • Tracked PR (not merged) → refresh via FindPRByNumber. If the PR was closed (not merged), clear the association and fall through to open-PR discovery.
  • No tracked PR → use FindPRForBranch (OPEN state only), preventing adoption of stale merged/closed PRs.

Additional fixes:

  • TUI data loader (data.go): FindPRDetailsForBranch also returned stale merged PRs via last: 1. Added a filter so PR details are only shown if the PR matches the tracked number or is OPEN.
  • FindPRByNumber (github.go): Now returns nil for zero-value GraphQL responses.
  • Test mocks: Several existing tests were missing GitHubClientOverride and silently hitting the real API. Fixed with proper mocks. Updated mock functions from FindAnyPRForBranchFn to FindPRByNumberFn where branches have tracked PRs.

Stack created with GitHub Stacks CLIGive Feedback 💬

Copilot AI review requested due to automatic review settings April 17, 2026 04:43
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 syncStackPRs to (a) use the stack API as source of truth when Stack.ID is 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

Comment thread cmd/utils.go Outdated
@skarim skarim force-pushed the skarim/ignore-merged-prs branch from 759b8d0 to 89d0a64 Compare April 17, 2026 05:24
@skarim skarim force-pushed the skarim/ignore-merged-prs branch from 89d0a64 to db8f91b Compare April 17, 2026 05:28
@skarim skarim force-pushed the skarim/preflight-stacks branch from 9c58a54 to 1537762 Compare April 17, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants