Skip to content

Commit

Permalink
refactor(nuget): Support skip-version during extract (renovatebot#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Jan 8, 2025
1 parent 351d9ef commit c043653
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/modules/manager/nuget/__fixtures__/sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Version="1.2.3" />
<PackageReference Include="Autofac" Version="4.5.0" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="1.1.2" />
Expand Down
67 changes: 67 additions & 0 deletions lib/modules/manager/nuget/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,43 @@ exports[`modules/manager/nuget/extract extractPackageFile() extracts all depende
"depName": "Microsoft.VisualStudio.Web.CodeGeneration.Tools",
"depType": "nuget",
},
{
"currentValue": "undefined",
"datasource": "nuget",
"depName": "NotUpdatable3",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable3",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable3",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable3",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable2",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable1",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"currentValue": "1.2.3",
"datasource": "nuget",
Expand Down Expand Up @@ -115,6 +152,36 @@ exports[`modules/manager/nuget/extract extractPackageFile() extracts all depende
"depName": "Microsoft.VisualStudio.Web.CodeGeneration.Tools",
"depType": "nuget",
},
{
"datasource": "nuget",
"depName": "NotUpdatable3",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable3",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable3",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable2",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"datasource": "nuget",
"depName": "NotUpdatable1",
"depType": "nuget",
"skipReason": "invalid-version",
},
{
"currentValue": "1.2.3",
"datasource": "nuget",
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/nuget/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('modules/manager/nuget/extract', () => {
const sample = Fixtures.get(packageFile);
const res = await extractPackageFile(sample, packageFile, config);
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(17);
expect(res?.deps).toHaveLength(23);
});

it('extracts msbuild sdk from the Sdk attr of Project element', async () => {
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('modules/manager/nuget/extract', () => {
const sample = Fixtures.get(packageFile);
const res = await extractPackageFile(sample, packageFile, config);
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(17);
expect(res?.deps).toHaveLength(22);
});

it('extracts ContainerBaseImage', async () => {
Expand Down
39 changes: 27 additions & 12 deletions lib/modules/manager/nuget/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { applyRegistries, findVersion, getConfiguredRegistries } from './util';
* so we don't include it in the extracting regexp
*/
const checkVersion = regEx(
`^\\s*(?:[[])?(?:(?<currentValue>[^"(,[\\]]+)\\s*(?:,\\s*[)\\]]|])?)\\s*$`,
/^\s*(?:[[])?(?:(?<currentValue>[^"(,[\]]+)\s*(?:,\s*[)\]]|])?)\s*$/,
);
const elemNames = new Set([
'PackageReference',
Expand Down Expand Up @@ -58,23 +58,38 @@ function extractDepsFromXml(xmlNode: XmlDocument): NugetPackageDependency[] {

if (elemNames.has(name)) {
const depName = attr?.Include || attr?.Update;
const version =
if (!depName) {
continue;
}

const dep: NugetPackageDependency = {
datasource: NugetDatasource.id,
depType: 'nuget',
depName,
};

let currentValue: string | undefined =
attr?.Version ??
attr?.version ??
child.valueWithPath('Version') ??
attr?.VersionOverride ??
child.valueWithPath('VersionOverride');
const currentValue = is.nonEmptyStringAndNotWhitespace(version)
? checkVersion.exec(version)?.groups?.currentValue?.trim()
: undefined;
if (depName && currentValue) {
results.push({
datasource: NugetDatasource.id,
depType: 'nuget',
depName,
currentValue,
});

if (!is.nonEmptyStringAndNotWhitespace(currentValue)) {
dep.skipReason = 'invalid-version';
}

currentValue = checkVersion
.exec(currentValue)
?.groups?.currentValue?.trim();

if (currentValue) {
dep.currentValue = currentValue;
} else {
dep.skipReason = 'invalid-version';
}

results.push(dep);
} else if (name === 'Sdk') {
const depName = attr?.Name;
const version = attr?.Version;
Expand Down

0 comments on commit c043653

Please sign in to comment.