Skip to content

Commit 30601a6

Browse files
committed
get FindTag (multiple), FindType, FindCommandName, FindDSCResourceName to work (PowerShell#857)
1 parent 8eb13e2 commit 30601a6

File tree

6 files changed

+89
-101
lines changed

6 files changed

+89
-101
lines changed

src/code/FindHelper.cs

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ internal class FindHelper
3939
private readonly string _psGalleryScriptsRepoName = "PSGalleryScripts";
4040
private readonly string _psGalleryUri = "https://www.powershellgallery.com/api/v2";
4141
private readonly string _poshTestGalleryRepoName = "PoshTestGallery";
42-
private readonly string _poshTestGalleryScriptsRepoName = "PoshTestGalleryScripts";
4342
private readonly string _poshTestGalleryUri = "https://www.poshtestgallery.com/api/v2";
4443
private bool _isADOFeedRepository;
4544
private bool _repositoryNameContainsWildcard;
@@ -132,68 +131,6 @@ public List<PSResourceInfo> FindByResourceName(
132131
return foundPackages;
133132
}
134133

135-
// Loop through repositoriesToSearch and if PSGallery or PoshTestGallery add its Scripts endpoint repo
136-
// to list with same priority as PSGallery repo.
137-
// This special casing is done to handle PSGallery and PoshTestGallery having 2 endpoints currently for different resources.
138-
for (int i = 0; i < repositoriesToSearch.Count; i++)
139-
{
140-
if (String.Equals(repositoriesToSearch[i].Uri.AbsoluteUri, _psGalleryUri, StringComparison.InvariantCultureIgnoreCase))
141-
{
142-
// special case: for PowerShellGallery, Module and Script resources have different endpoints so separate repositories have to be registered
143-
// with those endpoints in order for the NuGet APIs to search across both in the case where name includes '*'
144-
145-
// detect if Script repository needs to be added and/or Module repository needs to be skipped
146-
Uri psGalleryScriptsUri = new Uri("http://www.powershellgallery.com/api/v2/items/psscript/");
147-
PSRepositoryInfo psGalleryScripts = new PSRepositoryInfo(
148-
_psGalleryScriptsRepoName,
149-
psGalleryScriptsUri,
150-
repositoriesToSearch[i].Priority,
151-
trusted: false,
152-
credentialInfo: null,
153-
repositoriesToSearch[i].ApiVersion);
154-
155-
if (_type == ResourceType.None)
156-
{
157-
_cmdletPassedIn.WriteVerbose("Null Type provided, so add PSGalleryScripts repository");
158-
repositoriesToSearch.Insert(i + 1, psGalleryScripts);
159-
}
160-
else if (_type != ResourceType.None && _type == ResourceType.Script)
161-
{
162-
_cmdletPassedIn.WriteVerbose("Type Script provided, so add PSGalleryScripts and remove PSGallery (Modules only) from search consideration");
163-
repositoriesToSearch.Insert(i + 1, psGalleryScripts);
164-
repositoriesToSearch.RemoveAt(i); // remove PSGallery
165-
}
166-
}
167-
else if (String.Equals(repositoriesToSearch[i].Uri.AbsoluteUri, _poshTestGalleryUri, StringComparison.InvariantCultureIgnoreCase))
168-
{
169-
// special case: for PoshTestGallery, Module and Script resources have different endpoints so separate repositories have to be registered
170-
// with those endpoints in order for the NuGet APIs to search across both in the case where name includes '*'
171-
172-
// detect if Script repository needs to be added and/or Module repository needs to be skipped
173-
Uri poshTestGalleryScriptsUri = new Uri("https://www.poshtestgallery.com/api/v2/items/psscript/");
174-
PSRepositoryInfo poshTestGalleryScripts = new PSRepositoryInfo(
175-
_poshTestGalleryScriptsRepoName,
176-
poshTestGalleryScriptsUri,
177-
repositoriesToSearch[i].Priority,
178-
trusted: false,
179-
credentialInfo: null,
180-
repositoriesToSearch[i].ApiVersion);
181-
182-
if (_type == ResourceType.None)
183-
{
184-
_cmdletPassedIn.WriteVerbose("Null Type provided, so add PoshTestGalleryScripts repository");
185-
repositoriesToSearch.Insert(i + 1, poshTestGalleryScripts);
186-
}
187-
else if (_type != ResourceType.None && _type == ResourceType.Script)
188-
{
189-
_cmdletPassedIn.WriteVerbose("Type Script provided, so add PoshTestGalleryScripts and remove PoshTestGallery (Modules only) from search consideration");
190-
repositoriesToSearch.Insert(i + 1, poshTestGalleryScripts);
191-
repositoriesToSearch.RemoveAt(i); // remove PoshTestGallery
192-
}
193-
}
194-
195-
}
196-
197134
for (int i = 0; i < repositoriesToSearch.Count && _pkgsLeftToFind.Any(); i++)
198135
{
199136
_cmdletPassedIn.WriteVerbose(string.Format("Searching in repository {0}", repositoriesToSearch[i].Name));
@@ -230,7 +167,7 @@ private IEnumerable<PSResourceInfo> HttpSearchFromRepository(PSRepositoryInfo re
230167
if (_tag != null)
231168
{
232169
// TODO: this is currently very buggy and the url queries need to be fixed
233-
foreach (PSResourceInfo pkgs in HttpFindTags(repositoryInfo))
170+
foreach (PSResourceInfo pkgs in HttpFindTags(repositoryInfo, _type))
234171
{
235172
yield return pkgs;
236173
}
@@ -242,6 +179,7 @@ private IEnumerable<PSResourceInfo> HttpSearchFromRepository(PSRepositoryInfo re
242179
yield return pkg;
243180
}
244181
}
182+
245183
yield break;
246184
}
247185

@@ -757,9 +695,9 @@ private bool IsTagMatch(PSResourceInfo pkg)
757695
return _tag.Intersect(pkg.Tags, StringComparer.InvariantCultureIgnoreCase).ToList().Count > 0;
758696
}
759697

760-
private PSResourceInfo[] HttpFindTags(PSRepositoryInfo repository)
698+
private PSResourceInfo[] HttpFindTags(PSRepositoryInfo repository, ResourceType type)
761699
{
762-
return _httpFindPSResource.FindTags(_tag, repository, _prerelease, out string errRecord);
700+
return _httpFindPSResource.FindTags(_tag, repository, _prerelease, type, out string errRecord);
763701
// TODO: write out error
764702
}
765703

src/code/HttpFindPSResource.cs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Collections;
13
using System.Linq;
24
using System.Xml;
35
using Microsoft.PowerShell.PowerShellGet.UtilClasses;
@@ -56,31 +58,43 @@ public PSResourceInfo FindAll(PSRepositoryInfo repository, bool includePrereleas
5658
/// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:JSON'
5759
/// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:JSON'&includePrerelease=true
5860
/// </summary>
59-
public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, out string errRecord)
61+
public PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out string errRecord)
6062
{
61-
var response = v2ServerAPICall.FindTags(tags, repository, includePrerelease, out errRecord);
62-
63-
var elemList = ConvertResponseToXML(response);
63+
errRecord = String.Empty;
6464
List<PSResourceInfo> pkgsFound = new List<PSResourceInfo>();
65+
HashSet<string> tagPkgs = new HashSet<string>();
6566

66-
foreach (var element in elemList)
67+
foreach (string tag in tags)
6768
{
68-
PSResourceInfo.TryConvertFromXml(
69-
element,
70-
includePrerelease,
71-
out PSResourceInfo psGetInfo,
72-
repository.Name,
73-
out string errorMsg);
69+
string[] responses = v2ServerAPICall.FindTag(tag, repository, includePrerelease, type, out errRecord);
7470

75-
if (psGetInfo != null)
71+
foreach (string response in responses)
7672
{
77-
pkgsFound.Add(psGetInfo);
78-
}
79-
else
80-
{
81-
// TODO: Write error for corresponding null scenario
82-
errRecord = errorMsg;
83-
}
73+
var elemList = ConvertResponseToXML(response);
74+
75+
foreach (var element in elemList)
76+
{
77+
PSResourceInfo.TryConvertFromXml(
78+
element,
79+
includePrerelease,
80+
out PSResourceInfo psGetInfo,
81+
repository.Name,
82+
out string errorMsg);
83+
84+
if (psGetInfo != null && !tagPkgs.Contains(psGetInfo.Name))
85+
{
86+
tagPkgs.Add(psGetInfo.Name);
87+
pkgsFound.Add(psGetInfo);
88+
}
89+
else
90+
{
91+
// TODO: Write error for corresponding null scenario
92+
// TODO: array out of bounds exception when name does not exist
93+
// http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:PSCommand_Get-TargetResource'
94+
errRecord = errorMsg;
95+
}
96+
}
97+
}
8498
}
8599

