fix: address review findings (#1770)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
concurrency acquire/release pattern and error handling consistent across operations. */
|
||||
import type {
|
||||
ClassifiedError,
|
||||
GitPushTarget,
|
||||
IssueSourcePreference,
|
||||
ListWorkItemsResult,
|
||||
PRInfo,
|
||||
@@ -19,6 +20,7 @@ import { getPRConflictSummary } from './conflict-summary'
|
||||
import {
|
||||
execFileAsync,
|
||||
ghExecFileAsync,
|
||||
gitExecFileAsync,
|
||||
acquire,
|
||||
release,
|
||||
getOwnerRepo,
|
||||
@@ -73,6 +75,90 @@ export async function checkOrcaStarred(): Promise<boolean | null> {
|
||||
}
|
||||
}
|
||||
|
||||
function pickPushRemoteUrl(args: {
|
||||
originUrl: string | null
|
||||
cloneUrl: string
|
||||
sshUrl: string
|
||||
}): string {
|
||||
const { originUrl, cloneUrl, sshUrl } = args
|
||||
if (originUrl && (/^(git@|ssh:)/.test(originUrl) || originUrl.includes('ssh.github.com'))) {
|
||||
return sshUrl
|
||||
}
|
||||
return cloneUrl
|
||||
}
|
||||
|
||||
function sanitizeRemoteName(owner: string, repo: string): string {
|
||||
const slug = `${owner}-${repo}`
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9._-]+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.replace(/^[.-]+|[.-]+$/g, '')
|
||||
return slug ? `pr-${slug}` : 'pr-head'
|
||||
}
|
||||
|
||||
export async function getPullRequestPushTarget(
|
||||
repoPath: string,
|
||||
prNumber: number
|
||||
): Promise<GitPushTarget | null> {
|
||||
const ownerRepo = await getOwnerRepo(repoPath)
|
||||
if (!ownerRepo) {
|
||||
return null
|
||||
}
|
||||
|
||||
await acquire()
|
||||
try {
|
||||
const [{ stdout: prStdout }, origin] = await Promise.all([
|
||||
ghExecFileAsync(['api', `repos/${ownerRepo.owner}/${ownerRepo.repo}/pulls/${prNumber}`], {
|
||||
cwd: repoPath
|
||||
}),
|
||||
getOwnerRepoForRemote(repoPath, 'origin')
|
||||
])
|
||||
const pr = JSON.parse(prStdout) as {
|
||||
head?: {
|
||||
ref?: string
|
||||
repo?: {
|
||||
full_name?: string
|
||||
clone_url?: string
|
||||
ssh_url?: string
|
||||
owner?: { login?: string }
|
||||
name?: string
|
||||
} | null
|
||||
}
|
||||
}
|
||||
const headRepo = pr.head?.repo
|
||||
const branchName = pr.head?.ref?.trim()
|
||||
const owner = headRepo?.owner?.login?.trim()
|
||||
const repo = headRepo?.name?.trim() ?? headRepo?.full_name?.split('/')[1]?.trim()
|
||||
const cloneUrl = headRepo?.clone_url?.trim()
|
||||
const sshUrl = headRepo?.ssh_url?.trim()
|
||||
if (!owner || !repo || !branchName || !cloneUrl || !sshUrl) {
|
||||
return null
|
||||
}
|
||||
if (
|
||||
origin &&
|
||||
origin.owner.toLowerCase() === owner.toLowerCase() &&
|
||||
origin.repo.toLowerCase() === repo.toLowerCase()
|
||||
) {
|
||||
return { remoteName: 'origin', branchName }
|
||||
}
|
||||
|
||||
let originUrl: string | null = null
|
||||
try {
|
||||
const { stdout } = await gitExecFileAsync(['remote', 'get-url', 'origin'], { cwd: repoPath })
|
||||
originUrl = stdout.trim() || null
|
||||
} catch {
|
||||
originUrl = null
|
||||
}
|
||||
return {
|
||||
remoteName: sanitizeRemoteName(owner, repo),
|
||||
branchName,
|
||||
remoteUrl: pickPushRemoteUrl({ originUrl, cloneUrl, sshUrl })
|
||||
}
|
||||
} finally {
|
||||
release()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Star the Orca repo for the authenticated user.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user