From 813fffe7553dea9841608c56722fe0a6c82ab2a7 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Mon, 31 May 2021 11:15:51 -0500 Subject: [PATCH] Fill more info from GitHub for SpaceDock mods --- Netkan/Transformers/SpacedockTransformer.cs | 76 ++++++++++++--------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/Netkan/Transformers/SpacedockTransformer.cs b/Netkan/Transformers/SpacedockTransformer.cs index 05ff3bf37f..89c0013299 100644 --- a/Netkan/Transformers/SpacedockTransformer.cs +++ b/Netkan/Transformers/SpacedockTransformer.cs @@ -127,7 +127,6 @@ private Metadata TransformOne(Metadata metadata, JObject json, SpacedockMod sdMo TryAddResourceURL(metadata.Identifier, resourcesJson, "homepage", sdMod.website); TryAddResourceURL(metadata.Identifier, resourcesJson, "repository", sdMod.source_code); - TryAddResourceURL(metadata.Identifier, resourcesJson, "bugtracker", getBugtracker(sdMod.source_code)); resourcesJson.SafeAdd("spacedock", sdMod.GetPageUrl().OriginalString); if (sdMod.background != null) @@ -135,6 +134,49 @@ private Metadata TransformOne(Metadata metadata, JObject json, SpacedockMod sdMo TryAddResourceURL(metadata.Identifier, resourcesJson, "x_screenshot", sdMod.background.ToString()); } + if (!string.IsNullOrEmpty(sdMod.source_code)) + { + try + { + var uri = new Uri(sdMod.source_code); + if (uri.Host == "github.com") + { + var match = githubUrlPathPattern.Match(uri.AbsolutePath); + if (match.Success) + { + var owner = match.Groups["owner"].Value; + var repo = match.Groups["repo"].Value; + var repoInfo = _githubApi.GetRepo(new GithubRef( + $"#/ckan/github/{owner}/{repo}", false, false + )); + + if (sdMod.source_code != repoInfo.HtmlUrl) + { + // Overwrite resources.repository with GitHub API's report of the true URL, + // in case it has been moved or renamed + resourcesJson.Remove("repository"); + TryAddResourceURL(metadata.Identifier, resourcesJson, "repository", repoInfo.HtmlUrl); + } + // Fall back to homepage from GitHub + TryAddResourceURL(metadata.Identifier, resourcesJson, "homepage", repoInfo.Homepage); + if (repoInfo.HasIssues) + { + // Set bugtracker if repo has issues list + TryAddResourceURL(metadata.Identifier, resourcesJson, "bugtracker", $"{repoInfo.HtmlUrl}/issues"); + } + if (repoInfo.Archived) + { + Log.Warn("Repo is archived, consider freezing"); + } + } + } + } + catch + { + // Just give up, it's fine + } + } + Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); return new Metadata(json); @@ -206,38 +248,6 @@ private static List GetAuthors(SpacedockMod mod) return result; } - private string getBugtracker(string repoUrl) - { - if (!string.IsNullOrEmpty(repoUrl)) - { - try - { - var uri = new Uri(repoUrl); - if (uri.Host == "github.com") - { - var match = githubUrlPathPattern.Match(uri.AbsolutePath); - if (match.Success) - { - var owner = match.Groups["owner"].Value; - var repo = match.Groups["repo"].Value; - var repoInfo = _githubApi.GetRepo(new GithubRef( - $"#/ckan/github/{owner}/{repo}", false, false - )); - if (repoInfo.HasIssues) - { - return $"{repoInfo.HtmlUrl}/issues"; - } - } - } - } - catch - { - // Just give up, it's fine - } - } - return null; - } - private static readonly Regex githubUrlPathPattern = new Regex( "^/(?[^/]+)/(?[^/]+)", RegexOptions.Compiled