From 2fd1c474850a33f74d4b14daa328d6246ec0891b Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Tue, 18 Sep 2018 20:41:54 +0200
Subject: [PATCH] fix(github): catch fork patch errors and throw

---
 lib/platform/github/index.js | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/lib/platform/github/index.js b/lib/platform/github/index.js
index 536fa8e84a..4cd650173f 100644
--- a/lib/platform/github/index.js
+++ b/lib/platform/github/index.js
@@ -221,15 +221,24 @@ async function initRepo({
       );
       // This is a lovely "hack" by GitHub that lets us force update our fork's master
       // with the base commit from the parent repository
-      await get.patch(
-        `repos/${config.repository}/git/refs/heads/${config.baseBranch}`,
-        {
-          body: {
-            sha: parentSha,
-          },
-          token: forkToken || opts.token,
+      try {
+        await get.patch(
+          `repos/${config.repository}/git/refs/heads/${config.baseBranch}`,
+          {
+            body: {
+              sha: parentSha,
+            },
+            token: forkToken || opts.token,
+          }
+        );
+      } catch (err) /* istanbul ignore next */ {
+        if (
+          err.statusCode === 422 &&
+          err.message.startsWith('Object does not exist')
+        ) {
+          throw new Error('repository-changed');
         }
-      );
+      }
     } else {
       logger.info({ repository_fork: config.repository }, 'Created fork');
       // Wait an arbitrary 30s to hopefully give GitHub enough time for forking to complete
-- 
GitLab