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(manager/custom): allow packageName instead of depName #28834

Merged
merged 13 commits into from
May 5, 2024
Merged
2 changes: 1 addition & 1 deletion docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ The `regex` manager which is based on using Regular Expression named capture gro
You must have a named capture group matching (e.g. `(?<depName>.*)`) _or_ configure its corresponding template (e.g. `depNameTemplate`) for these fields:

- `datasource`
- `depName`
- `depName` and / or `packageName`
- `currentValue`

Use named capture group matching _or_ set a corresponding template.
Expand Down
10 changes: 10 additions & 0 deletions lib/config/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,16 @@ describe('config/validation', () => {
extractVersionTemplate: '^(?<version>v\\d+\\.\\d+)',
depTypeTemplate: 'apple',
},
{
customType: 'regex',
fileMatch: ['Dockerfile'],
matchStrings: ['ENV (?<currentValue>.*?)\\s'],
packageNameTemplate: 'foo',
datasourceTemplate: 'bar',
registryUrlTemplate: 'foobar',
extractVersionTemplate: '^(?<version>v\\d+\\.\\d+)',
depTypeTemplate: 'apple',
},
],
};
const { warnings, errors } = await configValidation.validateConfig(
Expand Down
10 changes: 9 additions & 1 deletion lib/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ function validateRegexManagerFields(
});
}

