From 660a208bd213852ceaaecd636eb59bcd6b8b5baa Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Sat, 16 Sep 2017 06:39:04 +0200
Subject: [PATCH] fix: retry api rate limit exceeded (#816)

---
 lib/api/gh-got-retry.js | 13 +++++++++++++
 test/api/github.spec.js |  6 ++++++
 2 files changed, 19 insertions(+)

diff --git a/lib/api/gh-got-retry.js b/lib/api/gh-got-retry.js
index a1922fcf60..ba2e6ce181 100644
--- a/lib/api/gh-got-retry.js
+++ b/lib/api/gh-got-retry.js
@@ -34,6 +34,19 @@ async function ghGotRetry(path, opts, retries = 5) {
       }
       return ghGotRetry(path, opts, retries - 1);
     }
+    if (
+      retries > 0 &&
+      err.statusCode === 403 &&
+      err.message &&
+      err.message.indexOf('API rate limit') === 0
+    ) {
+      logger.debug(`Retrying API rate limit`);
+      // istanbul ignore if
+      if (process.env.NODE_ENV !== 'test') {
+        await sleep(30000 / (retries * retries));
+      }
+      return ghGotRetry(path, opts, retries - 1);
+    }
     throw err;
   }
 }
diff --git a/test/api/github.spec.js b/test/api/github.spec.js
index 726e584d91..a3d59656dc 100644
--- a/test/api/github.spec.js
+++ b/test/api/github.spec.js
@@ -75,6 +75,12 @@ describe('api/github', () => {
           statusCode: 502,
         })
       );
+      ghGot.mockImplementationOnce(() =>
+        Promise.reject({
+          statusCode: 403,
+          message: 'API rate limit exceeded for x.',
+        })
+      );
       ghGot.mockImplementationOnce(() =>
         Promise.reject({
           statusCode: 403,
-- 
GitLab