mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-13 15:36:25 +00:00
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:
parent
48b9c5ca89
commit
5774fb3f2a
4 changed files with 99 additions and 25 deletions
|
@ -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;
|
||||||
|
|
60
lib/config/migrations/custom/package-rules-migration.spec.ts
Normal file
60
lib/config/migrations/custom/package-rules-migration.spec.ts
Normal 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'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
37
lib/config/migrations/custom/package-rules-migration.ts
Normal file
37
lib/config/migrations/custom/package-rules-migration.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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,
|
||||||
];
|
];
|
||||||
|
|
Loading…
Reference in a new issue