Skip to content

Commit

Permalink
fix(ng-dev/pr): Move the cleanup of the merge attempt to the finally …
Browse files Browse the repository at this point in the history
…block

Move the cleanup of the merge attempt to the finally block to ensure that the cleanup
of the generated branches occurs regardless of the success of the merge attempt.
  • Loading branch information
josephperrott committed Sep 13, 2021
1 parent 0c43579 commit eefc302
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions ng-dev/pr/merge/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,11 @@ export class PullRequestMergeTask {

// Branch or revision that is currently checked out so that we can switch back to
// it once the pull request has been merged.
let previousBranchOrRevision: null | string = null;
const previousBranchOrRevision = this.git.getCurrentBranchOrRevision();

// The following block runs Git commands as child processes. These Git commands can fail.
// We want to capture these command errors and return an appropriate merge request status.
try {
previousBranchOrRevision = this.git.getCurrentBranchOrRevision();

// Run preparations for the merge (e.g. fetching branches).
await strategy.prepare(pullRequest);

Expand All @@ -146,12 +144,6 @@ export class PullRequestMergeTask {
return {status: MergeStatus.FAILED, failure};
}

// Switch back to the previous branch. We need to do this before deleting the temporary
// branches because we cannot delete branches which are currently checked out.
this.git.run(['checkout', '-f', previousBranchOrRevision]);

await strategy.cleanup(pullRequest);

// Return a successful merge status.
return {status: MergeStatus.SUCCESS};
} catch (e) {
Expand All @@ -162,11 +154,11 @@ export class PullRequestMergeTask {
}
throw e;
} finally {
// Always try to restore the branch if possible. We don't want to leave
// the repository in a different state than before.
if (previousBranchOrRevision !== null) {
this.git.runGraceful(['checkout', '-f', previousBranchOrRevision]);
}
// Switch back to the previous branch. We need to do this before deleting the temporary
// branches because we cannot delete branches which are currently checked out.
this.git.run(['checkout', '-f', previousBranchOrRevision]);

await strategy.cleanup(pullRequest);
}
}
}

0 comments on commit eefc302

Please sign in to comment.