-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 c719277
Showing
8 changed files
with
99 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,36 @@ | ||
import type { RenovateConfig } from '../../types'; | ||
import type { Migration } from '../migration'; | ||
|
||
export class ReplacePropertyMigration implements Migration { | ||
protected readonly deprecatedPropertyName: string; | ||
|
||
protected readonly newPropertyName: string; | ||
|
||
constructor(deprecatedPropertyName: string, newPropertyName: string) { | ||
this.deprecatedPropertyName = deprecatedPropertyName; | ||
this.newPropertyName = newPropertyName; | ||
} | ||
|
||
run(config: RenovateConfig): RenovateConfig { | ||
return this.replaceProperty(config, config[this.deprecatedPropertyName]); | ||
} | ||
|
||
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,25 @@ | ||
import type { RenovateConfig } from '../types'; | ||
import { DeprecatePropertyMigration } from './base/deprecate-property-migration'; | ||
import { ReplacePropertyMigration } from './base/replace-property-migration'; | ||
import type { Migration } from './migration'; | ||
import { RequiredStatusChecksMigration } from './required-status-checks-migration'; | ||
|
||
export class MigrationsRunner { | ||
private static readonly migrations: ReadonlyArray<Migration> = [ | ||
new DeprecatePropertyMigration('maintainYarnLock'), | ||
new DeprecatePropertyMigration('gitFs'), | ||
new ReplacePropertyMigration('exposeEnv', 'exposeAllEnv'), | ||
new ReplacePropertyMigration('separatePatchReleases', 'separateMinorPatch'), | ||
new RequiredStatusChecksMigration(), | ||
]; | ||
|
||
static runAllMigrations(originalConfig: RenovateConfig): RenovateConfig { | ||
let config = originalConfig; | ||
|
||
for (const migration of MigrationsRunner.migrations) { | ||
config = migration.run(config); | ||
} | ||
|
||
return config; | ||
} | ||
} |
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 { | ||
constructor() { | ||
super('requiredStatusChecks', 'ignoreTests'); | ||
} | ||
|
||
if (this.originalConfig.requiredStatusChecks === null) { | ||
this.migratedConfig.ignoreTests = true; | ||
} | ||
override run(config: RenovateConfig): RenovateConfig { | ||
return this.replaceProperty( | ||
config, | ||
config.requiredStatusChecks === null ? true : undefined | ||
); | ||
} | ||
} |