Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved "GetPublishedAtUtcAsync" method efficiency #436

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Shared/PackageManagers/BaseProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public async Task<Dictionary<PackageURL, double>> IdentifySourceRepositoryAsync(
}

/// <inheritdoc />
public async Task<DateTime?> GetPublishedAtUtcAsync(PackageURL purl, bool useCache = true)
public virtual async Task<DateTime?> GetPublishedAtUtcAsync(PackageURL purl, bool useCache = true)
{
Check.NotNull(nameof(purl.Version), purl.Version);
DateTime? uploadTime = (await GetPackageMetadataAsync(purl, useCache))?.UploadTime?.ToUniversalTime();
Expand Down
17 changes: 17 additions & 0 deletions src/Shared/PackageManagers/NPMProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,23 @@ is JsonElement.ArrayEnumerator enumeratorElement &&
return metadata;
}

public override async Task<DateTime?> GetPublishedAtUtcAsync(PackageURL purl, bool useCache = true)
{
Check.NotNull(nameof(purl.Version), purl.Version);
HttpClient client = CreateHttpClient();
string? packageName = purl.HasNamespace() ? $"{purl.GetNamespaceFormatted()}/{purl.Name}" : purl.Name;
JsonDocument jsonDoc = await GetJsonCache(client, $"{ENV_NPM_API_ENDPOINT}/{packageName}", useCache);
if (jsonDoc.RootElement.TryGetProperty("time", out JsonElement time))
{
string? uploadTime = OssUtilities.GetJSONPropertyStringIfExists(time, purl.Version);
if (uploadTime != null)
{
return DateTime.Parse(uploadTime).ToUniversalTime();
}
}
return null;
pmalmsten marked this conversation as resolved.
Show resolved Hide resolved
}

public override JsonElement? GetVersionElement(JsonDocument? contentJSON, Version version)
{
if (contentJSON is null) { return null; }
Expand Down
23 changes: 23 additions & 0 deletions src/Shared/PackageManagers/PyPIProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,29 @@ public override async Task<IEnumerable<string>> EnumerateVersionsAsync(PackageUR
return metadata;
}

public override async Task<DateTime?> GetPublishedAtUtcAsync(PackageURL purl, bool useCache = true)
{
Check.NotNull(nameof(purl.Version), purl.Version);
HttpClient client = CreateHttpClient();

JsonDocument contentJSON = await GetJsonCache(client, $"{ENV_PYPI_ENDPOINT}/pypi/{purl.Name}/{purl.Version}/json");
JsonElement root = contentJSON.RootElement;

JsonElement.ArrayEnumerator? urlsArray = OssUtilities.GetJSONEnumerator(root.GetProperty("urls"));
DateTime? uploadTime = null;
if (urlsArray is not null)
{
foreach (JsonElement url in urlsArray.Value)
{
string? urlStr = OssUtilities.GetJSONPropertyStringIfExists(url, "url");
string? uploadTimeStr = OssUtilities.GetJSONPropertyStringIfExists(url, "upload_time");
uploadTime = uploadTimeStr != null ? DateTime.Parse(uploadTimeStr).ToUniversalTime() : null;
}
}
pmalmsten marked this conversation as resolved.
Show resolved Hide resolved

return uploadTime;
}

public override List<Version> GetVersions(JsonDocument? contentJSON)
{
List<Version> allVersions = new();
Expand Down
19 changes: 19 additions & 0 deletions src/oss-tests/ProjectManagerTests/PyPIProjectManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ public async Task PackageVersionExistsAsyncSucceeds(string purlString)
Assert.IsTrue(await _projectManager.PackageVersionExistsAsync(purl, useCache: false));
}

[DataTestMethod]
[DataRow("pkg:pypi/[email protected]", "2022-04-02T10:37:04")]
[DataRow("pkg:pypi/[email protected]", "2022-04-05T16:26:12")]
[DataRow("pkg:pypi/[email protected]", "2022-01-05T15:40:51")]
public async Task GetPublishedAtUtcSucceeds(string purlString, string? expectedTime = null)
{
PackageURL purl = new(purlString);
DateTime? time = await _projectManager.GetPublishedAtUtcAsync(purl, useCache: false);

if (expectedTime == null)
{
Assert.IsNull(time);
}
else
{
Assert.AreEqual(DateTime.Parse(expectedTime).ToUniversalTime(), time);
}
}

[DataTestMethod]
[DataRow("pkg:pypi/pandas", true)]
[DataRow("pkg:pypi/[email protected]", true)]
Expand Down