From 2b7e885edb96506ea69bc57df086f17f92a7b06e Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Wed, 7 Sep 2022 14:59:29 +0300
Subject: [PATCH] fix(github): Catch errors inside `fetchPr` call (#17666)

---
 lib/modules/platform/github/common.ts     |  1 +
 lib/modules/platform/github/index.spec.ts |  2 +-
 lib/modules/platform/github/index.ts      | 17 +++++++++++------
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/lib/modules/platform/github/common.ts b/lib/modules/platform/github/common.ts
index 121b52274b..fa993fca1e 100644
--- a/lib/modules/platform/github/common.ts
+++ b/lib/modules/platform/github/common.ts
@@ -8,6 +8,7 @@ import type { GhRestPr } from './types';
  * @see https://docs.github.com/en/rest/reference/pulls#list-pull-requests
  */
 export function coerceRestPr(pr: GhRestPr | null | undefined): Pr | null {
+  // istanbul ignore if
   if (!pr) {
     return null;
   }
diff --git a/lib/modules/platform/github/index.spec.ts b/lib/modules/platform/github/index.spec.ts
index 696919b49b..4f591154a4 100644
--- a/lib/modules/platform/github/index.spec.ts
+++ b/lib/modules/platform/github/index.spec.ts
@@ -2504,7 +2504,7 @@ describe('modules/platform/github/index', () => {
         )
         .reply(200, [])
         .get('/repos/some/repo/pulls/1234')
-        .reply(200);
+        .reply(404);
       await github.initRepo({ repository: 'some/repo' });
       const pr = await github.getPr(1234);
       expect(pr).toBeNull();
diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts
index 977af32b3c..db1b1baf49 100644
--- a/lib/modules/platform/github/index.ts
+++ b/lib/modules/platform/github/index.ts
@@ -582,12 +582,17 @@ function cachePr(pr?: Pr | null): void {
 
 // Fetch fresh Pull Request and cache it when possible
 async function fetchPr(prNo: number): Promise<Pr | null> {
-  const { body: ghRestPr } = await githubApi.getJson<GhRestPr>(
-    `repos/${config.parentRepo ?? config.repository}/pulls/${prNo}`
-  );
-  const result = coerceRestPr(ghRestPr);
-  cachePr(result);
-  return result;
+  try {
+    const { body: ghRestPr } = await githubApi.getJson<GhRestPr>(
+      `repos/${config.parentRepo ?? config.repository}/pulls/${prNo}`
+    );
+    const result = coerceRestPr(ghRestPr);
+    cachePr(result);
+    return result;
+  } catch (err) {
+    logger.warn({ err, prNo }, `GitHub fetchPr error`);
+    return null;
+  }
 }
 
 // Gets details for a PR
-- 
GitLab