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

Add support for package type queries in both local and remote sources #5991

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class SearchFilterFormatter : NuGetMessagePackFormatter<SearchFi
{
private const string IncludePrereleasePropertyName = "includeprerelease";
private const string IncludeDelistedPropertyName = "includedelisted";
private const string PackageTypesPropertyName = "packagetypes";
private const string PackageTypePropertyName = "packagetype";
private const string FilterPropertyName = "filter";
private const string OrderByPropertyName = "orderby";
private const string SupportedFrameworksPropertyName = "supportedframeworks";
Expand All @@ -30,7 +30,7 @@ private SearchFilterFormatter()
SearchFilterType? filterType = null;
SearchOrderBy? searchOrderBy = null;
IEnumerable<string>? supportedFrameworks = null;
IEnumerable<string>? packageTypes = null;
string? packageType = null;

int propertyCount = reader.ReadMapHeader();
for (int propertyIndex = 0; propertyIndex < propertyCount; propertyIndex++)
Expand All @@ -43,8 +43,8 @@ private SearchFilterFormatter()
case IncludeDelistedPropertyName:
includeDelisted = reader.ReadBoolean();
break;
case PackageTypesPropertyName:
packageTypes = options.Resolver.GetFormatter<IEnumerable<string>>()!.Deserialize(ref reader, options);
case PackageTypePropertyName:
packageType = reader.ReadString();
break;
case FilterPropertyName:
filterType = options.Resolver.GetFormatter<SearchFilterType?>()!.Deserialize(ref reader, options);
Expand All @@ -65,7 +65,7 @@ private SearchFilterFormatter()
{
SupportedFrameworks = supportedFrameworks,
OrderBy = searchOrderBy,
PackageTypes = packageTypes,
PackageType = packageType,
IncludeDelisted = includeDelisted,
};
}
Expand All @@ -77,8 +77,8 @@ protected override void SerializeCore(ref MessagePackWriter writer, SearchFilter
writer.Write(value.IncludePrerelease);
writer.Write(IncludeDelistedPropertyName);
writer.Write(value.IncludeDelisted);
writer.Write(PackageTypesPropertyName);
options.Resolver.GetFormatter<IEnumerable<string>>()!.Serialize(ref writer, value.PackageTypes, options);
writer.Write(PackageTypePropertyName);
writer.Write(value.PackageType);
writer.Write(FilterPropertyName);
options.Resolver.GetFormatter<SearchFilterType?>()!.Serialize(ref writer, value.Filter, options);
writer.Write(OrderByPropertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using NuGet.Common;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;

Expand Down Expand Up @@ -59,6 +60,12 @@ public async override Task<IEnumerable<IPackageSearchMetadata>> SearchAsync(
query = query.Where(package => ContainsAnyTerm(terms, package));
}

// Filter on package types
if (!string.IsNullOrEmpty(filters.PackageType))
{
query = query.Where(package => MatchesPackageType(package, filters.PackageType));
}

// Collapse to the highest version per id, if necessary
var collapsedQuery = filters?.Filter == SearchFilterType.IsLatestVersion ||
filters?.Filter == SearchFilterType.IsAbsoluteLatestVersion
Expand All @@ -77,6 +84,23 @@ public async override Task<IEnumerable<IPackageSearchMetadata>> SearchAsync(
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}

/// <summary>
/// Search package types to match specified filter. If no package types are provided,
/// accept package as long as the specified filter type is Dependency.
/// </summary>
private static bool MatchesPackageType(LocalPackageInfo package, string packageTypeName)
{
var packageTypes = package.Nuspec.GetPackageTypes();
if (packageTypes.Count == 0
&& PackageType.PackageTypeNameComparer.Equals(packageTypeName, PackageType.Dependency.Name))
{
return true;
}

return packageTypes
.Any(packageType => PackageType.PackageTypeNameComparer.Equals(packageType.Name, packageTypeName));
}

/// <summary>
/// Search Id, Tags, and Description to match the legacy local search behavior.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void
NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy?
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable<string>
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void
~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable<string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#nullable enable
NuGet.Protocol.Plugins.PluginDiscoverer.PluginDiscoverer() -> void
virtual NuGet.Protocol.Plugins.PluginFactory.Dispose() -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state, bool requiresDotnetHost) -> void
~NuGet.Protocol.Plugins.PluginManager.PluginManager(NuGet.Common.IEnvironmentVariableReader reader, System.Lazy<NuGet.Protocol.Plugins.IPluginDiscoverer> pluginDiscoverer, System.Func<System.TimeSpan, NuGet.Protocol.Plugins.PluginFactory> pluginFactoryCreator, System.Lazy<string> pluginsCacheDirectoryPath) -> void
~static readonly NuGet.Protocol.ServiceTypes.Version350 -> string
~virtual NuGet.Protocol.Plugins.PluginFactory.GetOrCreateAsync(NuGet.Protocol.Plugins.PluginFile pluginFile, System.Collections.Generic.IEnumerable<string> arguments, NuGet.Protocol.Plugins.IRequestHandlers requestHandlers, NuGet.Protocol.Plugins.ConnectionOptions options, System.Threading.CancellationToken sessionCancellationToken) -> System.Threading.Tasks.Task<NuGet.Protocol.Plugins.IPlugin>
NuGet.Protocol.Events.ProtocolDiagnostics.ProtocolDiagnosticServiceIndexEntryEventHandler
NuGet.Protocol.Events.ProtocolDiagnosticServiceIndexEntryEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void
NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy?
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable<string>
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void
~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable<string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#nullable enable
NuGet.Protocol.Plugins.PluginDiscoverer.PluginDiscoverer() -> void
virtual NuGet.Protocol.Plugins.PluginFactory.Dispose() -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state, bool requiresDotnetHost) -> void
~NuGet.Protocol.Plugins.PluginManager.PluginManager(NuGet.Common.IEnvironmentVariableReader reader, System.Lazy<NuGet.Protocol.Plugins.IPluginDiscoverer> pluginDiscoverer, System.Func<System.TimeSpan, NuGet.Protocol.Plugins.PluginFactory> pluginFactoryCreator, System.Lazy<string> pluginsCacheDirectoryPath) -> void
~static readonly NuGet.Protocol.ServiceTypes.Version350 -> string
~virtual NuGet.Protocol.Plugins.PluginFactory.GetOrCreateAsync(NuGet.Protocol.Plugins.PluginFile pluginFile, System.Collections.Generic.IEnumerable<string> arguments, NuGet.Protocol.Plugins.IRequestHandlers requestHandlers, NuGet.Protocol.Plugins.ConnectionOptions options, System.Threading.CancellationToken sessionCancellationToken) -> System.Threading.Tasks.Task<NuGet.Protocol.Plugins.IPlugin>
NuGet.Protocol.Events.ProtocolDiagnostics.ProtocolDiagnosticServiceIndexEntryEventHandler
NuGet.Protocol.Events.ProtocolDiagnosticServiceIndexEntryEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void
NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy?
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable<string>
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void
~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable<string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#nullable enable
NuGet.Protocol.Plugins.PluginDiscoverer.PluginDiscoverer() -> void
virtual NuGet.Protocol.Plugins.PluginFactory.Dispose() -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state, bool requiresDotnetHost) -> void
~NuGet.Protocol.Plugins.PluginManager.PluginManager(NuGet.Common.IEnvironmentVariableReader reader, System.Lazy<NuGet.Protocol.Plugins.IPluginDiscoverer> pluginDiscoverer, System.Func<System.TimeSpan, NuGet.Protocol.Plugins.PluginFactory> pluginFactoryCreator, System.Lazy<string> pluginsCacheDirectoryPath) -> void
~static readonly NuGet.Protocol.ServiceTypes.Version350 -> string
~virtual NuGet.Protocol.Plugins.PluginFactory.GetOrCreateAsync(NuGet.Protocol.Plugins.PluginFile pluginFile, System.Collections.Generic.IEnumerable<string> arguments, NuGet.Protocol.Plugins.IRequestHandlers requestHandlers, NuGet.Protocol.Plugins.ConnectionOptions options, System.Threading.CancellationToken sessionCancellationToken) -> System.Threading.Tasks.Task<NuGet.Protocol.Plugins.IPlugin>
NuGet.Protocol.Events.ProtocolDiagnostics.ProtocolDiagnosticServiceIndexEntryEventHandler
NuGet.Protocol.Events.ProtocolDiagnosticServiceIndexEntryEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,9 @@ private async Task<T> SearchPage<T>(
queryString += "&" + frameworks;
}

