From ef2a13f4f28f5b2c163991c0c5d4f53b4f45ea9f Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 8 Mar 2021 10:02:48 +0100
Subject: [PATCH] =?UTF-8?q?fix:=20don=E2=80=99t=20throw=20repository-chang?=
 =?UTF-8?q?ed=20from=20git=20layer=20(#9032)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/platform/bitbucket-server/index.ts | 4 ++++
 lib/util/git/index.ts                  | 6 +++---
 lib/util/http/github.ts                | 1 +
 lib/workers/branch/check-existing.ts   | 2 +-
 lib/workers/branch/index.ts            | 1 +
 5 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts
index 410d12fcf4..e70cfb24fb 100644
--- a/lib/platform/bitbucket-server/index.ts
+++ b/lib/platform/bitbucket-server/index.ts
@@ -408,6 +408,7 @@ export async function getBranchStatus(
   }
 
   if (!git.branchExists(branchName)) {
+    logger.debug('Branch does not exist - cannot fetch status');
     throw new Error(REPOSITORY_CHANGED);
   }
 
@@ -604,6 +605,9 @@ export async function addReviewers(
       err.statusCode === 409 &&
       !utils.isInvalidReviewersResponse(err)
     ) {
+      logger.debug(
+        '409 response to adding reviewers - has repository changed?'
+      );
       throw new Error(REPOSITORY_CHANGED);
     } else {
       throw err;
diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts
index 53d2f38299..a5bc85338c 100644
--- a/lib/util/git/index.ts
+++ b/lib/util/git/index.ts
@@ -11,7 +11,6 @@ import { join } from 'upath';
 import { configFileNames } from '../../config/app-strings';
 import {
   CONFIG_VALIDATION,
-  REPOSITORY_CHANGED,
   REPOSITORY_DISABLED,
   REPOSITORY_EMPTY,
   SYSTEM_INSUFFICIENT_DISK_SPACE,
@@ -697,8 +696,9 @@ export async function commitFiles({
       logger.error({ err }, 'Error committing files.');
       return null;
     }
-    logger.debug({ err }, 'Error committing files');
-    throw new Error(REPOSITORY_CHANGED);
+    logger.debug({ err }, 'Unknown error committing files');
+    // We don't know why this happened, so this will cause bubble up to a branch error
+    throw err;
   }
 }
 
diff --git a/lib/util/http/github.ts b/lib/util/http/github.ts
index 178793424b..d2424f8d06 100644
--- a/lib/util/http/github.ts
+++ b/lib/util/http/github.ts
@@ -109,6 +109,7 @@ function handleGotError(
     ) {
       throw err;
     } else if (err.body?.errors?.find((e: any) => e.code === 'invalid')) {
+      logger.debug({ err }, 'Received invalid response - aborting');
       throw new Error(REPOSITORY_CHANGED);
     }
     logger.debug({ err }, '422 Error thrown from GitHub');
diff --git a/lib/workers/branch/check-existing.ts b/lib/workers/branch/check-existing.ts
index 40ae19b4d6..c59aaa3ea6 100644
--- a/lib/workers/branch/check-existing.ts
+++ b/lib/workers/branch/check-existing.ts
@@ -24,7 +24,7 @@ export async function prAlreadyExisted(
     const prDetails = await platform.getPr(pr.number);
     // istanbul ignore if
     if (prDetails.state === PrState.Open) {
-      logger.debug('PR reopened');
+      logger.debug('PR reopened - aborting run');
       throw new Error(REPOSITORY_CHANGED);
     }
     return pr;
diff --git a/lib/workers/branch/index.ts b/lib/workers/branch/index.ts
index 8835a0eb11..717fdcf4fd 100644
--- a/lib/workers/branch/index.ts
+++ b/lib/workers/branch/index.ts
@@ -560,6 +560,7 @@ export async function processBranch(
     }
   } catch (err) /* istanbul ignore next */ {
     if (err.statusCode === 404) {
+      logger.debug({ err }, 'Received a 404 error - aborting run');
       throw new Error(REPOSITORY_CHANGED);
     }
     if (err.message === PLATFORM_RATE_LIMIT_EXCEEDED) {
-- 
GitLab