feat: Add bump version to nuget (#17903)

This commit is contained in:
bjuraga 2022-09-25 20:45:45 +02:00 committed by GitHub
parent aaaa6a1cfe
commit f4bbf71b0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 149 additions and 2 deletions

View file

@ -325,7 +325,7 @@ This is an advance field and it's recommend you seek a config review before appl
## bumpVersion ## bumpVersion
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. Currently this setting supports `helmv3`, `npm`, 'nuget', `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. 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. 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.

View file

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

View file

@ -3,6 +3,7 @@ import { NugetDatasource } from '../../datasource/nuget';
export { extractPackageFile } from './extract'; export { extractPackageFile } from './extract';
export { updateArtifacts } from './artifacts'; export { updateArtifacts } from './artifacts';
export { bumpPackageVersion } from './update';
export const language = ProgrammingLanguage.NET; export const language = ProgrammingLanguage.NET;

View file

@ -0,0 +1,80 @@
import { XmlDocument } from 'xmldoc';
import { bumpPackageVersion } from '.';
const simpleContent =
'<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><Version>0.0.1</Version></PropertyGroup></Project>';
const minimumContent =
'<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><Version>1</Version></PropertyGroup></Project>';
const prereleaseContent =
'<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><Version>1.0.0-1</Version></PropertyGroup></Project>';
describe('modules/manager/nuget/update', () => {
describe('bumpPackageVersion', () => {
it('bumps csproj version', () => {
const { bumpedContent } = bumpPackageVersion(
simpleContent,
'0.0.1',
'patch'
);
const project = new XmlDocument(bumpedContent!);
expect(project.valueWithPath('PropertyGroup.Version')).toBe('0.0.2');
});
it('does not bump version twice', () => {
const { bumpedContent } = bumpPackageVersion(
simpleContent,
'0.0.1',
'patch'
);
const { bumpedContent: bumpedContent2 } = bumpPackageVersion(
bumpedContent!,
'0.0.1',
'patch'
);
expect(bumpedContent).toEqual(bumpedContent2);
});
it('does not bump version if version is not a semantic version', () => {
const { bumpedContent } = bumpPackageVersion(
minimumContent,
'1',
'patch'
);
const project = new XmlDocument(bumpedContent!);
expect(project.valueWithPath('PropertyGroup.Version')).toBe('1');
});
it('does not bump version if csproj has no version', () => {
const { bumpedContent } = bumpPackageVersion(
minimumContent,
undefined,
'patch'
);
expect(bumpedContent).toEqual(minimumContent);
});
it('returns content if bumping errors', () => {
const { bumpedContent } = bumpPackageVersion(
simpleContent,
'0.0.1',
true as any
);
expect(bumpedContent).toEqual(simpleContent);
});
it('bumps csproj version with prerelease semver level', () => {
const { bumpedContent } = bumpPackageVersion(
prereleaseContent,
'1.0.0-1',
'prerelease'
);
const project = new XmlDocument(bumpedContent!);
expect(project.valueWithPath('PropertyGroup.Version')).toBe('1.0.0-2');
});
});
});

View file

@ -0,0 +1,66 @@
import semver, { ReleaseType } from 'semver';
import { XmlDocument } from 'xmldoc';
import { logger } from '../../../logger';
import { replaceAt } from '../../../util/string';
import type { BumpPackageVersionResult } from '../types';
export function bumpPackageVersion(
content: string,
currentValue: string | undefined,
bumpVersion: ReleaseType | string
): BumpPackageVersionResult {
logger.debug(
{ bumpVersion, currentValue },
'Checking if we should bump project version'
);
let bumpedContent = content;
if (!currentValue) {
logger.warn('Unable to bump project version, project has no version');
return { bumpedContent };
}
if (!semver.valid(currentValue)) {
logger.warn(
{ currentValue },
'Unable to bump project version, not a valid semver'
);
return { bumpedContent };
}
try {
const project = new XmlDocument(content);
const versionNode = project.descendantWithPath('PropertyGroup.Version')!;
const startTagPosition = versionNode.startTagPosition;
const versionPosition = content.indexOf(versionNode.val, startTagPosition);
const newProjVersion = semver.inc(currentValue, bumpVersion as ReleaseType);
if (!newProjVersion) {
throw new Error('semver inc failed');
}
logger.debug({ newProjVersion });
bumpedContent = replaceAt(
content,
versionPosition,
currentValue,
newProjVersion
);
if (bumpedContent === content) {
logger.debug('Version was already bumped');
} else {
logger.debug('project version bumped');
}
} catch (err) {
logger.warn(
{
content,
currentValue,
bumpVersion,
},
'Failed to bumpVersion'
);
}
return { bumpedContent };
}