if (filters.PackageTypes != null
&& filters.PackageTypes.Any())
if (!string.IsNullOrEmpty(filters.PackageType))
{
var types = string.Join("&",
filters.PackageTypes.Select(
s => "packageTypeFilter=" + s));
queryString += "&" + types;
queryString += "&packageType=" + filters.PackageType;
}

queryString += "&semVerLevel=2.0.0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,9 @@ public virtual async Task<JObject> SearchPage(string searchTerm, SearchFilter fi
queryString += "&" + frameworks;
}

if (filters.PackageTypes != null
&& filters.PackageTypes.Any())
if (!string.IsNullOrEmpty(filters.PackageType))
{
var types = string.Join("&",
filters.PackageTypes.Select(
s => "packageTypeFilter=" + s));
queryString += "&" + types;
queryString += "&packageType=" + filters.PackageType;
}

queryString += "&semVerLevel=2.0.0";
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Protocol/SearchFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public SearchFilter(bool includePrerelease, SearchFilterType? filter)
public bool IncludeDelisted { get; set; } = false;

/// <summary>
/// Restrict the search to certain package types.
/// Restrict the search to a specific package type.
/// </summary>
public IEnumerable<string> PackageTypes { get; set; } = Enumerable.Empty<string>();
public string PackageType { get; set; } = null;

