From d0fa221ae3d912cd783b0fa13c1e3af9f1aa1f2e Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Tue, 27 Feb 2018 19:50:16 +0100
Subject: [PATCH] fix: only rebase if pr is dirty (github)

---
 lib/platform/github/index.js                          | 7 +++++--
 lib/platform/gitlab/index.js                          | 4 ++++
 lib/workers/pr/index.js                               | 4 ++--
 test/platform/github/__snapshots__/index.spec.js.snap | 1 +
 test/platform/gitlab/__snapshots__/index.spec.js.snap | 2 ++
 test/workers/pr/index.spec.js                         | 3 ++-
 6 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js
index 480af2d401..71f9db1e90 100644
--- a/lib/platform/github/index.js
+++ b/lib/platform/github/index.js
@@ -773,8 +773,11 @@ async function getPr(prNo) {
   pr.displayNumber = `Pull Request #${pr.number}`;
   if (pr.state === 'open') {
     pr.branchName = pr.head ? pr.head.ref : undefined;
-    if (!pr.mergeable || pr.mergeable_state === 'dirty') {
-      logger.debug(`PR is not mergeable`);
+    if (pr.mergeable === true) {
+      pr.canMerge = true;
+    }
+    if (pr.mergeable_state === 'dirty') {
+      logger.debug('PR state is dirty so unmergeable');
       pr.isUnmergeable = true;
     }
     if (pr.commits === 1) {
diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js
index 15b65fcc95..a5e9805b26 100644
--- a/lib/platform/gitlab/index.js
+++ b/lib/platform/gitlab/index.js
@@ -452,7 +452,11 @@ async function getPr(iid) {
   pr.body = pr.description;
   if (pr.merge_status === 'cannot_be_merged') {
     logger.debug('pr cannot be merged');
+    pr.canMerge = false;
     pr.isUnmergeable = true;
+  } else {
+    // Actually.. we can't be sure
+    pr.canMerge = true;
   }
   // Check if the most recent branch commit is by us
   // If not then we don't allow it to be rebased, in case someone's changes would be lost
diff --git a/lib/workers/pr/index.js b/lib/workers/pr/index.js
index be18ca7cdf..a0cbfbaedd 100644
--- a/lib/workers/pr/index.js
+++ b/lib/workers/pr/index.js
@@ -343,8 +343,8 @@ async function checkAutoMerge(pr, config) {
       logger.debug({ pr });
       return false;
     }
-    if (config.requiredStatusChecks && pr.mergeable_state === 'unstable') {
-      logger.info('PR mergeable state is unstable');
+    if (config.requiredStatusChecks && pr.canMerge !== true) {
+      logger.info('PR is not ready for merge');
       return false;
     }
     // Check branch status
diff --git a/test/platform/github/__snapshots__/index.spec.js.snap b/test/platform/github/__snapshots__/index.spec.js.snap
index be846c2e0d..91fdb42415 100644
--- a/test/platform/github/__snapshots__/index.spec.js.snap
+++ b/test/platform/github/__snapshots__/index.spec.js.snap
@@ -465,6 +465,7 @@ Object {
     "sha": "5678",
   },
   "branchName": undefined,
+  "canMerge": true,
   "canRebase": true,
   "commits": 1,
   "displayNumber": "Pull Request #1",
diff --git a/test/platform/gitlab/__snapshots__/index.spec.js.snap b/test/platform/gitlab/__snapshots__/index.spec.js.snap
index 3367e9a10d..f9bdbd1b57 100644
--- a/test/platform/gitlab/__snapshots__/index.spec.js.snap
+++ b/test/platform/gitlab/__snapshots__/index.spec.js.snap
@@ -169,6 +169,7 @@ Object {
   },
   "body": undefined,
   "branchName": "some-branch",
+  "canMerge": true,
   "commits": 1,
   "deletions": 1,
   "displayNumber": "Merge Request #91",
@@ -206,6 +207,7 @@ exports[`platform/gitlab getPr(prNo) returns the PR 1`] = `
 Object {
   "body": "a merge request",
   "branchName": "some-branch",
+  "canMerge": false,
   "canRebase": true,
   "description": "a merge request",
   "displayNumber": "Merge Request #12345",
diff --git a/test/workers/pr/index.spec.js b/test/workers/pr/index.spec.js
index 076221c848..8464342e1f 100644
--- a/test/workers/pr/index.spec.js
+++ b/test/workers/pr/index.spec.js
@@ -36,6 +36,7 @@ describe('workers/pr', () => {
         head: {
           ref: 'somebranch',
         },
+        canMerge: true,
       };
     });
     afterEach(() => {
@@ -67,7 +68,7 @@ describe('workers/pr', () => {
     });
     it('should not automerge if enabled and pr is mergeable but unstable', async () => {
       config.automerge = true;
-      pr.mergeable_state = 'unstable';
+      pr.canMerge = undefined;
       await prWorker.checkAutoMerge(pr, config);
       expect(platform.mergePr.mock.calls.length).toBe(0);
     });
-- 
GitLab