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

refactor(nuget): Support skip-version during extract #33437

Merged
merged 6 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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",
zharinov marked this conversation as resolved.
Show resolved Hide resolved
"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
Loading