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:
Jinjing
2026-03-28 16:10:56 -07:00
committed by GitHub
parent 9d85347772
commit dbab855611
8 changed files with 74 additions and 8 deletions
+13
View File
@@ -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' })
+9 -2
View File
@@ -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