From 621badb964d85a34d1492a49db562b0ab853ff44 Mon Sep 17 00:00:00 2001
From: Adam Moss <adam.moss@bcs.org.uk>
Date: Mon, 5 Mar 2018 20:02:00 +0000
Subject: [PATCH] fix(allow-failure): enabled automerge when allowed failures
 occur

Closes: #1586

Signed-off-by: Adam Moss <adam.moss@bcs.org.uk>
---
 lib/platform/gitlab/index.js       | 10 ++++++----
 test/platform/gitlab/index.spec.js | 18 ++++++++++++++++--
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/lib/platform/gitlab/index.js b/lib/platform/gitlab/index.js
index 52d69e2ff2..af0549c3db 100644
--- a/lib/platform/gitlab/index.js
+++ b/lib/platform/gitlab/index.js
@@ -242,10 +242,12 @@ async function getBranchStatus(branchName, requiredStatusChecks) {
   res.body.forEach(check => {
     // If one is failed then don't overwrite that
     if (status !== 'failure') {
-      if (check.status === 'failed') {
-        status = 'failure';
-      } else if (check.status !== 'success') {
-        ({ status } = check);
+      if (!check.allow_failure) {
+        if (check.status === 'failed') {
+          status = 'failure';
+        } else if (check.status !== 'success') {
+          ({ status } = check);
+        }
       }
     }
   });
diff --git a/test/platform/gitlab/index.spec.js b/test/platform/gitlab/index.spec.js
index cfc9d94cf9..a2015e606b 100644
--- a/test/platform/gitlab/index.spec.js
+++ b/test/platform/gitlab/index.spec.js
@@ -308,9 +308,23 @@ describe('platform/gitlab', () => {
       const res = await gitlab.getBranchStatus('somebranch', []);
       expect(res).toEqual('success');
     });
-    it('returns failure if any are failed', async () => {
+    it('returns success if optional jobs fail', async () => {
       get.mockReturnValueOnce({
-        body: [{ status: 'success' }, { status: 'failed' }],
+        body: [
+          { status: 'success' },
+          { status: 'failed', allow_failure: true },
+        ],
+      });
+      const res = await gitlab.getBranchStatus('somebranch', []);
+      expect(res).toEqual('success');
+    });
+    it('returns failure if any mandatory jobs fails', async () => {
+      get.mockReturnValueOnce({
+        body: [
+          { status: 'success' },
+          { status: 'failed', allow_failure: true },
+          { status: 'failed' },
+        ],
       });
       const res = await gitlab.getBranchStatus('somebranch', []);
       expect(res).toEqual('failure');
-- 
GitLab