Skip to content

Commit d84cb42

Browse files
authored
Fix cocoapods version enumeration (#439)
1 parent 26a8cf5 commit d84cb42

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/Shared/PackageManagers/CocoapodsProjectManager.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Microsoft.CST.OpenSource.PackageManagers
44
{
5+
using AngleSharp.Html.Dom;
56
using AngleSharp.Html.Parser;
67
using Extensions;
78
using Helpers;
9+
using Newtonsoft.Json.Linq;
810
using PackageUrl;
911
using System;
1012
using System.Collections.Generic;
@@ -158,17 +160,22 @@ public override async Task<IEnumerable<string>> EnumerateVersionsAsync(PackageUR
158160
string? html = await GetHttpStringCache(httpClient, $"{ENV_COCOAPODS_SPECS_ENDPOINT}/Specs/{prefix}/{packageName}");
159161
HtmlParser parser = new();
160162
AngleSharp.Html.Dom.IHtmlDocument document = await parser.ParseDocumentAsync(html);
161-
AngleSharp.Dom.IHtmlCollection<AngleSharp.Dom.IElement> navItems = document.QuerySelectorAll("div.Details a.js-navigation-open");
163+
// Fetch the embedded react data
164+
string innerHtml = document.QuerySelector("script[data-target='react-app.embeddedData']").InnerHtml;
165+
// The contents of the script tag are JSON, so parse the innerHtml as a JObject
166+
JObject embeddedData = JObject.Parse(innerHtml);
167+
// use JsonPath to select the version numbers as JValues
168+
var versions = embeddedData.SelectTokens("$.payload.tree.items[*].name");
162169
List<string> versionList = new();
163170

164-
foreach (AngleSharp.Dom.IElement? navItem in navItems)
171+
// For each version add it to the list
172+
foreach (var version in versions)
165173
{
166-
if (string.IsNullOrWhiteSpace(Regex.Replace(navItem.TextContent, @"\s", "").Replace(".", "")))
174+
if (version is JValue jValue)
167175
{
168-
continue;
176+
Logger.Debug("Identified {0} version {1}.", packageName, jValue.Value);
177+
versionList.Add(jValue.Value.ToString());
169178
}
170-
Logger.Debug("Identified {0} version {1}.", packageName, navItem.TextContent);
171-
versionList.Add(navItem.TextContent);
172179
}
173180
return SortVersions(versionList.Distinct());
174181
}

src/oss-tests/DownloadTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public async Task Cargo_Download_Version_Succeeds(string purl, string targetFile
2727
}
2828

2929
[DataTestMethod]
30-
[Ignore("Skipping this test due to https://github.com/microsoft/OSSGadget/issues/437")]
3130
[DataRow("pkg:cocoapods/RandomKit", "RandomKit.podspec", 1)]
3231
public async Task Cocoapods_Download_Version_Succeeds(string purl, string targetFilename, int expectedDirectoryCount)
3332
{

0 commit comments

Comments
 (0)