From eefc302b87865394388e596a712ab471b4464807 Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Mon, 13 Sep 2021 14:16:20 -0700 Subject: [PATCH] fix(ng-dev/pr): Move the cleanup of the merge attempt to the finally 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. --- ng-dev/pr/merge/task.ts | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/ng-dev/pr/merge/task.ts b/ng-dev/pr/merge/task.ts index d37b94221..27bc0b4b5 100644 --- a/ng-dev/pr/merge/task.ts +++ b/ng-dev/pr/merge/task.ts @@ -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); @@ -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) { @@ -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); } } }