From b5ae2f596f2bab0b4892505e217e60d18347ff4d Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Mon, 10 Feb 2025 13:12:50 -0300
Subject: [PATCH] feat(cache): Relax requirements for cacheable response
 headers (#34134)

---
 lib/modules/platform/bitbucket/pr-cache.spec.ts    |  6 +++---
 .../http/cache/abstract-http-cache-provider.ts     | 14 ++++++++------
 .../cache/repository-http-cache-provider.spec.ts   | 14 --------------
 lib/util/http/cache/schema.ts                      |  4 ----
 4 files changed, 11 insertions(+), 27 deletions(-)

diff --git a/lib/modules/platform/bitbucket/pr-cache.spec.ts b/lib/modules/platform/bitbucket/pr-cache.spec.ts
index 5bf1d30da0..49db64d31d 100644
--- a/lib/modules/platform/bitbucket/pr-cache.spec.ts
+++ b/lib/modules/platform/bitbucket/pr-cache.spec.ts
@@ -74,7 +74,7 @@ describe('modules/platform/bitbucket/pr-cache', () => {
       },
     ]);
     expect(cache).toEqual({
-      httpCache: {},
+      httpCache: expect.toBeNonEmptyObject(),
       platform: {
         bitbucket: {
           pullRequestsCache: {
@@ -123,7 +123,7 @@ describe('modules/platform/bitbucket/pr-cache', () => {
       },
     ]);
     expect(cache).toEqual({
-      httpCache: {},
+      httpCache: expect.toBeNonEmptyObject(),
       platform: {
         bitbucket: {
           pullRequestsCache: {
@@ -170,7 +170,7 @@ describe('modules/platform/bitbucket/pr-cache', () => {
       { number: 1, title: 'title' },
     ]);
     expect(cache).toEqual({
-      httpCache: {},
+      httpCache: expect.toBeNonEmptyObject(),
       platform: {
         bitbucket: {
           pullRequestsCache: {
diff --git a/lib/util/http/cache/abstract-http-cache-provider.ts b/lib/util/http/cache/abstract-http-cache-provider.ts
index 46ab61e258..a1c3f7dcd7 100644
--- a/lib/util/http/cache/abstract-http-cache-provider.ts
+++ b/lib/util/http/cache/abstract-http-cache-provider.ts
@@ -65,15 +65,17 @@ export abstract class AbstractHttpCacheProvider implements HttpCacheProvider {
         httpResponse,
         timestamp,
       });
-      if (newHttpCache) {
-        logger.debug(
-          `http cache: saving ${url} (etag=${etag}, lastModified=${lastModified})`,
-        );
-        await this.persist(url, newHttpCache as HttpCache);
-      } else {
+
+      // istanbul ignore if: should never happen
+      if (!newHttpCache) {
         logger.debug(`http cache: failed to persist cache for ${url}`);
+        return resp;
       }
 
+      logger.debug(
+        `http cache: saving ${url} (etag=${etag}, lastModified=${lastModified})`,
+      );
+      await this.persist(url, newHttpCache as HttpCache);
       return resp;
     }
 
diff --git a/lib/util/http/cache/repository-http-cache-provider.spec.ts b/lib/util/http/cache/repository-http-cache-provider.spec.ts
index 5b070114ce..a50121cced 100644
--- a/lib/util/http/cache/repository-http-cache-provider.spec.ts
+++ b/lib/util/http/cache/repository-http-cache-provider.spec.ts
@@ -1,6 +1,5 @@
 import { Http } from '..';
 import * as httpMock from '../../../../test/http-mock';
-import { logger } from '../../../../test/util';
 import { resetCache } from '../../cache/repository';
 import { repoCacheProvider } from './repository-http-cache-provider';
 
@@ -59,19 +58,6 @@ describe('util/http/cache/repository-http-cache-provider', () => {
     });
   });
 
-  it('reports if cache could not be persisted', async () => {
-    httpMock
-      .scope('https://example.com')
-      .get('/foo/bar')
-      .reply(200, { msg: 'Hello, world!' });
-
-    await http.getJsonUnchecked('https://example.com/foo/bar');
-
-    expect(logger.logger.debug).toHaveBeenCalledWith(
-      'http cache: failed to persist cache for https://example.com/foo/bar',
-    );
-  });
-
   it('handles abrupt cache reset', async () => {
     const scope = httpMock.scope('https://example.com');
 
diff --git a/lib/util/http/cache/schema.ts b/lib/util/http/cache/schema.ts
index c9b9db1e29..fe69554ddc 100644
--- a/lib/util/http/cache/schema.ts
+++ b/lib/util/http/cache/schema.ts
@@ -7,10 +7,6 @@ export const HttpCacheSchema = z
     httpResponse: z.unknown(),
     timestamp: z.string(),
   })
-  .refine(
-    ({ etag, lastModified }) => etag ?? lastModified,
-    'Cache object should have `etag` or `lastModified` fields',
-  )
   .nullable()
   .catch(null);
 export type HttpCache = z.infer<typeof HttpCacheSchema>;
-- 
GitLab