Skip to content

Commit

Permalink
fix(package-rules): migrate matchers with string values (#31083)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh authored Aug 28, 2024
1 parent 0bc5cc1 commit ded9a7b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/config/__snapshots__/migration.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ exports[`config/migration migrateConfig(config, parentConfig) migrates config 1`
"groupName": "angular packages",
"matchPackageNames": [
"/^(@angular|typescript)/",
"!foo",
],
},
{
Expand Down
45 changes: 45 additions & 0 deletions lib/config/migrations/custom/package-rules-migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
expect(PackageRulesMigration).toMigrate(
{
Expand Down
57 changes: 34 additions & 23 deletions lib/config/migrations/custom/package-rules-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,57 @@ function renameKeys(packageRule: PackageRule): PackageRule {
function mergeMatchers(packageRule: PackageRule): PackageRule {
const newPackageRule: PackageRule = { ...packageRule };
for (const [key, val] of Object.entries(packageRule)) {
const patterns = is.string(val) ? [val] : val;

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

0 comments on commit ded9a7b

Please sign in to comment.