feat: implement custom.<customMgrName> syntax for matchManagers (#24112)

This commit is contained in:
RahulGautamSingh 2023-08-27 22:17:10 +05:45 committed by GitHub
parent b2c6cbe0c8
commit 9c322cdcee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 2 deletions

View file

@ -0,0 +1,26 @@
import { MatchManagersMigration } from './match-managers-migration';
describe('config/migrations/custom/match-managers-migration', () => {
it('migrates old custom manager syntax to new one', () => {
expect(MatchManagersMigration).toMigrate(
{
matchManagers: ['npm', 'regex', 'custom.someMgr'],
},
{
matchManagers: ['npm', 'custom.regex', 'custom.someMgr'],
}
);
});
// coverage
it('only migrates when necessary', () => {
expect(MatchManagersMigration).not.toMigrate(
{
matchManagers: undefined,
},
{
matchManagers: undefined,
}
);
});
});

View file

@ -0,0 +1,18 @@
import is from '@sindresorhus/is';
import { isCustomManager } from '../../../modules/manager/custom';
import { AbstractMigration } from '../base/abstract-migration';
export class MatchManagersMigration extends AbstractMigration {
override readonly propertyName = 'matchManagers';
override run(value: unknown): void {
if (!is.array<string>(value, is.string)) {
return;
}
const newValue = value.map((manager) =>
isCustomManager(manager) ? `custom.${manager}` : manager
);
this.rewrite(newValue);
}
}

View file

@ -3,7 +3,7 @@ import { check } from './managers';
describe('config/validation-helpers/managers', () => { describe('config/validation-helpers/managers', () => {
it('should have no errors', () => { it('should have no errors', () => {
const res = check({ const res = check({
resolvedRule: { matchManagers: ['npm', 'regex'] }, resolvedRule: { matchManagers: ['npm', 'regex', 'custom.regex'] },
currentPath: '', currentPath: '',
}); });
expect(res).toEqual([]); expect(res).toEqual([]);

View file

@ -13,7 +13,8 @@ export function check({
if (Array.isArray(resolvedRule.matchManagers)) { if (Array.isArray(resolvedRule.matchManagers)) {
if ( if (
resolvedRule.matchManagers.find( resolvedRule.matchManagers.find(
(confManager) => !allManagersList.includes(confManager) (confManager) =>
!allManagersList.includes(confManager.replace('custom.', ''))
) )
) { ) {
managersErrMessage = `${currentPath}: managersErrMessage = `${currentPath}:

View file

@ -47,5 +47,17 @@ describe('util/package-rules/managers', () => {
); );
expect(result).toBeFalse(); expect(result).toBeFalse();
}); });
it('should match custom managers', () => {
const result = managersMatcher.matches(
{
manager: 'regex',
},
{
matchManagers: ['custom.regex'],
}
);
expect(result).toBeTrue();
});
}); });
}); });

View file

@ -1,5 +1,6 @@
import is from '@sindresorhus/is'; import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types'; import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { isCustomManager } from '../../modules/manager/custom';
import { Matcher } from './base'; import { Matcher } from './base';
export class ManagersMatcher extends Matcher { export class ManagersMatcher extends Matcher {
@ -13,6 +14,9 @@ export class ManagersMatcher extends Matcher {
if (is.undefined(manager) || !manager) { if (is.undefined(manager) || !manager) {
return false; return false;
} }
if (isCustomManager(manager)) {
return matchManagers.includes(`custom.${manager}`);
}
return matchManagers.includes(manager); return matchManagers.includes(manager);
} }
} }