Skip to content

Commit

Permalink
Fixes KSP-CKAN#22: Add an asset_match regular expression to Github as…
Browse files Browse the repository at this point in the history
…sets
  • Loading branch information
hakan42 committed Jan 8, 2015
1 parent f4d93af commit 7435ad9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
35 changes: 31 additions & 4 deletions Github/GithubAPI.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using log4net;
using Newtonsoft.Json.Linq;

Expand All @@ -11,7 +12,7 @@ namespace CKAN.NetKAN
{
public static class GithubAPI
{

private static readonly string asset_match = "/asset_match/";
private static readonly Uri api_base = new Uri("https://api.github.com/");
private static readonly ILog log = LogManager.GetLogger(typeof (KSAPI));
private static readonly WebClient web = new WebClient();
Expand Down Expand Up @@ -60,6 +61,15 @@ public static string Call(string path)
/// </summary>
public static GithubRelease GetLatestRelease( string repository, bool prerelease )
{
string assetFilter = ".zip";

int asset_match_index = repository.IndexOf (asset_match);
if (asset_match_index > -1) {
assetFilter = repository.Substring (asset_match_index + asset_match.Length);
repository = repository.Substring (0, asset_match_index);
log.DebugFormat ("Asset Filter: '{0}'", assetFilter);
}

string json = Call ("repos/" + repository + "/releases");
log.Debug("Parsing JSON...");
JArray releases = JArray.Parse(json);
Expand All @@ -68,12 +78,29 @@ public static GithubRelease GetLatestRelease( string repository, bool prerelease

// Finding the most recent *stable* release means filtering
// out on pre-releases.
GithubRelease result = null;

var final_releases = releases.Where(x => (bool) x["prerelease"] == prerelease);
foreach (JObject release in releases)
{
// First, check for prerelease status...
if (prerelease == (bool)release ["prerelease"])
{
JArray assets = (JArray) release ["assets"];
foreach (JObject asset in assets)
{
// Then, check against the regex, which might default to ".zip"
if (Regex.IsMatch ((string) asset ["name"], assetFilter, RegexOptions.IgnoreCase))
{
log.DebugFormat ("Hit on {0}", asset.ToString ());
result = new GithubRelease (release, asset);
return result;
}
}
}
}

return !final_releases.Any() ? null : new GithubRelease(final_releases.Cast<JObject>().First());
return result;
}

}
}

21 changes: 10 additions & 11 deletions Github/GithubRelease.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,28 @@ public class GithubRelease : CkanInflator

private static readonly ILog log = LogManager.GetLogger(typeof (GithubRelease));

public GithubRelease (JObject parsed_json)
public GithubRelease (JObject parsed_json, JObject asset)
{
version = new Version( parsed_json["tag_name"].ToString() );
author = parsed_json["author"]["login"].ToString();

// GH #290, we need to look for the first asset which is a zip, otherwise we
// end up picking up manuals, pictures of cats, and all sorts of other things.

JToken asset = parsed_json["assets"].Children().FirstOrDefault(
asset_info => asset_info["content_type"].ToString() == "application/x-zip-compressed" ||
asset_info["content_type"].ToString() == "application/zip" ||
asset_info["name"].ToString().EndsWith(".zip", StringComparison.OrdinalIgnoreCase));
if (("application/x-zip-compressed".Equals (asset ["content_type"])) ||
("application/zip".Equals (asset ["content_type"])) ||
(asset ["name"].ToString ().EndsWith (".zip", StringComparison.OrdinalIgnoreCase)))
{
size = (long) asset["size"];
download = new Uri( asset["browser_download_url"].ToString() );

if (asset == null)
log.DebugFormat("Download {0} is {1} bytes", download, size);
}
else
{
// TODO: A proper kraken, please!
throw new Kraken("Cannot find download");
}

size = (long) asset["size"];
download = new Uri( asset["browser_download_url"].ToString() );

log.DebugFormat("Download {0} is {1} bytes", download, size);
}

override public void InflateMetadata(JObject metadata, string filename, object context)
Expand Down

0 comments on commit 7435ad9

Please sign in to comment.