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

Provide fresh auto updater in releases #2212

Merged
merged 2 commits into from
Dec 22, 2017
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 32 additions & 8 deletions Core/Net/AutoUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ public class AutoUpdate

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

/// <summary>
/// The list of releases containing ckan.exe and AutoUpdater.exe
/// </summary>
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");
/// <summary>
/// Old release list that just contains the auto updater,
/// used as a fallback when missing from main release
/// </summary>
private static readonly Uri oldLatestUpdaterReleaseApiUrl = new Uri("https://api.github.com/repos/KSP-CKAN/CKAN-autoupdate/releases/latest");

private Tuple<Uri, long> fetchedUpdaterUrl;
private Tuple<Uri, long> fetchedCkanUrl;
Expand Down Expand Up @@ -80,8 +86,22 @@ public void FetchLatestReleaseInfo()

try
{
fetchedUpdaterUrl = RetrieveUrl(MakeRequest(latestUpdaterReleaseApiUrl));
fetchedCkanUrl = RetrieveUrl(response);
fetchedCkanUrl = RetrieveUrl(response, 0);
// Check whether the release includes the auto updater
foreach (var asset in response.assets)
{
string url = asset.browser_download_url.ToString();
if (url.EndsWith("AutoUpdater.exe"))
{
fetchedUpdaterUrl = new Tuple<Uri, long>(new Uri(url), (long)asset.size);
break;
}
}
if (fetchedUpdaterUrl == null)
{
// Older releases don't include the auto updater
fetchedUpdaterUrl = RetrieveUrl(MakeRequest(oldLatestUpdaterReleaseApiUrl), 0);
}
}
catch (Kraken)
{
Expand Down Expand Up @@ -148,15 +168,19 @@ public void StartUpdateProcess(bool launchCKANAfterUpdate, IUser user = null)
/// from the provided github API response
/// </summary>
/// <returns>The URL to the downloadable asset.</returns>
internal Tuple<Uri, long> RetrieveUrl(dynamic response)
internal Tuple<Uri, long> 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<Uri, long>(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<Uri, long>(new Uri(url), (long)asset.size);
}

/// <summary>
Expand Down
11 changes: 5 additions & 6 deletions Tests/Core/Net/AutoUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public class AutoUpdate
public void FetchCkanUrl()
{
Assert.Throws<CKAN.Kraken>(delegate
{
Fetch(test_ckan_release);
}
);
{
Fetch(test_ckan_release, 0);
});
}

[Test]
Expand Down Expand Up @@ -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);
}
}
}