Skip to content

Commit 0fc7827

Browse files
authored
Fix 'name' bug with v2 JFrog Artifactory (#1535)
1 parent 17a6178 commit 0fc7827

File tree

2 files changed

+68
-65
lines changed

2 files changed

+68
-65
lines changed

src/code/PSResourceInfo.cs

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -490,76 +490,79 @@ public static bool TryConvertFromXml(
490490
{
491491
Hashtable metadata = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
492492

493-
var childNodes = entry.ChildNodes;
494-
foreach (XmlElement child in childNodes)
493+
var entryChildNodes = entry.ChildNodes;
494+
foreach (XmlElement entryChild in entryChildNodes)
495495
{
496-
var key = child.LocalName;
497-
var value = child.InnerText;
496+
var entryKey = entryChild.LocalName;
498497

499-
if (key.Equals("Title"))
498+
// For repositories such as JFrog's Artifactory, there is no 'Id' property, just 'title' (which contains the name of the pkg).
499+
// However, other repos, like PSGallery include the name of the pkg in the 'Id' property and leave 'title' empty.
500+
// In JFrog's Artifactory, 'title' exists both as a child of the 'entry' node and as a child of the 'properties' node,
501+
// though sometimes 'title' under the 'properties' node can be empty (so default to using the former).
502+
if (entryKey.Equals("title"))
500503
{
501-
// For repositories such as JFrog's Artifactory, there is no 'Id' property, just 'Title' (which contains the name of the pkg).
502-
// However, other repos, like PSGallery include the name of the pkg in the 'Id' property and leave 'Title' empty.
503-
504-
// First check to see that both 'Title' and 'Id' exist in the child nodes.
505-
// If both exist, take 'Id', otherwise just take 'Title'.
506-
bool containsID = false;
507-
foreach (XmlElement childNode in childNodes)
504+
metadata["Id"] = entryChild.InnerText;
505+
}
506+
else if (entryKey.Equals("properties"))
507+
{
508+
var propertyChildNodes = entryChild.ChildNodes;
509+
foreach (XmlElement propertyChild in propertyChildNodes)
508510
{
509-
if (childNode.LocalName == "Id")
511+
var propertyKey = propertyChild.LocalName;
512+
var propertyValue = propertyChild.InnerText;
513+
514+
if (propertyKey.Equals("Title"))
510515
{
511-
containsID = true;
516+
if (!metadata.ContainsKey("Id"))
517+
{
518+
metadata["Id"] = propertyValue;
519+
}
512520
}
513-
}
521+
if (propertyKey.Equals("Version"))
522+
{
523+
metadata[propertyKey] = ParseHttpVersion(propertyValue, out string prereleaseLabel);
524+
metadata["Prerelease"] = prereleaseLabel;
525+
}
526+
else if (propertyKey.EndsWith("Url"))
527+
{
528+
metadata[propertyKey] = ParseHttpUrl(propertyValue) as Uri;
529+
}
530+
else if (propertyKey.Equals("Tags"))
531+
{
532+
metadata[propertyKey] = propertyValue.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
533+
}
534+
else if (propertyKey.Equals("Published"))
535+
{
536+
metadata[propertyKey] = ParseHttpDateTime(propertyValue);
537+
}
538+
else if (propertyKey.Equals("Dependencies"))
539+
{
540+
metadata[propertyKey] = ParseHttpDependencies(propertyValue);
541+
}
542+
else if (propertyKey.Equals("IsPrerelease"))
543+
{
544+
bool.TryParse(propertyValue, out bool isPrerelease);
514545

515-
if (!containsID)
516-
{
517-
metadata["Id"] = value;
518-
}
519-
}
520-
if (key.Equals("Version"))
521-
{
522-
metadata[key] = ParseHttpVersion(value, out string prereleaseLabel);
523-
metadata["Prerelease"] = prereleaseLabel;
524-
}
525-
else if (key.EndsWith("Url"))
526-
{
527-
metadata[key] = ParseHttpUrl(value) as Uri;
528-
}
529-
else if (key.Equals("Tags"))
530-
{
531-
metadata[key] = value.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
532-
}
533-
else if (key.Equals("Published"))
534-
{
535-
metadata[key] = ParseHttpDateTime(value);
536-
}
537-
else if (key.Equals("Dependencies"))
538-
{
539-
metadata[key] = ParseHttpDependencies(value);
540-
}
541-
else if (key.Equals("IsPrerelease"))
542-
{
543-
bool.TryParse(value, out bool isPrerelease);
546+
metadata[propertyKey] = isPrerelease;
547+
}
548+
else if (propertyKey.Equals("NormalizedVersion"))
549+
{
550+
if (!NuGetVersion.TryParse(propertyValue, out NuGetVersion parsedNormalizedVersion))
551+
{
552+
errorMsg = string.Format(
553+
CultureInfo.InvariantCulture,
554+
@"TryReadPSGetInfo: Cannot parse NormalizedVersion");
544555

545-
metadata[key] = isPrerelease;
546-
}
547-
else if (key.Equals("NormalizedVersion"))
548-
{
549-
if (!NuGetVersion.TryParse(value, out NuGetVersion parsedNormalizedVersion))
550-
{
551-
errorMsg = string.Format(
552-
CultureInfo.InvariantCulture,
553-
@"TryReadPSGetInfo: Cannot parse NormalizedVersion");
556+
parsedNormalizedVersion = new NuGetVersion("1.0.0.0");
557+
}
554558

555-
parsedNormalizedVersion = new NuGetVersion("1.0.0.0");
559+
metadata[propertyKey] = parsedNormalizedVersion;
560+
}
561+
else
562+
{
563+
metadata[propertyKey] = propertyValue;
564+
}
556565
}
557-
558-
metadata[key] = parsedNormalizedVersion;
559-
}
560-
else
561-
{
562-
metadata[key] = value;
563566
}
564567
}
565568

src/code/V2ResponseUtil.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ public XmlNode[] ConvertResponseToXML(string httpResponse) {
7575
XmlDocument doc = new XmlDocument();
7676
doc.LoadXml(httpResponse);
7777

78-
XmlNodeList elemList = doc.GetElementsByTagName("m:properties");
79-
80-
XmlNode[] nodes = new XmlNode[elemList.Count];
81-
for (int i = 0; i < elemList.Count; i++)
78+
XmlNodeList entryNode = doc.GetElementsByTagName("entry");
79+
80+
XmlNode[] nodes = new XmlNode[entryNode.Count];
81+
for (int i = 0; i < entryNode.Count; i++)
8282
{
83-
nodes[i] = elemList[i];
83+
nodes[i] = entryNode[i];
8484
}
8585

8686
return nodes;

0 commit comments

Comments
 (0)