Skip to content

Commit

Permalink
Fix cocoapods version enumeration (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfs authored Jul 8, 2023
1 parent 26a8cf5 commit d84cb42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/Shared/PackageManagers/CocoapodsProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

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

foreach (AngleSharp.Dom.IElement? navItem in navItems)
// For each version add it to the list
foreach (var version in versions)
{
if (string.IsNullOrWhiteSpace(Regex.Replace(navItem.TextContent, @"\s", "").Replace(".", "")))
if (version is JValue jValue)
{
continue;
Logger.Debug("Identified {0} version {1}.", packageName, jValue.Value);
versionList.Add(jValue.Value.ToString());

Check warning on line 177 in src/Shared/PackageManagers/CocoapodsProjectManager.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

Dereference of a possibly null reference.

Check warning on line 177 in src/Shared/PackageManagers/CocoapodsProjectManager.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

Possible null reference argument for parameter 'item' in 'void List<string>.Add(string item)'.
}
Logger.Debug("Identified {0} version {1}.", packageName, navItem.TextContent);
versionList.Add(navItem.TextContent);
}
return SortVersions(versionList.Distinct());
}
Expand Down
1 change: 0 additions & 1 deletion src/oss-tests/DownloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public async Task Cargo_Download_Version_Succeeds(string purl, string targetFile
}

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

0 comments on commit d84cb42

Please sign in to comment.