feat(package-rules): support template compilation for sourceUrl (#41932)

Add template compilation support for sourceUrl in package rules,
allowing users to dynamically construct source URLs using template
syntax. This enables use cases like OpenTofu providers where the
source URL needs to be constructed from the package name.
This commit is contained in:
Timo Hankamer
2026-03-19 11:16:52 +01:00
committed by GitHub
parent 4ac878989e
commit f593ffca20
3 changed files with 54 additions and 0 deletions
+16
View File
@@ -3856,6 +3856,8 @@ Use this field to set the directory in which the package is present at the sourc
Use this field to set the source URL for a package, including overriding an existing one.
Source URLs are necessary to link to the source of the package and in order to look up changelogs.
The `sourceUrl` field supports template compilation, allowing you to dynamically construct URLs based on package information.
```json title="Setting the source URL for the dummy package"
{
"packageRules": [
@@ -3867,6 +3869,20 @@ Source URLs are necessary to link to the source of the package and in order to l
}
```
```json title="Using templates to construct source URLs for OpenTofu providers"
{
"packageRules": [
{
"matchDatasources": ["terraform-provider"],
"registryUrls": ["https://registry.opentofu.org"],
"sourceUrl": "https://github.com/{{replace '/' '/terraform-provider-' packageName}}"
}
]
}
```
In the example above, a package name like `hashicorp/aws` will be transformed to `https://github.com/hashicorp/terraform-provider-aws`.
## patch
Add to this object if you wish to define rules that apply only to patch updates.
+35
View File
@@ -1282,4 +1282,39 @@ describe('util/package-rules/index', () => {
expect(res.depName).toBe('node');
expect(res.packageName).toBe('docker.io/library/node');
});
it('compiles sourceUrl with template helper functions', async () => {
const config: TestConfig = {
datasource: 'terraform-provider',
depName: 'aws',
packageName: 'hashicorp/aws',
packageRules: [
{
matchDatasources: ['terraform-provider'],
sourceUrl:
'https://github.com/{{replace "/" "/terraform-provider-" packageName}}',
},
],
};
const res = await applyPackageRules(config);
expect(res.sourceUrl).toBe(
'https://github.com/hashicorp/terraform-provider-aws',
);
});
it('compiles sourceUrl with template variables', async () => {
const config: TestConfig = {
datasource: 'terraform-provider',
depName: 'aws',
packageName: 'hashicorp/aws',
packageRules: [
{
matchDatasources: ['terraform-provider'],
sourceUrl: 'https://github.com/{{packageName}}',
},
],
};
const res = await applyPackageRules(config);
expect(res.sourceUrl).toBe('https://github.com/hashicorp/aws');
});
});
+3
View File
@@ -90,6 +90,9 @@ export async function applyPackageRules<T extends PackageRuleInputConfig>(
);
config.packageName = compile(toApply.overridePackageName, config);
}
if (isString(toApply.sourceUrl)) {
toApply.sourceUrl = compile(toApply.sourceUrl, config);
}
delete toApply.overrideDatasource;
delete toApply.overrideDepName;
delete toApply.overridePackageName;