-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deprecation info decorator, add dgml visualization tests (#29)
- Loading branch information
Showing
18 changed files
with
280 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using Logging; | ||
using Microsoft.Extensions.Logging; | ||
using NuGet.Packaging.Core; | ||
using NuGet.Protocol.Core.Types; | ||
|
||
namespace Common | ||
{ | ||
public class DeprecationInfoDecorator : IPackageDependencyNodeDecorator | ||
{ | ||
private readonly List<SourceRepository> _sourceRepositories; | ||
private readonly SourceCacheContext _sourceCacheContext; | ||
private readonly Dictionary<PackageIdentity, bool> PackageDeprecationData = new(); | ||
private readonly List<PackageMetadataResource> _packageMetadataResources = new(); | ||
private bool _packageMetadataResourcesAcquired; | ||
|
||
public DeprecationInfoDecorator(List<SourceRepository> sourceRepositories, SourceCacheContext sourceCacheContext) | ||
{ | ||
_sourceRepositories = sourceRepositories ?? throw new ArgumentNullException(nameof(sourceRepositories)); | ||
_sourceCacheContext = sourceCacheContext ?? throw new ArgumentNullException(nameof(sourceCacheContext)); | ||
} | ||
|
||
public async Task DecorateAsync(PackageDependencyNode dependencyNode, CancellationToken cancellationToken) | ||
{ | ||
if (PackageDeprecationData.TryGetValue(dependencyNode.Identity, out var isPackageDeprecated)) | ||
{ | ||
dependencyNode.Identity.Deprecated = isPackageDeprecated; | ||
return; | ||
} | ||
|
||
if (_packageMetadataResourcesAcquired) | ||
{ | ||
await InitializeMetadataResource(cancellationToken); | ||
_packageMetadataResourcesAcquired = true; | ||
} | ||
|
||
bool isDeprecated = await IsPackageDeprecatedAsync(dependencyNode.Identity, cancellationToken); | ||
PackageDeprecationData.Add(dependencyNode.Identity, isDeprecated); | ||
dependencyNode.Identity.Deprecated = isDeprecated; | ||
} | ||
|
||
private async Task<bool> IsPackageDeprecatedAsync(PackageIdentity packageIdentity, CancellationToken cancellationToken) | ||
{ | ||
List<Task<IPackageSearchMetadata>>? results = new(_packageMetadataResources.Count); | ||
|
||
bool isDeprecated = false; | ||
foreach (PackageMetadataResource packageMetadataResource in _packageMetadataResources) | ||
{ | ||
var packageMetadata = packageMetadataResource.GetMetadataAsync(packageIdentity, _sourceCacheContext, NuGet.Common.NullLogger.Instance, cancellationToken); | ||
if (packageMetadata != null) | ||
{ | ||
results.Add(packageMetadata); | ||
} | ||
} | ||
await Task.WhenAll(results); | ||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
} | ||
foreach (var result in results) | ||
{ | ||
if (result?.Result != null) | ||
{ | ||
isDeprecated = isDeprecated || await result.Result.GetDeprecationMetadataAsync() != null; | ||
} | ||
} | ||
|
||
return isDeprecated; | ||
} | ||
|
||
private async Task InitializeMetadataResource(CancellationToken cancellationToken) | ||
{ | ||
List<Task<PackageMetadataResource?>>? results = new(_sourceRepositories.Count); | ||
|
||
foreach (SourceRepository source in _sourceRepositories) | ||
{ | ||
var metadataResource = GetMetadataResourceAsync(source, _sourceCacheContext, cancellationToken); | ||
if (metadataResource != null) | ||
{ | ||
results.Add(metadataResource); | ||
} | ||
} | ||
|
||
await Task.WhenAll(results); | ||
|
||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
} | ||
foreach (var result in results) | ||
{ | ||
if (result?.Result != null) | ||
{ | ||
_packageMetadataResources.Add(result.Result); | ||
} | ||
} | ||
} | ||
|
||
static async Task<PackageMetadataResource?> GetMetadataResourceAsync(SourceRepository source, SourceCacheContext cacheContext, CancellationToken cancellationToken) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
PackageMetadataResource metadataResource = | ||
await source.GetResourceAsync<PackageMetadataResource>(cancellationToken); | ||
return metadataResource; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...zerTool.Test/compiler/resources/diamonddependency.withvulnerabilitiesanddeprecations.dgml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<DirectedGraph xmlns="http://schemas.microsoft.com/vs/2009/dgml"> | ||
<Nodes> | ||
<Node Id="TestProject.1.0.0" Label="TestProject.1.0.0" Category="Project" /> | ||
<Node Id="A.1.0.0" Label="A.1.0.0" Category="VulnerablePackage" /> | ||
<Node Id="B.1.0.0" Label="B.1.0.0" Category="DeprecatedPackage" /> | ||
<Node Id="C.1.1.0" Label="C.1.1.0" Category="VulnerableAndDeprecatedPackage" /> | ||
</Nodes> | ||
<Links> | ||
<Link Source="TestProject.1.0.0" Target="A.1.0.0" Label="[1.0.0, )" /> | ||
<Link Source="TestProject.1.0.0" Target="B.1.0.0" Label="[1.0.0, )" /> | ||
<Link Source="A.1.0.0" Target="C.1.1.0" Label="[1.0.0, )" /> | ||
<Link Source="B.1.0.0" Target="C.1.1.0" Label="[1.1.0, )" /> | ||
</Links> | ||
<Categories> | ||
<Category Id="Project" Background="Lightblue" StrokeThickness="2" /> | ||
<Category Id="Package" Background="None" StrokeThickness="1" /> | ||
<Category Id="VulnerablePackage" Background="None" StrokeThickness="4" Stroke="Red" /> | ||
<Category Id="DeprecatedPackage" Background="Yellow" StrokeThickness="1" /> | ||
<Category Id="VulnerableAndDeprecatedPackage" Background="Yellow" StrokeThickness="4" Stroke="Red" /> | ||
</Categories> | ||
|
||
</DirectedGraph> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.