From 19328d488acee74d26c2655ef0fc3a3d30291a2d Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sat, 9 Dec 2017 12:56:22 -0600 Subject: [PATCH 1/2] Provide fresh auto updater in releases - Add AutoUpdater.exe to release file list in travis-ci - Look for auto updater file in main release - Update tests now that releases may contain multiple files --- .travis.yml | 1 + Core/Net/AutoUpdate.cs | 33 +++++++++++++++++++++++++-------- Tests/Core/Net/AutoUpdate.cs | 11 +++++------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index dcc874f1fd..fc1f60b4be 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,6 +60,7 @@ deploy: - _build/repack/$BUILD_CONFIGURATION/ckan.exe - _build/osx/CKAN.dmg - _build/deb/ckan_*.deb + - _build/out/AutoUpdater/$BUILD_CONFIGURATION/bin/AutoUpdater.exe on: repo: KSP-CKAN/CKAN tags: true diff --git a/Core/Net/AutoUpdate.cs b/Core/Net/AutoUpdate.cs index f836b3c3d0..12c4ab1c94 100644 --- a/Core/Net/AutoUpdate.cs +++ b/Core/Net/AutoUpdate.cs @@ -20,10 +20,16 @@ public class AutoUpdate private static readonly ILog log = LogManager.GetLogger(typeof(AutoUpdate)); + /// + /// The list of releases containing ckan.exe and AutoUpdater.exe + /// private static readonly Uri latestCKANReleaseApiUrl = new Uri("https://api.github.com/repos/KSP-CKAN/CKAN/releases/latest"); - private static readonly Uri latestUpdaterReleaseApiUrl = new Uri( - "https://api.github.com/repos/KSP-CKAN/CKAN-autoupdate/releases/latest"); + /// + /// Old release list that just contains the auto updater, + /// used as a fallback when missing from main release + /// + private static readonly Uri oldLatestUpdaterReleaseApiUrl = new Uri("https://api.github.com/repos/KSP-CKAN/CKAN-autoupdate/releases/latest"); private Tuple fetchedUpdaterUrl; private Tuple fetchedCkanUrl; @@ -80,8 +86,15 @@ public void FetchLatestReleaseInfo() try { - fetchedUpdaterUrl = RetrieveUrl(MakeRequest(latestUpdaterReleaseApiUrl)); - fetchedCkanUrl = RetrieveUrl(response); + fetchedCkanUrl = RetrieveUrl(response, 0); + // Check whether the release includes the auto updater + if (response.assets.Count >= 4) { + // Last asset is AutoUpdater.exe + fetchedUpdaterUrl = RetrieveUrl(response, 3); + } else { + // Older releases don't include the auto updater + fetchedUpdaterUrl = RetrieveUrl(MakeRequest(oldLatestUpdaterReleaseApiUrl), 0); + } } catch (Kraken) { @@ -148,15 +161,19 @@ public void StartUpdateProcess(bool launchCKANAfterUpdate, IUser user = null) /// from the provided github API response /// /// The URL to the downloadable asset. - internal Tuple RetrieveUrl(dynamic response) + internal Tuple RetrieveUrl(dynamic response, int whichOne) { if (response.assets.Count == 0) { throw new Kraken("The latest release isn't uploaded yet."); } - var firstAsset = response.assets[0]; - string url = firstAsset.browser_download_url.ToString(); - return new Tuple(new Uri(url), (long)firstAsset.size); + else if (whichOne >= response.assets.Count) + { + throw new Kraken($"Asset index {whichOne} does not exist."); + } + var asset = response.assets[whichOne]; + string url = asset.browser_download_url.ToString(); + return new Tuple(new Uri(url), (long)asset.size); } /// diff --git a/Tests/Core/Net/AutoUpdate.cs b/Tests/Core/Net/AutoUpdate.cs index 3185f294d3..01f6eefd4a 100644 --- a/Tests/Core/Net/AutoUpdate.cs +++ b/Tests/Core/Net/AutoUpdate.cs @@ -18,10 +18,9 @@ public class AutoUpdate public void FetchCkanUrl() { Assert.Throws(delegate - { - Fetch(test_ckan_release); - } - ); + { + Fetch(test_ckan_release, 0); + }); } [Test] @@ -53,9 +52,9 @@ public void ExtractReleaseNotes(string body, string expected, string comment) ); } - private void Fetch(Uri url) + private void Fetch(Uri url, int whichOne) { - CKAN.AutoUpdate.Instance.RetrieveUrl(CKAN.AutoUpdate.Instance.MakeRequest(url)); + CKAN.AutoUpdate.Instance.RetrieveUrl(CKAN.AutoUpdate.Instance.MakeRequest(url), whichOne); } } } From 050acdb491f420fbfefb7b48834a1c53a4be1bf7 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Thu, 21 Dec 2017 17:39:16 -0600 Subject: [PATCH 2/2] Check download name instead of index --- Core/Net/AutoUpdate.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Core/Net/AutoUpdate.cs b/Core/Net/AutoUpdate.cs index 12c4ab1c94..c6d0e46344 100644 --- a/Core/Net/AutoUpdate.cs +++ b/Core/Net/AutoUpdate.cs @@ -88,10 +88,17 @@ public void FetchLatestReleaseInfo() { fetchedCkanUrl = RetrieveUrl(response, 0); // Check whether the release includes the auto updater - if (response.assets.Count >= 4) { - // Last asset is AutoUpdater.exe - fetchedUpdaterUrl = RetrieveUrl(response, 3); - } else { + foreach (var asset in response.assets) + { + string url = asset.browser_download_url.ToString(); + if (url.EndsWith("AutoUpdater.exe")) + { + fetchedUpdaterUrl = new Tuple(new Uri(url), (long)asset.size); + break; + } + } + if (fetchedUpdaterUrl == null) + { // Older releases don't include the auto updater fetchedUpdaterUrl = RetrieveUrl(MakeRequest(oldLatestUpdaterReleaseApiUrl), 0); }