From c7b613cd5ba24a16a8074f7a797316f233cb0431 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Fri, 4 Oct 2024 01:06:27 -0300 Subject: [PATCH] Account for source repo name/ownership changes for nuget stats Packages may have specified a certain owner/repo information, which can later be changed on github. Repositories can be renamed, and ownership transferred, which would now leave an inconsistency in how we report since the current info on github wouldn't match a published package. We account for this by first resolving the full name of the owner/repo we find in package metadata, which would account for any renaming that happened. --- src/Commands/NuGetStatsCommand.cs | 6 ++++++ src/Core/GraphQueries.cs | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Commands/NuGetStatsCommand.cs b/src/Commands/NuGetStatsCommand.cs index de002ff..20b85f4 100644 --- a/src/Commands/NuGetStatsCommand.cs +++ b/src/Commands/NuGetStatsCommand.cs @@ -12,6 +12,7 @@ using NuGet.Packaging; using NuGet.Packaging.Core; using NuGet.Protocol.Core.Types; +using NuGet.Protocol.Providers; using NuGet.Versioning; using Polly; using Spectre.Console; @@ -386,10 +387,15 @@ await Parallel.ForEachAsync(tasks, paralell, async (source, cancellation) => if (ownerRepo != null) { + // Account for repo renames/ownership transfers + if (await graph.QueryAsync(GraphQueries.RepositoryFullName(ownerRepo)) is { } fullName) + ownerRepo = fullName; + // Check contributors only once per repo, since multiple packages can come out of the same repository if (!model.Repositories.ContainsKey(ownerRepo)) { var contribs = await graph.QueryAsync(GraphQueries.RepositoryContributors(ownerRepo)); + if (contribs?.Length == 0) { // Make sure we haven't exhausted the GH API rate limit diff --git a/src/Core/GraphQueries.cs b/src/Core/GraphQueries.cs index 94b6d27..9b9c842 100644 --- a/src/Core/GraphQueries.cs +++ b/src/Core/GraphQueries.cs @@ -886,6 +886,14 @@ ... on Organization { } }; + /// + /// Gets the current full name of the specified owner/repo (might have been renamed and/or moved to another owner). + /// + public static GraphQuery RepositoryFullName(string ownerRepo) => new($"/repos/{ownerRepo}", ".full_name") + { + IsLegacy = true, + }; + /// /// Gets rate limit information. ///