From 8b7ef1dfa95137b23832df074aea56cd37d651be Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Tue, 21 Feb 2023 15:41:19 +0530 Subject: [PATCH 01/15] throw error during migrtaion --- .../custom/host-rules-migration.spec.ts | 29 +++++++++ .../migrations/custom/host-rules-migration.ts | 59 ++++++++++++++----- lib/util/host-rules.ts | 32 +--------- 3 files changed, 74 insertions(+), 46 deletions(-) diff --git a/lib/config/migrations/custom/host-rules-migration.spec.ts b/lib/config/migrations/custom/host-rules-migration.spec.ts index fe399a79db42eb..2c1fb719c9ea4e 100644 --- a/lib/config/migrations/custom/host-rules-migration.spec.ts +++ b/lib/config/migrations/custom/host-rules-migration.spec.ts @@ -10,6 +10,8 @@ describe('config/migrations/custom/host-rules-migration', () => { { domainName: 'domain.com/', token: '123test' }, { matchHost: 'domain.com/', token: '123test' }, { hostName: 'some.domain.com', token: '123test' }, + { endpoint: 'domain.com/', token: '123test' }, + { host: 'some.domain.com', token: '123test' }, ], } as any, { @@ -19,8 +21,35 @@ describe('config/migrations/custom/host-rules-migration', () => { { matchHost: 'https://domain.com/', token: '123test' }, { matchHost: 'https://domain.com/', token: '123test' }, { matchHost: 'some.domain.com', token: '123test' }, + { matchHost: 'https://domain.com/', token: '123test' }, + { matchHost: 'some.domain.com', token: '123test' }, ], } ); }); + + it('throws when multiple hosts are present', () => { + expect(() => + new HostRulesMigration( + { + hostRules: [ + { + matchHost: 'https://some.domain.com', + baseUrl: 'https://some.domain.com', + token: '123test', + }, + ], + } as any, + {} + ).run([ + { + matchHost: 'https://some.domain.com', + baseUrl: 'https://some.domain.com', + token: '123test', + }, + ]) + ).toThrow( + `hostRules cannot contain more than one host-matching field - use "matchHost" only.` + ); + }); }); diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index 45bd9f13f51204..861b40792bf852 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -1,13 +1,24 @@ import is from '@sindresorhus/is'; import type { HostRule } from '../../../types'; +import { clone } from '../../../util/clone'; import { AbstractMigration } from '../base/abstract-migration'; +interface LegacyHostRule { + hostName?: string; + domainName?: string; + baseUrl?: string; + host?: string; + endpoint?: string; +} + export class HostRulesMigration extends AbstractMigration { override readonly propertyName = 'hostRules'; - override run(value: Record[]): void { + override run(value: (LegacyHostRule & HostRule)[]): void { const newHostRules: HostRule[] = []; - for (const hostRule of value) { + for (let hostRule of value) { + hostRule = migrateRule(hostRule); + const newRule: any = {}; for (const [key, value] of Object.entries(hostRule)) { @@ -25,19 +36,6 @@ export class HostRulesMigration extends AbstractMigration { continue; } - if ( - key === 'endpoint' || - key === 'host' || - key === 'baseUrl' || - key === 'hostName' || - key === 'domainName' - ) { - if (is.string(value)) { - newRule.matchHost ??= massageUrl(value); - } - continue; - } - newRule[key] = value; } @@ -48,6 +46,37 @@ export class HostRulesMigration extends AbstractMigration { } } +function migrateRule(rule: LegacyHostRule & HostRule): HostRule { + const cloned: LegacyHostRule & HostRule = clone(rule); + delete cloned.hostName; + delete cloned.domainName; + delete cloned.baseUrl; + delete cloned.endpoint; + delete cloned.host; + const result: HostRule = cloned; + + const { matchHost } = result; + const { hostName, domainName, baseUrl, endpoint, host } = rule; + const hostValues = [ + matchHost, + hostName, + domainName, + baseUrl, + endpoint, + host, + ].filter(Boolean); + if (hostValues.length === 1) { + const [matchHost] = hostValues; + result.matchHost = massageUrl(matchHost!); + } else if (hostValues.length > 1) { + throw new Error( + `hostRules cannot contain more than one host-matching field - use "matchHost" only.` + ); + } + + return result; +} + function massageUrl(url: string): string { if (!url.includes('://') && url.includes('/')) { return 'https://' + url; diff --git a/lib/util/host-rules.ts b/lib/util/host-rules.ts index 0f5e7219451779..ed865d78a4c543 100644 --- a/lib/util/host-rules.ts +++ b/lib/util/host-rules.ts @@ -9,37 +9,7 @@ import { parseUrl, validateUrl } from './url'; let hostRules: HostRule[] = []; -interface LegacyHostRule { - hostName?: string; - domainName?: string; - baseUrl?: string; -} - -function migrateRule(rule: LegacyHostRule & HostRule): HostRule { - const cloned: LegacyHostRule & HostRule = clone(rule); - delete cloned.hostName; - delete cloned.domainName; - delete cloned.baseUrl; - const result: HostRule = cloned; - - const { matchHost } = result; - const { hostName, domainName, baseUrl } = rule; - const hostValues = [matchHost, hostName, domainName, baseUrl].filter(Boolean); - if (hostValues.length === 1) { - const [matchHost] = hostValues; - result.matchHost = matchHost; - } else if (hostValues.length > 1) { - throw new Error( - `hostRules cannot contain more than one host-matching field - use "matchHost" only.` - ); - } - - return result; -} - -export function add(params: HostRule): void { - const rule = migrateRule(params); - +export function add(rule: HostRule): void { const confidentialFields: (keyof HostRule)[] = ['password', 'token']; if (rule.matchHost) { const parsedUrl = parseUrl(rule.matchHost); From b345f1881ee5af4a4b33a706c00c53d236d0ea27 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Tue, 21 Feb 2023 16:03:52 +0530 Subject: [PATCH 02/15] use CONFIG_VALIDATION error --- lib/config/migrations/custom/host-rules-migration.spec.ts | 5 ++--- lib/config/migrations/custom/host-rules-migration.ts | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/config/migrations/custom/host-rules-migration.spec.ts b/lib/config/migrations/custom/host-rules-migration.spec.ts index 2c1fb719c9ea4e..142375dd82de7e 100644 --- a/lib/config/migrations/custom/host-rules-migration.spec.ts +++ b/lib/config/migrations/custom/host-rules-migration.spec.ts @@ -1,3 +1,4 @@ +import { CONFIG_VALIDATION } from '../../../constants/error-messages'; import { HostRulesMigration } from './host-rules-migration'; describe('config/migrations/custom/host-rules-migration', () => { @@ -48,8 +49,6 @@ describe('config/migrations/custom/host-rules-migration', () => { token: '123test', }, ]) - ).toThrow( - `hostRules cannot contain more than one host-matching field - use "matchHost" only.` - ); + ).toThrow(CONFIG_VALIDATION); }); }); diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index 861b40792bf852..75aa385eb5cdf9 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -69,9 +69,9 @@ function migrateRule(rule: LegacyHostRule & HostRule): HostRule { const [matchHost] = hostValues; result.matchHost = massageUrl(matchHost!); } else if (hostValues.length > 1) { - throw new Error( - `hostRules cannot contain more than one host-matching field - use "matchHost" only.` - ); + const error = new Error('config-validation'); + error.validationError = `hostRules cannot contain more than one host-matching field - use "matchHost" only.`; + throw error; } return result; From 3c5b7ad77fad42d363b68bba7823047490f5f078 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Tue, 21 Feb 2023 16:57:55 +0530 Subject: [PATCH 03/15] fix tests --- lib/util/host-rules.spec.ts | 56 ++++--------------------------------- 1 file changed, 6 insertions(+), 50 deletions(-) diff --git a/lib/util/host-rules.spec.ts b/lib/util/host-rules.spec.ts index 6513bf9e50ac4c..ec947b158ccece 100644 --- a/lib/util/host-rules.spec.ts +++ b/lib/util/host-rules.spec.ts @@ -15,50 +15,6 @@ describe('util/host-rules', () => { clear(); }); - describe('add()', () => { - it('throws if both domainName and hostName', () => { - expect(() => - add({ - hostType: 'azure', - domainName: 'github.com', - hostName: 'api.github.com', - } as HostRule) - ).toThrow(); - }); - - it('throws if both domainName and baseUrl', () => { - expect(() => - add({ - hostType: 'azure', - domainName: 'github.com', - matchHost: 'https://api.github.com', - } as HostRule) - ).toThrow(); - }); - - it('throws if both hostName and baseUrl', () => { - expect(() => - add({ - hostType: 'azure', - hostName: 'api.github.com', - matchHost: 'https://api.github.com', - } as HostRule) - ).toThrow(); - }); - - it('supports baseUrl-only', () => { - add({ - matchHost: 'https://some.endpoint', - username: 'user1', - password: 'pass1', - }); - expect(find({ url: 'https://some.endpoint/v3/' })).toEqual({ - password: 'pass1', - username: 'user1', - }); - }); - }); - describe('find()', () => { beforeEach(() => { clear(); @@ -71,11 +27,11 @@ describe('util/host-rules', () => { it('needs exact host matches', () => { add({ hostType: NugetDatasource.id, - hostName: 'nuget.org', + matchHost: 'nuget.org', username: 'root', password: 'p4$$w0rd', token: undefined, - } as HostRule); + }); expect(find({ hostType: NugetDatasource.id })).toEqual({}); expect( find({ hostType: NugetDatasource.id, url: 'https://nuget.org' }) @@ -109,7 +65,7 @@ describe('util/host-rules', () => { it('matches on domainName', () => { add({ - domainName: 'github.com', + matchHost: 'github.com', token: 'def', } as HostRule); expect( @@ -192,7 +148,7 @@ describe('util/host-rules', () => { it('matches on hostName', () => { add({ - hostName: 'nuget.local', + matchHost: 'nuget.local', token: 'abc', } as HostRule); expect( @@ -291,7 +247,7 @@ describe('util/host-rules', () => { }); add({ hostType: NugetDatasource.id, - hostName: 'my.local.registry', + matchHost: 'my.local.registry', token: 'def', } as HostRule); add({ @@ -324,7 +280,7 @@ describe('util/host-rules', () => { it('needs exact host matches', () => { const hostRule = { hostType: 'nuget', - hostName: 'nuget.org', + matchHost: 'nuget.org', username: 'root', password: 'p4$$w0rd', }; From 1c0e606d47525f6fde4a18f6341a471c8fe5c9cc Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Tue, 21 Feb 2023 17:24:46 +0530 Subject: [PATCH 04/15] spply suggestions --- lib/config/migrations/custom/host-rules-migration.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index 75aa385eb5cdf9..a164086c3cc41f 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -1,4 +1,5 @@ import is from '@sindresorhus/is'; +import { CONFIG_VALIDATION } from '../../../constants/error-messages'; import type { HostRule } from '../../../types'; import { clone } from '../../../util/clone'; import { AbstractMigration } from '../base/abstract-migration'; @@ -64,13 +65,16 @@ function migrateRule(rule: LegacyHostRule & HostRule): HostRule { baseUrl, endpoint, host, - ].filter(Boolean); + ].filter(is.string); if (hostValues.length === 1) { const [matchHost] = hostValues; - result.matchHost = massageUrl(matchHost!); + result.matchHost = massageUrl(matchHost); } else if (hostValues.length > 1) { - const error = new Error('config-validation'); - error.validationError = `hostRules cannot contain more than one host-matching field - use "matchHost" only.`; + const error = new Error(CONFIG_VALIDATION); + error.validationSource = 'config'; + error.validationMessage = `hostRules cannot contain more than one host-matching field - use "matchHost" only.`; + error.validationError = + 'The renovate configuration file contains some invalid settings'; throw error; } From 7bc58c77fdd89170235270c81c1df56cd4485dbf Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Wed, 22 Feb 2023 01:07:40 +0530 Subject: [PATCH 05/15] fix test --- lib/config/migration.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/config/migration.spec.ts b/lib/config/migration.spec.ts index 960ca990c15b49..fcb40a6609d2d6 100644 --- a/lib/config/migration.spec.ts +++ b/lib/config/migration.spec.ts @@ -20,7 +20,6 @@ describe('config/migration', () => { { platform: 'docker', endpoint: 'https://docker.io', - host: 'docker.io', username: 'some-username', password: 'some-password', }, From e725000949066cfea1be6594df57d14e941bf16f Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Thu, 23 Feb 2023 05:35:53 +0530 Subject: [PATCH 06/15] fix test --- lib/modules/platform/index.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/modules/platform/index.spec.ts b/lib/modules/platform/index.spec.ts index 88f69b9d869a02..a1cee1460629cd 100644 --- a/lib/modules/platform/index.spec.ts +++ b/lib/modules/platform/index.spec.ts @@ -69,6 +69,7 @@ describe('modules/platform/index', () => { { hostType: 'bitbucket', matchHost: 'api.bitbucket.org', + resolvedHost: 'api.bitbucket.org', password: '123', username: 'abc', }, @@ -109,6 +110,7 @@ describe('modules/platform/index', () => { { hostType: 'github', matchHost: 'ghe.renovatebot.com', + resolvedHost: 'ghe.renovatebot.com', token: '123', username: 'abc', }, From f45e152c49c30454ec50ac3f2d270ad6ad25fa30 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Wed, 22 Mar 2023 09:10:02 +0530 Subject: [PATCH 07/15] clone params-obj before modifying --- lib/modules/platform/index.spec.ts | 1 - lib/util/host-rules.ts | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/modules/platform/index.spec.ts b/lib/modules/platform/index.spec.ts index e5f090b011e448..8c85df4db67d55 100644 --- a/lib/modules/platform/index.spec.ts +++ b/lib/modules/platform/index.spec.ts @@ -70,7 +70,6 @@ describe('modules/platform/index', () => { { hostType: 'bitbucket', matchHost: 'api.bitbucket.org', - resolvedHost: 'api.bitbucket.org', password: '123', username: 'abc', }, diff --git a/lib/util/host-rules.ts b/lib/util/host-rules.ts index ed865d78a4c543..c7acec49a1089f 100644 --- a/lib/util/host-rules.ts +++ b/lib/util/host-rules.ts @@ -9,7 +9,8 @@ import { parseUrl, validateUrl } from './url'; let hostRules: HostRule[] = []; -export function add(rule: HostRule): void { +export function add(params: HostRule): void { + const rule = clone(params); const confidentialFields: (keyof HostRule)[] = ['password', 'token']; if (rule.matchHost) { const parsedUrl = parseUrl(rule.matchHost); From 85aef37083de4a251e67a0daef34e14172d20372 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Wed, 22 Mar 2023 10:32:05 +0530 Subject: [PATCH 08/15] fix test --- lib/modules/platform/index.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/modules/platform/index.spec.ts b/lib/modules/platform/index.spec.ts index 8c85df4db67d55..8ae91610c3c266 100644 --- a/lib/modules/platform/index.spec.ts +++ b/lib/modules/platform/index.spec.ts @@ -110,7 +110,6 @@ describe('modules/platform/index', () => { { hostType: 'github', matchHost: 'ghe.renovatebot.com', - resolvedHost: 'ghe.renovatebot.com', token: '123', username: 'abc', }, From 588fda11eb86b3b96333c8f9a5d2f8fa03a0c830 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Thu, 27 Apr 2023 02:36:20 +0530 Subject: [PATCH 09/15] - revert changes to add() and migrateRule() - refactor custom migration code - update type of LegacyHostRule --- .../migrations/custom/host-rules-migration.ts | 33 ++--------- lib/util/host-rules.spec.ts | 56 +++++++++++++++++-- lib/util/host-rules.ts | 33 ++++++++++- 3 files changed, 88 insertions(+), 34 deletions(-) diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index 513b8c2a52d37d..55d0a188015757 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -1,25 +1,17 @@ import is from '@sindresorhus/is'; import { CONFIG_VALIDATION } from '../../../constants/error-messages'; import type { HostRule } from '../../../types'; -import { clone } from '../../../util/clone'; +import type { LegacyHostRule } from '../../../util/host-rules'; import { AbstractMigration } from '../base/abstract-migration'; import { migrateDatasource } from './datasource-migration'; -interface LegacyHostRule { - hostName?: string; - domainName?: string; - baseUrl?: string; - host?: string; - endpoint?: string; -} - export class HostRulesMigration extends AbstractMigration { override readonly propertyName = 'hostRules'; override run(value: (LegacyHostRule & HostRule)[]): void { const newHostRules: HostRule[] = []; - for (let hostRule of value) { - hostRule = migrateRule(hostRule); + for (const hostRule of value) { + validateHostRule(hostRule); const newRule: any = {}; @@ -68,16 +60,8 @@ export class HostRulesMigration extends AbstractMigration { } } -function migrateRule(rule: LegacyHostRule & HostRule): HostRule { - const cloned: LegacyHostRule & HostRule = clone(rule); - delete cloned.hostName; - delete cloned.domainName; - delete cloned.baseUrl; - delete cloned.endpoint; - delete cloned.host; - const result: HostRule = cloned; - - const { matchHost } = result; +function validateHostRule(rule: LegacyHostRule & HostRule): void { + const { matchHost } = rule; const { hostName, domainName, baseUrl, endpoint, host } = rule; const hostValues = [ matchHost, @@ -87,10 +71,7 @@ function migrateRule(rule: LegacyHostRule & HostRule): HostRule { endpoint, host, ].filter(is.string); - if (hostValues.length === 1) { - const [matchHost] = hostValues; - result.matchHost = massageUrl(matchHost); - } else if (hostValues.length > 1) { + if (hostValues.length > 1) { const error = new Error(CONFIG_VALIDATION); error.validationSource = 'config'; error.validationMessage = `hostRules cannot contain more than one host-matching field - use "matchHost" only.`; @@ -98,8 +79,6 @@ function migrateRule(rule: LegacyHostRule & HostRule): HostRule { 'The renovate configuration file contains some invalid settings'; throw error; } - - return result; } function massageUrl(url: string): string { diff --git a/lib/util/host-rules.spec.ts b/lib/util/host-rules.spec.ts index ec947b158ccece..6513bf9e50ac4c 100644 --- a/lib/util/host-rules.spec.ts +++ b/lib/util/host-rules.spec.ts @@ -15,6 +15,50 @@ describe('util/host-rules', () => { clear(); }); + describe('add()', () => { + it('throws if both domainName and hostName', () => { + expect(() => + add({ + hostType: 'azure', + domainName: 'github.com', + hostName: 'api.github.com', + } as HostRule) + ).toThrow(); + }); + + it('throws if both domainName and baseUrl', () => { + expect(() => + add({ + hostType: 'azure', + domainName: 'github.com', + matchHost: 'https://api.github.com', + } as HostRule) + ).toThrow(); + }); + + it('throws if both hostName and baseUrl', () => { + expect(() => + add({ + hostType: 'azure', + hostName: 'api.github.com', + matchHost: 'https://api.github.com', + } as HostRule) + ).toThrow(); + }); + + it('supports baseUrl-only', () => { + add({ + matchHost: 'https://some.endpoint', + username: 'user1', + password: 'pass1', + }); + expect(find({ url: 'https://some.endpoint/v3/' })).toEqual({ + password: 'pass1', + username: 'user1', + }); + }); + }); + describe('find()', () => { beforeEach(() => { clear(); @@ -27,11 +71,11 @@ describe('util/host-rules', () => { it('needs exact host matches', () => { add({ hostType: NugetDatasource.id, - matchHost: 'nuget.org', + hostName: 'nuget.org', username: 'root', password: 'p4$$w0rd', token: undefined, - }); + } as HostRule); expect(find({ hostType: NugetDatasource.id })).toEqual({}); expect( find({ hostType: NugetDatasource.id, url: 'https://nuget.org' }) @@ -65,7 +109,7 @@ describe('util/host-rules', () => { it('matches on domainName', () => { add({ - matchHost: 'github.com', + domainName: 'github.com', token: 'def', } as HostRule); expect( @@ -148,7 +192,7 @@ describe('util/host-rules', () => { it('matches on hostName', () => { add({ - matchHost: 'nuget.local', + hostName: 'nuget.local', token: 'abc', } as HostRule); expect( @@ -247,7 +291,7 @@ describe('util/host-rules', () => { }); add({ hostType: NugetDatasource.id, - matchHost: 'my.local.registry', + hostName: 'my.local.registry', token: 'def', } as HostRule); add({ @@ -280,7 +324,7 @@ describe('util/host-rules', () => { it('needs exact host matches', () => { const hostRule = { hostType: 'nuget', - matchHost: 'nuget.org', + hostName: 'nuget.org', username: 'root', password: 'p4$$w0rd', }; diff --git a/lib/util/host-rules.ts b/lib/util/host-rules.ts index c7acec49a1089f..917b7cb4185ff9 100644 --- a/lib/util/host-rules.ts +++ b/lib/util/host-rules.ts @@ -9,8 +9,39 @@ import { parseUrl, validateUrl } from './url'; let hostRules: HostRule[] = []; +export interface LegacyHostRule { + hostName?: string; + domainName?: string; + baseUrl?: string; + host?: string; + endpoint?: string; +} + +export function migrateRule(rule: LegacyHostRule & HostRule): HostRule { + const cloned: LegacyHostRule & HostRule = clone(rule); + delete cloned.hostName; + delete cloned.domainName; + delete cloned.baseUrl; + const result: HostRule = cloned; + + const { matchHost } = result; + const { hostName, domainName, baseUrl } = rule; + const hostValues = [matchHost, hostName, domainName, baseUrl].filter(Boolean); + if (hostValues.length === 1) { + const [matchHost] = hostValues; + result.matchHost = matchHost; + } else if (hostValues.length > 1) { + throw new Error( + `hostRules cannot contain more than one host-matching field - use "matchHost" only.` + ); + } + + return result; +} + export function add(params: HostRule): void { - const rule = clone(params); + const rule = migrateRule(params); + const confidentialFields: (keyof HostRule)[] = ['password', 'token']; if (rule.matchHost) { const parsedUrl = parseUrl(rule.matchHost); From 9a9f9a7bee03b76590b76305e4dac6faf296b6ad Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Fri, 28 Apr 2023 01:55:26 +0530 Subject: [PATCH 10/15] fix lint issue --- lib/util/host-rules.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/util/host-rules.ts b/lib/util/host-rules.ts index 45012008f7ce12..1ff39a50df6d41 100644 --- a/lib/util/host-rules.ts +++ b/lib/util/host-rules.ts @@ -16,7 +16,6 @@ export interface LegacyHostRule { endpoint?: string; } - export function migrateRule(rule: LegacyHostRule & HostRule): HostRule { const cloned: LegacyHostRule & HostRule = structuredClone(rule); delete cloned.hostName; From 08e0ff47dd77ccd2b9749377451dbd2e58e8355b Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Wed, 17 May 2023 03:06:02 +0530 Subject: [PATCH 11/15] allow duplicate hosts with warning --- .../custom/host-rules-migration.spec.ts | 15 +++++++++++-- .../migrations/custom/host-rules-migration.ts | 22 +++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/config/migrations/custom/host-rules-migration.spec.ts b/lib/config/migrations/custom/host-rules-migration.spec.ts index 7b0dd08d9bb8ee..8a14eb0d743a71 100644 --- a/lib/config/migrations/custom/host-rules-migration.spec.ts +++ b/lib/config/migrations/custom/host-rules-migration.spec.ts @@ -11,6 +11,12 @@ describe('config/migrations/custom/host-rules-migration', () => { baseUrl: 'https://some.domain.com', token: '123test', }, + { + hostType: 'dotnet', + baseUrl: 'https://some.domain.com', + matchHost: 'https://some.domain.com', + token: '123test', + }, { hostType: 'adoptium-java', domainName: 'domain.com', @@ -25,6 +31,11 @@ describe('config/migrations/custom/host-rules-migration', () => { } as any, { hostRules: [ + { + hostType: 'dotnet-version', + matchHost: 'https://some.domain.com', + token: '123test', + }, { hostType: 'dotnet-version', matchHost: 'https://some.domain.com', @@ -58,7 +69,7 @@ describe('config/migrations/custom/host-rules-migration', () => { { hostRules: [ { - matchHost: 'https://some.domain.com', + matchHost: 'https://some-diff.domain.com', baseUrl: 'https://some.domain.com', token: '123test', }, @@ -67,7 +78,7 @@ describe('config/migrations/custom/host-rules-migration', () => { {} ).run([ { - matchHost: 'https://some.domain.com', + matchHost: 'https://some-diff.domain.com', baseUrl: 'https://some.domain.com', token: '123test', }, diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index 55d0a188015757..798fc5f6bc3ea1 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -1,5 +1,6 @@ import is from '@sindresorhus/is'; import { CONFIG_VALIDATION } from '../../../constants/error-messages'; +import { logger } from '../../../logger'; import type { HostRule } from '../../../types'; import type { LegacyHostRule } from '../../../util/host-rules'; import { AbstractMigration } from '../base/abstract-migration'; @@ -12,7 +13,6 @@ export class HostRulesMigration extends AbstractMigration { const newHostRules: HostRule[] = []; for (const hostRule of value) { validateHostRule(hostRule); - const newRule: any = {}; for (const [key, value] of Object.entries(hostRule)) { @@ -72,12 +72,20 @@ function validateHostRule(rule: LegacyHostRule & HostRule): void { host, ].filter(is.string); if (hostValues.length > 1) { - const error = new Error(CONFIG_VALIDATION); - error.validationSource = 'config'; - error.validationMessage = `hostRules cannot contain more than one host-matching field - use "matchHost" only.`; - error.validationError = - 'The renovate configuration file contains some invalid settings'; - throw error; + const distinctHostValues = new Set(hostValues); + // check if the host values are duplicated + if (distinctHostValues.size > 1) { + const error = new Error(CONFIG_VALIDATION); + error.validationSource = 'config'; + error.validationMessage = `hostRules cannot contain more than one host-matching field - use "matchHost" only.`; + error.validationError = + 'The renovate configuration file contains some invalid settings'; + throw error; + } else { + logger.warn( + 'Duplicate host values found, please only use `matchHost` to specify the host' + ); + } } } From 02c995278f8e6f52a4e02012717277ff9bf807b9 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Fri, 19 May 2023 13:23:28 +0530 Subject: [PATCH 12/15] log key/value pairs --- .../migrations/custom/host-rules-migration.ts | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index 798fc5f6bc3ea1..f7d778d04ac999 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -61,18 +61,30 @@ export class HostRulesMigration extends AbstractMigration { } function validateHostRule(rule: LegacyHostRule & HostRule): void { - const { matchHost } = rule; - const { hostName, domainName, baseUrl, endpoint, host } = rule; - const hostValues = [ + const { matchHost, hostName, domainName, baseUrl, endpoint, host } = rule; + const hosts: Record = removeUndefinedFields({ matchHost, hostName, domainName, baseUrl, endpoint, host, - ].filter(is.string); - if (hostValues.length > 1) { - const distinctHostValues = new Set(hostValues); + }); + + function removeUndefinedFields( + obj: Record + ): Record { + const keys = Object.keys(obj); + for (const key of keys) { + if (obj[key] === undefined) { + delete obj[key]; + } + } + return obj; + } + + if (Object.keys(hosts).length > 1) { + const distinctHostValues = new Set(Object.values(hosts)); // check if the host values are duplicated if (distinctHostValues.size > 1) { const error = new Error(CONFIG_VALIDATION); @@ -83,6 +95,7 @@ function validateHostRule(rule: LegacyHostRule & HostRule): void { throw error; } else { logger.warn( + { hosts }, 'Duplicate host values found, please only use `matchHost` to specify the host' ); } From bcf59c1395e9ce2b63be1c276a0077adf06c7519 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Fri, 19 May 2023 14:02:01 +0530 Subject: [PATCH 13/15] refactor --- lib/config/migrations/custom/host-rules-migration.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index f7d778d04ac999..ba1ad619d116ab 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -74,13 +74,13 @@ function validateHostRule(rule: LegacyHostRule & HostRule): void { function removeUndefinedFields( obj: Record ): Record { - const keys = Object.keys(obj); - for (const key of keys) { - if (obj[key] === undefined) { - delete obj[key]; + const result: Record = {}; + for (const key in obj) { + if (is.string(obj[key])) { + result[key] = obj[key]; } } - return obj; + return result; } if (Object.keys(hosts).length > 1) { From 9fe92b1416a634d3de328c88afd8902033d91a70 Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Fri, 19 May 2023 14:13:24 +0530 Subject: [PATCH 14/15] use for..of --- lib/config/migrations/custom/host-rules-migration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index ba1ad619d116ab..1b8ca3e1fff7be 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -75,7 +75,7 @@ function validateHostRule(rule: LegacyHostRule & HostRule): void { obj: Record ): Record { const result: Record = {}; - for (const key in obj) { + for (const key of Object.keys(obj)) { if (is.string(obj[key])) { result[key] = obj[key]; } From 2b9bbd55fe72afb07b85e33fc98c70b252b2271e Mon Sep 17 00:00:00 2001 From: RahulGautamSingh Date: Fri, 19 May 2023 14:19:55 +0530 Subject: [PATCH 15/15] remove nested fns --- .../migrations/custom/host-rules-migration.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/config/migrations/custom/host-rules-migration.ts b/lib/config/migrations/custom/host-rules-migration.ts index 1b8ca3e1fff7be..b773828813cb92 100644 --- a/lib/config/migrations/custom/host-rules-migration.ts +++ b/lib/config/migrations/custom/host-rules-migration.ts @@ -71,18 +71,6 @@ function validateHostRule(rule: LegacyHostRule & HostRule): void { host, }); - function removeUndefinedFields( - obj: Record - ): Record { - const result: Record = {}; - for (const key of Object.keys(obj)) { - if (is.string(obj[key])) { - result[key] = obj[key]; - } - } - return result; - } - if (Object.keys(hosts).length > 1) { const distinctHostValues = new Set(Object.values(hosts)); // check if the host values are duplicated @@ -109,3 +97,15 @@ function massageUrl(url: string): string { return url; } } + +function removeUndefinedFields( + obj: Record +): Record { + const result: Record = {}; + for (const key of Object.keys(obj)) { + if (is.string(obj[key])) { + result[key] = obj[key]; + } + } + return result; +}