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 5 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
15 changes: 15 additions & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,21 @@ Use this field to restrict rules to a particular language. e.g.
}
```

### matchRepositoryPatterns

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

```json
{
"packageRules": [
{
"matchRepositoryPatterns": ["^literal/repo$", "-archived$"],
"enabled": false
}
]
}
```

### matchBaseBranches

Use this field to restrict rules to a particular branch. e.g.
Expand Down
14 changes: 14 additions & 0 deletions lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,20 @@ const options: RenovateOptions[] = [
cli: false,
env: false,
},
{
name: 'matchRepositoryPatterns',
description:
'List of repositories to match (e.g. `["-archived$"]`). Valid only within a `packageRules` object.',
type: 'array',
subType: 'string',
format: 'regex',
allowString: true,
stage: 'package',
parent: 'packageRules',
mergeable: true,
cli: false,
env: false,
},
{
name: 'matchBaseBranches',
description:
Expand Down
2 changes: 2 additions & 0 deletions lib/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export interface PackageRule
matchPackageNames?: string[];
matchPackagePatterns?: string[];
matchPackagePrefixes?: string[];
matchRepositoryPatterns?: string[];
excludeDepNames?: string[];
excludeDepPatterns?: string[];
excludePackageNames?: string[];
Expand Down Expand Up @@ -480,6 +481,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',
'matchRepositoryPatterns',
];
if (key === 'packageRules') {
for (const [subIndex, packageRule] of val.entries()) {
Expand Down
2 changes: 2 additions & 0 deletions lib/util/package-rules/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PackageNameMatcher } from './package-names';
import { PackagePatternsMatcher } from './package-patterns';
import { PackagePrefixesMatcher } from './package-prefixes';
import { PathsMatcher } from './paths';
import { RepositoryPatternsMatcher } from './repository-patterns';
import { SourceUrlPrefixesMatcher } from './sourceurl-prefixes';
import { SourceUrlsMatcher } from './sourceurls';
import type { MatcherApi } from './types';
Expand Down Expand Up @@ -41,3 +42,4 @@ matchers.push([new MergeConfidenceMatcher()]);
matchers.push([new SourceUrlsMatcher(), new SourceUrlPrefixesMatcher()]);
matchers.push([new CurrentValueMatcher()]);
matchers.push([new CurrentVersionMatcher()]);
matchers.push([new RepositoryPatternsMatcher()]);
43 changes: 43 additions & 0 deletions lib/util/package-rules/repository-patterns.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { RepositoryPatternsMatcher } from './repository-patterns';

describe('util/package-rules/repository-patterns', () => {
const packageNameMatcher = new RepositoryPatternsMatcher();

describe('match', () => {
setchy marked this conversation as resolved.
Show resolved Hide resolved
it('should return false if repository is not defined', () => {
const result = packageNameMatcher.matches(
{
repository: undefined,
},
{
matchRepositoryPatterns: ['org/repo'],
}
);
expect(result).toBeFalse();
});

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

it('should return true if repository matches pattern', () => {
const result = packageNameMatcher.matches(
{
repository: 'org/repo-archived',
},
{
matchRepositoryPatterns: ['-archived$'],
}
);
expect(result).toBeTrue();
});
});
});
44 changes: 44 additions & 0 deletions lib/util/package-rules/repository-patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { logger } from '../../logger';
import { regEx } from '../regex';
import { Matcher } from './base';
import { massagePattern } from './utils';

export class RepositoryPatternsMatcher extends Matcher {
override matches(
{ repository }: PackageRuleInputConfig, // TODO - need correct fields
setchy marked this conversation as resolved.
Show resolved Hide resolved
{ matchRepositoryPatterns }: PackageRule
): boolean | null {
if (is.undefined(matchRepositoryPatterns)) {
return null;
}

if (is.undefined(repository)) {
return false;
}

const reposToMatchAgainst = [repository];
setchy marked this conversation as resolved.
Show resolved Hide resolved

let isMatch = false;
for (const repositoryPattern of matchRepositoryPatterns) {
if (
reposToMatchAgainst.some((repo) =>
isRepositoryMatch(repositoryPattern, repo)
setchy marked this conversation as resolved.
Show resolved Hide resolved
)
setchy marked this conversation as resolved.
Show resolved Hide resolved
) {
isMatch = true;
}
}
return isMatch;
}
}

function isRepositoryMatch(repoPattern: string, repo: string): boolean {
const re = regEx(massagePattern(repoPattern));
if (re.test(repo)) {
logger.trace(`${repo} matches against ${String(re)}`);
return true;
}
return false;
}