Skip to content
Open
5 changes: 1 addition & 4 deletions DependencyChecker.Test/DependencyChecker.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,14 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NuGet.Common" Version="6.12.1" />
<PackageReference Include="NuGet.Configuration" Version="6.12.1" />
<PackageReference Include="NuGet.Frameworks" Version="6.12.1" />
<PackageReference Include="NuGet.Packaging" Version="6.12.1" />
<PackageReference Include="NuGet.Packaging.Core.Types" Version="4.2.0" />
<PackageReference Include="NuGet.Protocol.Core.Types" Version="4.2.0" />
<PackageReference Include="NuGet.Protocol.Core.v3" Version="4.2.0" />
<PackageReference Include="NuGet.Versioning" Version="6.12.1" />
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
<PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
Expand Down
2 changes: 1 addition & 1 deletion DependencyChecker.Test/NuGetConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void TestMethod1()
{

var settings = Settings.LoadDefaultSettings(root: null);
var sourceRepositoryProvider = new SourceRepositoryProvider(settings, Repository.Provider.GetCoreV3());
var sourceRepositoryProvider = new SourceRepositoryProvider(new PackageSourceProvider(settings), Repository.Provider.GetCoreV3());
var beforeCount = sourceRepositoryProvider.GetRepositories().Count();

var option = new Options()
Expand Down
4 changes: 1 addition & 3 deletions DependencyChecker/DependencyChecker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
<PackageReference Include="NuGet.Configuration" Version="6.12.1" />
<PackageReference Include="NuGet.Frameworks" Version="6.12.1" />
<PackageReference Include="NuGet.Packaging" Version="6.12.1" />
<PackageReference Include="NuGet.Packaging.Core.Types" Version="4.2.0" />
<PackageReference Include="NuGet.Protocol.Core.Types" Version="4.2.0" />
<PackageReference Include="NuGet.Protocol.Core.v3" Version="4.2.0" />
<PackageReference Include="NuGet.Protocol" Version="6.12.1" />
<PackageReference Include="NuGet.Versioning" Version="6.12.1" />
<PackageReference Include="Stubble.Core" Version="1.10.8" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
Expand Down
81 changes: 55 additions & 26 deletions DependencyChecker/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Newtonsoft.Json;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using Stubble.Core.Builders;
Expand All @@ -13,13 +12,12 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

using Stubble.Core.Settings;

namespace DependencyChecker
{
public class Runner
Expand All @@ -29,6 +27,7 @@ public class Runner
private readonly ILogger _logger = new Logger();
private readonly List<PackageMetadataResource> _packageMetadataResources = new();
private readonly Dictionary<string, IPackageSearchMetadata> _currentPackageCache = new();
private readonly SourceCacheContext _sourceCacheContext = new();
public readonly List<CodeProject> CodeProjects = new();
private Options _options;

Expand Down Expand Up @@ -152,11 +151,11 @@ private void CreateOutputDocument()
if (_options.SortByOutdated)
{
SortPackageStatusesInPlace(CodeProjects);
}
}
var projectsContent = stubble.Render(contentTemplate, new { Projects = CodeProjects });

// Insert content into report file
// Insert content into report file
var report = reportTemplate.Replace("{{PROJECTS}}", projectsContent);
var directory = new FileInfo(_options.ReportPath).Directory.FullName;
Directory.CreateDirectory(directory);
Expand Down Expand Up @@ -313,16 +312,15 @@ private async Task<IPackageSearchMetadata> LoadPackageIntoCache(string packageId
{
foreach (PackageMetadataResource packageMetadataResource in _packageMetadataResources)
{
// Todo: Include Prerelease option
var results = (await packageMetadataResource.GetMetadataAsync(packageId, _options.IncludePrereleases, false, _logger, CancellationToken.None))
var results = (await packageMetadataResource.GetMetadataAsync(packageId, _options.IncludePrereleases, false, _sourceCacheContext, _logger, CancellationToken.None))
.ToList();

if (!results.Any()) continue;

IPackageSearchMetadata searchResult = results
.OrderByDescending(r => r.Published)
.First();

_currentPackageCache.Add(packageId, searchResult);
return searchResult;
}
Expand All @@ -337,6 +335,11 @@ private async Task<IPackageSearchMetadata> LoadPackageIntoCache(string packageId
private void Initialize()
{
_logger.LogInformation("Using Sources:");
var username = Environment.GetEnvironmentVariable("DEPC_NUGET_USERNAME") ?? "noUserProvided";
var password = Environment.GetEnvironmentVariable("DEPC_NUGET_PASSWORD") ?? string.Empty;
var sourcesPrefix = Environment.GetEnvironmentVariable("DEPC_NUGET_SOURCESPREFIX")?.Split(',').ToList() ?? [];
var packageSourceFilter = Environment.GetEnvironmentVariable("DEPC_NUGET_PACKAGE_SOURCE_FILTER") ?? "";
var validAuthenticationTypes = Environment.GetEnvironmentVariable("DEPC_NUGET_VALID_AUTHENTICATION_TYPES") ?? "basic,negotiate";

var settings = Settings.LoadDefaultSettings(null);
if (!string.IsNullOrEmpty(_options.CustomNuGetFile))
Expand All @@ -356,45 +359,71 @@ private void Initialize()
}
}

var sourceRepositoryProvider = new SourceRepositoryProvider(settings, Repository.Provider.GetCoreV3());
var sourceRepository = sourceRepositoryProvider.GetRepositories();
foreach (var repository in sourceRepository)
var sourceRepositoryProvider = new SourceRepositoryProvider(new PackageSourceProvider(settings), Repository.Provider.GetCoreV3());
var sourceRepositories = sourceRepositoryProvider.GetRepositories();
foreach (var sourceRepository in sourceRepositories)
{
var resource = repository.GetResource<PackageMetadataResource>();
_packageMetadataResources.Add(resource);
_logger.LogInformation(" " + repository + " \t - \t" + repository.PackageSource.Source);
Sources.Add(repository.PackageSource.Source);
var sourceUri = sourceRepository.PackageSource.Source;
var sourceName = sourceRepository.PackageSource.Name;

if (!string.IsNullOrEmpty(packageSourceFilter) && Regex.IsMatch(sourceName, packageSourceFilter, RegexOptions.IgnoreCase))
{
_logger.LogInformation($" Skipping source {sourceUri} because it matches the provided 'DEPC_NUGET_PACKAGE_SOURCE_FILTER'");
continue;
}

var ps = new PackageSource(sourceUri);
if (sourcesPrefix.Any(sp => sourceUri.StartsWith(sp, StringComparison.OrdinalIgnoreCase)) &&
!string.IsNullOrEmpty(password))
{
_logger.LogInformation($" Adding {sourceUri} with credentials");
ps = new PackageSource(sourceUri, sourceName)
{
Credentials = new PackageSourceCredential(
source: sourceUri,
username: username,
passwordText: password,
isPasswordClearText: true,
validAuthenticationTypesText: validAuthenticationTypes)
};
}

var sr = new SourceRepository(ps, Repository.Provider.GetCoreV3());
var metadataResource = sr.GetResource<PackageMetadataResource>();
_packageMetadataResources.Add(metadataResource);
_logger.LogInformation(" " + sr + " \t - \t" + sr.PackageSource.Source);
Sources.Add(sr.PackageSource.Source);


}


if (!string.IsNullOrEmpty(_options.AzureArtifactsFeedUri))
{
_logger.LogInformation($"Adding Azure Feed: {_options.AzureArtifactsFeedUri}");
var username = Environment.GetEnvironmentVariable("BUILD_REQUESTEDFOREMAIL");
var token = Environment.GetEnvironmentVariable("SYSTEM_ACCESSTOKEN");
var requestedForEmail = Environment.GetEnvironmentVariable("BUILD_REQUESTEDFOREMAIL");
var systemAccessToken = Environment.GetEnvironmentVariable("SYSTEM_ACCESSTOKEN");

if (string.IsNullOrEmpty(username))
if (string.IsNullOrEmpty(requestedForEmail))
{
throw new Exception("Username not provided");
throw new Exception("Username not provided (BUILD_REQUESTEDFOREMAIL)");
}

if (string.IsNullOrEmpty(token))
if (string.IsNullOrEmpty(systemAccessToken))
{
throw new Exception("This features needs access to the OAuth token to query DevOps Artifacts. Please activate OAuth Access for this stage. See https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops#system-variables");
}

_logger.LogInformation("Adding DevOps Feed with the provided credentials...");
var ps = new PackageSource(_options.AzureArtifactsFeedUri)
{
Credentials = new PackageSourceCredential(_options.AzureArtifactsFeedUri, username, token, true, "basic,negotiate")
Credentials = new PackageSourceCredential(_options.AzureArtifactsFeedUri, requestedForEmail, systemAccessToken, true, validAuthenticationTypes)
};

var sr = new SourceRepository(ps, Repository.Provider.GetCoreV3());
var metadataResource = sr.GetResource<PackageMetadataResource>();
_packageMetadataResources.Add(metadataResource);
Sources.Add(sr.PackageSource.Source);


}

_logger.LogInformation(string.Empty); // Blank line
Expand Down
6 changes: 6 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
trigger:
- '*'

variables:
major: 1
minor: 0

name: $(major).$(minor)$(Rev:.r)

pool:
vmImage: windows-latest

Expand Down
3 changes: 1 addition & 2 deletions buildtask/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"sourceMap": false,
"moduleResolution": "node",
"outDir": "dist/",
"noImplicitAny": false,
"watch":false
"noImplicitAny": false
}
}