Skip to content

Commit 2f04077

Browse files
Bill LeoutsakosBill Leoutsakos
authored andcommitted
fix(pi): recheck update PR ambiguity
1 parent 0ac1fae commit 2f04077

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

apps/sim/executor/handlers/pi/cloud-backend.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,22 @@ describe('runCloudPi', () => {
797797
expect(result.prUrl).toBe('https://github.com/octo/demo/pull/7')
798798
})
799799

800+
it('fails when a second PR for the branch appears during authoring', async () => {
801+
mockExistingBranchPullRequest()
802+
mockExecuteTool.mockResolvedValueOnce({
803+
success: true,
804+
output: { items: [{ number: 7 }, { number: 8 }], count: 2 },
805+
})
806+
807+
await expect(runCloudBranchPi(branchParams(), { onEvent: vi.fn() })).rejects.toThrow(
808+
/multiple open pull requests/
809+
)
810+
expect(
811+
mockExecuteTool.mock.calls.some(([tool]: [string]) => tool === 'github_update_pr')
812+
).toBe(false)
813+
expect(mockRunBabysit).not.toHaveBeenCalled()
814+
})
815+
800816
it('does not claim a push happened when no-op authoring is followed by a PR error', async () => {
801817
mockRun.mockImplementation((command: string) => {
802818
if (command.includes('git clone')) {
@@ -840,6 +856,12 @@ describe('runCloudPi', () => {
840856
return Promise.resolve({ stdout: '__NO_CHANGES__=1', stderr: '', exitCode: 0 })
841857
})
842858
mockExecuteTool.mockImplementation((tool: string) => {
859+
if (tool === 'github_list_prs_v2') {
860+
return Promise.resolve({
861+
success: true,
862+
output: { items: [{ number: 7 }], count: 1 },
863+
})
864+
}
843865
if (tool === 'github_pr_v2') {
844866
return Promise.resolve(existingPullRequestOutput())
845867
}

apps/sim/executor/handlers/pi/cloud-backend.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,7 @@ async function ensureUpdatePullRequest(
296296
secrets: readonly string[],
297297
signal?: AbortSignal
298298
): Promise<OpenedPullRequest> {
299-
if (existingPullRequest) {
300-
return updatePullRequest(params, existingPullRequest, secrets, signal)
301-
}
302-
const newlyCreatedPullRequest = await findOpenPrForBranch(
299+
const currentPullRequest = await findOpenPrForBranch(
303300
{
304301
owner: params.owner,
305302
repo: params.repo,
@@ -308,8 +305,14 @@ async function ensureUpdatePullRequest(
308305
},
309306
signal
310307
)
311-
if (newlyCreatedPullRequest) {
312-
return updatePullRequest(params, newlyCreatedPullRequest, secrets, signal)
308+
if (existingPullRequest) {
309+
if (currentPullRequest && currentPullRequest.pullNumber !== existingPullRequest.pullNumber) {
310+
throw new Error(`The open pull request for branch ${branch} changed during authoring`)
311+
}
312+
return updatePullRequest(params, existingPullRequest, secrets, signal)
313+
}
314+
if (currentPullRequest) {
315+
return updatePullRequest(params, currentPullRequest, secrets, signal)
313316
}
314317
const base = params.baseBranch?.trim() || (await repositoryDefaultBranch(params, signal))
315318
const draft = params.babysit ? false : params.prState !== 'ready'

0 commit comments

Comments
 (0)