Skip to content

Commit 27db036

Browse files
authored
Update V2 logic for count property (#1380)
1 parent 0e23043 commit 27db036

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/code/RepositorySettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ private static XDocument LoadXDocument(string filePath)
812812

813813
private static PSRepositoryInfo.APIVersion GetRepoAPIVersion(Uri repoUri)
814814
{
815-
if (repoUri.AbsoluteUri.EndsWith("api/v2", StringComparison.OrdinalIgnoreCase))
815+
if (repoUri.AbsoluteUri.EndsWith("/v2", StringComparison.OrdinalIgnoreCase))
816816
{
817817
// Scenario: V2 server protocol repositories (i.e PSGallery)
818818
return PSRepositoryInfo.APIVersion.v2;

src/code/V2ServerAPICalls.cs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,42 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
11351135
try
11361136
{
11371137
doc.LoadXml(httpResponse);
1138+
1139+
bool countSearchSucceeded = false;
1140+
XmlNodeList elemList = doc.GetElementsByTagName("m:count");
1141+
if (elemList.Count > 0)
1142+
{
1143+
countSearchSucceeded = true;
1144+
XmlNode node = elemList[0];
1145+
if (node == null || String.IsNullOrWhiteSpace(node.InnerText))
1146+
{
1147+
countSearchSucceeded = false;
1148+
errRecord = new ErrorRecord(
1149+
new PSArgumentException("Count property from server response was empty, invalid or not present."),
1150+
"GetCountFromResponseFailure",
1151+
ErrorCategory.InvalidData,
1152+
this);
1153+
}
1154+
else
1155+
{
1156+
countSearchSucceeded = int.TryParse(node.InnerText, out count);
1157+
}
1158+
}
1159+
1160+
if (!countSearchSucceeded)
1161+
{
1162+
// 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.
1163+
elemList = doc.GetElementsByTagName("d:Id");
1164+
if (elemList.Count > 0)
1165+
{
1166+
count = elemList.Count;
1167+
errRecord = null;
1168+
}
1169+
else
1170+
{
1171+
_cmdletPassedIn.WriteDebug($"Property 'count' and 'd:Id' could not be found in response. This may indicate that the package could not be found");
1172+
}
1173+
}
11381174
}
11391175
catch (XmlException e)
11401176
{
@@ -1144,17 +1180,6 @@ public int GetCountFromResponse(string httpResponse, out ErrorRecord errRecord)
11441180
ErrorCategory.InvalidData,
11451181
this);
11461182
}
1147-
if (errRecord != null)
1148-
{
1149-
return count;
1150-
}
1151-
1152-
XmlNodeList elemList = doc.GetElementsByTagName("m:count");
1153-
if (elemList.Count > 0)
1154-
{
1155-
XmlNode node = elemList[0];
1156-
count = int.Parse(node.InnerText);
1157-
}
11581183

11591184
return count;
11601185
}

0 commit comments

Comments
 (0)