Skip to content

Commit

Permalink
feat: migratePresets (#10111)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed May 22, 2021
1 parent 35d481f commit 66820cf
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 2 deletions.
18 changes: 18 additions & 0 deletions docs/usage/self-hosted-configuration.md
Expand Up @@ -247,6 +247,24 @@ If left as default (null), a random short ID will be selected.

## logFileLevel

## migratePresets

Use this if you have repositories that extend from a particular preset, which has now been renamed or removed.
This is handy if you have a large number of repositories that all extend from a particular preset which you want to rename, without the hassle of manually updating every repository individually.
Use an empty string to indicate that the preset should be ignored rather than replaced.

Example:

```js
modules.exports = {
migratePresets: {
'@company': 'local>org/renovate-config',
},
};
```

In the above example any reference to the `@company` preset will be replaced with `local>org/renovate-config`.

## onboarding

Set this to `false` only if all three statements are true:
Expand Down
8 changes: 8 additions & 0 deletions lib/config/__snapshots__/migration.spec.ts.snap
Expand Up @@ -51,6 +51,14 @@ Object {
}
`;

exports[`config/migration it migrates presets 1`] = `
Object {
"extends": Array [
"local>org/renovate-config",
],
}
`;

exports[`config/migration migrateConfig(config, parentConfig) does not migrate multi days 1`] = `
Object {
"schedule": "after 5:00pm on wednesday and thursday",
Expand Down
1 change: 1 addition & 0 deletions lib/config/admin.ts
Expand Up @@ -14,6 +14,7 @@ const repoAdminOptions = [
'dockerUser',
'dryRun',
'exposeAllEnv',
'migratePresets',
'privateKey',
'localDir',
'cacheDir',
Expand Down
11 changes: 11 additions & 0 deletions lib/config/definitions.ts
Expand Up @@ -148,6 +148,17 @@ const options: RenovateOptions[] = [
allowString: true,
cli: false,
},
{
name: 'migratePresets',
description:
'Define presets here which have been removed or renamed and should be migrated automatically.',
type: 'object',
admin: true,
default: {},
additionalProperties: {
type: 'string',
},
},
{
name: 'description',
description: 'Plain text description for a config or preset.',
Expand Down
18 changes: 18 additions & 0 deletions lib/config/migration.spec.ts
@@ -1,5 +1,6 @@
import { getName } from '../../test/util';
import { PLATFORM_TYPE_GITHUB } from '../constants/platforms';
import { setAdminConfig } from './admin';
import { getConfig } from './defaults';
import * as configMigration from './migration';
import type {
Expand Down Expand Up @@ -682,4 +683,21 @@ describe(getName(), () => {
expect(isMigrated).toBe(true);
expect(migratedConfig).toMatchSnapshot();
});
it('it migrates presets', () => {
setAdminConfig({
migratePresets: {
'@org': 'local>org/renovate-config',
'@org2/foo': '',
},
});
const config: RenovateConfig = {
extends: ['@org', '@org2/foo'],
} as any;
const { isMigrated, migratedConfig } = configMigration.migrateConfig(
config,
defaultConfig
);
expect(isMigrated).toBe(true);
expect(migratedConfig).toMatchSnapshot();
});
});
8 changes: 7 additions & 1 deletion lib/config/migration.ts
Expand Up @@ -3,6 +3,7 @@ import is from '@sindresorhus/is';
import { dequal } from 'dequal';
import { logger } from '../logger';
import { clone } from '../util/clone';
import { getAdminConfig } from './admin';
import { getOptions } from './definitions';
import { removedPresets } from './presets/common';
import type {
Expand Down Expand Up @@ -54,6 +55,7 @@ export function migrateConfig(
'optionalDependencies',
'peerDependencies',
];
const { migratePresets } = getAdminConfig();
for (const [key, val] of Object.entries(config)) {
if (removedOptions.includes(key)) {
delete migratedConfig[key];
Expand Down Expand Up @@ -260,7 +262,11 @@ export function migrateConfig(
for (let i = 0; i < presets.length; i += 1) {
const preset = presets[i];
if (is.string(preset)) {
const newPreset = removedPresets[preset];
let newPreset = removedPresets[preset];
if (newPreset !== undefined) {
presets[i] = newPreset;
}
newPreset = migratePresets?.[preset];
if (newPreset !== undefined) {
presets[i] = newPreset;
}
Expand Down
1 change: 1 addition & 0 deletions lib/config/types.ts
Expand Up @@ -96,6 +96,7 @@ export interface RepoAdminConfig {
dockerUser?: string;
dryRun?: boolean;
exposeAllEnv?: boolean;
migratePresets?: Record<string, string>;
privateKey?: string | Buffer;
localDir?: string;
cacheDir?: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/config/validation.ts
Expand Up @@ -514,7 +514,7 @@ export async function validateConfig(
message: `Invalid \`${currentPath}.${key}.${res}\` configuration: value is not a url`,
});
}
} else if (key === 'customEnvVariables') {
} else if (['customEnvVariables', 'migratePresets'].includes(key)) {
const res = validatePlainObject(val);
if (res !== true) {
errors.push({
Expand Down

0 comments on commit 66820cf

Please sign in to comment.