Skip to content

Commit

Permalink
feat(helmv3): Add support for bumpVersion (#7670)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebryant authored Nov 10, 2020
1 parent 8844549 commit ad50398
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ This is an advance field and it's recommend you seek a config review before appl

## bumpVersion

Currently this setting supports `npm` only, so raise a feature request if you have a use for it with other package managers.
Currently this setting supports `helmv3` and `npm` only, so raise a feature request if you have a use for it with other package managers.
It's purpose is if you want Renovate to update the `version` field within your file's `package.json` any time it updates dependencies within.
Usually this is for automatic release purposes, so that you don't need to add another step after Renovate before you can release a new version.

Configure this value to `"patch"`, `"minor"` or `"major"` to have Renovate update the version in your edited `package.json`.
e.g. if you wish Renovate to always increase the target `package.json` version with a patch update, configure this to `"patch"`.

You can also configure this field to `"mirror:x"` where `x` is the name of a package in the `package.json`.
For `npm` only you can also configure this field to `"mirror:x"` where `x` is the name of a package in the `package.json`.
Doing so means that the `package.json` `version` field will mirror whatever the version is that `x` depended on.
Make sure that version is a pinned version of course, as otherwise it won't be valid.

Expand Down
2 changes: 1 addition & 1 deletion lib/config/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ const options: RenovateOptions[] = [
},
{
name: 'bumpVersion',
description: 'Bump the version in the package.json being updated',
description: 'Bump the version in the package file being updated',
type: 'string',
allowedValues: ['major', 'minor', 'patch'],
},
Expand Down
6 changes: 6 additions & 0 deletions lib/manager/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ export interface ManagerApi {
language?: string;
supportsLockFileMaintenance?: boolean;

bumpPackageVersion?(
content: string,
currentValue: string,
bumpVersion: ReleaseType | string
): Result<string | null>;

extractAllPackageFiles?(
config: ExtractConfig,
files: string[]
Expand Down
4 changes: 4 additions & 0 deletions lib/manager/helmv3/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Object {
],
},
],
"packageFileVersion": "0.1.0",
}
`;

Expand All @@ -34,6 +35,7 @@ Object {
],
},
],
"packageFileVersion": "0.1.0",
}
`;

