From 1bacabcb7e4a1b8db45e1772aefd616ae59be80a Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Tue, 1 Nov 2022 12:46:22 +0300
Subject: [PATCH] fix(git): Ensure `baseBranch` is checked out before git-based
 merge (#18687)

---
 .../repository/update/branch/automerge.spec.ts     | 14 ++++++++++++--
 lib/workers/repository/update/branch/automerge.ts  |  3 ++-
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/lib/workers/repository/update/branch/automerge.spec.ts b/lib/workers/repository/update/branch/automerge.spec.ts
index ba707e461a..06e307a396 100644
--- a/lib/workers/repository/update/branch/automerge.spec.ts
+++ b/lib/workers/repository/update/branch/automerge.spec.ts
@@ -66,18 +66,28 @@ describe('workers/repository/update/branch/automerge', () => {
     it('returns false if automerge fails', async () => {
       config.automerge = true;
       config.automergeType = 'branch';
+      config.baseBranch = 'test-branch';
       platform.getBranchStatus.mockResolvedValueOnce(BranchStatus.green);
       git.mergeBranch.mockImplementationOnce(() => {
         throw new Error('merge error');
       });
-      expect(await tryBranchAutomerge(config)).toBe('failed');
+
+      const res = await tryBranchAutomerge(config);
+
+      expect(res).toBe('failed');
+      expect(git.checkoutBranch).toHaveBeenCalled();
     });
 
     it('returns true if automerge succeeds', async () => {
       config.automerge = true;
       config.automergeType = 'branch';
+      config.baseBranch = 'test-branch';
       platform.getBranchStatus.mockResolvedValueOnce(BranchStatus.green);
-      expect(await tryBranchAutomerge(config)).toBe('automerged');
+
+      const res = await tryBranchAutomerge(config);
+
+      expect(res).toBe('automerged');
+      expect(git.checkoutBranch).toHaveBeenCalledWith('test-branch');
     });
 
     it('returns true if automerge succeeds (dry-run)', async () => {
diff --git a/lib/workers/repository/update/branch/automerge.ts b/lib/workers/repository/update/branch/automerge.ts
index 9bbbc551e9..99808bf23e 100644
--- a/lib/workers/repository/update/branch/automerge.ts
+++ b/lib/workers/repository/update/branch/automerge.ts
@@ -4,7 +4,7 @@ import type { RenovateConfig } from '../../../../config/types';
 import { logger } from '../../../../logger';
 import { platform } from '../../../../modules/platform';
 import { BranchStatus } from '../../../../types';
-import { mergeBranch } from '../../../../util/git';
+import { checkoutBranch, mergeBranch } from '../../../../util/git';
 import { isScheduledNow } from './schedule';
 import { resolveBranchStatus } from './status-checks';
 
@@ -43,6 +43,7 @@ export async function tryBranchAutomerge(
         // TODO: types (#7154)
         logger.info(`DRY-RUN: Would automerge branch ${config.branchName!}`);
       } else {
+        await checkoutBranch(config.baseBranch!);
         await mergeBranch(config.branchName!);
       }
       logger.info({ branch: config.branchName }, 'Branch automerged');
-- 
GitLab