feat(maven): bumpVersion support (#14201)

This commit is contained in:
Kaj Ström 2022-02-17 10:57:13 +02:00 committed by GitHub
parent dfa553da11
commit c424a84f87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 145 additions and 6 deletions

View file

@ -316,11 +316,11 @@ This is an advance field and it's recommend you seek a config review before appl
## bumpVersion
Currently this setting supports `helmv3`, `npm` and `sbt` only, so raise a feature request if you have a use for it with other package managers.
Its purpose is if you want Renovate to update the `version` field within your file's `package.json` any time it updates dependencies within.
Currently this setting supports `helmv3`, `npm`, `maven` and `sbt` only, so raise a feature request if you have a use for it with other package managers.
Its purpose is if you want Renovate to update the `version` field within your package file 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`.
Configure this value to `"patch"`, `"minor"` or `"major"` to have Renovate update the version in your edited package file.
e.g. if you wish Renovate to always increase the target `package.json` version with a patch update, configure this to `"patch"`.
For `npm` only you can also configure this field to `"mirror:x"` where `x` is the name of a package in the `package.json`.

View file

@ -1194,7 +1194,7 @@ const options: RenovateOptions[] = [
description: 'Bump the version in the package file being updated.',
type: 'string',
allowedValues: ['major', 'minor', 'patch'],
supportedManagers: ['helmv3', 'npm', 'sbt'],
supportedManagers: ['helmv3', 'npm', 'maven', 'sbt'],
},
// Major/Minor/Patch
{

View file

@ -209,5 +209,6 @@ Object {
},
},
"packageFile": null,
"packageFileVersion": "0.0.1",
}
`;

View file

@ -198,6 +198,7 @@ Array [
},
],
"packageFile": "random.pom.xml",
"packageFileVersion": "0.0.1",
"parent": "../pom.xml",
},
]
@ -390,6 +391,7 @@ Array [
},
],
"packageFile": "child.pom.xml",
"packageFileVersion": "0.0.1",
"parent": "parent.pom.xml",
},
]

View file

@ -130,6 +130,7 @@ describe('manager/maven/extract', () => {
deps: [],
mavenProps: {},
packageFile: null,
packageFileVersion: '1',
});
});
});

View file

@ -247,6 +247,10 @@ export function extractPackage(
result.parent = resolveParentFile(packageFile, parentPath);
}
if (project.childNamed('version')) {
result.packageFileVersion = project.valueWithPath('version').trim();
}
return result;
}

View file

@ -3,7 +3,7 @@ import { MavenDatasource } from '../../datasource/maven';
import * as mavenVersioning from '../../versioning/maven';
export { extractAllPackageFiles } from './extract';
export { updateDependency } from './update';
export { bumpPackageVersion, updateDependency } from './update';
export const language = ProgrammingLanguage.Java;

View file

@ -0,0 +1,66 @@
import { XmlDocument } from 'xmldoc';
import { Fixtures } from '../../../test/fixtures';
import * as pomUpdater from './update';
const simpleContent = Fixtures.get(`simple.pom.xml`);
const minimumContent = Fixtures.get(`minimum.pom.xml`);
describe('manager/maven/update', () => {
describe('bumpPackageVersion', () => {
it('bumps pom.xml version', () => {
const { bumpedContent } = pomUpdater.bumpPackageVersion(
simpleContent,
'0.0.1',
'patch'
);
const project = new XmlDocument(bumpedContent);
expect(project.valueWithPath('version')).toBe('0.0.2');
});
it('does not bump version twice', () => {
const { bumpedContent } = pomUpdater.bumpPackageVersion(
simpleContent,
'0.0.1',
'patch'
);
const { bumpedContent: bumpedContent2 } = pomUpdater.bumpPackageVersion(
bumpedContent,
'0.0.1',
'patch'
);
expect(bumpedContent).toEqual(bumpedContent2);
});
it('does not bump version if version is not a semantic version', () => {
const { bumpedContent } = pomUpdater.bumpPackageVersion(
minimumContent,
'1',
'patch'
);
const project = new XmlDocument(bumpedContent);
expect(project.valueWithPath('version')).toBe('1');
});
it('does not bump version if pom.xml has no version', () => {
const { bumpedContent } = pomUpdater.bumpPackageVersion(
minimumContent,
undefined,
'patch'
);
expect(bumpedContent).toEqual(minimumContent);
});
it('returns content if bumping errors', () => {
const { bumpedContent } = pomUpdater.bumpPackageVersion(
simpleContent,
'0.0.1',
true as any
);
expect(bumpedContent).toEqual(simpleContent);
});
});
});

View file

@ -1,5 +1,12 @@
import semver, { ReleaseType } from 'semver';
import { XmlDocument } from 'xmldoc';
import { logger } from '../../logger';
import type { UpdateDependencyConfig, Upgrade } from '../types';
import { replaceAt } from '../../util/string';
import type {
BumpPackageVersionResult,
UpdateDependencyConfig,
Upgrade,
} from '../types';
export function updateAtPosition(
fileContent: string,
@ -44,3 +51,61 @@ export function updateDependency({
}
return `${spaces}${updatedContent}`;
}
export function bumpPackageVersion(
content: string,
currentValue: string | undefined,
bumpVersion: ReleaseType | string
): BumpPackageVersionResult {
logger.debug(
{ bumpVersion, currentValue },
'Checking if we should bump pom.xml version'
);
let bumpedContent = content;
if (!currentValue) {
logger.warn('Unable to bump pom.xml version, pom.xml has no version');
return { bumpedContent };
}
if (!semver.valid(currentValue)) {
logger.warn('Unable to bump pom.xml version, not a valid semver');
return { bumpedContent };
}
try {
const project = new XmlDocument(content);
const versionNode = project.childNamed('version');
const startTagPosition = versionNode.startTagPosition;
const versionPosition = content.indexOf(versionNode.val, startTagPosition);
const newPomVersion = semver.inc(currentValue, bumpVersion as ReleaseType);
if (!newPomVersion) {
throw new Error('semver inc failed');
}
logger.debug({ newPomVersion });
bumpedContent = replaceAt(
content,
versionPosition,
currentValue,
newPomVersion
);
if (bumpedContent === content) {
logger.debug('Version was already bumped');
} else {
logger.debug('pom.xml version bumped');
}
} catch (err) {
logger.warn(
{
content,
currentValue,
bumpVersion,
},
'Failed to bumpVersion'
);
}
return { bumpedContent };
}