Skip to content

Commit

Permalink
Sort package vulnerability advisories by severity in descending order (
Browse files Browse the repository at this point in the history
  • Loading branch information
jebriede authored Oct 13, 2021
1 parent 4e62c16 commit 8923757
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,27 +415,17 @@ public IReadOnlyCollection<PackageVulnerabilityMetadataContextInfo> PackageVulne
private set
{
_packageVulnerabilities = value;
PackageVulnerabilityMaxSeverity = value?.Max(v => v.Severity) ?? -1;

OnPropertyChanged(nameof(PackageVulnerabilities));
OnPropertyChanged(nameof(PackageVulnerabilityMaxSeverity));
OnPropertyChanged(nameof(IsPackageVulnerable));
OnPropertyChanged(nameof(PackageVulnerabilityCount));
}
}

private int _packageVulnerabilityMaxSeverity = -1;
public int PackageVulnerabilityMaxSeverity
{
get => _packageVulnerabilityMaxSeverity;
private set
{
if (_packageVulnerabilityMaxSeverity != value)
{
_packageVulnerabilityMaxSeverity = value;

OnPropertyChanged(nameof(PackageVulnerabilityMaxSeverity));
}
}
get => PackageVulnerabilities?.FirstOrDefault()?.Severity ?? -1;
}

public bool IsPackageVulnerable
Expand Down Expand Up @@ -509,8 +499,7 @@ public DetailedPackageMetadata PackageMetadata
PackageDeprecationReasons = newDeprecationReasons;
PackageDeprecationAlternatePackageText = newAlternatePackageText;

IEnumerable<PackageVulnerabilityMetadataContextInfo> vulnerabilities = _packageMetadata?.Vulnerabilities;
PackageVulnerabilities = vulnerabilities?.ToList();
PackageVulnerabilities = _packageMetadata?.Vulnerabilities?.ToList();

OnPropertyChanged(nameof(PackageMetadata));
OnPropertyChanged(nameof(IsPackageDeprecated));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ private async Task ReloadPackageMetadataAsync()

DeprecationMetadata = deprecationMetadata;
IsPackageDeprecated = deprecationMetadata != null;
VulnerabilityMaxSeverity = packageMetadata?.Vulnerabilities?.Max(v => v.Severity) ?? -1;
VulnerabilityMaxSeverity = packageMetadata?.Vulnerabilities?.FirstOrDefault()?.Severity ?? -1;
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public static PackageSearchMetadataContextInfo Create(IPackageSearchMetadata pac
IsListed = packageSearchMetadata.IsListed,
DependencySets = packageSearchMetadata.DependencySets?.ToList(),
DownloadCount = packageSearchMetadata.DownloadCount,
Vulnerabilities = packageSearchMetadata.Vulnerabilities?.Select(vulnerability => new PackageVulnerabilityMetadataContextInfo(vulnerability.AdvisoryUrl, vulnerability.Severity)).ToArray(),
Vulnerabilities = packageSearchMetadata.Vulnerabilities?
.Select(vulnerability => new PackageVulnerabilityMetadataContextInfo(vulnerability.AdvisoryUrl, vulnerability.Severity))
.OrderByDescending(v => v.Severity).ToArray(),
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ public void VulnerabilityCountWhenMetadataHasVulnerability_Calculated()
Assert.Equal(_testInstance.PackageVulnerabilityCount, _testData.TestData.Vulnerabilities.Count());
}

[Fact]
public void PackageVulnerabilities_WhenMetadataHasVulnerability_IsOrderedBySeverityDescending()
{
IEnumerable<PackageVulnerabilityMetadataContextInfo> sortedTestVulnerabilities =
_testData.TestData.Vulnerabilities
.OrderByDescending(v => v.Severity)
.Select(v => new PackageVulnerabilityMetadataContextInfo(v.AdvisoryUrl, v.Severity));

Assert.Equal(sortedTestVulnerabilities, _testInstance.PackageVulnerabilities);
}

[Fact]
public async Task SetCurrentPackageAsync_SortsVersions_ByNuGetVersionDesc()
{
Expand Down

0 comments on commit 8923757

Please sign in to comment.