const mandatoryFields = ['depName', 'currentValue', 'datasource'];
const mandatoryFields = ['currentValue', 'datasource'];
for (const field of mandatoryFields) {
if (!hasField(customManager, field)) {
errors.push({
Expand All @@ -876,6 +876,14 @@ function validateRegexManagerFields(
});
}
}

const nameFields = ['depName', 'packageName'];
if (!nameFields.some((field) => hasField(customManager, field))) {
errors.push({
topic: 'Configuration Error',
message: `Regex Managers must contain depName or packageName regex groups or templates`,
});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ exports[`modules/manager/custom/regex/index extracts registryUrl 1`] = `
{
"currentValue": "8.12.13",
"datasource": "helm",
"depName": "prometheus-operator",
"packageName": "prometheus-operator",
"registryUrls": [
"https://charts.helm.sh/stable",
],
Expand All @@ -212,7 +212,7 @@ exports[`modules/manager/custom/regex/index extracts registryUrl 1`] = `
"matchStrings": [
"chart:
*repository: (?<registryUrl>.*?)
*name: (?<depName>.*?)
*name: (?<packageName>.*?)
*version: (?<currentValue>.*)
",
],
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/manager/custom/regex/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('modules/manager/custom/regex/index', () => {
it('returns null if no dependencies found', async () => {
const config = {
matchStrings: [
'ENV .*?_VERSION=(?<currentValue>.*) # (?<datasource>.*?)/(?<depName>[^&]*?)(\\&versioning=(?<versioning>[^&]*?))?\\s',
'ENV .*?_VERSION=(?<currentValue>.*) # (?<datasource>.*?)/(?<packageName>[^&]*?)(\\&versioning=(?<versioning>[^&]*?))?\\s',
],
versioningTemplate:
'{{#if versioning}}{{versioning}}{{else}}semver{{/if}}',
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('modules/manager/custom/regex/index', () => {
it('extracts registryUrl', async () => {
const config = {
matchStrings: [
'chart:\n *repository: (?<registryUrl>.*?)\n *name: (?<depName>.*?)\n *version: (?<currentValue>.*)\n',
'chart:\n *repository: (?<registryUrl>.*?)\n *name: (?<packageName>.*?)\n *version: (?<currentValue>.*)\n',
],
datasourceTemplate: 'helm',
};
Expand All @@ -121,7 +121,7 @@ describe('modules/manager/custom/regex/index', () => {
{
currentValue: '8.12.13',
datasource: 'helm',
depName: 'prometheus-operator',
packageName: 'prometheus-operator',
registryUrls: ['https://charts.helm.sh/stable'],
},
],
Expand Down
30 changes: 10 additions & 20 deletions lib/modules/manager/custom/regex/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ Before Renovate can look up a dependency and decide about updates, it needs this
Configuration-wise, it works like this:

- You must capture the `currentValue` of the dependency in a named capture group
- You must have either a `depName` capture group or a `depNameTemplate` config field
- You can optionally have a `packageName` capture group or a `packageNameTemplate` if it differs from `depName`
- You must have either a `depName` or `packageName` capture group, or use on of the respective template fields ( `depNameTemplate` and `packageNameTemplate` )
- You must have either a `datasource` capture group or a `datasourceTemplate` config field
- You can optionally have a `depType` capture group or a `depTypeTemplate` config field
- You can optionally have a `versioning` capture group or a `versioningTemplate` config field. If neither are present, Renovate will use `semver-coerced` as the default
Expand Down Expand Up @@ -88,15 +87,13 @@ But you don't want to write a regex custom manager rule for _each_ variable.
Instead you enhance your `Dockerfile` like this:

```Dockerfile
ARG IMAGE=node:12@sha256:6e5264cd4cfaefd7174b2bc10c7f9a1c2b99d98d127fc57a802d264da9fb43bd
FROM ${IMAGE}
viceice marked this conversation as resolved.
Show resolved Hide resolved
# renovate: datasource=github-tags depName=nodejs/node versioning=node
ENV NODE_VERSION=10.19.0
# renovate: datasource=github-releases depName=composer/composer
# renovate: datasource=github-tags depName=node packageName=nodejs/node versioning=node
ENV NODE_VERSION=20.10.0
# renovate: datasource=github-releases depName=composer packageName=composer/composer
ENV COMPOSER_VERSION=1.9.3
# renovate: datasource=docker depName=docker versioning=docker
# renovate: datasource=docker packageName=docker versioning=docker
ENV DOCKER_VERSION=19.03.1
# renovate: datasource=npm depName=yarn
# renovate: datasource=npm packageName=yarn
rarkins marked this conversation as resolved.
Show resolved Hide resolved
ENV YARN_VERSION=1.19.1
```

Expand All @@ -109,18 +106,11 @@ You could configure Renovate to update the `Dockerfile` like this:
"customManagers": [
{
"customType": "regex",
"fileMatch": ["^Dockerfile$"],
"description": "Update _VERSION variables in Dockerfiles",
"fileMatch": ["(^|/|\\.)Dockerfile$", "(^|/)Dockerfile\\.[^/]*$"],
"matchStrings": [
"datasource=(?<datasource>.*?) depName=(?<depName>.*?)( versioning=(?<versioning>.*?))?\\sENV .*?_VERSION=(?<currentValue>.*)\\s"
],
"versioningTemplate": "{{#if versioning}}{{{versioning}}}{{else}}semver{{/if}}"
},
{
"fileMatch": ["^Dockerfile$"],
"matchStrings": [
"ARG IMAGE=(?<depName>.*?):(?<currentValue>.*?)@(?<currentDigest>sha256:[a-f0-9]+)\\s"
],
"datasourceTemplate": "docker"
"# renovate: datasource=(?<datasource>[a-z-]+?)(?: depName=(?<depName>.+?))? packageName=(?<packageName>.+?)(?: versioning=(?<versioning>[a-z-]+?))?\\s(?:ENV|ARG) .+?_VERSION=(?<currentValue>.+?)\\s"
]
}
]
}
Expand Down
4 changes: 3 additions & 1 deletion lib/modules/manager/custom/regex/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ export function isValidDependency({
depName,
currentValue,
currentDigest,
packageName,
}: PackageDependency): boolean {
// check if all the fields are set
return (
is.nonEmptyStringAndNotWhitespace(depName) &&
(is.nonEmptyStringAndNotWhitespace(depName) ||
is.nonEmptyStringAndNotWhitespace(packageName)) &&
(is.nonEmptyStringAndNotWhitespace(currentDigest) ||
is.nonEmptyStringAndNotWhitespace(currentValue))
);
Expand Down