86100
return pkgsFound.ToArray();

src/code/IFindPSResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IFindPSResource
2121
/// - No prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:JSON'
2222
/// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:JSON'&includePrerelease=true
2323
/// </summary>
24-
PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, out string errRecord);
24+
PSResourceInfo[] FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out string errRecord);
2525

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

src/code/IServerAPICalls.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public interface IServerAPICalls
2727

2828

2929
/// <summary>
30-
/// Find method which allows for searching for packages with tag(s) from a repository and returns latest version for each.
30+
/// Find method which allows for searching for packages with tag from a repository and returns latest version for each.
3131
/// Examples: Search -Tag "JSON" -Repository PSGallery
3232
/// API call:
3333
/// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:JSON'&includePrerelease=true
3434
/// </summary>
35-
string FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, out string errRecord);
35+
string[] FindTag(string tag, PSRepositoryInfo repository, bool includePrerelease, ResourceType _type, out string errRecord);
3636

3737

3838
/// <summary>

src/code/PSResourceInfo.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ namespace Microsoft.PowerShell.PowerShellGet.UtilClasses
1818
{
1919
#region Enums
2020

21-
[Flags]
2221
public enum ResourceType
2322
{
24-
None = 0x0,
25-
Module = 0x1,
26-
Script = 0x2,
27-
Command = 0x4,
28-
DscResource = 0x8
23+
None,
24+
Module,
25+
Script,
26+
Command,
27+
DscResource
2928
}
3029

3130
public enum VersionType

src/code/V2ServerAPICalls.cs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.PowerShell.PowerShellGet.UtilClasses;
55
using System;
6+
using System.Collections.Generic;
67
using System.Net.Http;
78
using NuGet.Versioning;
89

@@ -66,21 +67,57 @@ public string FindAllWithPrerelease(PSRepositoryInfo repository, out string errR
6667

6768

6869
/// <summary>
69-
/// Find method which allows for searching for packages with tag(s) from a repository and returns latest version for each.
70+
/// Find method which allows for searching for packages with tag from a repository and returns latest version for each.
7071
/// Examples: Search -Tag "JSON" -Repository PSGallery
7172
/// API call:
7273
/// - Include prerelease: http://www.powershellgallery.com/api/v2/Search()?$filter=IsAbsoluteLatestVersion&searchTerm=tag:JSON&includePrerelease=true
7374
/// </summary>
74-
public string FindTags(string[] tags, PSRepositoryInfo repository, bool includePrerelease, out string errRecord)
75+
public string[] FindTag(string tag, PSRepositoryInfo repository, bool includePrerelease, ResourceType type, out string errRecord)
7576
{
76-
var tagsString = String.Join(" ", tags);
77+
// scenarios with type + tags:
78+
// type: None -> search both endpoints
79+
// type: M -> just search Module endpoint
80+
// type: S -> just search Scripts end point
81+
// type: DSCResource -> just search Modules
82+
// type: Command -> just search Modules
83+
errRecord = String.Empty;
7784
var prereleaseFilter = includePrerelease ? "&includePrerelease=true" : string.Empty;
85+
List<string> responses = new List<string>();
7886

79-
// There are no quotations around tag(s) in the url because this should be an "or" operation
80-
var requestUrlV2 = $"{repository.Uri}/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:{tagsString}'{prereleaseFilter}&{select}";
87+
if (type == ResourceType.Script || type == ResourceType.None)
88+
{
89+
// $"{repository.Uri}/items/psscript/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:{tag}+tag:PSScript'{prereleaseFilter}&{select}";
90+
var scriptsRequestUrlV2 = $"{repository.Uri}/items/psscript/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:{tag}'{prereleaseFilter}&{select}";
91+
responses.Add(HttpRequestCall(requestUrlV2: scriptsRequestUrlV2, out string scriptErrorRecord));
92+
// TODO: add error handling here
93+
}
94+
95+
if (type != ResourceType.Script)
96+
{
97+
if (type == ResourceType.None || type == ResourceType.Module)
98+
{
99+
// type: Module or Command or DSCResource or None
100+
var modulesRequestUrlV2 = $"{repository.Uri}/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:{tag}'{prereleaseFilter}&{select}";
101+
responses.Add(HttpRequestCall(requestUrlV2: modulesRequestUrlV2, out string moduleErrorRecord));
102+
}
103+
else if (type == ResourceType.Command)
104+
{
105+
// http://www.powershellgallery.com/api/v2/Search()?$filter=IsLatestVersion&searchTerm='tag:PSCommand_Get-TargetResource'
106+
var commandRequestUrlV2 = $"{repository.Uri}/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:PSCommand_{tag}'{prereleaseFilter}&{select}";
107+
responses.Add(HttpRequestCall(requestUrlV2: commandRequestUrlV2, out string commandErrorRecord));
108+
}
109+
else
110+
{
111+
// DSCResource type
112+
var dscResourceRequestUrlV2 = $"{repository.Uri}/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='tag:PSDscResource_{tag}'{prereleaseFilter}&{select}";
113+
responses.Add(HttpRequestCall(requestUrlV2: dscResourceRequestUrlV2, out string dscResourceErrorRecord));
114+
}
81115

82-
Console.WriteLine(requestUrlV2);
83-
return HttpRequestCall(requestUrlV2, out errRecord);
116+
// TODO: add error handling here
117+
118+
}
119+
120+
return responses.ToArray();
84121
}
85122

86123

0 commit comments

Comments
 (0)