Skip to content

Commit 0a9a56f

Browse files
authored
Skip branch creation on PR saga retry (#1455)
## TL;DR Prevent duplicate branch creation attempts when the PR saga is retried by checking if the target branch already exists before attempting to create it. closes #1443 ## Problem When the PR creation saga is retried, it was attempting to create a branch that may have already been created in the previous attempt, causing unnecessary operations and potential failures. ## Changes - Added a check to compare the current branch against the target branch name before executing branch creation - Only create the branch if the current branch differs from the target branch name (indicating it hasn't been created yet) - On retry scenarios, the branch creation step is skipped since we're already on the desired branch - Refactored `originalBranch` variable to `currentBranch` for clarity since we check it at the start ## How did you test this? This is a defensive fix for the retry path in PR creation. The logic ensures that if a saga is retried after branch creation has already succeeded, we won't attempt to recreate the branch.
1 parent 0e0896f commit 0a9a56f

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

apps/code/src/main/services/git/create-pr-saga.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,28 @@ export class CreatePrSaga extends Saga<CreatePrSagaInput, CreatePrSagaOutput> {
6666
let { commitMessage, prTitle, prBody } = input;
6767

6868
if (input.branchName) {
69-
this.deps.onProgress(
70-
"creating-branch",
71-
`Creating branch ${input.branchName}...`,
69+
const currentBranch = await this.readOnlyStep("get-original-branch", () =>
70+
this.deps.getCurrentBranch(directoryPath),
7271
);
7372

74-
const originalBranch = await this.readOnlyStep(
75-
"get-original-branch",
76-
() => this.deps.getCurrentBranch(directoryPath),
77-
);
73+
// on retry, do not attempt to re-create the branch
74+
if (currentBranch !== input.branchName) {
75+
this.deps.onProgress(
76+
"creating-branch",
77+
`Creating branch ${input.branchName}...`,
78+
);
7879

79-
await this.step({
80-
name: "creating-branch",
81-
execute: () => this.deps.createBranch(directoryPath, input.branchName!),
82-
rollback: async () => {
83-
if (originalBranch) {
84-
await this.deps.checkoutBranch(directoryPath, originalBranch);
85-
}
86-
},
87-
});
80+
await this.step({
81+
name: "creating-branch",
82+
execute: () =>
83+
this.deps.createBranch(directoryPath, input.branchName!),
84+
rollback: async () => {
85+
if (currentBranch) {
86+
await this.deps.checkoutBranch(directoryPath, currentBranch);
87+
}
88+
},
89+
});
90+
}
8891
}
8992

9093
const changedFiles = await this.readOnlyStep("check-changes", () =>

0 commit comments

Comments
 (0)