| 16 | await Instance.provide({ |
| 17 | directory: process.cwd(), |
| 18 | async fn() { |
| 19 | const project = Instance.project |
| 20 | if (project.vcs !== "git") { |
| 21 | UI.error("Could not find git repository. Please run this command from a git repository.") |
| 22 | process.exit(1) |
| 23 | } |
| 24 | |
| 25 | const prNumber = args.number |
| 26 | const localBranchName = `pr/${prNumber}` |
| 27 | UI.println(`Fetching and checking out PR #${prNumber}...`) |
| 28 | |
| 29 | // Use gh pr checkout with custom branch name |
| 30 | const result = await $`gh pr checkout ${prNumber} --branch ${localBranchName} --force`.nothrow() |
| 31 | |
| 32 | if (result.exitCode !== 0) { |
| 33 | UI.error(`Failed to checkout PR #${prNumber}. Make sure you have gh CLI installed and authenticated.`) |
| 34 | process.exit(1) |
| 35 | } |
| 36 | |
| 37 | // Fetch PR info for fork handling and session link detection |
| 38 | const prInfoResult = |
| 39 | await $`gh pr view ${prNumber} --json headRepository,headRepositoryOwner,isCrossRepository,headRefName,body`.nothrow() |
| 40 | |
| 41 | let sessionId: string | undefined |
| 42 | |
| 43 | if (prInfoResult.exitCode === 0) { |
| 44 | const prInfoText = prInfoResult.text() |
| 45 | if (prInfoText.trim()) { |
| 46 | const prInfo = JSON.parse(prInfoText) |
| 47 | |
| 48 | // Handle fork PRs |
| 49 | if (prInfo && prInfo.isCrossRepository && prInfo.headRepository && prInfo.headRepositoryOwner) { |
| 50 | const forkOwner = prInfo.headRepositoryOwner.login |
| 51 | const forkName = prInfo.headRepository.name |
| 52 | const remoteName = forkOwner |
| 53 | |
| 54 | // Check if remote already exists |
| 55 | const remotes = (await $`git remote`.nothrow().text()).trim() |
| 56 | if (!remotes.split("\n").includes(remoteName)) { |
| 57 | await $`git remote add ${remoteName} https://github.com/${forkOwner}/${forkName}.git`.nothrow() |
| 58 | UI.println(`Added fork remote: ${remoteName}`) |
| 59 | } |
| 60 | |
| 61 | // Set upstream to the fork so pushes go there |
| 62 | const headRefName = prInfo.headRefName |
| 63 | await $`git branch --set-upstream-to=${remoteName}/${headRefName} ${localBranchName}`.nothrow() |
| 64 | } |
| 65 | |
| 66 | // Check for arctic session link in PR body |
| 67 | if (prInfo && prInfo.body) { |
| 68 | const sessionMatch = prInfo.body.match(/https:\/\/arctic\.ai\/s\/([a-zA-Z0-9_-]+)/) |
| 69 | if (sessionMatch) { |
| 70 | const sessionUrl = sessionMatch[0] |
| 71 | UI.println(`Found arctic session: ${sessionUrl}`) |
| 72 | UI.println(`Importing session...`) |
| 73 | |
| 74 | const importResult = await $`arctic import ${sessionUrl}`.nothrow() |
| 75 | if (importResult.exitCode === 0) { |