Persist and repair target base branch for PR and MR worktrees (#5540)
* Resolve and fetch the review target branch (compareBaseRef) during PR and MR worktree creation. * Persist this ref on worktree metadata instead of pinning the head SHA. * Dynamically repair existing worktrees with stale commit SHA compare bases in the Source Control UI using linked review metadata.
This commit is contained in:
@@ -2483,6 +2483,7 @@ export async function getPRForBranchOutcome(
|
||||
: {}),
|
||||
...(data.mergeStateStatus !== undefined ? { mergeStateStatus: data.mergeStateStatus } : {}),
|
||||
headSha: data.headRefOid,
|
||||
...(data.baseRefName ? { baseRefName: data.baseRefName } : {}),
|
||||
prRepo: dataRepo ?? undefined,
|
||||
headRepo: dataHeadRepo ?? undefined,
|
||||
conflictSummary
|
||||
|
||||
@@ -26,8 +26,10 @@ describe('resolveGitHubPrStartPoint', () => {
|
||||
remoteUrl: 'git@github.com:contributor/orca.git'
|
||||
}
|
||||
})
|
||||
const fetchRemoteTrackingRef = vi.fn(async () => {
|
||||
throw new Error('fatal: could not find remote ref')
|
||||
const fetchRemoteTrackingRef = vi.fn(async (_remote: string, branch: string) => {
|
||||
if (branch === 'feat/onboarding-model-choice-782') {
|
||||
throw new Error('fatal: could not find remote ref')
|
||||
}
|
||||
})
|
||||
const gitExec = vi.fn(async (args: string[]) => {
|
||||
if (args[0] === 'rev-parse') {
|
||||
@@ -40,6 +42,7 @@ describe('resolveGitHubPrStartPoint', () => {
|
||||
repoPath: '/repo-root',
|
||||
prNumber: 1849,
|
||||
headRefName: 'feat/onboarding-model-choice-782',
|
||||
baseRefName: 'main',
|
||||
gitExec,
|
||||
fetchRemoteTrackingRef,
|
||||
resolveRemote: async () => 'origin'
|
||||
@@ -49,9 +52,11 @@ describe('resolveGitHubPrStartPoint', () => {
|
||||
'origin',
|
||||
'feat/onboarding-model-choice-782'
|
||||
)
|
||||
expect(fetchRemoteTrackingRef).toHaveBeenCalledWith('origin', 'main')
|
||||
expect(gitExec).toHaveBeenCalledWith(['fetch', 'origin', 'refs/pull/1849/head'])
|
||||
expect(result).toEqual({
|
||||
baseBranch: 'def456',
|
||||
compareBaseRef: 'refs/remotes/origin/main',
|
||||
headSha: 'def456',
|
||||
branchNameOverride: 'feat/onboarding-model-choice-782',
|
||||
pushTarget: {
|
||||
@@ -124,6 +129,7 @@ describe('resolveGitHubPrStartPoint', () => {
|
||||
getWorkItemMock.mockResolvedValue({
|
||||
type: 'pr',
|
||||
branchName: 'contributor/fix',
|
||||
baseRefName: 'main',
|
||||
isCrossRepository: true
|
||||
})
|
||||
getPullRequestPushTargetMock.mockResolvedValue({
|
||||
@@ -152,6 +158,7 @@ describe('resolveGitHubPrStartPoint', () => {
|
||||
expect(getWorkItemMock).toHaveBeenCalledWith('/repo-root', 1738, 'pr', null)
|
||||
expect(result).toEqual({
|
||||
baseBranch: 'abc123',
|
||||
compareBaseRef: 'refs/remotes/origin/main',
|
||||
headSha: 'abc123',
|
||||
branchNameOverride: 'contributor/fix',
|
||||
pushTarget: {
|
||||
@@ -215,15 +222,18 @@ describe('resolveGitHubPrStartPoint', () => {
|
||||
repoPath: '/repo-root',
|
||||
prNumber: 42,
|
||||
headRefName: 'feature/add-feature',
|
||||
baseRefName: 'develop',
|
||||
gitExec,
|
||||
fetchRemoteTrackingRef,
|
||||
resolveRemote: async () => 'origin'
|
||||
})
|
||||
|
||||
expect(fetchRemoteTrackingRef).toHaveBeenCalledWith('origin', 'feature/add-feature')
|
||||
expect(fetchRemoteTrackingRef).toHaveBeenCalledWith('origin', 'develop')
|
||||
expect(gitExec).toHaveBeenCalledWith(['rev-parse', '--verify', 'origin/feature/add-feature'])
|
||||
expect(result).toEqual({
|
||||
baseBranch: 'abc123',
|
||||
compareBaseRef: 'refs/remotes/origin/develop',
|
||||
headSha: 'abc123',
|
||||
branchNameOverride: 'feature/add-feature',
|
||||
pushTarget: { remoteName: 'origin', branchName: 'feature/add-feature' }
|
||||
|
||||
@@ -8,6 +8,7 @@ type ResolveGitHubPrStartPointArgs = {
|
||||
repoPath: string
|
||||
prNumber: number
|
||||
headRefName?: string
|
||||
baseRefName?: string
|
||||
isCrossRepository?: boolean
|
||||
connectionId?: string | null
|
||||
gitExec: GitExec
|
||||
@@ -21,6 +22,7 @@ export async function resolveGitHubPrStartPoint(
|
||||
args: ResolveGitHubPrStartPointArgs
|
||||
): Promise<ResolveGitHubPrStartPointResult> {
|
||||
let headRefName = args.headRefName?.trim() ?? ''
|
||||
let baseRefName = args.baseRefName?.trim() ?? ''
|
||||
let isCrossRepository = args.isCrossRepository === true
|
||||
let pushTarget: GitPushTarget | undefined
|
||||
let maintainerCanModify: boolean | undefined
|
||||
@@ -50,6 +52,7 @@ export async function resolveGitHubPrStartPoint(
|
||||
return { error: `PR #${args.prNumber} not found.` }
|
||||
}
|
||||
headRefName = (item.branchName ?? '').trim()
|
||||
baseRefName = (item.baseRefName ?? '').trim()
|
||||
if (!headRefName) {
|
||||
return { error: `PR #${args.prNumber} has no head branch.` }
|
||||
}
|
||||
@@ -69,6 +72,21 @@ export async function resolveGitHubPrStartPoint(
|
||||
return { error: error instanceof Error ? error.message : 'Could not resolve git remote.' }
|
||||
}
|
||||
|
||||
const compareBaseRef = baseRefName ? `refs/remotes/${remote}/${baseRefName}` : undefined
|
||||
|
||||
const fetchCompareBaseRef = async (): Promise<{ error: string } | null> => {
|
||||
if (!baseRefName) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
await args.fetchRemoteTrackingRef(remote, baseRefName)
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error)
|
||||
return { error: `Failed to fetch ${remote}/${baseRefName}: ${message.split('\n')[0]}` }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const fetchPullRequestHeadSha = async (): Promise<{ baseBranch: string } | { error: string }> => {
|
||||
const pullRef = `refs/pull/${args.prNumber}/head`
|
||||
try {
|
||||
@@ -100,11 +118,16 @@ export async function resolveGitHubPrStartPoint(
|
||||
if ('error' in result) {
|
||||
return result
|
||||
}
|
||||
const compareBaseFetchError = await fetchCompareBaseRef()
|
||||
if (compareBaseFetchError) {
|
||||
return compareBaseFetchError
|
||||
}
|
||||
// Why: adopt the contributor's branch name locally (mirroring the same-repo
|
||||
// return below) so fork-PR worktrees aren't renamed with the maintainer's
|
||||
// branch prefix (e.g. `me/866`). The push refspec still targets the fork.
|
||||
return {
|
||||
...result,
|
||||
...(compareBaseRef ? { compareBaseRef } : {}),
|
||||
headSha: result.baseBranch,
|
||||
branchNameOverride: headRefName,
|
||||
...(pushTarget ? { pushTarget } : {}),
|
||||
@@ -122,8 +145,13 @@ export async function resolveGitHubPrStartPoint(
|
||||
const result = await fetchPullRequestHeadSha()
|
||||
if (!('error' in result)) {
|
||||
await resolvePushTarget()
|
||||
const compareBaseFetchError = await fetchCompareBaseRef()
|
||||
if (compareBaseFetchError) {
|
||||
return compareBaseFetchError
|
||||
}
|
||||
return {
|
||||
...result,
|
||||
...(compareBaseRef ? { compareBaseRef } : {}),
|
||||
headSha: result.baseBranch,
|
||||
branchNameOverride: headRefName,
|
||||
...(pushTarget ? { pushTarget } : {}),
|
||||
@@ -147,9 +175,14 @@ export async function resolveGitHubPrStartPoint(
|
||||
if (!headSha) {
|
||||
return { error: `Empty SHA resolving PR #${args.prNumber} head.` }
|
||||
}
|
||||
const compareBaseFetchError = await fetchCompareBaseRef()
|
||||
if (compareBaseFetchError) {
|
||||
return compareBaseFetchError
|
||||
}
|
||||
|
||||
return {
|
||||
baseBranch: headSha,
|
||||
...(compareBaseRef ? { compareBaseRef } : {}),
|
||||
headSha,
|
||||
branchNameOverride: headRefName,
|
||||
pushTarget: { remoteName: remote, branchName: headRefName }
|
||||
|
||||
Reference in New Issue
Block a user