From 7f7a5ec45c240aeb0d78e26b6caf1a47dc14ab19 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 12 Oct 2024 11:17:07 +0200 Subject: [PATCH 1/4] feat(packageRules): overrideDatasource, overrideDepName, overridePackageName --- docs/usage/configuration-options.md | 53 ++++++++++++++++++++++++ lib/config/options/index.ts | 30 ++++++++++++++ lib/util/package-rules/index.spec.ts | 60 ++++++++++++++++++++++++++++ lib/util/package-rules/index.ts | 31 ++++++++++++++ 4 files changed, 174 insertions(+) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 279c61f0adb5e5..42888446bb529f 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -3045,6 +3045,59 @@ Tokens can be configured via `hostRules` using the `"merge-confidence"` `hostTyp } ``` +### overrideDatasource + +If a particular `datasource`/`packageName` combination has a lookup problem, you may be able to fix it by _changing_ `datasource` and potentially also `packageName`. +Here is an example: + +```json +{ + "packageRules": [ + { + "matchDatasources": ["docker"], + "matchPackageNames": ["renovate/renovate"], + "overrideDatasource": "npm", + "overridePackageName": "renovate" + } + ] +} +``` + +`overrideDatasource` does not support template compilation. +Be cautious as using this setting incorrectly could break all lookups. + +### overrideDepName + +Be careful using this feature because it may cause undesirable changes such as to branch names. + +In Renovate terminology, `packageName` is the exact package name needing to be looked up on a registry, while `depName` is essentially the "pretty" name. +For example, the `packageName` is `docker.io/library/node` while the `depName` might be `node` for short. + +`depName` is used in PR titles as well as branch names, so changes to `depName` will have effects on those. + +`overrideDepName` supports template compilation. +Example: + +```json +{ + "packageRules": [ + { + "matchDatasources": ["docker"], + "overrideDepName": "{{replace 'docker.io/library/' '' depName}}" + } + ] +} +``` + +Be cautious as using this setting incorrectly could break all lookups. + +### overridePackageName + +See the `overrideDatasource` documentation for an example of use. +`overridePackageName` supports template compilation. + +Be cautious as using this setting incorrectly could break all lookups. + ### prPriority Sometimes Renovate needs to rate limit its creation of PRs, e.g. hourly or concurrent PR limits. diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts index 21900eb2e3adf1..d4181614f39fdc 100644 --- a/lib/config/options/index.ts +++ b/lib/config/options/index.ts @@ -1905,6 +1905,36 @@ const options: RenovateOptions[] = [ cli: false, env: false, }, + { + name: 'overrideDatasource', + description: 'Override the datasource value.', + type: 'string', + stage: 'package', + parents: ['packageRules'], + cli: false, + env: false, + advancedUse: true, + }, + { + name: 'overrideDepName', + description: 'Override the depName value.', + type: 'string', + stage: 'package', + parents: ['packageRules'], + cli: false, + env: false, + advancedUse: true, + }, + { + name: 'overridePackageName', + description: 'Override the packageName value.', + type: 'string', + stage: 'package', + parents: ['packageRules'], + cli: false, + env: false, + advancedUse: true, + }, { name: 'bbAutoResolvePrTasks', description: diff --git a/lib/util/package-rules/index.spec.ts b/lib/util/package-rules/index.spec.ts index 2b78b17d251ccb..bb7999b8b65add 100644 --- a/lib/util/package-rules/index.spec.ts +++ b/lib/util/package-rules/index.spec.ts @@ -1174,4 +1174,64 @@ describe('util/package-rules/index', () => { expect(res.x).toBe(1); }); + + it('overrides', async () => { + const config: TestConfig = { + datasource: 'npm', + depName: 'foo', + packageName: 'bar', + packageRules: [ + { + matchDatasources: ['npm'], + matchDepNames: ['foo'], + overridePackageName: 'baz', + }, + { + matchDatasources: ['npm'], + matchPackageNames: ['baz'], + overrideDepName: 'f', + }, + { + matchDepNames: ['f'], + overrideDatasource: 'composer', + }, + { + matchDatasources: ['composer'], + matchDepNames: ['f'], + matchPackageNames: ['baz'], + enabled: false, + }, + ], + }; + let res = await applyPackageRules(config); + expect(res.packageName).toBe('baz'); + res = await applyPackageRules(res); + expect(res.depName).toBe('f'); + res = await applyPackageRules(res); + expect(res.datasource).toBe('composer'); + res = await applyPackageRules(res); + expect(res).toMatchObject({ + datasource: 'composer', + depName: 'f', + packageName: 'baz', + enabled: false, + }); + }); + + it('overrides with templates', async () => { + const config: TestConfig = { + datasource: 'docker', + depName: 'docker.io/library/node', + packageName: 'docker.io/library/node', + packageRules: [ + { + matchDatasources: ['docker'], + overrideDepName: '{{replace "docker.io/library/" "" depName}}', + }, + ], + }; + const res = await applyPackageRules(config); + expect(res.depName).toBe('node'); + expect(res.packageName).toBe('docker.io/library/node'); + }); }); diff --git a/lib/util/package-rules/index.ts b/lib/util/package-rules/index.ts index ab82e2169b0e33..e835018a6a6c1b 100644 --- a/lib/util/package-rules/index.ts +++ b/lib/util/package-rules/index.ts @@ -5,6 +5,7 @@ import type { PackageRule, PackageRuleInputConfig } from '../../config/types'; import { logger } from '../../logger'; import type { StageName } from '../../types/skip-reason'; import matchers from './matchers'; +import { compile } from '../template'; async function matchesRule( inputConfig: PackageRuleInputConfig, @@ -57,6 +58,36 @@ export async function applyPackageRules( delete config.skipReason; delete config.skipStage; } + if ( + is.string(toApply.overrideDatasource) && + toApply.overrideDatasource !== config.datasource + ) { + logger.debug( + `Overriding datasource from ${config.datasource} to ${toApply.overrideDatasource} for ${config.depName}`, + ); + config.datasource = toApply.overrideDatasource; + } + if ( + is.string(toApply.overrideDepName) && + toApply.overrideDepName !== config.depName + ) { + logger.debug( + `Overriding depName from ${config.depName} to ${toApply.overrideDepName}`, + ); + config.depName = compile(toApply.overrideDepName, config); + } + if ( + is.string(toApply.overridePackageName) && + toApply.overridePackageName !== config.packageName + ) { + logger.debug( + `Overriding packageName from ${config.packageName} to ${toApply.overridePackageName} for ${config.depName}`, + ); + config.packageName = compile(toApply.overridePackageName, config); + } + delete toApply.overrideDatasource; + delete toApply.overrideDepName; + delete toApply.overridePackageName; config = mergeChildConfig(config, toApply); } } From a6af212e799f87bfbbd65f71cdf639743cb9fe4a Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 12 Oct 2024 13:22:18 +0200 Subject: [PATCH 2/4] fix lint --- lib/util/package-rules/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util/package-rules/index.ts b/lib/util/package-rules/index.ts index e835018a6a6c1b..4f2ce60387b461 100644 --- a/lib/util/package-rules/index.ts +++ b/lib/util/package-rules/index.ts @@ -4,8 +4,8 @@ import { mergeChildConfig } from '../../config'; import type { PackageRule, PackageRuleInputConfig } from '../../config/types'; import { logger } from '../../logger'; import type { StageName } from '../../types/skip-reason'; -import matchers from './matchers'; import { compile } from '../template'; +import matchers from './matchers'; async function matchesRule( inputConfig: PackageRuleInputConfig, From 7e34e0ee20fc460d299e7205c5d72c2a4a076b0e Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 12 Oct 2024 15:23:43 +0200 Subject: [PATCH 3/4] Update configuration-options.md Co-authored-by: Sebastian Poxhofer --- docs/usage/configuration-options.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 42888446bb529f..918612ade4db27 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -3093,7 +3093,7 @@ Be cautious as using this setting incorrectly could break all lookups. ### overridePackageName -See the `overrideDatasource` documentation for an example of use. +See the [`overrideDatasource`](#overrideDatasource) documentation for an example of use. `overridePackageName` supports template compilation. Be cautious as using this setting incorrectly could break all lookups. From 7dc99c1e1f43fe5f5714a61f593504d67657baea Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 12 Oct 2024 15:38:37 +0200 Subject: [PATCH 4/4] Update configuration-options.md --- docs/usage/configuration-options.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 918612ade4db27..0a71b938bfbb22 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -3093,7 +3093,7 @@ Be cautious as using this setting incorrectly could break all lookups. ### overridePackageName -See the [`overrideDatasource`](#overrideDatasource) documentation for an example of use. +See the [`overrideDatasource`](#overridedatasource) documentation for an example of use. `overridePackageName` supports template compilation. Be cautious as using this setting incorrectly could break all lookups.