From e4d5a0e2fd57daf7c4d27a54358fb2d3d940ee73 Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Sat, 4 Jun 2022 14:02:31 +0300
Subject: [PATCH] fix(github): Throw on GraphQL errors during cache fetching
 (#15891)

---
 .../github-releases/cache/cache-base.spec.ts  | 24 ++++++++++++++++++-
 .../github-releases/cache/cache-base.ts       |  8 ++++++-
 lib/util/http/github.ts                       |  1 -
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/lib/modules/datasource/github-releases/cache/cache-base.spec.ts b/lib/modules/datasource/github-releases/cache/cache-base.spec.ts
index 640fcdcc7f..b36e1ce61a 100644
--- a/lib/modules/datasource/github-releases/cache/cache-base.spec.ts
+++ b/lib/modules/datasource/github-releases/cache/cache-base.spec.ts
@@ -234,7 +234,7 @@ describe('modules/datasource/github-releases/cache/cache-base', () => {
     ]);
   });
 
-  it('returns cached values on server errors', async () => {
+  it('throws for http errors', async () => {
     packageCache.get.mockResolvedValueOnce({
       items: {
         v1: { version: 'v1', releaseTimestamp: t1, bar: 'aaa' },
@@ -257,4 +257,26 @@ describe('modules/datasource/github-releases/cache/cache-base', () => {
     expect(packageCache.get).toHaveBeenCalled();
     expect(packageCache.set).not.toHaveBeenCalled();
   });
+
+  it('throws for graphql errors', async () => {
+    packageCache.get.mockResolvedValueOnce({
+      items: {},
+      createdAt: t3,
+      updatedAt: t3,
+    });
+    responses = [
+      {
+        statusCode: 200,
+        headers: {},
+        body: { errors: [{ message: 'Ooops' }] },
+      },
+    ];
+    const cache = new TestCache(http, { resetDeltaMinutes: 0 });
+
+    await expect(cache.getItems({ packageName: 'foo/bar' })).rejects.toThrow(
+      'Ooops'
+    );
+    expect(packageCache.get).toHaveBeenCalled();
+    expect(packageCache.set).not.toHaveBeenCalled();
+  });
 });
diff --git a/lib/modules/datasource/github-releases/cache/cache-base.ts b/lib/modules/datasource/github-releases/cache/cache-base.ts
index 3a846a3199..4ed5742ae2 100644
--- a/lib/modules/datasource/github-releases/cache/cache-base.ts
+++ b/lib/modules/datasource/github-releases/cache/cache-base.ts
@@ -203,7 +203,13 @@ export abstract class AbstractGithubDatasourceCache<
           });
           pagesRemained -= 1;
 
-          const data = graphqlRes.body.data;
+          const { data, errors } = graphqlRes.body;
+
+          const errorMessage = errors?.[0]?.message;
+          if (errorMessage) {
+            throw Error(errorMessage);
+          }
+
           if (data) {
             const {
               nodes: fetchedItems,
diff --git a/lib/util/http/github.ts b/lib/util/http/github.ts
index e4b4391276..dfb697e9d4 100644
--- a/lib/util/http/github.ts
+++ b/lib/util/http/github.ts
@@ -50,7 +50,6 @@ export interface GithubGraphqlResponse<T = unknown> {
   errors?: {
     type?: string;
     message: string;
-    locations: unknown;
   }[];
 }
 
-- 
GitLab