From 042b44cfbfc68ef080ef258df056d712ceda97de Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Tue, 24 Oct 2023 11:34:19 +0200
Subject: [PATCH] fix(github): make automatic hostRules opt-in experimental
 (#25361)

---
 .../github/__snapshots__/index.spec.ts.snap   | 124 ------------------
 lib/modules/platform/github/index.spec.ts     |   2 +
 lib/modules/platform/github/index.ts          |   5 +-
 lib/modules/platform/github/readme.md         |   5 +-
 lib/modules/platform/index.spec.ts            |   1 +
 5 files changed, 11 insertions(+), 126 deletions(-)

diff --git a/lib/modules/platform/github/__snapshots__/index.spec.ts.snap b/lib/modules/platform/github/__snapshots__/index.spec.ts.snap
index 46adade588..9bbc6ed9a5 100644
--- a/lib/modules/platform/github/__snapshots__/index.spec.ts.snap
+++ b/lib/modules/platform/github/__snapshots__/index.spec.ts.snap
@@ -31,37 +31,6 @@ exports[`modules/platform/github/index initPlatform() should support default end
 {
   "endpoint": "https://api.github.com/",
   "gitAuthor": undefined,
-  "hostRules": [
-    {
-      "hostType": "docker",
-      "matchHost": "ghcr.io",
-      "password": "123test",
-      "username": "USERNAME",
-    },
-    {
-      "hostType": "npm",
-      "matchHost": "npm.pkg.github.com",
-      "token": "123test",
-    },
-    {
-      "hostType": "rubygems",
-      "matchHost": "rubygems.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-    {
-      "hostType": "maven",
-      "matchHost": "maven.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-    {
-      "hostType": "nuget",
-      "matchHost": "nuget.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-  ],
   "renovateUsername": "renovate-bot",
   "token": "123test",
 }
@@ -71,37 +40,6 @@ exports[`modules/platform/github/index initPlatform() should support default end
 {
   "endpoint": "https://api.github.com/",
   "gitAuthor": undefined,
-  "hostRules": [
-    {
-      "hostType": "docker",
-      "matchHost": "ghcr.io",
-      "password": "123test",
-      "username": "USERNAME",
-    },
-    {
-      "hostType": "npm",
-      "matchHost": "npm.pkg.github.com",
-      "token": "123test",
-    },
-    {
-      "hostType": "rubygems",
-      "matchHost": "rubygems.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-    {
-      "hostType": "maven",
-      "matchHost": "maven.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-    {
-      "hostType": "nuget",
-      "matchHost": "nuget.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-  ],
   "renovateUsername": "renovate-bot",
   "token": "123test",
 }
@@ -111,37 +49,6 @@ exports[`modules/platform/github/index initPlatform() should support default end
 {
   "endpoint": "https://api.github.com/",
   "gitAuthor": "undefined <user@domain.com>",
-  "hostRules": [
-    {
-      "hostType": "docker",
-      "matchHost": "ghcr.io",
-      "password": "123test",
-      "username": "USERNAME",
-    },
-    {
-      "hostType": "npm",
-      "matchHost": "npm.pkg.github.com",
-      "token": "123test",
-    },
-    {
-      "hostType": "rubygems",
-      "matchHost": "rubygems.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-    {
-      "hostType": "maven",
-      "matchHost": "maven.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-    {
-      "hostType": "nuget",
-      "matchHost": "nuget.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-  ],
   "renovateUsername": "renovate-bot",
   "token": "123test",
 }
@@ -151,37 +58,6 @@ exports[`modules/platform/github/index initPlatform() should support gitAuthor a
 {
   "endpoint": "https://api.github.com/",
   "gitAuthor": "renovate@whitesourcesoftware.com",
-  "hostRules": [
-    {
-      "hostType": "docker",
-      "matchHost": "ghcr.io",
-      "password": "123test",
-      "username": "USERNAME",
-    },
-    {
-      "hostType": "npm",
-      "matchHost": "npm.pkg.github.com",
-      "token": "123test",
-    },
-    {
-      "hostType": "rubygems",
-      "matchHost": "rubygems.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-    {
-      "hostType": "maven",
-      "matchHost": "maven.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-    {
-      "hostType": "nuget",
-      "matchHost": "nuget.pkg.github.com",
-      "password": "123test",
-      "username": "renovate-bot",
-    },
-  ],
   "renovateUsername": "renovate-bot",
   "token": "123test",
 }
diff --git a/lib/modules/platform/github/index.spec.ts b/lib/modules/platform/github/index.spec.ts
index 2401d7895d..1ad9921a51 100644
--- a/lib/modules/platform/github/index.spec.ts
+++ b/lib/modules/platform/github/index.spec.ts
@@ -48,6 +48,7 @@ describe('modules/platform/github/index', () => {
 
     const repoCache = repository.getCache();
     delete repoCache.platform;
+    delete process.env.RENOVATE_X_GITHUB_HOST_RULES;
   });
 
   describe('initPlatform()', () => {
@@ -162,6 +163,7 @@ describe('modules/platform/github/index', () => {
     });
 
     it('should autodetect email/user on default endpoint with GitHub App', async () => {
+      process.env.RENOVATE_X_GITHUB_HOST_RULES = 'true';
       httpMock
         .scope(githubApiHost, {
           reqheaders: {
diff --git a/lib/modules/platform/github/index.ts b/lib/modules/platform/github/index.ts
index abf9d94fd6..bdc39b5457 100644
--- a/lib/modules/platform/github/index.ts
+++ b/lib/modules/platform/github/index.ts
@@ -204,7 +204,10 @@ export async function initPlatform({
     renovateUsername,
     token,
   };
-  if (platformResult.endpoint === 'https://api.github.com/') {
+  if (
+    process.env.RENOVATE_X_GITHUB_HOST_RULES &&
+    platformResult.endpoint === 'https://api.github.com/'
+  ) {
     logger.debug('Adding GitHub token as GHCR password');
     platformResult.hostRules = [
       {
diff --git a/lib/modules/platform/github/readme.md b/lib/modules/platform/github/readme.md
index 74846bac46..41d134bb4a 100644
--- a/lib/modules/platform/github/readme.md
+++ b/lib/modules/platform/github/readme.md
@@ -87,7 +87,7 @@ A way to get the user id of a GitHub app is to [query the user API](https://docs
 
 ## Package Registry Credentials
 
-When Renovate runs against repositories on `github.com`, it automatically provisions `hostRules` for these GitHub Packages registries using the platform token:
+When Renovate runs against repositories on `github.com`, and the environment variable `RENOVATE_X_GITHUB_HOST_RULES` is set, then Renovate automatically provisions `hostRules` for these GitHub Packages registries using the platform token:
 
 - `ghcr.io`
 - `maven.pkg.github.com`
@@ -95,6 +95,9 @@ When Renovate runs against repositories on `github.com`, it automatically provis
 - `nuget.pkg.github.com`
 - `rubygems.pkg.github.com`
 
+<!-- prettier-ignore -->
+!!! warning Users have reported that this feature is not working correctly, so it has been reverted to experimental mode.
+
 ## Features awaiting implementation
 
 - The `automergeStrategy` configuration option has not been implemented for this platform, and all values behave as if the value `auto` was used. Renovate will use the merge strategy configured in the GitHub repository itself, and this cannot be overridden yet
diff --git a/lib/modules/platform/index.spec.ts b/lib/modules/platform/index.spec.ts
index 9e8093f141..fa5587877d 100644
--- a/lib/modules/platform/index.spec.ts
+++ b/lib/modules/platform/index.spec.ts
@@ -11,6 +11,7 @@ jest.unmock('./scm');
 describe('modules/platform/index', () => {
   beforeEach(() => {
     jest.resetModules();
+    process.env.RENOVATE_X_GITHUB_HOST_RULES = 'true';
   });
 
   it('validates', () => {
-- 
GitLab