From 2af433094c9daa441013080bf45c230583b2993c Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 21 Jul 2018 08:38:13 +0200
Subject: [PATCH] fix(github): treat 401 Bad Credentials as platform failure

---
 lib/platform/github/gh-got-wrapper.js       |  6 +++++-
 lib/workers/branch/index.js                 |  5 ++++-
 lib/workers/repository/error.js             |  5 +++++
 test/platform/github/gh-got-wrapper.spec.js | 19 +++++++++++++++++++
 test/workers/repository/error.spec.js       |  1 +
 5 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/lib/platform/github/gh-got-wrapper.js b/lib/platform/github/gh-got-wrapper.js
index 2d29de453b..026a555f86 100644
--- a/lib/platform/github/gh-got-wrapper.js
+++ b/lib/platform/github/gh-got-wrapper.js
@@ -129,15 +129,19 @@ async function get(path, options, retries = 5) {
       err.message &&
       err.message.includes('Bad credentials')
     ) {
+      const rateLimit = err.headers ? err.headers['x-ratelimit-limit'] : -1;
       logger.info(
         {
           err,
           message: err.message,
-          rateLimit: err.headers ? err.headers['x-ratelimit-limit'] : -1,
+          rateLimit,
           body: err.response ? err.response.body : undefined,
         },
         'Bad credentials'
       );
+      if (rateLimit === 60) {
+        throw new Error('platform-failure');
+      }
       throw new Error('bad-credentials');
     }
     throw err;
diff --git a/lib/workers/branch/index.js b/lib/workers/branch/index.js
index b3b634c7fd..db887a3cc2 100644
--- a/lib/workers/branch/index.js
+++ b/lib/workers/branch/index.js
@@ -234,7 +234,10 @@ async function processBranch(branchConfig, packageFiles) {
       logger.debug('Passing lockfile-error up');
       throw err;
     }
-    if (err.message !== 'registry-failure') {
+    if (
+      err.message !== 'registry-failure' &&
+      err.message !== 'platform-failure'
+    ) {
       logger.error({ err }, `Error updating branch: ${err.message}`);
     }
     // Don't throw here - we don't want to stop the other renovations
diff --git a/lib/workers/repository/error.js b/lib/workers/repository/error.js
index 5852c143b1..b90ea2a74a 100644
--- a/lib/workers/repository/error.js
+++ b/lib/workers/repository/error.js
@@ -66,6 +66,11 @@ async function handleError(config, err) {
     delete config.branchList; // eslint-disable-line no-param-reassign
     return err.message;
   }
+  if (err.message === 'platform-failure') {
+    logger.info('Platform error - skipping');
+    delete config.branchList; // eslint-disable-line no-param-reassign
+    return err.message;
+  }
   if (err.message === 'rate-limit-exceeded') {
     logger.warn('Rate limit exceeded - aborting');
     delete config.branchList; // eslint-disable-line no-param-reassign
diff --git a/test/platform/github/gh-got-wrapper.spec.js b/test/platform/github/gh-got-wrapper.spec.js
index 4d883f005f..533333f957 100644
--- a/test/platform/github/gh-got-wrapper.spec.js
+++ b/test/platform/github/gh-got-wrapper.spec.js
@@ -90,6 +90,25 @@ describe('platform/gh-got-wrapper', () => {
     expect(e).toBeDefined();
     expect(e.message).toEqual('bad-credentials');
   });
+  it('should throw platform failure', async () => {
+    ghGot.mockImplementationOnce(() =>
+      Promise.reject({
+        statusCode: 401,
+        message: 'Bad credentials. (401)',
+        headers: {
+          'x-ratelimit-limit': 60,
+        },
+      })
+    );
+    let e;
+    try {
+      await get('some-url');
+    } catch (err) {
+      e = err;
+    }
+    expect(e).toBeDefined();
+    expect(e.message).toEqual('platform-failure');
+  });
   it('should throw for blob size', async () => {
     ghGot.mockImplementationOnce(() =>
       Promise.reject({
diff --git a/test/workers/repository/error.spec.js b/test/workers/repository/error.spec.js
index 8c07834e9c..0506d51e57 100644
--- a/test/workers/repository/error.spec.js
+++ b/test/workers/repository/error.spec.js
@@ -25,6 +25,7 @@ describe('workers/repository/error', () => {
       'forbidden',
       'rate-limit-exceeded',
       'lockfile-error',
+      'platform-failure',
     ];
     errors.forEach(err => {
       it(`errors ${err}`, async () => {
-- 
GitLab