Skip to content

Commit 17616ee

Browse files
authored
Fix up bugs with find tags (#859)
1 parent 50bf6eb commit 17616ee

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

src/code/FindHelper.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ internal class FindHelper
2929
private CancellationToken _cancellationToken;
3030
private readonly PSCmdlet _cmdletPassedIn;
3131
private List<string> _pkgsLeftToFind;
32+
private List<string> _tagsLeftToFind;
33+
3234
private ResourceType _type;
3335
private string _version;
3436
private SwitchParameter _prerelease = false;
@@ -246,7 +248,7 @@ public List<PSResourceInfo> FindTag(
246248
_credential = credential;
247249

248250
List<PSResourceInfo> foundPackages = new List<PSResourceInfo>();
249-
List<string> tagsLeftToFind = new List<string>(tag);
251+
_tagsLeftToFind = new List<string>(tag);
250252

251253
if (tag.Length == 0)
252254
{
@@ -292,18 +294,30 @@ public List<PSResourceInfo> FindTag(
292294
return foundPackages;
293295
}
294296

295-
for (int i = 0; i < repositoriesToSearch.Count && tagsLeftToFind.Any(); i++)
297+
for (int i = 0; i < repositoriesToSearch.Count && _tagsLeftToFind.Any(); i++)
296298
{
299+
if (_type != ResourceType.None && repositoriesToSearch[i].Name != "PSGallery")
300+
{
301+
_cmdletPassedIn.ThrowTerminatingError(new ErrorRecord(
302+
new PSInvalidOperationException("-Type parameter is only supported with the PowerShellGallery."),
303+
"ErrorUsingTypeParameter",
304+
ErrorCategory.InvalidOperation,
305+
this));
306+
}
307+
297308
_cmdletPassedIn.WriteVerbose(string.Format("Searching in repository {0}", repositoriesToSearch[i].Name));
298309
if (repositoriesToSearch[i].ApiVersion == PSRepositoryInfo.APIVersion.v2)
299-
{
300-
// tag1, tag2, tag3
301-
310+
{
302311
// TODO: didn't really finsh come back here
303312
foreach (PSResourceInfo cmdInfo in HttpSearchFromRepository(repositoriesToSearch[i]))
304313
{
305314
foundPackages.Add(cmdInfo);
306-
}
315+
}
316+
}
317+
if (repositoriesToSearch[i].ApiVersion == PSRepositoryInfo.APIVersion.v3)
318+
{
319+
// TODO: implement this when we work on v3 requests
320+
break;
307321
}
308322
}
309323

@@ -321,11 +335,17 @@ private IEnumerable<PSResourceInfo> HttpSearchFromRepository(PSRepositoryInfo re
321335
// -Name parameter and -Tag parameter are exclusive
322336
if (_tag != null)
323337
{
338+
HashSet<string> tagsFound;
324339
// TODO: this is currently very buggy and the url queries need to be fixed
325-
foreach (PSResourceInfo pkgs in HttpFindTags(repositoryInfo, _type))
340+
foreach (PSResourceInfo pkgs in HttpFindTags(repositoryInfo, _type, out tagsFound))
326341
{
327342
yield return pkgs;
328343
}
344+
345+
foreach(string tag in tagsFound)
346+
{
347+
_tagsLeftToFind.Remove(tag);
348+
}
329349
}
330350
else if (_pkgsLeftToFind.Count > 0)
331351
{
@@ -337,7 +357,6 @@ private IEnumerable<PSResourceInfo> HttpSearchFromRepository(PSRepositoryInfo re
337357

338358
yield break;
339359
}
340-
341360
// TODO: ADD command search, dscresource search, etc.
342361
}
343362

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

853-
private PSResourceInfo[] HttpFindTags(PSRepositoryInfo repository, ResourceType type)
872+
private PSResourceInfo[] HttpFindTags(PSRepositoryInfo repository, ResourceType type, out HashSet<string> tagsFound)
854873
{
855-
return _httpFindPSResource.FindTags(_tag, repository, _prerelease, type, out string errRecord);
874+
return _httpFindPSResource.FindTags(_tag, repository, _prerelease, type, out tagsFound, out string errRecord);
856875
// TODO: write out error
857876
}
858877

src/code/FindPSResource.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,12 @@ private void ProcessTagParameterSet()
285285
return;
286286
}
287287

288-
List<PSResourceInfo> foundPackages = _findHelper.FindByResourceName(
289-
name: Utils.EmptyStrArray,
288+
List<PSResourceInfo> foundPackages = _findHelper.FindTag(
290289
type: Type,
291-
version: Version,
292290
prerelease: Prerelease,
293291
tag: tagsToSearch,
294292
repository: Repository,
295-
credential: Credential,
296-
includeDependencies: IncludeDependencies);
293+
credential: Credential);
297294

298295
foreach (var package in foundPackages)
299296
{

src/code/HttpFindPSResource.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ public PSResourceInfo FindAll(PSRepositoryInfo repository, bool includePrereleas
5858
/// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:JSON'
5959
/// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:JSON'&includePrerelease=true
6060
/// </summary>
61-
public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out string errRecord)
61+
public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out HashSet<string> tagsFound, out string errRecord)
6262
{
6363
errRecord = String.Empty;
6464
List<PSResourceInfo> pkgsFound = new List<PSResourceInfo>();
6565
HashSet<string> tagPkgs = new HashSet<string>();
66-
66+
tagsFound = new HashSet<string>();
67+
6768
// TAG example:
6869
// chocolatey, crescendo
6970
// > chocolatey === ModuleA
@@ -74,8 +75,6 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo
7475
{
7576
string[] responses = v2ServerAPICall.FindTag(tag, repository, includePrerelease, type, out errRecord);
7677

77-
// TODO: map the tag with the package which the tag came from
78-
7978
foreach (string response in responses)
8079
{
8180
var elemList = ConvertResponseToXML(response);
@@ -93,6 +92,7 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo
9392
{
9493
tagPkgs.Add(psGetInfo.Name);
9594
pkgsFound.Add(psGetInfo);
95+
tagsFound.Add(tag);
9696
}
9797
else
9898
{
@@ -102,7 +102,7 @@ public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, boo
102102
errRecord = errorMsg;
103103
}
104104
}
105-
}
105+
}
106106
}
107107

108108
return pkgsFound.ToArray();

src/code/IFindPSResource.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using Microsoft.PowerShell.PowerShellGet.UtilClasses;
23
using NuGet.Versioning;
34

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

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

0 commit comments

Comments
 (0)