From 1454b602fca407f3de47b74606cc7543edd29be5 Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Sat, 2 Jul 2022 19:41:02 +0300
Subject: [PATCH] fix(github): Shrink pages for specific types of errors
 (#16388)

---
 .../github-releases/cache/cache-base.spec.ts  | 32 +++++++++++++++++++
 .../github-releases/cache/cache-base.ts       | 14 ++++++++
 2 files changed, 46 insertions(+)

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 fa5e36c8ed..b0fa4162c6 100644
--- a/lib/modules/datasource/github-releases/cache/cache-base.spec.ts
+++ b/lib/modules/datasource/github-releases/cache/cache-base.spec.ts
@@ -345,6 +345,38 @@ describe('modules/datasource/github-releases/cache/cache-base', () => {
     expect(packageCache.set).not.toHaveBeenCalled();
   });
 
+  it('shrinks for some of graphql errors', async () => {
+    packageCache.get.mockResolvedValueOnce({
+      items: {},
+      createdAt: t3,
+      updatedAt: t3,
+    });
+    responses = [
+      {
+        statusCode: 200,
+        headers: {},
+        body: {
+          errors: [
+            { message: 'Something went wrong while executing your query.' },
+          ],
+        },
+      },
+      resp([{ name: 'v3', createdAt: t3, foo: 'ccc' }], true),
+      resp([{ name: 'v2', createdAt: t2, foo: 'bbb' }], true),
+      resp([{ name: 'v1', createdAt: t1, foo: 'aaa' }]),
+    ];
+    const cache = new TestCache(http, { resetDeltaMinutes: 0 });
+
+    const res = await cache.getItems({ packageName: 'foo/bar' });
+
+    expect(sortItems(res)).toMatchObject([
+      { version: 'v1', bar: 'aaa' },
+      { version: 'v2', bar: 'bbb' },
+      { version: 'v3', bar: 'ccc' },
+    ]);
+    expect(packageCache.set).toHaveBeenCalled();
+  });
+
   it('finds latest release timestamp correctly', () => {
     const cache = new TestCache(http);
     const ts = cache.getLastReleaseTimestamp({
diff --git a/lib/modules/datasource/github-releases/cache/cache-base.ts b/lib/modules/datasource/github-releases/cache/cache-base.ts
index 88cd539ce0..5fa8f50f11 100644
--- a/lib/modules/datasource/github-releases/cache/cache-base.ts
+++ b/lib/modules/datasource/github-releases/cache/cache-base.ts
@@ -1,4 +1,5 @@
 import { DateTime, DurationLikeObject } from 'luxon';
+import { logger } from '../../../../logger';
 import * as packageCache from '../../../../util/cache/package';
 import type {
   GithubGraphqlResponse,
@@ -265,6 +266,19 @@ export abstract class AbstractGithubDatasourceCache<
         while (pagesRemained > 0 && !stopIteration) {
           const res = await this.query(baseUrl, variables);
           if (res instanceof Error) {
+            if (
+              res.message.startsWith(
+                'Something went wrong while executing your query.' // #16343
+              ) &&
+              variables.count > 30
+            ) {
+              logger.warn(
+                `GitHub datasource cache: shrinking GraphQL page size due to error`
+              );
+              pagesRemained *= 2;
+              variables.count = Math.floor(variables.count / 2);
+              continue;
+            }
             throw res;
           }
 
-- 
GitLab