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

* use class-based migration for packageRules

* fix errors

* refactor: remove unnecessaru if statement

* refactor: use argument

* refactor: apply suggestions

* Update lib/config/migrations/custom/package-rules-migration.ts

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>

* Update lib/config/migrations/custom/package-rules-migration.ts

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>

* refactor: import type PackageRule

* Update lib/config/migrations/custom/package-rules-migration.ts

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>

Co-authored-by: Jamie Magee <jamie.magee@gmail.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
RahulGautamSingh 2022-07-11 10:29:06 +05:30 committed by GitHub
parent 48b9c5ca89
commit 5774fb3f2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 25 deletions

View file

@ -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 // Migrate nested packageRules
if (is.nonEmptyArray(migratedConfig.packageRules)) { if (is.nonEmptyArray(migratedConfig.packageRules)) {
const existingRules = migratedConfig.packageRules; const existingRules = migratedConfig.packageRules;

View file

@ -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'],
},
},
],
}
);
});
});

View file

@ -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;
}
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);
}
}

View file

@ -27,6 +27,7 @@ import { NodeMigration } from './custom/node-migration';
import { PackageFilesMigration } from './custom/package-files-migration'; import { PackageFilesMigration } from './custom/package-files-migration';
import { PackageNameMigration } from './custom/package-name-migration'; import { PackageNameMigration } from './custom/package-name-migration';
import { PackagePatternMigration } from './custom/package-pattern-migration'; import { PackagePatternMigration } from './custom/package-pattern-migration';
import { PackageRulesMigration } from './custom/package-rules-migration';
import { PackagesMigration } from './custom/packages-migration'; import { PackagesMigration } from './custom/packages-migration';
import { PathRulesMigration } from './custom/path-rules-migration'; import { PathRulesMigration } from './custom/path-rules-migration';
import { PinVersionsMigration } from './custom/pin-versions-migration'; import { PinVersionsMigration } from './custom/pin-versions-migration';
@ -130,6 +131,7 @@ export class MigrationsService {
DryRunMigration, DryRunMigration,
RequireConfigMigration, RequireConfigMigration,
PackageFilesMigration, PackageFilesMigration,
PackageRulesMigration,
NodeMigration, NodeMigration,
SemanticPrefixMigration, SemanticPrefixMigration,
]; ];