Skip to content

Fix up bugs with find tags #859

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 14, 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
39 changes: 29 additions & 10 deletions src/code/FindHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ internal class FindHelper
private CancellationToken _cancellationToken;
private readonly PSCmdlet _cmdletPassedIn;
private List<string> _pkgsLeftToFind;
private List<string> _tagsLeftToFind;

private ResourceType _type;
private string _version;
private SwitchParameter _prerelease = false;
Expand Down Expand Up @@ -246,7 +248,7 @@ public List<PSResourceInfo> FindTag(
_credential = credential;

List<PSResourceInfo> foundPackages = new List<PSResourceInfo>();
List<string> tagsLeftToFind = new List<string>(tag);
_tagsLeftToFind = new List<string>(tag);

if (tag.Length == 0)
{
Expand Down Expand Up @@ -292,18 +294,30 @@ public List<PSResourceInfo> FindTag(
return foundPackages;
}

for (int i = 0; i < repositoriesToSearch.Count && tagsLeftToFind.Any(); i++)
for (int i = 0; i < repositoriesToSearch.Count && _tagsLeftToFind.Any(); i++)
{
if (_type != ResourceType.None && repositoriesToSearch[i].Name != "PSGallery")
{
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException("-Type parameter is only supported with the PowerShellGallery."),
"ErrorUsingTypeParameter",
ErrorCategory.InvalidOperation,
this));
}

_cmdletPassedIn.WriteVerbose(string.Format("Searching in repository {0}", repositoriesToSearch[i].Name));
if (repositoriesToSearch[i].ApiVersion == PSRepositoryInfo.APIVersion.v2)
{
// tag1, tag2, tag3

{
// TODO: didn't really finsh come back here
foreach (PSResourceInfo cmdInfo in HttpSearchFromRepository(repositoriesToSearch[i]))
{
foundPackages.Add(cmdInfo);
}
}
}
if (repositoriesToSearch[i].ApiVersion == PSRepositoryInfo.APIVersion.v3)
{
// TODO: implement this when we work on v3 requests
break;
}
}

Expand All @@ -321,11 +335,17 @@ private IEnumerable<PSResourceInfo> HttpSearchFromRepository(PSRepositoryInfo re
// -Name parameter and -Tag parameter are exclusive
if (_tag != null)
{
HashSet<string> tagsFound;
// TODO: this is currently very buggy and the url queries need to be fixed
foreach (PSResourceInfo pkgs in HttpFindTags(repositoryInfo, _type))
foreach (PSResourceInfo pkgs in HttpFindTags(repositoryInfo, _type, out tagsFound))
{
yield return pkgs;
}

foreach(string tag in tagsFound)
{
_tagsLeftToFind.Remove(tag);
}
}
else if (_pkgsLeftToFind.Count > 0)
{
Expand All @@ -337,7 +357,6 @@ private IEnumerable<PSResourceInfo> HttpSearchFromRepository(PSRepositoryInfo re

yield break;
}

// TODO: ADD command search, dscresource search, etc.
}

Expand Down Expand Up @@ -850,9 +869,9 @@ private bool IsTagMatch(PSResourceInfo pkg)
return _tag.Intersect(pkg.Tags, StringComparer.InvariantCultureIgnoreCase).ToList().Count > 0;
}

private PSResourceInfo[] HttpFindTags(PSRepositoryInfo repository, ResourceType type)
private PSResourceInfo[] HttpFindTags(PSRepositoryInfo repository, ResourceType type, out HashSet<string> tagsFound)
{
return _httpFindPSResource.FindTags(_tag, repository, _prerelease, type, out string errRecord);
return _httpFindPSResource.FindTags(_tag, repository, _prerelease, type, out tagsFound, out string errRecord);
// TODO: write out error
}

Expand Down
7 changes: 2 additions & 5 deletions src/code/FindPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,15 +285,12 @@ private void ProcessTagParameterSet()
return;
}

List<PSResourceInfo> foundPackages = _findHelper.FindByResourceName(
name: Utils.EmptyStrArray,
List<PSResourceInfo> foundPackages = _findHelper.FindTag(
type: Type,
version: Version,
prerelease: Prerelease,
tag: tagsToSearch,
repository: Repository,
credential: Credential,
includeDependencies: IncludeDependencies);
credential: Credential);

foreach (var package in foundPackages)
{
Expand Down
10 changes: 5 additions & 5 deletions src/code/HttpFindPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public PSResourceInfo FindAll(PSRepositoryInfo repository, bool includePrereleas
/// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:JSON'
/// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:JSON'&includePrerelease=true
/// </summary>
public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out string errRecord)
public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out HashSet<string> tagsFound, out string errRecord)
{
errRecord = String.Empty;
List<PSResourceInfo> pkgsFound = new List<PSResourceInfo>();
HashSet<string> tagPkgs = new HashSet<string>();

tagsFound = new HashSet<string>();

// TAG example:
// chocolatey, crescendo
// > chocolatey === ModuleA
Expand All @@ -74,8 +75,6 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo
{
string[] responses = v2ServerAPICall.FindTag(tag, repository, includePrerelease, type, out errRecord);

// TODO: map the tag with the package which the tag came from

foreach (string response in responses)
{
var elemList = ConvertResponseToXML(response);
Expand All @@ -93,6 +92,7 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo
{
tagPkgs.Add(psGetInfo.Name);
pkgsFound.Add(psGetInfo);
tagsFound.Add(tag);
}
else
{
Expand All @@ -102,7 +102,7 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo
errRecord = errorMsg;
}
}
}
}
}

return pkgsFound.ToArray();
Expand Down
3 changes: 2 additions & 1 deletion src/code/IFindPSResource.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using Microsoft.PowerShell.PowerShellGet.UtilClasses;
using NuGet.Versioning;

Expand All @@ -21,7 +22,7 @@ public interface IFindPSResource
/// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:JSON'
/// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:JSON'&includePrerelease=true
/// </summary>
PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out string errRecord);
PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out HashSet<string> tagsFound, out string errRecord);

/// <summary>
/// Find method which allows for searching for packages with resource type specified from a repository and returns latest version for each.
Expand Down