From c459ba66d6b8bf4069dbd5abbd48ba3f95f67559 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Wed, 28 Jun 2017 13:20:31 +0200
Subject: [PATCH] Fix rebasing logic (#372)

* Add debug messages to API

* Fix rebase logic

* Fix tests

* Simplify logic

* Update branch.spec.js
---
 lib/api/github.js           |  2 ++
 lib/workers/branch.js       | 10 ++++------
 lib/workers/global.js       |  1 +
 test/workers/branch.spec.js |  3 +--
 4 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/lib/api/github.js b/lib/api/github.js
index cccc9d9eff..8bb9d241ed 100644
--- a/lib/api/github.js
+++ b/lib/api/github.js
@@ -386,10 +386,12 @@ async function getPr(prNo) {
   }
   if (!pr.isClosed) {
     if (pr.mergeable_state === 'dirty') {
+      logger.debug(`PR mergeable state is dirty`);
       pr.isUnmergeable = true;
     }
     if (pr.commits === 1) {
       // Only one commit was made - must have been renovate
+      logger.debug('Only 1 commit in PR so rebase is possible');
       pr.canRebase = true;
     } else {
       // Check if only one author of all commits
diff --git a/lib/workers/branch.js b/lib/workers/branch.js
index bd5cf53138..44c5464f7a 100644
--- a/lib/workers/branch.js
+++ b/lib/workers/branch.js
@@ -42,15 +42,13 @@ async function getParentBranch(branchName, config) {
   if (pr.isUnmergeable) {
     logger.debug('PR is unmergeable');
     if (pr.canRebase) {
-      if (config.platform === 'github') {
-        // Setting parentBranch back to undefined means that we'll use the default branch
-        logger.info(`Branch is not mergeable and needs rebasing`);
-        return undefined;
-      } else if (config.platform === 'gitlab') {
+      logger.info(`Branch is not mergeable and needs rebasing`);
+      if (config.isGitLab) {
         logger.info(`Deleting unmergeable branch in order to recreate/rebase`);
         await config.api.deleteBranch(branchName);
-        return undefined;
       }
+      // Setting parentBranch back to undefined means that we'll use the default branch
+      return undefined;
     }
     // Don't do anything different, but warn
     logger.warn(`Branch is not mergeable but can't be rebased`);
diff --git a/lib/workers/global.js b/lib/workers/global.js
index ce87cd6182..be3c78b878 100644
--- a/lib/workers/global.js
+++ b/lib/workers/global.js
@@ -34,5 +34,6 @@ function getRepositoryConfig(globalConfig, index) {
     repository: repoConfig.repository,
   });
   repoConfig.isGitHub = repoConfig.platform === 'github';
+  repoConfig.isGitLab = repoConfig.platform === 'gitlab';
   return configParser.filterConfig(repoConfig, 'repository');
 }
diff --git a/test/workers/branch.spec.js b/test/workers/branch.spec.js
index 0174244038..d61389b36e 100644
--- a/test/workers/branch.spec.js
+++ b/test/workers/branch.spec.js
@@ -16,7 +16,6 @@ describe('workers/branch', () => {
     const branchName = 'foo';
     beforeEach(() => {
       config = {
-        platform: 'github',
         api: {
           branchExists: jest.fn(() => true),
           deleteBranch: jest.fn(),
@@ -65,7 +64,7 @@ describe('workers/branch', () => {
       );
     });
     it('returns undefined if unmergeable and can rebase (gitlab)', async () => {
-      config.platform = 'gitlab';
+      config.isGitLab = true;
       config.api.getBranchPr.mockReturnValue({
         isUnmergeable: true,
         canRebase: true,
-- 
GitLab