Skip to content

Commit

Permalink
Update V2 logic for count property (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
anamnavi authored Aug 25, 2023
1 parent 0e23043 commit 27db036
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/code/RepositorySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ private static XDocument LoadXDocument(string filePath)

private static PSRepositoryInfo.APIVersion GetRepoAPIVersion(Uri repoUri)
{
if (repoUri.AbsoluteUri.EndsWith("api/v2", StringComparison.OrdinalIgnoreCase))
if (repoUri.AbsoluteUri.EndsWith("/v2", StringComparison.OrdinalIgnoreCase))
{
// Scenario: V2 server protocol repositories (i.e PSGallery)
return PSRepositoryInfo.APIVersion.v2;
Expand Down
47 changes: 36 additions & 11 deletions src/code/V2ServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,42 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
try
{
doc.LoadXml(httpResponse);

bool countSearchSucceeded = false;
XmlNodeList elemList = doc.GetElementsByTagName("m:count");
if (elemList.Count > 0)
{
countSearchSucceeded = true;
XmlNode node = elemList[0];
if (node == null || String.IsNullOrWhiteSpace(node.InnerText))
{
countSearchSucceeded = false;
errRecord = new ErrorRecord(
new PSArgumentException("Count property from server response was empty, invalid or not present."),
"GetCountFromResponseFailure",
ErrorCategory.InvalidData,
this);
}
else
{
countSearchSucceeded = int.TryParse(node.InnerText, out count);
}
}

if (!countSearchSucceeded)
{
// Note: not all V2 servers may have the 'count' property implemented or valid (i.e CloudSmith server), in this case try to get 'd:Id' property.
elemList = doc.GetElementsByTagName("d:Id");
if (elemList.Count > 0)
{
count = elemList.Count;
errRecord = null;
}
else
{
_cmdletPassedIn.WriteDebug($"Property 'count' and 'd:Id' could not be found in response. This may indicate that the package could not be found");
}
}
}
catch (XmlException e)
{
Expand All @@ -1144,17 +1180,6 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
ErrorCategory.InvalidData,
this);
}
if (errRecord != null)
{
return count;
}

XmlNodeList elemList = doc.GetElementsByTagName("m:count");
if (elemList.Count > 0)
{
XmlNode node = elemList[0];
count = int.Parse(node.InnerText);
}

return count;
}
Expand Down

0 comments on commit 27db036

Please sign in to comment.