fix(package-rules): migrate matchers with string values (#31083)

This commit is contained in:
RahulGautamSingh 2024-08-28 20:40:52 +05:30 committed by GitHub
parent 0bc5cc1b8c
commit ded9a7b007
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 80 additions and 23 deletions

View file

@ -191,6 +191,7 @@ exports[`config/migration migrateConfig(config, parentConfig) migrates config 1`
"groupName": "angular packages", "groupName": "angular packages",
"matchPackageNames": [ "matchPackageNames": [
"/^(@angular|typescript)/", "/^(@angular|typescript)/",
"!foo",
], ],
}, },
{ {

View file

@ -174,6 +174,51 @@ describe('config/migrations/custom/package-rules-migration', () => {
); );
}); });
it('should migrate all match/exclude when value is of type string', () => {
expect(PackageRulesMigration).toMigrate(
{
packageRules: [
{
matchPackagePatterns: 'pattern',
matchPackagePrefixes: 'prefix1',
matchSourceUrlPrefixes: 'prefix1',
excludePackageNames: 'excluded',
excludePackagePatterns: 'excludepattern',
excludePackagePrefixes: 'prefix1b',
matchDepPatterns: 'pattern',
matchDepPrefixes: 'prefix1',
excludeDepNames: 'excluded',
excludeDepPatterns: 'excludepattern',
excludeDepPrefixes: 'prefix1b',
automerge: true,
},
],
},
{
packageRules: [
{
matchPackageNames: [
'/pattern/',
'prefix1{/,}**',
'!excluded',
'!/excludepattern/',
'!prefix1b{/,}**',
],
matchDepNames: [
'/pattern/',
'prefix1{/,}**',
'!excluded',
'!/excludepattern/',
'!prefix1b{/,}**',
],
matchSourceUrls: ['prefix1{/,}**'],
automerge: true,
},
],
},
);
});
it('should migrate all match/exclude at once', () => { it('should migrate all match/exclude at once', () => {
expect(PackageRulesMigration).toMigrate( expect(PackageRulesMigration).toMigrate(
{ {

View file

@ -30,52 +30,57 @@ function renameKeys(packageRule: PackageRule): PackageRule {
function mergeMatchers(packageRule: PackageRule): PackageRule { function mergeMatchers(packageRule: PackageRule): PackageRule {
const newPackageRule: PackageRule = { ...packageRule }; const newPackageRule: PackageRule = { ...packageRule };
for (const [key, val] of Object.entries(packageRule)) { for (const [key, val] of Object.entries(packageRule)) {
const patterns = is.string(val) ? [val] : val;
// depName // depName
if (key === 'matchDepPrefixes') { if (key === 'matchDepPrefixes') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchDepNames ??= []; newPackageRule.matchDepNames ??= [];
newPackageRule.matchDepNames.push(...val.map((v) => `${v}{/,}**`)); newPackageRule.matchDepNames.push(...patterns.map((v) => `${v}{/,}**`));
} }
delete newPackageRule.matchDepPrefixes; delete newPackageRule.matchDepPrefixes;
} }
if (key === 'matchDepPatterns') { if (key === 'matchDepPatterns') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchDepNames ??= []; newPackageRule.matchDepNames ??= [];
newPackageRule.matchDepNames.push(...val.map((v) => `/${v}/`)); newPackageRule.matchDepNames.push(...patterns.map((v) => `/${v}/`));
} }
delete newPackageRule.matchDepPatterns; delete newPackageRule.matchDepPatterns;
} }
if (key === 'excludeDepNames') { if (key === 'excludeDepNames') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchDepNames ??= []; newPackageRule.matchDepNames ??= [];
newPackageRule.matchDepNames.push(...val.map((v) => `!${v}`)); newPackageRule.matchDepNames.push(...patterns.map((v) => `!${v}`));
} }
delete newPackageRule.excludeDepNames; delete newPackageRule.excludeDepNames;
} }
if (key === 'excludeDepPrefixes') { if (key === 'excludeDepPrefixes') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchDepNames ??= []; newPackageRule.matchDepNames ??= [];
newPackageRule.matchDepNames.push(...val.map((v) => `!${v}{/,}**`)); newPackageRule.matchDepNames.push(
...patterns.map((v) => `!${v}{/,}**`),
);
} }
delete newPackageRule.excludeDepPrefixes; delete newPackageRule.excludeDepPrefixes;
} }
if (key === 'excludeDepPatterns') { if (key === 'excludeDepPatterns') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchDepNames ??= []; newPackageRule.matchDepNames ??= [];
newPackageRule.matchDepNames.push(...val.map((v) => `!/${v}/`)); newPackageRule.matchDepNames.push(...patterns.map((v) => `!/${v}/`));
} }
delete newPackageRule.excludeDepPatterns; delete newPackageRule.excludeDepPatterns;
} }
// packageName // packageName
if (key === 'matchPackagePrefixes') { if (key === 'matchPackagePrefixes') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchPackageNames ??= []; newPackageRule.matchPackageNames ??= [];
newPackageRule.matchPackageNames.push(...val.map((v) => `${v}{/,}**`)); newPackageRule.matchPackageNames.push(
...patterns.map((v) => `${v}{/,}**`),
);
} }
delete newPackageRule.matchPackagePrefixes; delete newPackageRule.matchPackagePrefixes;
} }
if (key === 'matchPackagePatterns') { if (key === 'matchPackagePatterns') {
const patterns = is.string(val) ? [val] : val;
if (is.array(patterns, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchPackageNames ??= []; newPackageRule.matchPackageNames ??= [];
newPackageRule.matchPackageNames.push( newPackageRule.matchPackageNames.push(
@ -90,39 +95,45 @@ function mergeMatchers(packageRule: PackageRule): PackageRule {
delete newPackageRule.matchPackagePatterns; delete newPackageRule.matchPackagePatterns;
} }
if (key === 'excludePackageNames') { if (key === 'excludePackageNames') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchPackageNames ??= []; newPackageRule.matchPackageNames ??= [];
newPackageRule.matchPackageNames.push(...val.map((v) => `!${v}`)); newPackageRule.matchPackageNames.push(...patterns.map((v) => `!${v}`));
} }
delete newPackageRule.excludePackageNames; delete newPackageRule.excludePackageNames;
} }
if (key === 'excludePackagePrefixes') { if (key === 'excludePackagePrefixes') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchPackageNames ??= []; newPackageRule.matchPackageNames ??= [];
newPackageRule.matchPackageNames.push(...val.map((v) => `!${v}{/,}**`)); newPackageRule.matchPackageNames.push(
...patterns.map((v) => `!${v}{/,}**`),
);
} }
delete newPackageRule.excludePackagePrefixes; delete newPackageRule.excludePackagePrefixes;
} }
if (key === 'excludePackagePatterns') { if (key === 'excludePackagePatterns') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchPackageNames ??= []; newPackageRule.matchPackageNames ??= [];
newPackageRule.matchPackageNames.push(...val.map((v) => `!/${v}/`)); newPackageRule.matchPackageNames.push(
...patterns.map((v) => `!/${v}/`),
);
} }
delete newPackageRule.excludePackagePatterns; delete newPackageRule.excludePackagePatterns;
} }
// sourceUrl // sourceUrl
if (key === 'matchSourceUrlPrefixes') { if (key === 'matchSourceUrlPrefixes') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchSourceUrls ??= []; newPackageRule.matchSourceUrls ??= [];
newPackageRule.matchSourceUrls.push(...val.map((v) => `${v}{/,}**`)); newPackageRule.matchSourceUrls.push(
...patterns.map((v) => `${v}{/,}**`),
);
} }
delete newPackageRule.matchSourceUrlPrefixes; delete newPackageRule.matchSourceUrlPrefixes;
} }
// repository // repository
if (key === 'excludeRepositories') { if (key === 'excludeRepositories') {
if (is.array(val, is.string)) { if (is.array(patterns, is.string)) {
newPackageRule.matchRepositories ??= []; newPackageRule.matchRepositories ??= [];
newPackageRule.matchRepositories.push(...val.map((v) => `!${v}`)); newPackageRule.matchRepositories.push(...patterns.map((v) => `!${v}`));
} }
delete newPackageRule.excludeRepositories; delete newPackageRule.excludeRepositories;
} }