Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(manager/nuget): bump VersionPrefix in MSBuild project files #23464

Merged
merged 15 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/modules/manager/nuget/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
} from '../types';
import { extractMsbuildGlobalManifest } from './extract/global-manifest';
import type { DotnetToolsManifest } from './types';
import { getConfiguredRegistries } from './util';
import { findVersion, getConfiguredRegistries } from './util';

/**
* https://docs.microsoft.com/en-us/nuget/concepts/package-versioning
Expand Down Expand Up @@ -123,14 +123,14 @@ export async function extractPackageFile(
}

let deps: PackageDependency[] = [];
let packageFileVersion = undefined;
let packageFileVersion: string | undefined;
try {
const parsedXml = new XmlDocument(content);
deps = extractDepsFromXml(parsedXml).map((dep) => ({
...dep,
...(registryUrls && { registryUrls }),
}));
packageFileVersion = parsedXml.valueWithPath('PropertyGroup.Version');
packageFileVersion = findVersion(parsedXml)?.val;
} catch (err) {
logger.debug({ err, packageFile }, `Failed to parse XML`);
}
Expand Down
25 changes: 24 additions & 1 deletion lib/modules/manager/nuget/update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('modules/manager/nuget/update', () => {
expect(project.valueWithPath('PropertyGroup.Version')).toBe('1');
});

it('does not bump version if csproj has no version', () => {
it('does not bump version if extract found no version', () => {
const { bumpedContent } = bumpPackageVersion(
minimumContent,
undefined,
Expand All @@ -76,6 +76,18 @@ describe('modules/manager/nuget/update', () => {
expect(bumpedContent).toEqual(minimumContent);
});

it('does not bump version if csproj has no version', () => {
const originalContent =
'<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net6.0</TargetFramework></PropertyGroup></Project>';
const { bumpedContent } = bumpPackageVersion(
originalContent,
'0.0.1',
'patch'
);

expect(bumpedContent).toEqual(originalContent);
});

it('returns content if bumping errors', () => {
const { bumpedContent } = bumpPackageVersion(
simpleContent,
Expand All @@ -95,5 +107,16 @@ describe('modules/manager/nuget/update', () => {
const project = new XmlDocument(bumpedContent!);
expect(project.valueWithPath('PropertyGroup.Version')).toBe('1.0.0-2');
});

it('bumps csproj version prefix', () => {
const content =
'<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><VersionPrefix>1.0.0</VersionPrefix></PropertyGroup></Project>';
const { bumpedContent } = bumpPackageVersion(content, '1.0.0', 'patch');

const project = new XmlDocument(bumpedContent!);
expect(project.valueWithPath('PropertyGroup.VersionPrefix')).toBe(
'1.0.1'
);
});
});
});
23 changes: 17 additions & 6 deletions lib/modules/manager/nuget/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { XmlDocument } from 'xmldoc';
import { logger } from '../../../logger';
import { replaceAt } from '../../../util/string';
import type { BumpPackageVersionResult } from '../types';
import { findVersion } from './util';

export function bumpPackageVersion(
content: string,
Expand Down Expand Up @@ -30,8 +31,23 @@ export function bumpPackageVersion(

try {
const project = new XmlDocument(content);
const versionNode = project.descendantWithPath('PropertyGroup.Version')!;
const versionNode = findVersion(project);
if (!versionNode) {
logger.warn(
"Couldn't find Version or VersionPrefix in any PropertyGroup"
);
return { bumpedContent };
}

const currentProjVersion = versionNode.val;
if (currentProjVersion !== currentValue) {
logger.warn(
{ currentValue, currentProjVersion },
"currentValue passed to bumpPackageVersion() doesn't match value found"
);
return { bumpedContent };
}

const startTagPosition = versionNode.startTagPosition;
const versionPosition = content.indexOf(
currentProjVersion,
Expand All @@ -43,11 +59,6 @@ export function bumpPackageVersion(
throw new Error('semver inc failed');
}

if (currentProjVersion === newProjVersion) {
logger.debug('Version was already bumped');
return { bumpedContent };
}

logger.debug(`newProjVersion: ${newProjVersion}`);
bumpedContent = replaceAt(
content,
Expand Down
29 changes: 29 additions & 0 deletions lib/modules/manager/nuget/util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { XmlDocument } from 'xmldoc';
import { bumpPackageVersion } from './update';
import { findVersion } from './util';

describe('modules/manager/nuget/util', () => {
describe('findVersion', () => {
it('finds the version in a later property group', () => {
const content =
'<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net6.0</TargetFramework></PropertyGroup><PropertyGroup><Version>0.0.1</Version></PropertyGroup></Project>';
const { bumpedContent } = bumpPackageVersion(content, '0.0.1', 'patch');

const project = new XmlDocument(bumpedContent!);
const versionNode = findVersion(project);
const newVersion = versionNode!.val;
expect(newVersion).toBe('0.0.2');
});

it('picks version over versionprefix', () => {
const content =
'<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><VersionPrefix>0.0.5</VersionPrefix></PropertyGroup><PropertyGroup><Version>0.0.1</Version></PropertyGroup></Project>';
const { bumpedContent } = bumpPackageVersion(content, '0.0.1', 'patch');

const project = new XmlDocument(bumpedContent!);
const versionNode = findVersion(project);
const newVersion = versionNode!.val;
expect(newVersion).toBe('0.0.2');
});
});
});
13 changes: 12 additions & 1 deletion lib/modules/manager/nuget/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import upath from 'upath';
import { XmlDocument } from 'xmldoc';
import { XmlDocument, XmlElement } from 'xmldoc';
import { logger } from '../../../logger';
import { findUpLocal, readLocalFile } from '../../../util/fs';
import { regEx } from '../../../util/regex';
Expand Down Expand Up @@ -83,3 +83,14 @@ export async function getConfiguredRegistries(
}
return registries;
}

export function findVersion(parsedXml: XmlDocument): XmlElement | null {
for (const tag of ['Version', 'VersionPrefix']) {
for (const l1Elem of parsedXml.childrenNamed('PropertyGroup')) {
for (const l2Elem of l1Elem.childrenNamed(tag)) {
return l2Elem;
}
}
}
return null;
}