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

Sort GitHub releases #3571

Merged
merged 1 commit into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
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
68 changes: 20 additions & 48 deletions Netkan/Sources/Github/GithubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;

using log4net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using CKAN.NetKAN.Services;
using CKAN.Versioning;

Expand Down Expand Up @@ -67,58 +69,28 @@ public IEnumerable<GithubRelease> GetAllReleases(GithubRef reference)
{
var json = Call($"repos/{reference.Repository}/releases?per_page={perPage}&page={page}");
Log.Debug("Parsing JSON...");
var releases = JArray.Parse(json);

// Finding the most recent *stable* release means filtering
// out on pre-releases.

foreach (var release in releases)
{
// First, check for prerelease status...
if (reference.UsePrerelease == (bool)release["prerelease"])
{
var version = new ModuleVersion((string)release["tag_name"]);
var author = (string)release["author"]["login"];

List<GithubReleaseAsset> allAssets;

if (reference.UseSourceArchive)
{
Log.Debug("Using GitHub source archive");
Uri download = new Uri((string)release["zipball_url"]);
DateTime? updated = (DateTime)release["published_at"];
allAssets = new List<GithubReleaseAsset> { new GithubReleaseAsset(version.ToString(), download, updated) };
}
else
{
allAssets = new List<GithubReleaseAsset>();
var assets = (JArray)release["assets"];

foreach (var asset in assets.Where(asset => reference.Filter.IsMatch((string)asset["name"])))
{
Log.DebugFormat("Using GitHub asset: {0}", asset["name"]);
Uri download = new Uri((string)asset["browser_download_url"]);
DateTime? updated = (DateTime)asset["updated_at"];

allAssets.Add(new GithubReleaseAsset(
(string)asset["name"], download, updated
)
);
}
}

if (allAssets.Any())
{
yield return new GithubRelease(author, version, allAssets);
}
}
}

if (releases.Count < perPage)
var jsonReleases = JArray.Parse(json);
if (jsonReleases.Count < 1)
{
// That's all folks!
break;
}
var ghReleases = jsonReleases
.Select(rel => new GithubRelease(reference, rel))
.Where(ghRel =>
// Finding the most recent *stable* release means filtering
// out on pre-releases.
ghRel.PreRelease == reference.UsePrerelease
// Skip releases without assets
&& ghRel.Assets.Any())
// Insurance against GitHub returning them in the wrong order
.OrderByDescending(ghRel => ghRel.PublishedAt)
.ToList();

foreach (var ghRel in ghReleases)
{
yield return ghRel;
}
}
}

Expand Down
42 changes: 35 additions & 7 deletions Netkan/Sources/Github/GithubRelease.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
using System;
using System.Linq;
using System.Collections.Generic;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using CKAN.Versioning;

namespace CKAN.NetKAN.Sources.Github
{
public sealed class GithubRelease
internal sealed class GithubRelease
{
public string Author { get; }
public ModuleVersion Tag { get; }
public List<GithubReleaseAsset> Assets { get; }
public readonly string Author;
public readonly ModuleVersion Tag;
public readonly List<GithubReleaseAsset> Assets;
public readonly bool PreRelease;
public readonly DateTime? PublishedAt;

public GithubRelease(GithubRef reference, JToken json)
{
PreRelease = (bool)json["prerelease"];
Tag = new ModuleVersion((string)json["tag_name"]);
Author = (string)json["author"]["login"];
PublishedAt = (DateTime?)json["published_at"];
Assets = reference.UseSourceArchive
? new List<GithubReleaseAsset> {
new GithubReleaseAsset(
Tag.ToString(),
new Uri((string)json["zipball_url"]),
PublishedAt)
} : ((JArray)json["assets"])
.Where(asset => reference.Filter.IsMatch((string)asset["name"]))
.Select(asset => new GithubReleaseAsset(
(string)asset["name"],
new Uri((string)asset["browser_download_url"]),
(DateTime?)asset["updated_at"]))
.ToList();
}

public GithubRelease(string author, ModuleVersion tag, List<GithubReleaseAsset> assets)
{
Author = author;
Tag = tag;
Assets = assets;
Author = author;
Tag = tag;
Assets = assets;
}
}
}