Skip to content

Commit

Permalink
Original author order for SpaceDock
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Sep 3, 2024
1 parent 2ddf740 commit c3aff29
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
10 changes: 4 additions & 6 deletions Netkan/Extensions/JObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ public static void SafeMerge(this JObject jobject, string propertyName, JToken?
}
}

public static JToken? FromMessyList<T>(T? first, IEnumerable<T?>? rest) where T: notnull
public static JToken? ToJValueOrJArray<T>(this IEnumerable<T?> source) where T: notnull
{
var items = (rest ?? Enumerable.Empty<T?>())
.Append(first)
.OfType<T>()
.Distinct()
.ToList();
var items = source.OfType<T>()
.Distinct()
.ToList();
return items.Count switch
{
0 => null,
Expand Down
31 changes: 16 additions & 15 deletions Netkan/Transformers/GithubTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,22 @@ public static void SetRepoResources(GithubRepo repo, JObject resources)
}
}

private JToken? GetAuthors(GithubRepo repo, GithubRelease release)
=> JObjectExtensions.FromMessyList(
release.Author,
repo.TraverseNodes(r => r.ParentRepo == null
? null
: _api.GetRepo(new GithubRef($"#/ckan/github/{r.ParentRepo.FullName}",
false, _matchPreleases)))
.Reverse()
.SelectMany(r => r.Owner?.Type switch
{
userType => Enumerable.Repeat(r.Owner.Login, 1),
orgType => _api.getOrgMembers(r.Owner)
.Select(u => u.Login),
_ => Enumerable.Empty<string>()
}));
private JToken? GetAuthors(GithubRepo repo,
GithubRelease release)
=> repo.TraverseNodes(r => r.ParentRepo == null
? null
: _api.GetRepo(new GithubRef($"#/ckan/github/{r.ParentRepo.FullName}",
false, _matchPreleases)))
.Reverse()
.SelectMany(r => r.Owner?.Type switch
{
userType => Enumerable.Repeat(r.Owner.Login, 1),
orgType => _api.getOrgMembers(r.Owner)
.Select(u => u.Login),
_ => Enumerable.Empty<string>()
})
.Append(release.Author)
.ToJValueOrJArray();

private const string userType = "User";
private const string orgType = "Organization";
Expand Down
6 changes: 4 additions & 2 deletions Netkan/Transformers/SpacedockTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ private static void TryAddResourceURL(string identifier, JObject? resources, str
}

private static JToken? GetAuthors(SpacedockMod mod)
=> JObjectExtensions.FromMessyList(mod.author,
mod.shared_authors?.Select(i => i.Username));
=> Enumerable.Repeat(mod.author, 1)
.Concat(mod.shared_authors?.Select(i => i.Username)
?? Enumerable.Empty<string>())
.ToJValueOrJArray();

private static readonly Regex githubUrlPathPattern =
new Regex("^/(?<owner>[^/]+)/(?<repo>[^/]+)",
Expand Down

0 comments on commit c3aff29

Please sign in to comment.