From 5558b0d3daa1bd0cdecc18492d36d25c3ba2b3a8 Mon Sep 17 00:00:00 2001
From: RahulGautamSingh <rahultesnik@gmail.com>
Date: Mon, 29 Apr 2024 22:01:35 +0545
Subject: [PATCH] feat(config/self-hosted): `userAgent` (#28737)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 docs/usage/self-hosted-configuration.md | 5 +++++
 docs/usage/self-hosted-experimental.md  | 4 ----
 lib/config/global.ts                    | 1 +
 lib/config/options/index.ts             | 8 ++++++++
 lib/config/types.ts                     | 1 +
 lib/util/http/index.ts                  | 3 ++-
 6 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/docs/usage/self-hosted-configuration.md b/docs/usage/self-hosted-configuration.md
index 2c1af6afec..80fed5ff44 100644
--- a/docs/usage/self-hosted-configuration.md
+++ b/docs/usage/self-hosted-configuration.md
@@ -1110,6 +1110,11 @@ For example: `:warning:` will be replaced with `⚠️`.
 Some cloud providers offer services to receive metadata about the current instance, for example [AWS Instance metadata](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html) or [GCP VM metadata](https://cloud.google.com/compute/docs/metadata/overview).
 You can control if Renovate should try to access these services with the `useCloudMetadataServices` config option.
 
+## userAgent
+
+If set to any string, Renovate will use this as the `user-agent` it sends with HTTP requests.
+Otherwise, it will default to `RenovateBot/${renovateVersion} (https://github.com/renovatebot/renovate)`.
+
 ## username
 
 You may need to set a `username` if you:
diff --git a/docs/usage/self-hosted-experimental.md b/docs/usage/self-hosted-experimental.md
index fcf3c95a94..1dc8fd40f5 100644
--- a/docs/usage/self-hosted-experimental.md
+++ b/docs/usage/self-hosted-experimental.md
@@ -32,10 +32,6 @@ Skipping the check will speed things up, but may result in versions being return
 
 If set to any value, Renovate will always paginate requests to GitHub fully, instead of stopping after 10 pages.
 
-## `RENOVATE_USER_AGENT`
-
-If set to any string, Renovate will use this as the `user-agent` it sends with HTTP requests.
-
 ## `RENOVATE_X_AUTODISCOVER_REPO_ORDER`
 
 <!-- prettier-ignore -->
diff --git a/lib/config/global.ts b/lib/config/global.ts
index 21b2b56cc2..c713f8635f 100644
--- a/lib/config/global.ts
+++ b/lib/config/global.ts
@@ -33,6 +33,7 @@ export class GlobalConfig {
     'platform',
     'endpoint',
     'httpCacheTtlDays',
+    'userAgent',
   ];
 
   private static config: RepoGlobalConfig = {};
diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts
index 995e05b93c..76ae38a7e3 100644
--- a/lib/config/options/index.ts
+++ b/lib/config/options/index.ts
@@ -47,6 +47,14 @@ const options: RenovateOptions[] = [
     default: true,
     globalOnly: true,
   },
+  {
+    name: 'userAgent',
+    description:
+      'If set to any string, Renovate will use this as the `user-agent` it sends with HTTP requests.',
+    type: 'string',
+    default: null,
+    globalOnly: true,
+  },
   {
     name: 'allowPostUpgradeCommandTemplating',
     description:
diff --git a/lib/config/types.ts b/lib/config/types.ts
index 2e55575c98..ff309df752 100644
--- a/lib/config/types.ts
+++ b/lib/config/types.ts
@@ -159,6 +159,7 @@ export interface RepoGlobalConfig {
   privateKey?: string;
   privateKeyOld?: string;
   httpCacheTtlDays?: number;
+  userAgent?: string;
 }
 
 export interface LegacyAdminConfig {
diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts
index b93c659497..7c505db6ef 100644
--- a/lib/util/http/index.ts
+++ b/lib/util/http/index.ts
@@ -3,6 +3,7 @@ import merge from 'deepmerge';
 import got, { Options, RequestError } from 'got';
 import type { SetRequired } from 'type-fest';
 import { infer as Infer, type ZodError, ZodType } from 'zod';
+import { GlobalConfig } from '../../config/global';
 import { HOST_DISABLED } from '../../constants/error-messages';
 import { pkg } from '../../expose.cjs';
 import { logger } from '../../logger';
@@ -50,7 +51,7 @@ function applyDefaultHeaders(options: Options): void {
   options.headers = {
     ...options.headers,
     'user-agent':
-      process.env.RENOVATE_USER_AGENT ??
+      GlobalConfig.get('userAgent') ??
       `RenovateBot/${renovateVersion} (https://github.com/renovatebot/renovate)`,
   };
 }
-- 
GitLab