Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate inline with the same sort
- Loading branch information
1 parent
d55650f
commit 8d9a6b9
Showing
9 changed files
with
110 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
lib/config/migrations/base/deprecate-property-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { RenovateConfig } from '../../types'; | ||
import type { Migration } from '../migration'; | ||
|
||
export class DeprecatePropertyMigration implements Migration { | ||
private readonly deprecatedPropertyName: string; | ||
|
||
constructor(deprecatedPropertyName: string) { | ||
this.deprecatedPropertyName = deprecatedPropertyName; | ||
} | ||
|
||
run(config: RenovateConfig): RenovateConfig { | ||
// eslint-disable-next-line no-param-reassign | ||
delete config[this.deprecatedPropertyName]; | ||
|
||
return config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { RenovateConfig } from '../../types'; | ||
import type { Migration } from '../migration'; | ||
|
||
export abstract class ReplacePropertyMigration implements Migration { | ||
protected abstract readonly deprecatedPropertyName: string; | ||
|
||
protected abstract readonly newPropertyName: string; | ||
|
||
abstract run(config: RenovateConfig): RenovateConfig; | ||
|
||
protected replaceProperty( | ||
config: RenovateConfig, | ||
newValue?: unknown | ||
): Record<string, unknown> { | ||
const migratedConfig: Record<string, unknown> = {}; | ||
|
||
for (const [key, value] of Object.entries(config)) { | ||
if (key === this.deprecatedPropertyName) { | ||
if (newValue !== undefined) { | ||
migratedConfig[this.newPropertyName] = newValue; | ||
} | ||
} else { | ||
migratedConfig[key] = value; | ||
} | ||
} | ||
|
||
return migratedConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1 @@ | ||
import { RenovateConfig } from '../types'; | ||
import type { Migration } from './migration'; | ||
import { RequiredStatusChecksMigration } from './required-status-checks-migration'; | ||
|
||
export function applyMigrations( | ||
originalConfig: RenovateConfig, | ||
migratedConfig: RenovateConfig | ||
): RenovateConfig { | ||
const migrations: Migration[] = [ | ||
new RequiredStatusChecksMigration(originalConfig, migratedConfig), | ||
]; | ||
|
||
for (const migration of migrations) { | ||
migration.migrate(); | ||
} | ||
|
||
return migratedConfig; | ||
} | ||
export * from './migrations-runner'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,5 @@ | ||
import type { RenovateConfig } from '../types'; | ||
|
||
export abstract class Migration { | ||
protected readonly originalConfig: RenovateConfig; | ||
|
||
protected readonly migratedConfig: RenovateConfig; | ||
|
||
constructor(originalConfig: RenovateConfig, migratedConfig: RenovateConfig) { | ||
this.originalConfig = originalConfig; | ||
this.migratedConfig = migratedConfig; | ||
} | ||
|
||
abstract migrate(): void; | ||
|
||
protected delete(property: string): void { | ||
delete this.migratedConfig[property]; | ||
} | ||
export interface Migration { | ||
run(config: RenovateConfig): RenovateConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { RenovateConfig } from '../types'; | ||
import { DeprecatePropertyMigration } from './base/deprecate-property-migration'; | ||
import type { Migration } from './migration'; | ||
import { PathRulesMigration } from './path-rules-migration'; | ||
import { RequiredStatusChecksMigration } from './required-status-checks-migration'; | ||
|
||
export class MigrationsRunner { | ||
private static readonly migrations: ReadonlyArray<Migration> = [ | ||
new DeprecatePropertyMigration('maintainYarnLock'), | ||
new PathRulesMigration(), | ||
new RequiredStatusChecksMigration(), | ||
]; | ||
|
||
static runAllMigrations(originalConfig: RenovateConfig): RenovateConfig { | ||
let config = originalConfig; | ||
|
||
for (const migration of MigrationsRunner.migrations) { | ||
config = migration.run(config); | ||
} | ||
|
||
return config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { RenovateConfig } from '../types'; | ||
import { ReplacePropertyMigration } from './base/replace-property-migration'; | ||
|
||
export class PathRulesMigration extends ReplacePropertyMigration { | ||
protected override readonly deprecatedPropertyName = 'pathRules'; | ||
|
||
protected override readonly newPropertyName = 'packageRules'; | ||
|
||
override run(config: RenovateConfig): RenovateConfig { | ||
const { pathRules } = config; | ||
const packageRules = Array.isArray(config.packageRules) | ||
? config.packageRules | ||
: []; | ||
|
||
return this.replaceProperty( | ||
config, | ||
Array.isArray(pathRules) ? pathRules.concat(packageRules) : packageRules | ||
); | ||
} | ||
} |
14 changes: 4 additions & 10 deletions
14
lib/config/migrations/required-status-checks-migration.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import { Migration } from './migration'; | ||
import type { RenovateConfig } from '../types'; | ||
import { ReplacePropertyMigration } from './base/replace-property-migration'; | ||
|
||
export class RequiredStatusChecksMigration extends Migration { | ||
public migrate(): void { | ||
this.delete('requiredStatusChecks'); | ||
export class RequiredStatusChecksMigration extends ReplacePropertyMigration { | ||
protected override readonly deprecatedPropertyName = 'requiredStatusChecks'; | ||
|
||
if (this.originalConfig.requiredStatusChecks === null) { | ||
this.migratedConfig.ignoreTests = true; | ||
} | ||
protected override readonly newPropertyName = 'ignoreTests'; | ||
|
||
override run(config: RenovateConfig): RenovateConfig { | ||
return this.replaceProperty( | ||
config, | ||
config.requiredStatusChecks === null ? true : undefined | ||
); | ||
} | ||
} |