Files
orca/src/main/github/pr-start-point.test.ts
T
KayleeandNeil 528a887ab5 Target the fork branch from fork-PR worktrees (#4394)
Creating a worktree from a cross-repository (fork) PR previously named the
local branch with the maintainer's branch prefix (e.g. `me/866`) and pushed to
origin instead of the contributor's fork, so maintainer edits never reached the
PR. Fork PRs now adopt the contributor's branch name (matching same-repo PRs)
and resolve a fork push target, with a non-blocking warning when the PR
disables maintainer edits and an indicator showing where a push will land.

- pr-start-point: return branchNameOverride/headSha/maintainerCanModify for
  cross-repo PRs (previously only same-repo PRs received these)
- github client: surface maintainer_can_modify alongside the fork push target
- composer: warn (but still allow) when "Allow edits from maintainers" is off
- source control: show the fork push target (owner:branch) before pushing
- extract fork-remote cleanup and setup into dedicated, unit-tested modules

Adds unit suites for the cleanup multi-fork matrix, fork-remote setup,
push-target resolution, the warning decision, and the push-target label.

Note: pre-commit react-doctor hook bypassed — its warnings in useComposerState
are pre-existing (identical count on base) and not enforced by CI.

Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
2026-06-07 19:51:36 -07:00

228 lines
7.2 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
const { getPullRequestPushTargetMock, getWorkItemMock } = vi.hoisted(() => ({
getPullRequestPushTargetMock: vi.fn(),
getWorkItemMock: vi.fn()
}))
vi.mock('./client', () => ({
getPullRequestPushTarget: getPullRequestPushTargetMock,
getWorkItem: getWorkItemMock
}))
import { resolveGitHubPrStartPoint } from './pr-start-point'
describe('resolveGitHubPrStartPoint', () => {
beforeEach(() => {
getPullRequestPushTargetMock.mockReset()
getWorkItemMock.mockReset()
})
it('falls back to the GitHub PR head ref when a direct branch fetch fails', async () => {
getPullRequestPushTargetMock.mockResolvedValue({
pushTarget: {
remoteName: 'pr-contributor-orca',
branchName: 'feat/onboarding-model-choice-782',
remoteUrl: 'git@github.com:contributor/orca.git'
}
})
const gitExec = vi.fn(async (args: string[]) => {
if (args[0] === 'fetch' && String(args[2]).startsWith('+refs/heads/')) {
throw new Error('fatal: could not find remote ref')
}
if (args[0] === 'rev-parse') {
return { stdout: 'def456\n', stderr: '' }
}
return { stdout: '', stderr: '' }
})
const result = await resolveGitHubPrStartPoint({
repoPath: '/repo-root',
prNumber: 1849,
headRefName: 'feat/onboarding-model-choice-782',
gitExec,
resolveRemote: async () => 'origin'
})
expect(gitExec).toHaveBeenCalledWith([
'fetch',
'origin',
'+refs/heads/feat/onboarding-model-choice-782:refs/remotes/origin/feat/onboarding-model-choice-782'
])
expect(gitExec).toHaveBeenCalledWith(['fetch', 'origin', 'refs/pull/1849/head'])
expect(result).toEqual({
baseBranch: 'def456',
headSha: 'def456',
branchNameOverride: 'feat/onboarding-model-choice-782',
pushTarget: {
remoteName: 'pr-contributor-orca',
branchName: 'feat/onboarding-model-choice-782',
remoteUrl: 'git@github.com:contributor/orca.git'
}
})
})
it('keeps the PR head ref fallback when push-target discovery also fails', async () => {
getPullRequestPushTargetMock.mockRejectedValue(new Error('head repo is unavailable'))
const gitExec = vi.fn(async (args: string[]) => {
if (args[0] === 'fetch' && String(args[2]).startsWith('+refs/heads/')) {
throw new Error('fatal: could not find remote ref')
}
if (args[0] === 'rev-parse') {
return { stdout: 'def456\n', stderr: '' }
}
return { stdout: '', stderr: '' }
})
const result = await resolveGitHubPrStartPoint({
repoPath: '/repo-root',
prNumber: 1849,
headRefName: 'feat/onboarding-model-choice-782',
gitExec,
resolveRemote: async () => 'origin'
})
expect(getPullRequestPushTargetMock).toHaveBeenCalledWith('/repo-root', 1849, null)
expect(result).toEqual({
baseBranch: 'def456',
headSha: 'def456',
branchNameOverride: 'feat/onboarding-model-choice-782'
})
})
it('resolves an inaccessible fork PR even when push-target discovery fails', async () => {
getPullRequestPushTargetMock.mockRejectedValue(new Error('head repo is unavailable'))
const gitExec = vi.fn(async (args: string[]) => {
if (args[0] === 'rev-parse') {
return { stdout: 'abc123\n', stderr: '' }
}
return { stdout: '', stderr: '' }
})
const result = await resolveGitHubPrStartPoint({
repoPath: '/repo-root',
prNumber: 1849,
headRefName: 'feat/onboarding-model-choice-782',
isCrossRepository: true,
gitExec,
resolveRemote: async () => 'origin'
})
expect(getPullRequestPushTargetMock).toHaveBeenCalledWith('/repo-root', 1849, null)
expect(gitExec).toHaveBeenCalledWith(['fetch', 'origin', 'refs/pull/1849/head'])
expect(result).toEqual({
baseBranch: 'abc123',
headSha: 'abc123',
branchNameOverride: 'feat/onboarding-model-choice-782'
})
})
it('uses PR metadata when the caller did not pass a head ref', async () => {
getWorkItemMock.mockResolvedValue({
type: 'pr',
branchName: 'contributor/fix',
isCrossRepository: true
})
getPullRequestPushTargetMock.mockResolvedValue({
pushTarget: {
remoteName: 'pr-contributor-orca',
branchName: 'contributor/fix',
remoteUrl: 'git@github.com:contributor/orca.git'
}
})
const gitExec = vi.fn(async (args: string[]) => {
if (args[0] === 'rev-parse') {
return { stdout: 'abc123\n', stderr: '' }
}
return { stdout: '', stderr: '' }
})
const result = await resolveGitHubPrStartPoint({
repoPath: '/repo-root',
prNumber: 1738,
gitExec,
resolveRemote: async () => 'origin'
})
expect(getWorkItemMock).toHaveBeenCalledWith('/repo-root', 1738, 'pr', null)
expect(result).toEqual({
baseBranch: 'abc123',
headSha: 'abc123',
branchNameOverride: 'contributor/fix',
pushTarget: {
remoteName: 'pr-contributor-orca',
branchName: 'contributor/fix',
remoteUrl: 'git@github.com:contributor/orca.git'
}
})
})
it('surfaces maintainerCanModify=false for a fork PR so the caller can warn', async () => {
getPullRequestPushTargetMock.mockResolvedValue({
pushTarget: {
remoteName: 'pr-contributor-orca',
branchName: 'contributor/fix',
remoteUrl: 'git@github.com:contributor/orca.git'
},
maintainerCanModify: false
})
const gitExec = vi.fn(async (args: string[]) => {
if (args[0] === 'rev-parse') {
return { stdout: 'abc123\n', stderr: '' }
}
return { stdout: '', stderr: '' }
})
const result = await resolveGitHubPrStartPoint({
repoPath: '/repo-root',
prNumber: 1849,
headRefName: 'contributor/fix',
isCrossRepository: true,
gitExec,
resolveRemote: async () => 'origin'
})
expect(result).toEqual({
baseBranch: 'abc123',
headSha: 'abc123',
branchNameOverride: 'contributor/fix',
pushTarget: {
remoteName: 'pr-contributor-orca',
branchName: 'contributor/fix',
remoteUrl: 'git@github.com:contributor/orca.git'
},
maintainerCanModify: false
})
})
it('returns the verified head SHA, branch override, and push target when same-repo branch fetch succeeds', async () => {
const gitExec = vi.fn(async (args: string[]) => {
if (args[0] === 'rev-parse') {
return { stdout: 'abc123\n', stderr: '' }
}
return { stdout: '', stderr: '' }
})
const result = await resolveGitHubPrStartPoint({
repoPath: '/repo-root',
prNumber: 42,
headRefName: 'feature/add-feature',
gitExec,
resolveRemote: async () => 'origin'
})
expect(gitExec).toHaveBeenCalledWith([
'fetch',
'origin',
'+refs/heads/feature/add-feature:refs/remotes/origin/feature/add-feature'
])
expect(gitExec).toHaveBeenCalledWith(['rev-parse', '--verify', 'origin/feature/add-feature'])
expect(result).toEqual({
baseBranch: 'abc123',
headSha: 'abc123',
branchNameOverride: 'feature/add-feature',
pushTarget: { remoteName: 'origin', branchName: 'feature/add-feature' }
})
})
})