/// <summary>
/// The optional filter type. Absense of this value indicates that all versions should be returned.
Expand Down
3 changes: 2 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/ServiceTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class ServiceTypes
public static readonly string Version300rc = "/3.0.0-rc";
public static readonly string Version300 = "/3.0.0";
public static readonly string Version340 = "/3.4.0";
public static readonly string Version350 = "/3.5.0";
public static readonly string Version360 = "/3.6.0";
public static readonly string Versioned = "/Versioned";
public static readonly string Version470 = "/4.7.0";
Expand All @@ -20,7 +21,7 @@ public static class ServiceTypes
internal const string Version6110 = "/6.11.0";
internal const string Version6130 = "/6.13.0";

public static readonly string[] SearchQueryService = { "SearchQueryService" + Versioned, "SearchQueryService" + Version340, "SearchQueryService" + Version300beta };
public static readonly string[] SearchQueryService = { "SearchQueryService" + Versioned, "SearchQueryService" + Version350, "SearchQueryService" + Version300beta };
public static readonly string[] RegistrationsBaseUrl = { $"RegistrationsBaseUrl{Versioned}", $"RegistrationsBaseUrl{Version360}", $"RegistrationsBaseUrl{Version340}", $"RegistrationsBaseUrl{Version300rc}", $"RegistrationsBaseUrl{Version300beta}", "RegistrationsBaseUrl" };
public static readonly string[] SearchAutocompleteService = { "SearchAutocompleteService" + Versioned, "SearchAutocompleteService" + Version300beta };
public static readonly string[] ReportAbuse = { "ReportAbuseUriTemplate" + Versioned, "ReportAbuseUriTemplate" + Version300 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void SerializeThenDeserialize_WithValidArguments_RoundTrips(SearchFilter
Assert.Equal(expectedResult.IncludeDelisted, actualResult.IncludeDelisted);
Assert.Equal(expectedResult.IncludePrerelease, actualResult.IncludePrerelease);
Assert.Equal(expectedResult.OrderBy, actualResult.OrderBy);
Assert.Equal(expectedResult.PackageTypes, actualResult.PackageTypes);
Assert.Equal(expectedResult.PackageType, actualResult.PackageType);
Assert.Equal(expectedResult.SupportedFrameworks, actualResult.SupportedFrameworks);
}

Expand All @@ -31,7 +31,7 @@ public void SerializeThenDeserialize_WithValidArguments_RoundTrips(SearchFilter
{
IncludeDelisted = true,
OrderBy = SearchOrderBy.Id,
PackageTypes = new List<string>() { "packageType1", "packageType2" },
PackageType = "packageType",
SupportedFrameworks = new List<string>() { ".Net451", ".Net452" }
}
},
Expand Down
Loading