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(package-rules): add matchRepositories / excludeRepositories #23085

Merged
merged 21 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,23 @@ The categories can be found in the [manager documentation](./modules/manager/ind
}
```

### matchRepositories
setchy marked this conversation as resolved.
Show resolved Hide resolved

Use this field to restrict rules to a particular repository. e.g.

```json
{
"packageRules": [
{
"matchRepositories": ["literal/repo", "/^some/.*$/", "**/*-archived"],
"enabled": false
}
]
}
```

It will use `regex` if starting/ending with `/`, otherwise it will use a `minimatch`.

### matchBaseBranches

Use this field to restrict rules to a particular branch. e.g.
Expand Down
13 changes: 13 additions & 0 deletions lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,19 @@ const options: RenovateOptions[] = [
cli: false,
env: false,
},
{
name: 'matchRepositories',
description:
'List of repositories to match (e.g. `["**/*-archived"]`). Valid only within a `packageRules` object.',
type: 'array',
subType: 'string',
allowString: true,
stage: 'package',
parent: 'packageRules',
mergeable: true,
cli: false,
env: false,
},
{
name: 'matchBaseBranches',
description:
Expand Down
3 changes: 3 additions & 0 deletions lib/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,13 @@ export interface PackageRule
matchPackageNames?: string[];
matchPackagePatterns?: string[];
matchPackagePrefixes?: string[];
matchRepositories?: string[];
excludeDepNames?: string[];
excludeDepPatterns?: string[];
excludePackageNames?: string[];
excludePackagePatterns?: string[];
excludePackagePrefixes?: string[];
excludeRepositories?: string[];
matchCurrentValue?: string;
matchCurrentVersion?: string;
matchSourceUrlPrefixes?: string[];
Expand Down Expand Up @@ -482,6 +484,7 @@ export interface PackageRuleInputConfig extends Record<string, unknown> {
manager?: string;
datasource?: string;
packageRules?: (PackageRule & PackageRuleInputConfig)[];
repository?: string;
}

export interface ConfigMigration {
Expand Down
1 change: 1 addition & 0 deletions lib/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ export async function validateConfig(
'matchSourceUrls',
'matchUpdateTypes',
'matchConfidence',
'matchRepositories',
];
if (key === 'packageRules') {
for (const [subIndex, packageRule] of val.entries()) {
Expand Down
30 changes: 30 additions & 0 deletions lib/util/package-rules/match.ts
rarkins marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import is from '@sindresorhus/is';
import { minimatch } from 'minimatch';
import { logger } from '../../logger';
import { regEx } from '../regex';

export function matchRegexOrMinimatch(pattern: string, input: string): boolean {
if (pattern.length > 2 && pattern.startsWith('/') && pattern.endsWith('/')) {
try {
const regex = regEx(pattern.slice(1, -1));
return regex.test(input);
} catch (err) {
logger.once.warn({ err, pattern }, 'Invalid regex pattern');
return false;
}
}
return minimatch(input, pattern, { dot: true });
rarkins marked this conversation as resolved.
Show resolved Hide resolved
}

export function anyMatchRegexOrMinimatch(
patterns: string[] | undefined,
input: string | undefined
): boolean | null {
if (is.undefined(patterns)) {
return null;
}
if (is.undefined(input)) {
return false;
}
return patterns.some((pattern) => matchRegexOrMinimatch(pattern, input));
}
2 changes: 2 additions & 0 deletions lib/util/package-rules/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MergeConfidenceMatcher } from './merge-confidence';
import { PackageNameMatcher } from './package-names';
import { PackagePatternsMatcher } from './package-patterns';
import { PackagePrefixesMatcher } from './package-prefixes';
import { RepositoriesMatcher } from './repositories';
import { SourceUrlPrefixesMatcher } from './sourceurl-prefixes';
import { SourceUrlsMatcher } from './sourceurls';
import type { MatcherApi } from './types';
Expand All @@ -38,4 +39,5 @@ matchers.push([new MergeConfidenceMatcher()]);
matchers.push([new SourceUrlsMatcher(), new SourceUrlPrefixesMatcher()]);
matchers.push([new CurrentValueMatcher()]);
matchers.push([new CurrentVersionMatcher()]);
matchers.push([new RepositoriesMatcher()]);
matchers.push([new CategoriesMatcher()]);
201 changes: 201 additions & 0 deletions lib/util/package-rules/repositories.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import { RepositoriesMatcher } from './repositories';

describe('util/package-rules/repositories', () => {
const packageNameMatcher = new RepositoriesMatcher();

describe('match', () => {
it('should return null if match repositories is not defined', () => {
const result = packageNameMatcher.matches(
{
repository: 'org/repo',
},
{
matchRepositories: undefined,
}
);
expect(result).toBeNull();
});

it('should return false if repository is not defined', () => {
const result = packageNameMatcher.matches(
{
repository: undefined,
},
{
matchRepositories: ['org/repo'],
}
);
expect(result).toBeFalse();
});

it('should return true if repository matches regex pattern', () => {
const result = packageNameMatcher.matches(
{
repository: 'org/repo',
},
{
matchRepositories: ['/^org/repo$/'],
}
);
expect(result).toBeTrue();
});

it('should return false if repository has invalid regex pattern', () => {
const result = packageNameMatcher.matches(
{
repository: 'org/repo',
},
{
matchRepositories: ['/[/'],
}
);
expect(result).toBeFalse();
});

it('should return false if repository does not match regex pattern', () => {
const result = packageNameMatcher.matches(
{
repository: 'org/repo',
},
{
matchRepositories: ['/^org/other-repo$/'],
}
);
expect(result).toBeFalse();
});

it('should return true if repository matches minimatch pattern', () => {
const result = packageNameMatcher.matches(
{
repository: 'org/repo',
},
{
matchRepositories: ['org/**'],
}
);
expect(result).toBeTrue();
});

it('should return false if repository does not match minimatch pattern', () => {
const result = packageNameMatcher.matches(
{
repository: 'org/repo',
},
{
matchRepositories: ['other-org/**'],
}
);
expect(result).toBeFalse();
});

it('should return true if repository matches at least one pattern', () => {
const result = packageNameMatcher.matches(
{
repository: 'org/repo-archived',
},
{
matchRepositories: ['/^org/repo$/', '**/*-archived'],
}
);
expect(result).toBeTrue();
});
});

describe('excludes', () => {
it('should return null if exclude repositories is not defined', () => {
const result = packageNameMatcher.excludes(
{
repository: 'org/repo',
},
{
excludeRepositories: undefined,
}
);
expect(result).toBeNull();
});

it('should return false if exclude repository is not defined', () => {
const result = packageNameMatcher.excludes(
{
repository: undefined,
},
{
excludeRepositories: ['org/repo'],
}
);
expect(result).toBeFalse();
});

it('should return true if exclude repository matches regex pattern', () => {
const result = packageNameMatcher.excludes(
{
repository: 'org/repo',
},
{
excludeRepositories: ['/^org/repo$/'],
}
);
expect(result).toBeTrue();
});

it('should return false if exclude repository has invalid regex pattern', () => {
const result = packageNameMatcher.excludes(
{
repository: 'org/repo',
},
{
excludeRepositories: ['/[/'],
}
);
expect(result).toBeFalse();
});

it('should return false if exclude repository does not match regex pattern', () => {
const result = packageNameMatcher.excludes(
{
repository: 'org/repo',
},
{
excludeRepositories: ['/^org/other-repo$/'],
}
);
expect(result).toBeFalse();
});

it('should return true if exclude repository matches minimatch pattern', () => {
const result = packageNameMatcher.excludes(
{
repository: 'org/repo',
},
{
excludeRepositories: ['org/**'],
}
);
expect(result).toBeTrue();
});

it('should return false if exclude repository does not match minimatch pattern', () => {
const result = packageNameMatcher.excludes(
{
repository: 'org/repo',
},
{
excludeRepositories: ['other-org/**'],
}
);
expect(result).toBeFalse();
});

it('should return true if exclude repository matches at least one pattern', () => {
const result = packageNameMatcher.excludes(
{
repository: 'org/repo-archived',
},
{
excludeRepositories: ['/^org/repo$/', '**/*-archived'],
}
);
expect(result).toBeTrue();
});
});
});
19 changes: 19 additions & 0 deletions lib/util/package-rules/repositories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { Matcher } from './base';
import { anyMatchRegexOrMinimatch } from './match';

export class RepositoriesMatcher extends Matcher {
override matches(
{ repository }: PackageRuleInputConfig,
{ matchRepositories }: PackageRule
): boolean | null {
return anyMatchRegexOrMinimatch(matchRepositories, repository);
}

override excludes(
{ repository }: PackageRuleInputConfig,
{ excludeRepositories }: PackageRule
): boolean | null {
return anyMatchRegexOrMinimatch(excludeRepositories, repository);
}
rarkins marked this conversation as resolved.
Show resolved Hide resolved
}