Skip to content

Commit

Permalink
(NuGet#9) Add fallback to packages endpoint on missing findpackagesen…
Browse files Browse the repository at this point in the history
…dpoint

This commit adds a fallback to make use of the Packages()
endpoint when the feed that is being used does not report
that it supports the use of the FindPackagesById endpoint.

This is needed as a fallback as there are some feeds
available that does not implement FindPackagesById.
  • Loading branch information
AdmiringWorm authored and gep13 committed Mar 14, 2023
1 parent 79857ea commit a59e93e
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 18 deletions.
33 changes: 33 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/LegacyFeed/ChocolateyV2FeedParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
//////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using NuGet.Common;
using NuGet.Protocol.Core.Types;

namespace NuGet.Protocol
{
Expand Down Expand Up @@ -44,6 +49,34 @@ public sealed partial class V2FeedParser : IV2FeedParser
private static readonly XName _xnamePackageScanFlagResult = XName.Get("PackageScanFlagResult", DataServicesNS);
#pragma warning restore IDE1006 // Naming Styles

public Task<IReadOnlyList<V2FeedPackageInfo>> GetPackageVersionsAsync(string id, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
return GetPackageVersionsAsync(id, includeUnlisted: true, includePreRelease: true, sourceCacheContext: sourceCacheContext, log: log, token: token);
}

public async Task<IReadOnlyList<V2FeedPackageInfo>> GetPackageVersionsAsync(string id, bool includeUnlisted, bool includePreRelease, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
var filter = new SearchFilter(includePreRelease, null)
{
ExactPackageId = true,
IncludeDelisted = includeUnlisted,
OrderBy = SearchOrderBy.Version
};

var uri = _queryBuilder.BuildGetPackagesUri(id, filter, null, null);

var packages = await QueryV2FeedAsync(
uri,
id,
max: -1,
ignoreNotFounds: true,
sourceCacheContext: sourceCacheContext,
log: log,
token: token);

return packages.Items;
}


/// <summary>
/// Retrieve an XML <see cref="DateTime"/> value safely
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public class DependencyInfoResourceV2Feed : DependencyInfoResource
private readonly FrameworkReducer _frameworkReducer = new FrameworkReducer();
private readonly SourceRepository _source;

public DependencyInfoResourceV2Feed(V2FeedParser feedParser, SourceRepository source)
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private readonly ILegacyFeedCapabilityResource _feedCapabilities;

public DependencyInfoResourceV2Feed(V2FeedParser feedParser, ILegacyFeedCapabilityResource feedCapabilities, SourceRepository source)
{
if (feedParser == null)
{
Expand All @@ -37,8 +43,13 @@ public DependencyInfoResourceV2Feed(V2FeedParser feedParser, SourceRepository so

_feedParser = feedParser;
_source = source;
_feedCapabilities = feedCapabilities;
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public override async Task<SourcePackageDependencyInfo> ResolvePackage(
PackageIdentity package,
NuGetFramework projectFramework,
Expand Down Expand Up @@ -78,7 +89,13 @@ public override async Task<IEnumerable<SourcePackageDependencyInfo>> ResolvePack

try
{
var packages = await _feedParser.FindPackagesByIdAsync(packageId, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
var packages = await FindPackageById(packageId, includeUnlisted: true, includePrerelease: true, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

var results = new List<SourcePackageDependencyInfo>();

Expand Down Expand Up @@ -112,7 +129,7 @@ public override async Task<IEnumerable<SourcePackageDependencyInfo>> ResolvePack

try
{
var packages = await _feedParser.FindPackagesByIdAsync(packageId, true, includePrerelease, sourceCacheContext, log, token);
var packages = await FindPackageById(packageId, true, includePrerelease, sourceCacheContext, log, token);

var results = new List<SourcePackageDependencyInfo>();

Expand Down Expand Up @@ -170,5 +187,25 @@ private SourcePackageDependencyInfo CreateDependencyInfo(

return result;
}

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private async Task<IReadOnlyList<V2FeedPackageInfo>> FindPackageById(string packageId, bool includeUnlisted, bool includePrerelease, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
if (await _feedCapabilities.SupportsFindPackagesByIdAsync(log, token))
{
return await _feedParser.FindPackagesByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
else
{
return await _feedParser.GetPackageVersionsAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,17 @@ public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceReposito
var httpSource = await source.GetResourceAsync<HttpSourceResource>(token);
var parser = new V2FeedParser(httpSource.HttpSource, serviceDocument.BaseAddress, source.PackageSource.Source);

resource = new DependencyInfoResourceV2Feed(parser, source);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

var feedCapabilityResource = new LegacyFeedCapabilityResourceV2Feed(parser,
serviceDocument.BaseAddress);
resource = new DependencyInfoResourceV2Feed(parser, feedCapabilityResource, source);

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}

return new Tuple<bool, INuGetResource>(resource != null, resource);
Expand Down
25 changes: 25 additions & 0 deletions src/NuGet.Core/NuGet.Protocol/LegacyFeed/IV2FeedParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

using System.Collections.Generic;

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
Expand All @@ -25,5 +34,21 @@ Task<V2FeedPage> GetSearchPageAsync(
int take,
ILogger log,
CancellationToken token);

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

Task<IReadOnlyList<V2FeedPackageInfo>> GetPackageVersionsAsync(
string id,
bool includeUnlisted,
bool includePreRelease,
SourceCacheContext sourceCacheContext,
ILogger log,
CancellationToken token);

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
}
41 changes: 39 additions & 2 deletions src/NuGet.Core/NuGet.Protocol/LegacyFeed/MetadataResourceV2Feed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public class MetadataResourceV2Feed : MetadataResource
private readonly V2FeedParser _feedParser;
private readonly SourceRepository _source;

public MetadataResourceV2Feed(V2FeedParser feedParser, SourceRepository source)
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private readonly ILegacyFeedCapabilityResource _feedCapabilities;

public MetadataResourceV2Feed(V2FeedParser feedParser, ILegacyFeedCapabilityResource feedCapabilities, SourceRepository source)
{
if (feedParser == null)
{
Expand All @@ -28,8 +34,13 @@ public MetadataResourceV2Feed(V2FeedParser feedParser, SourceRepository source)

_feedParser = feedParser;
_source = source;
_feedCapabilities = feedCapabilities;
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public override async Task<IEnumerable<KeyValuePair<string, NuGetVersion>>> GetLatestVersions(IEnumerable<string> packageIds, bool includePrerelease, bool includeUnlisted,
SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
Expand Down Expand Up @@ -71,7 +82,13 @@ public override async Task<IEnumerable<NuGetVersion>> GetVersions(string package

try
{
var packages = await _feedParser.FindPackagesByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
var packages = await FindPackageById(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

return packages.Select(p => p.Version).ToArray();
}
Expand Down Expand Up @@ -105,5 +122,25 @@ public override async Task<bool> Exists(string packageId, bool includePrerelease

return versions.Any();
}

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private async Task<IReadOnlyList<V2FeedPackageInfo>> FindPackageById(string packageId, bool includeUnlisted, bool includePrerelease, SourceCacheContext sourceCacheContext, ILogger log, CancellationToken token)
{
if (await _feedCapabilities.SupportsFindPackagesByIdAsync(log, token))
{
return await _feedParser.FindPackagesByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
else
{
return await _feedParser.GetPackageVersionsAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceReposito
var httpSource = await source.GetResourceAsync<HttpSourceResource>(token);
var parser = new V2FeedParser(httpSource.HttpSource, serviceDocument.BaseAddress, source.PackageSource.Source);

resource = new MetadataResourceV2Feed(parser, source);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

var feedCapabilityResource = new LegacyFeedCapabilityResourceV2Feed(parser, serviceDocument.BaseAddress);
resource = new MetadataResourceV2Feed(parser, feedCapabilityResource, source);

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}

return new Tuple<bool, INuGetResource>(resource != null, resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class PackageMetadataResourceV2Feed : PackageMetadataResource
//////////////////////////////////////////////////////////

private readonly IHttpSource _httpSource;
private readonly ILegacyFeedCapabilityResource _feedCapabilities;

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
Expand All @@ -27,8 +28,13 @@ public class PackageMetadataResourceV2Feed : PackageMetadataResource
private readonly Configuration.PackageSource _packageSource;
private readonly V2FeedParser _feedParser;

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public PackageMetadataResourceV2Feed(
HttpSourceResource httpSourceResource,
ILegacyFeedCapabilityResource feedCapabilities,
string baseAddress,
Configuration.PackageSource packageSource)
{
Expand All @@ -45,8 +51,13 @@ public PackageMetadataResourceV2Feed(
_httpSource = httpSourceResource.HttpSource;
_packageSource = packageSource;
_feedParser = new V2FeedParser(_httpSource, baseAddress, packageSource.Source);
_feedCapabilities = feedCapabilities;
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

public override async Task<IEnumerable<IPackageSearchMetadata>> GetMetadataAsync(
string packageId,
bool includePrerelease,
Expand All @@ -55,7 +66,13 @@ public override async Task<IEnumerable<IPackageSearchMetadata>> GetMetadataAsync
Common.ILogger log,
CancellationToken token)
{
var packages = await _feedParser.FindPackagesByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
var packages = await FindPackageByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

var metadataCache = new MetadataReferenceCache();
var filter = new SearchFilter(includePrerelease);
Expand All @@ -81,5 +98,25 @@ public override async Task<IPackageSearchMetadata> GetMetadataAsync(
}
return null;
}

//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

private async Task<IReadOnlyList<V2FeedPackageInfo>> FindPackageByIdAsync(string packageId, bool includeUnlisted, bool includePrerelease, SourceCacheContext sourceCacheContext, Common.ILogger log, CancellationToken token)
{
if (await _feedCapabilities.SupportsFindPackagesByIdAsync(log, token))
{
return await _feedParser.FindPackagesByIdAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
else
{
return await _feedParser.GetPackageVersionsAsync(packageId, includeUnlisted, includePrerelease, sourceCacheContext, log, token);
}
}

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceReposito

var serviceDocument = await source.GetResourceAsync<ODataServiceDocumentResourceV2>(token);

resource = new PackageMetadataResourceV2Feed(httpSourceResource, serviceDocument.BaseAddress, source.PackageSource);
//////////////////////////////////////////////////////////
// Start - Chocolatey Specific Modification
//////////////////////////////////////////////////////////

var parser = new V2FeedParser(httpSourceResource.HttpSource, serviceDocument.BaseAddress, source.PackageSource.Source);
var feedCapabilityResource = new LegacyFeedCapabilityResourceV2Feed(parser, serviceDocument.BaseAddress);

resource = new PackageMetadataResourceV2Feed(httpSourceResource, feedCapabilityResource, serviceDocument.BaseAddress, source.PackageSource);

//////////////////////////////////////////////////////////
// End - Chocolatey Specific Modification
//////////////////////////////////////////////////////////
}

return new Tuple<bool, INuGetResource>(resource != null, resource);
Expand Down
Loading

0 comments on commit a59e93e

Please sign in to comment.