Skip to content

Commit

Permalink
feat: replace deprecated dependencies with their replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee committed Apr 10, 2020
1 parent 7640ca0 commit a8752ec
Show file tree
Hide file tree
Showing 19 changed files with 257 additions and 85 deletions.
8 changes: 8 additions & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,14 @@ Use this field to match rules against types of updates. For example to apply a s
}
```

### replacementName

Valid in `packageRules`

### replacementVersion

Valid in `packageRules`

## patch

Add to this object if you wish to define rules that apply only to patch updates. See also `major` and `minor` configuration options.
Expand Down
18 changes: 9 additions & 9 deletions lib/config/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Object {
"commitBody": null,
"commitBodyTable": false,
"commitMessage": "{{{commitMessagePrefix}}} {{{commitMessageAction}}} {{{commitMessageTopic}}} {{{commitMessageExtra}}} {{{commitMessageSuffix}}}",
"commitMessageAction": "Update",
"commitMessageExtra": "to {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{toVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}",
"commitMessageAction": "{{# if isReplacement}}Replace{{else}}Update{{/if}}",
"commitMessageExtra": "{{#if isReplacement}} with {{newName}}{{else}}to {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{toVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}{{/if}}",
"commitMessagePrefix": null,
"commitMessageSuffix": null,
"commitMessageTopic": "dependency {{depName}}",
Expand Down Expand Up @@ -68,7 +68,7 @@ Object {
"Change",
],
"prBodyDefinitions": Object {
"Change": "\`{{{displayFrom}}}\` -> \`{{{displayTo}}}\`",
"Change": "{{#if isReplacement}}{{newName}}{{else}}\`{{{displayFrom}}}\` -> \`{{{displayTo}}}\`{{/if}}",
"Current value": "{{{currentValue}}}",
"New value": "{{{newValue}}}",
"Package": "{{{depNameLinked}}}",
Expand Down Expand Up @@ -155,8 +155,8 @@ Object {
"commitBody": null,
"commitBodyTable": false,
"commitMessage": "{{{commitMessagePrefix}}} {{{commitMessageAction}}} {{{commitMessageTopic}}} {{{commitMessageExtra}}} {{{commitMessageSuffix}}}",
"commitMessageAction": "Update",
"commitMessageExtra": "to {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{toVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}",
"commitMessageAction": "{{# if isReplacement}}Replace{{else}}Update{{/if}}",
"commitMessageExtra": "{{#if isReplacement}} with {{newName}}{{else}}to {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{toVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}{{/if}}",
"commitMessagePrefix": null,
"commitMessageSuffix": null,
"commitMessageTopic": "dependency {{depName}}",
Expand Down Expand Up @@ -281,7 +281,7 @@ Object {
"Change",
],
"prBodyDefinitions": Object {
"Change": "[{{#if displayFrom}}\`{{{displayFrom}}}\` -> {{else}}{{#if currentValue}}\`{{{currentValue}}}\` -> {{/if}}{{/if}}{{#if displayTo}}\`{{{displayTo}}}\`{{else}}\`{{{newValue}}}\`{{/if}}](https://renovatebot.com/diffs/npm/{{replace '/' '%2f' depName}}/{{{fromVersion}}}/{{{toVersion}}})",
"Change": "{{#if isReplacement}}{{newName}}{{else}}[{{#if displayFrom}}\`{{{displayFrom}}}\` -> {{else}}{{#if currentValue}}\`{{{currentValue}}}\` -> {{/if}}{{/if}}{{#if displayTo}}\`{{{displayTo}}}\`{{else}}\`{{{newValue}}}\`{{/if}}](https://renovatebot.com/diffs/npm/{{replace '/' '%2f' depName}}/{{{fromVersion}}}/{{{toVersion}}}){{/if}}",
"Current value": "{{{currentValue}}}",
"New value": "{{{newValue}}}",
"Package": "{{{depNameLinked}}}",
Expand Down Expand Up @@ -383,8 +383,8 @@ Object {
"commitBody": null,
"commitBodyTable": false,
"commitMessage": "{{{commitMessagePrefix}}} {{{commitMessageAction}}} {{{commitMessageTopic}}} {{{commitMessageExtra}}} {{{commitMessageSuffix}}}",
"commitMessageAction": "Update",
"commitMessageExtra": "to {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{toVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}",
"commitMessageAction": "{{# if isReplacement}}Replace{{else}}Update{{/if}}",
"commitMessageExtra": "{{#if isReplacement}} with {{newName}}{{else}}to {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{toVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}{{/if}}",
"commitMessagePrefix": null,
"commitMessageSuffix": null,
"commitMessageTopic": "dependency {{depName}}",
Expand Down Expand Up @@ -510,7 +510,7 @@ Object {
"Change",
],
"prBodyDefinitions": Object {
"Change": "\`{{{displayFrom}}}\` -> \`{{{displayTo}}}\`",
"Change": "{{#if isReplacement}}{{newName}}{{else}}\`{{{displayFrom}}}\` -> \`{{{displayTo}}}\`{{/if}}",
"Current value": "{{{currentValue}}}",
"New value": "{{{newValue}}}",
"Package": "{{{depNameLinked}}}",
Expand Down
14 changes: 8 additions & 6 deletions lib/config/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,27 @@ export type UpdateType =
| 'lockFileMaintenance'
| 'lockfileUpdate'
| 'rollback'
| 'bump';
| 'bump'
| 'replacement';

// TODO: Proper typings
export interface PackageRule
extends RenovateSharedConfig,
UpdateConfig,
Record<string, any> {
paths?: string[];
languages?: string[];
baseBranchList?: string[];
datasources?: string[];
depTypeList?: string[];
packageNames?: string[];
packagePatterns?: string[];
excludePackageNames?: string[];
excludePackagePatterns?: string[];
languages?: string[];
matchCurrentVersion?: string | Range;
packageNames?: string[];
packagePatterns?: string[];
paths?: string[];
replacementName?: string;
replacementVersion?: string;
sourceUrlPrefixes?: string[];

updateTypes?: UpdateType[];
}

Expand Down
27 changes: 24 additions & 3 deletions lib/config/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,26 @@ const options: RenovateOptions[] = [
cli: false,
env: false,
},
{
name: 'replacementName',
description: '',
type: 'string',
stage: 'package',
parent: 'packageRules',
mergeable: true,
cli: false,
env: false,
},
{
name: 'replacementVersion',
description: '',
type: 'string',
stage: 'package',
parent: 'packageRules',
mergeable: true,
cli: false,
env: false,
},
{
name: 'updateTypes',
description:
Expand Down Expand Up @@ -1217,7 +1237,7 @@ const options: RenovateOptions[] = [
name: 'commitMessageAction',
description: 'Action verb to use in commit messages and PR titles',
type: 'string',
default: 'Update',
default: '{{# if isReplacement}}Replace{{else}}Update{{/if}}',
cli: false,
},
{
Expand All @@ -1233,7 +1253,7 @@ const options: RenovateOptions[] = [
'Extra description used after the commit message topic - typically the version',
type: 'string',
default:
'to {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{toVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}',
'{{#if isReplacement}} with {{newName}}{{else}}to {{#if isMajor}}v{{{newMajor}}}{{else}}{{#if isSingleVersion}}v{{{toVersion}}}{{else}}{{{newValue}}}{{/if}}{{/if}}{{/if}}',
cli: false,
},
{
Expand Down Expand Up @@ -1587,7 +1607,8 @@ const options: RenovateOptions[] = [
Update: '{{{updateType}}}',
'Current value': '{{{currentValue}}}',
'New value': '{{{newValue}}}',
Change: '`{{{displayFrom}}}` -> `{{{displayTo}}}`',
Change:
'{{#if isReplacement}}{{newName}}{{else}}`{{{displayFrom}}}` -> `{{{displayTo}}}`{{/if}}',
References: '{{{references}}}',
'Package file': '{{{packageFile}}}',
},
Expand Down
4 changes: 2 additions & 2 deletions lib/config/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RenovateConfig } from './common';
import { logger } from '../logger';
import { clone } from '../util/clone';
import * as definitions from './definitions';
import { getOptions } from './definitions';

export function mergeChildConfig<T, TChild>(
parent: T,
Expand All @@ -14,7 +14,7 @@ export function mergeChildConfig<T, TChild>(
const parentConfig = clone(parent);
const childConfig = clone(child);
const config: Record<string, any> = { ...parentConfig, ...childConfig };
for (const option of definitions.getOptions()) {
for (const option of getOptions()) {
if (
option.mergeable &&
childConfig[option.name] &&
Expand Down
4 changes: 4 additions & 0 deletions lib/datasource/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface GetPkgReleasesConfig extends ReleasesConfigBase {
depName: string;
lookupName?: string;
versioning?: string;
replacementName?: string;
replacementVersion?: string;
}

export function isGetPkgReleasesConfig(
Expand Down Expand Up @@ -59,6 +61,8 @@ export interface ReleaseResult {
name?: string;
pkgName?: string;
releases: Release[];
replacementName?: string;
replacementVersion?: string;
sourceUrl?: string;
tags?: string[];
versions?: any;
Expand Down
22 changes: 20 additions & 2 deletions lib/datasource/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import * as datasource from '.';
import * as datasourceDocker from './docker';
import * as datasourceGithubTags from './github-tags';
import * as datasourceNpm from './npm';
import { mocked } from '../../test/util';
import { loadModules } from '../util/modules';
import { mocked, getName } from '../../test/util';

jest.mock('./docker');
jest.mock('./npm');

const npmDatasource = mocked(datasourceNpm);

describe('datasource/index', () => {
describe(getName(__filename), () => {
beforeEach(() => {
jest.resetAllMocks();
global.repoCache = {};
});

it('returns datasources', () => {
expect(datasource.getDatasources()).toBeDefined();
expect(datasource.getDatasourceList()).toBeDefined();
Expand Down Expand Up @@ -117,4 +122,17 @@ describe('datasource/index', () => {
});
expect(res.sourceUrl).toEqual('https://github.com/Jasig/cas');
});
it('applies replacements', async () => {
npmDatasource.getReleases.mockResolvedValue({
releases: [{ version: '1.0.0' }],
});
const res = await datasource.getPkgReleases({
datasource: datasourceNpm.id,
depName: 'abc',
replacementName: 'def',
replacementVersion: '2.0.0',
});
expect(res.replacementName).toEqual('def');
expect(res.replacementVersion).toEqual('2.0.0');
});
});
16 changes: 15 additions & 1 deletion lib/datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ function load(datasource: string): Promise<Datasource> {

type GetReleasesInternalConfig = GetReleasesConfig & GetPkgReleasesConfig;

function applyReplacements(
dep: ReleaseResult,
config: GetReleasesInternalConfig
): ReleaseResult {
if (config.replacementName && config.replacementVersion) {
return {
...dep,
replacementName: config.replacementName,
replacementVersion: config.replacementVersion,
};
}
return dep;
}

function resolveRegistryUrls(
datasource: Datasource,
extractedUrls: string[]
Expand All @@ -53,7 +67,7 @@ async function fetchReleases(
registryUrls,
});
addMetaData(dep, datasourceName, config.lookupName);
return dep;
return applyReplacements(dep, config);
}

function getRawReleases(
Expand Down
1 change: 1 addition & 0 deletions lib/manager/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export interface Upgrade<T = Record<string, any>>
newDigest?: string;
newFrom?: string;
newMajor?: number;
newName?: string;
newValue?: string;
newVersion?: string;
packageFile?: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export const defaultConfig = {
versioning: npmVersioning.id,
prBodyDefinitions: {
Change:
"[{{#if displayFrom}}`{{{displayFrom}}}` -> {{else}}{{#if currentValue}}`{{{currentValue}}}` -> {{/if}}{{/if}}{{#if displayTo}}`{{{displayTo}}}`{{else}}`{{{newValue}}}`{{/if}}](https://renovatebot.com/diffs/npm/{{replace '/' '%2f' depName}}/{{{fromVersion}}}/{{{toVersion}}})",
"{{#if isReplacement}}{{newName}}{{else}}[{{#if displayFrom}}`{{{displayFrom}}}` -> {{else}}{{#if currentValue}}`{{{currentValue}}}` -> {{/if}}{{/if}}{{#if displayTo}}`{{{displayTo}}}`{{else}}`{{{newValue}}}`{{/if}}](https://renovatebot.com/diffs/npm/{{replace '/' '%2f' depName}}/{{{fromVersion}}}/{{{toVersion}}}){{/if}}",
},
};
26 changes: 26 additions & 0 deletions lib/manager/npm/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,32 @@ describe('workers/branch/package-json', () => {
});
expect(testContent).toBeNull();
});
it('returns null file empty', () => {
const upgrade = {
depType: 'blah',
depName: 'angular-touch-not',
newValue: '1.5.8',
};
const testContent = npmUpdater.updateDependency({
fileContent: input01Content,
upgrade,
});
expect(testContent).toBeNull();
});
it('replaces package', () => {
const upgrade = {
depType: 'dependencies',
depName: 'config',
newName: 'abc',
newValue: '2.0.0',
};
const testContent = npmUpdater.updateDependency({
fileContent: input01Content,
upgrade,
});
expect(JSON.parse(testContent).dependencies.config).toBeUndefined();
expect(JSON.parse(testContent).dependencies.abc).toEqual('2.0.0');
});
});
describe('.bumpPackageVersion()', () => {
const content = JSON.stringify({
Expand Down
Loading

0 comments on commit a8752ec

Please sign in to comment.