Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(packageRules): overrideDatasource, overrideDepName, overridePackageName #31925

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`](#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.
Expand Down
30 changes: 30 additions & 0 deletions lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 60 additions & 0 deletions lib/util/package-rules/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
31 changes: 31 additions & 0 deletions lib/util/package-rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mergeChildConfig } from '../../config';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { logger } from '../../logger';
import type { StageName } from '../../types/skip-reason';
import { compile } from '../template';
import matchers from './matchers';

async function matchesRule(
Expand Down Expand Up @@ -57,6 +58,36 @@ export async function applyPackageRules<T extends PackageRuleInputConfig>(
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);
}
}
Expand Down
Loading