Skip to content

Commit

Permalink
feat: matchFiles (#8769)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Feb 19, 2021
1 parent fca4514 commit 6b49402
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,18 @@ Use the syntax `!/ /` like the following:
}
```

### matchFiles

Renovate will compare `matchFiles` for an exact match against the dependency's package file.

For example the following would match `package.json` but not `package/frontend/package.json`:

```
"matchFiles": ["package.json"],
```

Use `matchPaths` instead if you need more flexible matching.

### matchPackageNames

Use this field if you want to have one or more exact name matches in your package rule.
Expand Down
2 changes: 1 addition & 1 deletion lib/config/__snapshots__/validation.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Array [
},
Object {
"depName": "Configuration Error",
"message": "packageRules: Each packageRule must contain at least one selector (matchPaths, matchLanguages, matchBaseBranches, matchManagers, matchDatasources, matchDepTypes, matchPackageNames, matchPackagePatterns, excludePackageNames, excludePackagePatterns, matchCurrentVersion, matchSourceUrlPrefixes, matchUpdateTypes). If you wish for configuration to apply to all packages, it is not necessary to place it inside a packageRule at all.",
"message": "packageRules: Each packageRule must contain at least one selector (matchPackageRules, matchPaths, matchLanguages, matchBaseBranches, matchManagers, matchDatasources, matchDepTypes, matchPackageNames, matchPackagePatterns, excludePackageNames, excludePackagePatterns, matchCurrentVersion, matchSourceUrlPrefixes, matchUpdateTypes). If you wish for configuration to apply to all packages, it is not necessary to place it inside a packageRule at all.",
},
Object {
"depName": "Configuration Error",
Expand Down
1 change: 1 addition & 0 deletions lib/config/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export interface PackageRule
extends RenovateSharedConfig,
UpdateConfig,
Record<string, any> {
matchFiles?: string[];
matchPaths?: string[];
matchLanguages?: string[];
matchBaseBranches?: string[];
Expand Down
11 changes: 11 additions & 0 deletions lib/config/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,17 @@ const options: RenovateOptions[] = [
cli: false,
env: false,
},
{
name: 'matchFiles',
description:
'List of strings to do an exact match against package files with full path. Applicable inside packageRules only.',
type: 'array',
subType: 'string',
stage: 'repository',
parent: 'packageRules',
cli: false,
env: false,
},
{
name: 'matchPaths',
description:
Expand Down
1 change: 1 addition & 0 deletions lib/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export async function validateConfig(
}

const selectors = [
'matchPackageRules',
'matchPaths',
'matchLanguages',
'matchBaseBranches',
Expand Down
22 changes: 22 additions & 0 deletions lib/util/package-rules.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,28 @@ describe('applyPackageRules()', () => {
expect(res1.x).toBeUndefined();
expect(res2.x).toBeDefined();
});
it('matches packageFiles', () => {
const config: TestConfig = {
packageFile: 'examples/foo/package.json',
packageRules: [
{
matchFiles: ['package.json'],
x: 1,
},
],
};
const res1 = applyPackageRules({
...config,
depName: 'test',
});
expect(res1.x).toBeUndefined();
config.packageFile = 'package.json';
const res2 = applyPackageRules({
...config,
depName: 'test',
});
expect(res2.x).toBeDefined();
});
it('matches paths', () => {
const config: TestConfig = {
packageFile: 'examples/foo/package.json',
Expand Down
8 changes: 8 additions & 0 deletions lib/util/package-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function matchesRule(inputConfig: Config, packageRule: PackageRule): boolean {
datasource,
} = inputConfig;
// Setting empty arrays simplifies our logic later
const matchFiles = packageRule.matchFiles || [];
const matchPaths = packageRule.matchPaths || [];
const matchLanguages = packageRule.matchLanguages || [];
const matchBaseBranches = packageRule.matchBaseBranches || [];
Expand All @@ -64,6 +65,13 @@ function matchesRule(inputConfig: Config, packageRule: PackageRule): boolean {
) {
matchPackagePatterns = ['.*'];
}
if (matchFiles.length) {
const isMatch = matchFiles.some((fileName) => packageFile === fileName);
if (!isMatch) {
return false;
}
positiveMatch = true;
}
if (matchPaths.length) {
const isMatch = matchPaths.some(
(rulePath) =>
Expand Down

0 comments on commit 6b49402

Please sign in to comment.