fix: guard against empty branch names in PR check status lookups (#182)
During a rebase with detached HEAD, the branch name can be empty or resolve to empty after stripping refs/heads/. This caused gh to return arbitrary PRs and created invalid cache keys like "path::". Guard all branch-to-cache-key paths: getPRForBranch bails before acquiring the semaphore, renderer components skip cache lookups, and syncPRChecksStatus validates the normalized branch.
This commit is contained in:
@@ -97,6 +97,19 @@ describe('getPRForBranch', () => {
|
||||
expect(pr?.state).toBe('draft')
|
||||
})
|
||||
|
||||
it('returns null for empty branch (e.g. during rebase with detached HEAD)', async () => {
|
||||
const pr = await getPRForBranch('/repo-root', '')
|
||||
expect(pr).toBeNull()
|
||||
// Should not call gh at all
|
||||
expect(execFileAsyncMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns null for refs/heads/ only branch (detached after strip)', async () => {
|
||||
const pr = await getPRForBranch('/repo-root', 'refs/heads/')
|
||||
expect(pr).toBeNull()
|
||||
expect(execFileAsyncMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('returns null when pr list returns an empty array', async () => {
|
||||
execFileAsyncMock
|
||||
.mockResolvedValueOnce({ stdout: 'git@github.com:acme/widgets.git\n' })
|
||||
|
||||
@@ -74,11 +74,18 @@ async function getOwnerRepo(repoPath: string): Promise<{ owner: string; repo: st
|
||||
* Returns null if gh is not installed, or no PR exists for the branch.
|
||||
*/
|
||||
export async function getPRForBranch(repoPath: string, branch: string): Promise<PRInfo | null> {
|
||||
// Strip refs/heads/ prefix if present
|
||||
const branchName = branch.replace(/^refs\/heads\//, '')
|
||||
|
||||
// During a rebase the worktree is in detached HEAD and branch is empty.
|
||||
// An empty --head filter causes gh to return an arbitrary PR — bail early.
|
||||
if (!branchName) {
|
||||
return null
|
||||
}
|
||||
|
||||
await acquire()
|
||||
try {
|
||||
const ownerRepo = await getOwnerRepo(repoPath)
|
||||
// Strip refs/heads/ prefix if present
|
||||
const branchName = branch.replace(/^refs\/heads\//, '')
|
||||
let data: {
|
||||
number: number
|
||||
title: string
|
||||
|
||||
Reference in New Issue
Block a user