From 95076df302d4791b339d9798ce3f15dfd18f0ed4 Mon Sep 17 00:00:00 2001
From: Rein Achten <rein.achten@tomtom.com>
Date: Tue, 10 Oct 2023 13:14:07 +0200
Subject: [PATCH] fix: deep merge config and platformInfo to avoid losing
 hostRules (#25116)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 lib/modules/platform/index.spec.ts | 101 +++++++++++++++++++++--------
 lib/modules/platform/index.ts      |  10 ++-
 2 files changed, 82 insertions(+), 29 deletions(-)

diff --git a/lib/modules/platform/index.spec.ts b/lib/modules/platform/index.spec.ts
index 660e33344d..0c9df4dd74 100644
--- a/lib/modules/platform/index.spec.ts
+++ b/lib/modules/platform/index.spec.ts
@@ -119,34 +119,81 @@ describe('modules/platform/index', () => {
     });
   });
 
-  it('merges platform hostRules with additionalHostRules', async () => {
-    const config = {
-      platform: 'github' as PlatformId,
-      endpoint: 'https://api.github.com',
-      gitAuthor: 'user@domain.com',
-      username: 'abc',
-      token: '123',
-    };
+  describe('when platform endpoint is https://api.github.com/', () => {
+    it('merges config hostRules with platform hostRules', async () => {
+      const config = {
+        platform: 'github' as PlatformId,
+        endpoint: 'https://api.github.com',
+        gitAuthor: 'user@domain.com',
+        username: 'abc',
+        token: '123',
+        hostRules: [
+          {
+            hostType: 'github',
+            matchHost: 'github.com',
+            token: '456',
+            username: 'def',
+          },
+        ],
+      };
 
-    expect(await platform.initPlatform(config)).toEqual({
-      endpoint: 'https://api.github.com/',
-      gitAuthor: 'user@domain.com',
-      hostRules: [
-        {
-          hostType: 'docker',
-          matchHost: 'ghcr.io',
-          password: '123',
-          username: 'USERNAME',
-        },
-        {
-          hostType: 'github',
-          matchHost: 'api.github.com',
-          token: '123',
-          username: 'abc',
-        },
-      ],
-      platform: 'github',
-      renovateUsername: 'abc',
+      expect(await platform.initPlatform(config)).toEqual({
+        endpoint: 'https://api.github.com/',
+        gitAuthor: 'user@domain.com',
+        hostRules: [
+          {
+            hostType: 'github',
+            matchHost: 'github.com',
+            token: '456',
+            username: 'def',
+          },
+          {
+            hostType: 'docker',
+            matchHost: 'ghcr.io',
+            password: '123',
+            username: 'USERNAME',
+          },
+          {
+            hostType: 'github',
+            matchHost: 'api.github.com',
+            token: '123',
+            username: 'abc',
+          },
+        ],
+        platform: 'github',
+        renovateUsername: 'abc',
+      });
+    });
+
+    it('merges platform hostRules with additionalHostRules', async () => {
+      const config = {
+        platform: 'github' as PlatformId,
+        endpoint: 'https://api.github.com',
+        gitAuthor: 'user@domain.com',
+        username: 'abc',
+        token: '123',
+      };
+
+      expect(await platform.initPlatform(config)).toEqual({
+        endpoint: 'https://api.github.com/',
+        gitAuthor: 'user@domain.com',
+        hostRules: [
+          {
+            hostType: 'docker',
+            matchHost: 'ghcr.io',
+            password: '123',
+            username: 'USERNAME',
+          },
+          {
+            hostType: 'github',
+            matchHost: 'api.github.com',
+            token: '123',
+            username: 'abc',
+          },
+        ],
+        platform: 'github',
+        renovateUsername: 'abc',
+      });
     });
   });
 });
diff --git a/lib/modules/platform/index.ts b/lib/modules/platform/index.ts
index 7b2a3f20cf..7c9b4bc4df 100644
--- a/lib/modules/platform/index.ts
+++ b/lib/modules/platform/index.ts
@@ -47,7 +47,14 @@ export async function initPlatform(config: AllConfig): Promise<AllConfig> {
   setPlatformApi(config.platform!);
   // TODO: types
   const platformInfo = await platform.initPlatform(config);
-  const returnConfig: any = { ...config, ...platformInfo };
+  const returnConfig: any = {
+    ...config,
+    ...platformInfo,
+    hostRules: [
+      ...(config.hostRules ?? []),
+      ...(platformInfo?.hostRules ?? []),
+    ],
+  };
   // istanbul ignore else
   if (config?.gitAuthor) {
     logger.debug(`Using configured gitAuthor (${config.gitAuthor})`);
@@ -75,7 +82,6 @@ export async function initPlatform(config: AllConfig): Promise<AllConfig> {
       delete returnConfig[field];
     }
   });
-  returnConfig.hostRules = returnConfig.hostRules || [];
   const typedPlatformRule = {
     ...platformRule,
     hostType: returnConfig.platform,
-- 
GitLab