From c4c62c2f169d6bbb4c4229d10acfd0a8d283c7f4 Mon Sep 17 00:00:00 2001 From: Matt Pannella Date: Fri, 14 Oct 2022 00:23:18 -0400 Subject: [PATCH 1/2] use release info from cores api, to limit the need to hit github's api switched to v1 api end point --- Updater.cs | 35 +++++++++++++++++++++++++++------ models/Asset.cs | 9 +++++++++ models/Core.cs | 2 ++ models/Release.cs | 8 ++++++++ models/Settings/CoreSettings.cs | 2 +- services/CoresService.cs | 2 +- services/GithubApiService.cs | 30 ++++++++++++++++++++++------ 7 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 models/Asset.cs create mode 100644 models/Release.cs diff --git a/Updater.cs b/Updater.cs index 7e9d49ef..8aed2673 100644 --- a/Updater.cs +++ b/Updater.cs @@ -148,17 +148,24 @@ public async Task RunUpdates() Repo? repo = core.repository; _writeMessage("Checking Core: " + name); bool allowPrerelease = _settingsManager.GetCoreSettings(core.identifier).allowPrerelease; - List? releases = await _fetchReleases(repo.owner, repo.name, _githubApiKey); - if(releases == null) { - continue; + + var mostRecentRelease = core.release; + var prerelease = core.prerelease; + if(allowPrerelease && mostRecentRelease == null) { + mostRecentRelease = prerelease; + } + else if(allowPrerelease && prerelease != null) { + string semver1 = SemverUtil.FindSemver(mostRecentRelease.tag_name); + string semver2 = SemverUtil.FindSemver(prerelease.tag_name); + if(SemverUtil.SemverCompare(semver2, semver1)) { + mostRecentRelease = prerelease; + } } - var mostRecentRelease = _getMostRecentRelease(releases, allowPrerelease); if(mostRecentRelease == null) { _writeMessage("No releases found. Skipping"); continue; } string tag_name = mostRecentRelease.tag_name; - List assets = mostRecentRelease.assets; string releaseSemver = SemverUtil.FindSemver(tag_name); @@ -205,6 +212,8 @@ public async Task RunUpdates() // might need to search for the right zip here if there's more than one //iterate through assets to find the zip release + var release = await _fetchRelease(repo.owner, repo.name, tag_name, _githubApiKey); + List assets = release.assets; foreach(Github.Asset asset in assets) { if(!ZIP_TYPES.Contains(asset.content_type)) { //not a zip file. move on @@ -305,7 +314,9 @@ private void Divide() private string BuildAssetUrl(DependencyFile asset) { string archive = _settingsManager.GetConfig().archive_name; - if(archive != null && asset.archive_zip != null) { + if(asset.file_name != null && asset.archive_zip == null && asset.archive_file == null && !asset.zip) { + return ARCHIVE_BASE_URL + "/" + archive + "/" + asset.file_name; + } else if(archive != null && asset.archive_zip != null) { return ARCHIVE_BASE_URL + "/" + archive + "/" + asset.archive_zip + ".zip/" + asset.file_name; } else if(archive != null && asset.archive_file != null) { return ARCHIVE_BASE_URL + "/" + archive + "/" + asset.archive_file; @@ -339,6 +350,18 @@ private Github.Release _getMostRecentRelease(List releases, bool } } + private async Task? _fetchRelease(string user, string repository, string tag_name, string token = "") + { + try { + var release = await GithubApi.GetRelease(user, repository, tag_name, token); + return release; + } catch (HttpRequestException e) { + _writeMessage("Error communicating with Github API."); + _writeMessage(e.Message); + return null; + } + } + private async Task _getAsset(string downloadLink, string coreName) { bool updated = false; diff --git a/models/Asset.cs b/models/Asset.cs new file mode 100644 index 00000000..e37fdd6c --- /dev/null +++ b/models/Asset.cs @@ -0,0 +1,9 @@ +namespace pannella.analoguepocket; + +public class Asset +{ + public string platform { get; set; } + public string filename { get; set; } + public bool core_specific { get; set; } + public List extensions { get; set; } +} \ No newline at end of file diff --git a/models/Core.cs b/models/Core.cs index 697ef2b4..715e9c83 100644 --- a/models/Core.cs +++ b/models/Core.cs @@ -5,6 +5,8 @@ public class Core public string? identifier { get; set; } public Repo? repository { get; set; } public string? platform { get; set; } + public Release release { get; set; } + public Release? prerelease { get; set; } public override string ToString() { diff --git a/models/Release.cs b/models/Release.cs new file mode 100644 index 00000000..c16937ae --- /dev/null +++ b/models/Release.cs @@ -0,0 +1,8 @@ +namespace pannella.analoguepocket; + +public class Release +{ + public string tag_name { get; set; } + public string release_date { get; set; } + public List assets { get; set; } +} \ No newline at end of file diff --git a/models/Settings/CoreSettings.cs b/models/Settings/CoreSettings.cs index d899ac10..1bfd273c 100644 --- a/models/Settings/CoreSettings.cs +++ b/models/Settings/CoreSettings.cs @@ -8,6 +8,6 @@ public class CoreSettings public CoreSettings() { skip = false; - allowPrerelease = false; + allowPrerelease = true; } } \ No newline at end of file diff --git a/services/CoresService.cs b/services/CoresService.cs index f98bfc00..e699f474 100644 --- a/services/CoresService.cs +++ b/services/CoresService.cs @@ -4,7 +4,7 @@ namespace pannella.analoguepocket; public static class CoresService { - private const string END_POINT = "https://joshcampbell191.github.io/openfpga-cores-inventory/api/v0/analogue-pocket/cores.json"; + private const string END_POINT = "https://joshcampbell191.github.io/openfpga-cores-inventory/api/v1/analogue-pocket/cores.json"; public static async Task> GetCores() { diff --git a/services/GithubApiService.cs b/services/GithubApiService.cs index 81d73d79..f1a76107 100644 --- a/services/GithubApiService.cs +++ b/services/GithubApiService.cs @@ -10,6 +10,29 @@ public static class GithubApi public static async Task> GetReleases(string user, string repository, string? token = "") { string url = String.Format(END_POINT, user, repository); + var responseBody = await CallAPI(url, token); + + List? releases = JsonSerializer.Deserialize>(responseBody); + + if(releases == null) { + releases = new List(); + } + + return releases; + } + + public static async Task GetRelease(string user, string repository, string tag_name, string? token = "") + { + string url = String.Format(END_POINT, user, repository) + "/tags/" + tag_name; + + var responseBody = await CallAPI(url, token); + Github.Release? release = JsonSerializer.Deserialize(responseBody); + + return release; + } + + private static async Task CallAPI(string url, string? token = "") + { var client = new HttpClient(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var request = new HttpRequestMessage @@ -26,12 +49,7 @@ public static class GithubApi response.EnsureSuccessStatusCode(); var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false); - List? releases = JsonSerializer.Deserialize>(responseBody); - if(releases == null) { - releases = new List(); - } - - return releases; + return responseBody; } } \ No newline at end of file From 136eb3a19894fc354194bfc3fbcc47f0f82ddea5 Mon Sep 17 00:00:00 2001 From: Matt Pannella Date: Fri, 14 Oct 2022 07:58:17 -0400 Subject: [PATCH 2/2] version bump --- pocket_updater.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pocket_updater.csproj b/pocket_updater.csproj index 2c256f14..d698a8dc 100644 --- a/pocket_updater.csproj +++ b/pocket_updater.csproj @@ -6,7 +6,7 @@ net6.0 enable enable - 2.4.0 + 2.5.0 Keep your Analogue Pocket up to date 2022 Matt Pannella Matt Pannella