Skip to content

Integrate FindVersionGlobbing V2 into FindPSResource #853

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

Merged
merged 1 commit into from
Nov 9, 2022
Merged
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
27 changes: 18 additions & 9 deletions src/code/FindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ private IEnumerable<PSResourceInfo> HttpSearchAcrossNamesInRepository(PSReposito
{
VersionRange versionRange = null;
NuGetVersion nugetVersion = null;
bool isSingleVersion = false;

if (_version != null)
{
Expand All @@ -350,11 +349,6 @@ private IEnumerable<PSResourceInfo> HttpSearchAcrossNamesInRepository(PSReposito
this));
yield break;
}

if (versionRange.MinVersion == versionRange.MaxVersion)
{
isSingleVersion = true;
}
}

// Checking
Expand All @@ -370,7 +364,7 @@ private IEnumerable<PSResourceInfo> HttpSearchAcrossNamesInRepository(PSReposito
}

// Note: For a single version, we have 1 or more name, with or without globbing
if (isSingleVersion)
if (nugetVersion != null || string.IsNullOrEmpty(_version))
{
foreach (string pkgName in _pkgsLeftToFind.ToArray())
{
Expand All @@ -385,9 +379,24 @@ private IEnumerable<PSResourceInfo> HttpSearchAcrossNamesInRepository(PSReposito
{
// call 'FindNameGlobbing' or 'FindNameGlobbingAndVersion'
}
else
else if (nugetVersion != null)
{
PSResourceInfo foundPkg = _httpFindPSResource.FindName(pkgName, repository, _prerelease, out string errRecord);
// TODO: User should not use -Prerelease parameter with a specific version. Write out some kind of messaging (warning, error, verbose) to inform user of this
// if they attempt this combination
PSResourceInfo foundPkg = _httpFindPSResource.FindVersion(pkgName, nugetVersion.ToNormalizedString(), repository, out string errRecord);
if (foundPkg != null)
{
if (!_repositoryNameContainsWildcard)
{
_pkgsLeftToFind.Remove(pkgName);
}

yield return foundPkg;
}
}
else {
// If no version is specified, just retrieve the latest version
PSResourceInfo foundPkg = _httpFindPSResource.FindName(pkgName, repository, _prerelease, out string errRecord);
if (foundPkg != null)
{
if (!_repositoryNameContainsWildcard)
Expand Down
5 changes: 0 additions & 5 deletions src/code/PSResourceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,6 @@ public static bool TryConvertFromXml(
else if (key.Equals("IsPrerelease"))
{
bool.TryParse(value, out bool isPrerelease);
// For FindName
if (!includePrerelease && isPrerelease)
{
return false;
}

metadata[key] = isPrerelease;
}
Expand Down
11 changes: 6 additions & 5 deletions src/code/V2ServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class V2ServerAPICalls : IServerAPICalls
#region Members

private static readonly HttpClient s_client = new HttpClient();
private static readonly string select = "&$select=Id,Version,Authors,Copyright,Dependencies,Description,IconUrl,IsPrerelease,Published,ProjectUrl,ReleaseNotes,Tags,LicenseUrl,CompanyName";
private static readonly string select = "$select=Id,Version,Authors,Copyright,Dependencies,Description,IconUrl,IsPrerelease,Published,ProjectUrl,ReleaseNotes,Tags,LicenseUrl,CompanyName";

#endregion

Expand Down Expand Up @@ -173,7 +173,7 @@ public string FindName(string packageName, PSRepositoryInfo repository, bool inc
var prerelease = includePrerelease ? "IsAbsoluteLatestVersion" : "IsLatestVersion";

// This should return the latest stable version or the latest prerelease version (respectively)
var requestUrlV2 = $"{repository.Uri.ToString()}/FindPackagesById()?id='{packageName}'?&$orderby=Version desc&$filter={prerelease}{select}";
var requestUrlV2 = $"{repository.Uri.ToString()}/FindPackagesById()?id='{packageName}'?&$orderby=Version desc&$filter={prerelease}&{select}";

return HttpRequestCall(requestUrlV2, out errRecord);
}
Expand Down Expand Up @@ -202,7 +202,8 @@ public string FindNameGlobbing(string packageName, PSRepositoryInfo repository,
// both return metadata, but responses are different.
var prerelease = includePrerelease ? "IsAbsoluteLatestVersion" : "IsLatestVersion";

var requestUrlV2 = $"{repository.Uri.ToString()}/Search()?searchTerm='{packageName}'&$filter={prerelease}{select}";
// TODO: "&" in url may need to be changed to "?"
var requestUrlV2 = $"{repository.Uri.ToString()}/Search()?searchTerm='{packageName}'&$filter={prerelease}&{select}";

return HttpRequestCall(requestUrlV2, out errRecord);
}
Expand Down Expand Up @@ -260,7 +261,7 @@ public string FindVersionGlobbing(string packageName, VersionRange versionRange,
// will need to filter additionally, if IncludePrerelease=false, by default we get stable + prerelease both back
// Current bug: Find PSGet -Version "2.0.*" -> https://www.powershellgallery.com/api/v2//FindPackagesById()?id='PowerShellGet'&includePrerelease=false&$filter= Version gt '2.0.*' and Version lt '2.1'
// Make sure to include quotations around the package name
var requestUrlV2 = $"{repository.Uri.ToString()}/FindPackagesById()?id='{packageName}'{select}&$filter=IsPrerelease eq {includePrerelease}";
var requestUrlV2 = $"{repository.Uri.ToString()}/FindPackagesById()?id='{packageName}'&{select}&$filter=IsPrerelease eq {includePrerelease}";

//and IsPrerelease eq false
// ex:
Expand Down Expand Up @@ -327,7 +328,7 @@ public string FindVersionGlobbing(string packageName, VersionRange versionRange,
/// </summary>
public string FindVersion(string packageName, string version, PSRepositoryInfo repository, out string errRecord) {
// Quotations around package name and version do not matter, same metadata gets returned.
var requestUrlV2 = $"{repository.Uri.ToString()}/Packages(Id='{packageName}', Version='{version}') ";
var requestUrlV2 = $"{repository.Uri.ToString()}/Packages(Id='{packageName}', Version='{version}')?{select}";

return HttpRequestCall(requestUrlV2, out errRecord);
}
Expand Down