Skip to content

Commit

Permalink
Merge #3390 Fill more info from GitHub for SpaceDock mods
Browse files Browse the repository at this point in the history
  • Loading branch information
DasSkelett committed Jun 12, 2021
2 parents 8f59934 + 813fffe commit da3ee06
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file.

- [Multiple] Cache permanent redirects (#3389 by: HebaruSan; reviewed: DasSkelett)
- [Multiple] Allow YAML for human-edited metadata (YAMLKAN) (#3367 by: HebaruSan; reviewed: DasSkelett)
- [Netkan] Fill more info from GitHub for SpaceDock mods (#3390 by: HebaruSan; reviewed: DasSkelett)

## v1.30.4 (Hubble)

Expand Down
76 changes: 43 additions & 33 deletions Netkan/Transformers/SpacedockTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,56 @@ 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)
{
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);
Expand Down Expand Up @@ -206,38 +248,6 @@ private static List<string> 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(
"^/(?<owner>[^/]+)/(?<repo>[^/]+)",
RegexOptions.Compiled
Expand Down

0 comments on commit da3ee06

Please sign in to comment.