From c5edc5d54edaed93084b27026a7adf9c52e79296 Mon Sep 17 00:00:00 2001 From: jingyang <k.jingyang@gmail.com> Date: Thu, 2 Mar 2023 19:58:13 +0800 Subject: [PATCH] fix: checkout base branch after commit files (#20658) Co-authored-by: Rhys Arkins <rhys@arkins.net> --- .../repository/update/branch/index.spec.ts | 22 +++++++++++++++++++ lib/workers/repository/update/branch/index.ts | 3 +++ 2 files changed, 25 insertions(+) diff --git a/lib/workers/repository/update/branch/index.spec.ts b/lib/workers/repository/update/branch/index.spec.ts index ff9b3fac8a..da9c8329a4 100644 --- a/lib/workers/repository/update/branch/index.spec.ts +++ b/lib/workers/repository/update/branch/index.spec.ts @@ -2145,5 +2145,27 @@ describe('workers/repository/update/branch/index', () => { expect(automerge.tryBranchAutomerge).not.toHaveBeenCalled(); expect(prWorker.ensurePr).toHaveBeenCalledTimes(1); }); + + it('checks out baseBranch after committing files', async () => { + getUpdated.getUpdatedPackageFiles.mockResolvedValueOnce({ + ...updatedPackageFiles, + }); + npmPostExtract.getAdditionalFiles.mockResolvedValueOnce({ + artifactErrors: [], + updatedArtifacts: [], + }); + config.baseBranch = 'main'; + await branchWorker.processBranch(config); + expect(git.checkoutBranch).toHaveBeenLastCalledWith('main'); + // Check that the last checkoutBranch call is after the only commitFilesToBranch call + const checkoutBranchCalledTimes = git.checkoutBranch.mock.calls.length; + expect( + commit.commitFilesToBranch.mock.invocationCallOrder[0] + ).toBeLessThan( + git.checkoutBranch.mock.invocationCallOrder[ + checkoutBranchCalledTimes - 1 + ] + ); + }); }); }); diff --git a/lib/workers/repository/update/branch/index.ts b/lib/workers/repository/update/branch/index.ts index b3e609ee10..b779a277aa 100644 --- a/lib/workers/repository/update/branch/index.ts +++ b/lib/workers/repository/update/branch/index.ts @@ -498,6 +498,9 @@ export async function processBranch( } commitSha = await commitFilesToBranch(config); + // Checkout to base branch to ensure that the next branch processing always starts with git being on the baseBranch + // baseBranch is not checked out at the start of processBranch() due to pull/16246 + await checkoutBranch(config.baseBranch); updatesVerified = true; } // istanbul ignore if -- GitLab