Expand Down Expand Up @@ -63,6 +65,7 @@ Object {
"skipReason": "no-repository",
},
],
"packageFileVersion": "0.1.0",
}
`;

Expand All @@ -86,5 +89,6 @@ Object {
"skipReason": "local-dependency",
},
],
"packageFileVersion": "0.1.0",
}
`;
15 changes: 15 additions & 0 deletions lib/manager/helmv3/__snapshots__/update.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lib/manager/helmv3/update .bumpPackageVersion() increments 1`] = `
"apiVersion: v2
name: test
version: 0.0.3
"
`;

exports[`lib/manager/helmv3/update .bumpPackageVersion() updates 1`] = `
"apiVersion: v2
name: test
version: 0.1.0
"
`;
2 changes: 2 additions & 0 deletions lib/manager/helmv3/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function extractPackageFile(
logger.debug({ fileName }, 'Failed to parse helm Chart.yaml');
return null;
}
const packageFileVersion = chart.version;
let deps: PackageDependency[] = [];
if (!is.nonEmptyArray(chart?.dependencies)) {
logger.debug({ fileName }, 'Chart has no dependencies');
Expand Down Expand Up @@ -84,6 +85,7 @@ export function extractPackageFile(
const res = {
deps,
datasource: datasourceHelm.id,
packageFileVersion,
};
return res;
}
1 change: 1 addition & 0 deletions lib/manager/helmv3/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { updateArtifacts } from './artifacts';
export { extractPackageFile } from './extract';
export { bumpPackageVersion } from './update';

export const supportsLockFileMaintenance = true;

Expand Down
34 changes: 34 additions & 0 deletions lib/manager/helmv3/update.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import yaml from 'js-yaml';
import * as helmv3Updater from './update';

describe('lib/manager/helmv3/update', () => {
describe('.bumpPackageVersion()', () => {
const content = yaml.safeDump({
apiVersion: 'v2',
name: 'test',
version: '0.0.2',
});
it('increments', () => {
const res = helmv3Updater.bumpPackageVersion(content, '0.0.2', 'patch');
expect(res).toMatchSnapshot();
expect(res).not.toEqual(content);
});
it('no ops', () => {
const res = helmv3Updater.bumpPackageVersion(content, '0.0.1', 'patch');
expect(res).toEqual(content);
});
it('updates', () => {
const res = helmv3Updater.bumpPackageVersion(content, '0.0.1', 'minor');
expect(res).toMatchSnapshot();
expect(res).not.toEqual(content);
});
it('returns content if bumping errors', () => {
const res = helmv3Updater.bumpPackageVersion(
content,
'0.0.2',
true as any
);
expect(res).toEqual(content);
});
});
});
41 changes: 41 additions & 0 deletions lib/manager/helmv3/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ReleaseType, inc } from 'semver';
import { logger } from '../../logger';

export function bumpPackageVersion(
content: string,
currentValue: string,
bumpVersion: ReleaseType | string
): string {
logger.debug(
{ bumpVersion, currentValue },
'Checking if we should bump Chart.yaml version'
);
let newChartVersion: string;
try {
newChartVersion = inc(currentValue, bumpVersion as ReleaseType);
if (!newChartVersion) {
throw new Error('semver inc failed');
}
logger.debug({ newChartVersion });
const bumpedContent = content.replace(
/^(version:\s*).*$/m,
`$1${newChartVersion}`
);
if (bumpedContent === content) {
logger.debug('Version was already bumped');
} else {
logger.debug('Bumped Chart.yaml version');
}
return bumpedContent;
} catch (err) {
logger.warn(
{
content,
currentValue,
bumpVersion,
},
'Failed to bumpVersion'
);
return content;
}
}
2 changes: 1 addition & 1 deletion lib/manager/npm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { LANGUAGE_JAVASCRIPT } from '../../constants/languages';
import * as npmVersioning from '../../versioning/npm';

export { extractAllPackageFiles } from './extract';
export { updateDependency } from './update';
export { bumpPackageVersion, updateDependency } from './update';
export { getRangeStrategy } from './range';

export const language = LANGUAGE_JAVASCRIPT;
Expand Down
15 changes: 2 additions & 13 deletions lib/manager/npm/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ export function bumpPackageVersion(
currentValue: string,
bumpVersion: ReleaseType | string
): string {
if (!bumpVersion) {
return content;
}
logger.debug(
{ bumpVersion, currentValue },
'Checking if we should bump package.json version'
Expand Down Expand Up @@ -89,11 +86,7 @@ export function updateDependency({
const oldVersion: string = parsedContents[depType][depName];
if (oldVersion === newValue) {
logger.trace('Version is already updated');
return bumpPackageVersion(
fileContent,
upgrade.packageFileVersion,
upgrade.bumpVersion
);
return fileContent;
}
// Update the file = this is what we want
parsedContents[depType][depName] = newValue;
Expand Down Expand Up @@ -180,11 +173,7 @@ export function updateDependency({
}
}
}
return bumpPackageVersion(
newFileContent,
upgrade.packageFileVersion,
upgrade.bumpVersion
);
return newFileContent;
} catch (err) {
logger.debug({ err }, 'updateDependency error');
return null;
Expand Down
28 changes: 28 additions & 0 deletions lib/workers/branch/__snapshots__/get-updated.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`workers/branch/get-updated getUpdatedPackageFiles() bumps versions in autoReplace managers 1`] = `
Object {
"artifactErrors": Array [],
"reuseExistingBranch": undefined,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [
Object {
"contents": "version: 0.0.2",
"name": "undefined",
},
],
}
`;

exports[`workers/branch/get-updated getUpdatedPackageFiles() bumps versions in updateDependency managers 1`] = `
Object {
"artifactErrors": Array [],
"reuseExistingBranch": undefined,
"updatedArtifacts": Array [],
"updatedPackageFiles": Array [
Object {
"contents": "new version",
"name": "undefined",
},
],
}
`;

exports[`workers/branch/get-updated getUpdatedPackageFiles() handles autoreplace base updated 1`] = `
Object {
"artifactErrors": Array [],
Expand Down
25 changes: 25 additions & 0 deletions lib/workers/branch/get-updated.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { defaultConfig, git, mocked } from '../../../test/util';
import * as datasourceGitSubmodules from '../../datasource/git-submodules';
import * as _composer from '../../manager/composer';
import * as _gitSubmodules from '../../manager/git-submodules';
import * as _helmv3 from '../../manager/helmv3';
import * as _npm from '../../manager/npm';
import { BranchConfig } from '../common';
import * as _autoReplace from './auto-replace';
import { getUpdatedPackageFiles } from './get-updated';

const composer = mocked(_composer);
const gitSubmodules = mocked(_gitSubmodules);
const helmv3 = mocked(_helmv3);
const npm = mocked(_npm);
const autoReplace = mocked(_autoReplace);

jest.mock('../../manager/composer');
jest.mock('../../manager/helmv3');
jest.mock('../../manager/npm');
jest.mock('../../manager/git-submodules');
jest.mock('../../util/git');
Expand Down Expand Up @@ -169,5 +172,27 @@ describe('workers/branch/get-updated', () => {
const res = await getUpdatedPackageFiles(config);
expect(res).toMatchSnapshot();
});
it('bumps versions in updateDependency managers', async () => {
config.upgrades.push({
branchName: undefined,
bumpVersion: 'patch',
manager: 'npm',
});
npm.updateDependency.mockReturnValue('old version');
npm.bumpPackageVersion.mockReturnValue('new version');
const res = await getUpdatedPackageFiles(config);
expect(res).toMatchSnapshot();
});
it('bumps versions in autoReplace managers', async () => {
config.upgrades.push({
branchName: undefined,
bumpVersion: 'patch',
manager: 'helmv3',
});
autoReplace.doAutoReplace.mockResolvedValueOnce('version: 0.0.1');
helmv3.bumpPackageVersion.mockReturnValue('version: 0.0.2');
const res = await getUpdatedPackageFiles(config);
expect(res).toMatchSnapshot();
});
});
});
19 changes: 17 additions & 2 deletions lib/workers/branch/get-updated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ export async function getUpdatedPackageFiles(
reuseExistingBranch: false,
});
}
const bumpPackageVersion = get(manager, 'bumpPackageVersion');
const updateDependency = get(manager, 'updateDependency');
if (!updateDependency) {
const res = await doAutoReplace(
let res = await doAutoReplace(
upgrade,
existingContent,
reuseExistingBranch
);
if (res) {
if (bumpPackageVersion && upgrade.bumpVersion) {
res = await bumpPackageVersion(
res,
upgrade.packageFileVersion,
upgrade.bumpVersion
);
}
if (res === existingContent) {
logger.debug({ packageFile, depName }, 'No content changed');
if (upgrade.rangeStrategy === 'update-lockfile') {
Expand All @@ -84,10 +92,17 @@ export async function getUpdatedPackageFiles(
logger.error({ packageFile, depName }, 'Could not autoReplace');
throw new Error(WORKER_FILE_UPDATE_FAILED);
}
const newContent = await updateDependency({
let newContent = await updateDependency({
fileContent: existingContent,
upgrade,
});
if (bumpPackageVersion && upgrade.bumpVersion) {
newContent = await bumpPackageVersion(
newContent,
upgrade.packageFileVersion,
upgrade.bumpVersion
);
}
if (!newContent) {
if (config.reuseExistingBranch) {
logger.debug(
Expand Down

0 comments on commit ad50398

Please sign in to comment.