From e4a73c23203f6c6795d492b593bc62074fe7405d Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sun, 11 Nov 2018 07:09:39 +0100
Subject: [PATCH] fix(github): detect status checks automerge failure
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

.. and don’t create PRs.

Closes #2786
---
 lib/platform/github/storage.js     |  4 ++++
 lib/workers/branch/automerge.js    | 10 ++++------
 test/platform/github/index.spec.js | 23 +++++++++++++++++++++++
 3 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/lib/platform/github/storage.js b/lib/platform/github/storage.js
index c06e6b7476..ed086e8132 100644
--- a/lib/platform/github/storage.js
+++ b/lib/platform/github/storage.js
@@ -171,6 +171,10 @@ class Storage {
         await get.patch(url, options);
         logger.debug({ branch: branchName }, 'Branch merged');
       } catch (err) {
+        if (err.message.includes('required status checks are expected')) {
+          logger.debug('Branch is not ready for merge: ' + err.message);
+          throw new Error('not ready');
+        }
         logger.info({ err }, `Error pushing branch merge for ${branchName}`);
         throw new Error('Branch automerge failed');
       }
diff --git a/lib/workers/branch/automerge.js b/lib/workers/branch/automerge.js
index 423a1db9e9..2e878306d4 100644
--- a/lib/workers/branch/automerge.js
+++ b/lib/workers/branch/automerge.js
@@ -22,14 +22,12 @@ async function tryBranchAutomerge(config) {
       logger.info({ branch: config.branchName }, 'Branch automerged');
       return 'automerged'; // Branch no longer exists
     } catch (err) {
-      logger.info({ err }, `Failed to automerge branch`);
       // istanbul ignore if
-      if (
-        err.message &&
-        err.message.includes('required status checks are expected')
-      ) {
-        return 'not yet';
+      if (err.message === 'not ready') {
+        logger.info('Branch is not ready for automerge');
+        return 'not ready';
       }
+      logger.info({ err }, `Failed to automerge branch`);
       return 'failed';
     }
   } else if (['failure', 'error'].includes(branchStatus)) {
diff --git a/test/platform/github/index.spec.js b/test/platform/github/index.spec.js
index a41c2dcd9a..94495a99a7 100644
--- a/test/platform/github/index.spec.js
+++ b/test/platform/github/index.spec.js
@@ -928,6 +928,29 @@ describe('platform/github', () => {
       expect(get.put.mock.calls).toMatchSnapshot();
       expect(get.delete.mock.calls).toMatchSnapshot();
     });
+    it('should throw not ready', async () => {
+      await initRepo({
+        repository: 'some/repo',
+        token: 'token',
+      }); // getBranchCommit
+      get.mockImplementationOnce(() => ({
+        body: {
+          object: {
+            sha: '1235',
+          },
+        },
+      }));
+      get.patch.mockImplementationOnce(() => {
+        throw new Error('3 of 3 required status checks are expected.');
+      });
+      let e;
+      try {
+        await github.mergeBranch('thebranchname', 'branch');
+      } catch (err) {
+        e = err;
+      }
+      expect(e.message).toEqual('not ready');
+    });
   });
   describe('getBranchLastCommitTime', () => {
     it('should return a Date', async () => {
-- 
GitLab