From 40a048d67daee3e65a7721cd450213342b952f3f Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sun, 7 Mar 2021 10:00:20 +0100
Subject: [PATCH] =?UTF-8?q?fix:=20don=E2=80=99t=20rebase=20for=20automerge?=
 =?UTF-8?q?=20when=20rebaseWhen=3Dconflicted=20(#9011)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 lib/workers/branch/reuse.spec.ts | 6 ++----
 lib/workers/branch/reuse.ts      | 8 +++++---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/lib/workers/branch/reuse.spec.ts b/lib/workers/branch/reuse.spec.ts
index 0d60da376f..762ed32024 100644
--- a/lib/workers/branch/reuse.spec.ts
+++ b/lib/workers/branch/reuse.spec.ts
@@ -159,15 +159,13 @@ describe(getName(__filename), () => {
       expect(git.isBranchModified).not.toHaveBeenCalled();
     });
 
-    it('returns false if automerge, rebaseWhen=conflicted and stale', async () => {
+    it('returns true if automerge, rebaseWhen=conflicted and stale', async () => {
       config.rebaseWhen = 'conflicted';
       config.automerge = true;
       git.branchExists.mockReturnValueOnce(true);
       git.isBranchStale.mockResolvedValueOnce(true);
       const res = await shouldReuseExistingBranch(config);
-      expect(res.reuseExistingBranch).toBe(false);
-      expect(git.isBranchStale).toHaveBeenCalled();
-      expect(git.isBranchModified).toHaveBeenCalled();
+      expect(res.reuseExistingBranch).toBe(true);
     });
   });
 });
diff --git a/lib/workers/branch/reuse.ts b/lib/workers/branch/reuse.ts
index 33e7663dd3..c105d1f203 100644
--- a/lib/workers/branch/reuse.ts
+++ b/lib/workers/branch/reuse.ts
@@ -48,8 +48,8 @@ export async function shouldReuseExistingBranch(
 
   if (
     config.rebaseWhen === 'behind-base-branch' ||
-    (config.rebaseWhen !== 'never' && config.automerge === true) ||
-    (config.rebaseWhen === 'auto' && (await platform.getRepoForceRebase()))
+    (config.rebaseWhen === 'auto' &&
+      (config.automerge || (await platform.getRepoForceRebase())))
   ) {
     if (await isBranchStale(branchName)) {
       logger.debug(`Branch is stale and needs rebasing`);
@@ -63,7 +63,9 @@ export async function shouldReuseExistingBranch(
     }
     logger.debug('Branch is up-to-date');
   } else {
-    logger.debug('Skipping stale branch check');
+    logger.debug(
+      `Skipping stale branch check due to rebaseWhen=${config.rebaseWhen}`
+    );
   }
 
   // Now check if PR is unmergeable. If so then we also rebase
-- 
GitLab