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

refactor(migrations): class-based migration for packageRules #16390

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions lib/config/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,6 @@ export function migrateConfig(config: RenovateConfig): MigratedConfig {
}
}
}
if (is.array(migratedConfig.packageRules)) {
const newRules: PackageRule[] = [];
const renameMap = {
paths: 'matchPaths',
languages: 'matchLanguages',
baseBranchList: 'matchBaseBranches',
managers: 'matchManagers',
datasources: 'matchDatasources',
depTypeList: 'matchDepTypes',
packageNames: 'matchPackageNames',
packagePatterns: 'matchPackagePatterns',
sourceUrlPrefixes: 'matchSourceUrlPrefixes',
updateTypes: 'matchUpdateTypes',
} as const;
for (const packageRule of migratedConfig.packageRules) {
const newRuleObj = {} as PackageRule;
for (const [oldKey, ruleVal] of Object.entries(packageRule)) {
const key = renameMap[oldKey as keyof typeof renameMap] ?? oldKey;
// TODO: fix types #7154
newRuleObj[key] = ruleVal as never;
}
newRules.push(newRuleObj);
}
migratedConfig.packageRules = newRules;
}
// Migrate nested packageRules
if (is.nonEmptyArray(migratedConfig.packageRules)) {
const existingRules = migratedConfig.packageRules;
Expand Down
60 changes: 60 additions & 0 deletions lib/config/migrations/custom/package-rules-migration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { RenovateConfig } from '../../types';
import { MigrationsService } from '../migrations-service';
import { PackageRulesMigration, renameMap } from './package-rules-migration';

describe('config/migrations/custom/package-rules-migration', () => {
it('should preserve config order', () => {
const originalConfig: RenovateConfig = {
packageRules: [
{
paths: [],
labels: ['linting'],
baseBranchList: [],
languages: [],
managers: [],
datasources: [],
depTypeList: [],
addLabels: [],
packageNames: [],
packagePatterns: [],
sourceUrlPrefixes: [],
updateTypes: [],
},
],
};
const migratedPackageRules =
MigrationsService.run(originalConfig).packageRules;

const mappedProperties = Object.keys(migratedPackageRules![0]);
const expectedMappedProperties = Object.keys(
originalConfig.packageRules![0]
).map((key) => renameMap[key as keyof typeof renameMap] ?? key);

expect(expectedMappedProperties).toEqual(mappedProperties);
});

it('should not migrate nested packageRules', () => {
expect(PackageRulesMigration).toMigrate(
{
packageRules: [
{
paths: [],
packgageRules: {
languages: ['javascript'],
},
},
],
},
{
packageRules: [
{
matchPaths: [],
packgageRules: {
languages: ['javascript'],
},
},
],
}
);
});
});
37 changes: 37 additions & 0 deletions lib/config/migrations/custom/package-rules-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { PackageRule } from '../../types';
import { AbstractMigration } from '../base/abstract-migration';

export const renameMap = {
paths: 'matchPaths',
languages: 'matchLanguages',
baseBranchList: 'matchBaseBranches',
managers: 'matchManagers',
datasources: 'matchDatasources',
depTypeList: 'matchDepTypes',
packageNames: 'matchPackageNames',
packagePatterns: 'matchPackagePatterns',
sourceUrlPrefixes: 'matchSourceUrlPrefixes',
updateTypes: 'matchUpdateTypes',
};
type RenameMapKey = keyof typeof renameMap;

function renameKeys(packageRule: PackageRule): PackageRule {
const newPackageRule: PackageRule = {};
for (const [key, val] of Object.entries(packageRule)) {
newPackageRule[renameMap[key as RenameMapKey] ?? key] = val;
}
return newPackageRule;
}
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved

export class PackageRulesMigration extends AbstractMigration {
override readonly propertyName = 'packageRules';

override run(value: unknown): void {
let packageRules = (this.get('packageRules') as PackageRule[]) ?? [];
packageRules = Array.isArray(packageRules) ? [...packageRules] : [];

packageRules = packageRules.map(renameKeys);

this.rewrite(packageRules);
}
}
2 changes: 2 additions & 0 deletions lib/config/migrations/migrations-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { NodeMigration } from './custom/node-migration';
import { PackageFilesMigration } from './custom/package-files-migration';
import { PackageNameMigration } from './custom/package-name-migration';
import { PackagePatternMigration } from './custom/package-pattern-migration';
import { PackageRulesMigration } from './custom/package-rules-migration';
import { PackagesMigration } from './custom/packages-migration';
import { PathRulesMigration } from './custom/path-rules-migration';
import { PinVersionsMigration } from './custom/pin-versions-migration';
Expand Down Expand Up @@ -130,6 +131,7 @@ export class MigrationsService {
DryRunMigration,
RequireConfigMigration,
PackageFilesMigration,
PackageRulesMigration,
NodeMigration,
SemanticPrefixMigration,
];
Expand Down