From aa84074d76c12e72c4cee3f07862b2571f439bc4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dragomir=20=C8=9Aurcanu?= <dragomirt22@gmail.com>
Date: Thu, 7 Jun 2018 15:42:51 +0300
Subject: [PATCH] feat: comment in PR when branch automerge fails (#2058)

This PR adds the feature of commenting on a failed automerge. It's done by adding a conditional in `lib/workers/branch/automerge.js` which, in case of receiving `failure` or `error` from the `getBranchStatus` function, returns the "branch status error" value. Another modification is in `lib/workers/branch/index.js`, which is an adition to the failure response of the `tryBranchAutomerge` function. The added functionality is the ability to add a comment to the PR which had a failure automerging. In case of receiving the aforementioned "branch status error" value, to the comment is appended a note which emphasize the fact that there're multiple failed status checks.

Closes #1934
---
 lib/workers/branch/automerge.js       |  2 ++
 lib/workers/branch/index.js           |  2 ++
 lib/workers/pr/index.js               | 10 ++++++++++
 test/workers/branch/automerge.spec.js |  6 ++++++
 test/workers/pr/index.spec.js         |  1 +
 5 files changed, 21 insertions(+)

diff --git a/lib/workers/branch/automerge.js b/lib/workers/branch/automerge.js
index b9f94eec86..7d22e0990d 100644
--- a/lib/workers/branch/automerge.js
+++ b/lib/workers/branch/automerge.js
@@ -24,6 +24,8 @@ async function tryBranchAutomerge(config) {
       logger.info({ err }, `Failed to automerge branch`);
       return 'failed';
     }
+  } else if (['failure', 'error'].includes(branchStatus)) {
+    return 'branch status error';
   } else {
     logger.debug(`Branch status is "${branchStatus}" - skipping automerge`);
   }
diff --git a/lib/workers/branch/index.js b/lib/workers/branch/index.js
index bf6d48cff9..913765e08c 100644
--- a/lib/workers/branch/index.js
+++ b/lib/workers/branch/index.js
@@ -177,10 +177,12 @@ async function processBranch(branchConfig, packageFiles) {
       return 'automerged';
     } else if (
       mergeStatus === 'automerge aborted - PR exists' ||
+      mergeStatus === 'branch status error' ||
       mergeStatus === 'failed'
     ) {
       logger.info({ mergeStatus }, 'Branch automerge not possible');
       config.forcePr = true;
+      config.branchAutomergeFailureMessage = mergeStatus;
     }
   } catch (err) /* istanbul ignore next */ {
     if (err.message === 'rate-limit-exceeded') {
diff --git a/lib/workers/pr/index.js b/lib/workers/pr/index.js
index f39462fb35..661da99d15 100644
--- a/lib/workers/pr/index.js
+++ b/lib/workers/pr/index.js
@@ -293,6 +293,16 @@ async function ensurePr(prConfig) {
       }
       return null;
     }
+    if (config.branchAutomergeFailureMessage) {
+      const subject = 'Branch automerge failure';
+      let content =
+        'This PR was configured for branch automerge, however this is not possible so it has been raised as a PR instead.';
+      if (config.branchAutomergeFailureMessage === 'branch status error') {
+        content += '\n___\n * Branch has one or more failed status checks';
+      }
+      logger.info('Adding branch automerge failure message to PR');
+      platform.ensureComment(pr.number, subject, content);
+    }
     // Skip assign and review if automerging PR
     if (config.automerge && branchStatus !== 'failure') {
       logger.debug(
diff --git a/test/workers/branch/automerge.spec.js b/test/workers/branch/automerge.spec.js
index fc10e0fc06..b86c18ef97 100644
--- a/test/workers/branch/automerge.spec.js
+++ b/test/workers/branch/automerge.spec.js
@@ -24,6 +24,12 @@ describe('workers/branch/automerge', () => {
       platform.getBranchStatus.mockReturnValueOnce('pending');
       expect(await tryBranchAutomerge(config)).toBe('no automerge');
     });
+    it('returns branch status error if branch status is failure', async () => {
+      config.automerge = true;
+      config.automergeType = 'branch-push';
+      platform.getBranchStatus.mockReturnValueOnce('failure');
+      expect(await tryBranchAutomerge(config)).toBe('branch status error');
+    });
     it('returns false if PR exists', async () => {
       platform.getBranchPr.mockReturnValueOnce({});
       config.automerge = true;
diff --git a/test/workers/pr/index.spec.js b/test/workers/pr/index.spec.js
index fac8cb0bd1..044a4ee95f 100644
--- a/test/workers/pr/index.spec.js
+++ b/test/workers/pr/index.spec.js
@@ -298,6 +298,7 @@ describe('workers/pr', () => {
     it('should create PR if branch tests failed', async () => {
       config.automerge = true;
       config.automergeType = 'branch-push';
+      config.branchAutomergeFailureMessage = 'branch status error';
       platform.getBranchStatus.mockReturnValueOnce('failure');
       const pr = await prWorker.ensurePr(config);
       expect(pr).toMatchObject({ displayNumber: 'New Pull Request' });
-- 
GitLab