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

Fill more info from GitHub for SpaceDock mods #3390

Merged
merged 1 commit into from
Jun 12, 2021
Merged
Changes from all 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
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