async Task <GithubRelease> GetGithubRelease(UpdaterComponent component) { using (var client = new HttpClient()) { #if DEBUG //https://github.com/settings/applications/new GET ONE HERE //MAKE SURE YOU DO NOT COMMIT THIS TOKEN IF YOU ADD IT! ONLY USE FOR DEVELOPMENT THEN REMOVE! //(bypasses retarded rate limit) string secret = string.Empty; //?client_id=CLIENT_ID_HERE&client_secret=CLIENT_SECRET_HERE" #else string secret = string.Empty; #endif //Github's API requires a user agent header, it'll 403 without it client.DefaultRequestHeaders.Add("User-Agent", "TeknoParrot"); var reponame = !string.IsNullOrEmpty(component.reponame) ? component.reponame : component.name; var url = $"https://api.github.com/repos/{(!string.IsNullOrEmpty(component.userName) ? component.userName : "******")}/{reponame}/releases/tags/{component.name}{secret}"; Debug.WriteLine($"Updater url for {component.name}: {url}"); var response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { var release = await response.Content.ReadAsAsync <GithubRelease>(); return(release); } return(null); } }
private async void CheckGithub(UpdaterComponent component) { try { var githubRelease = await GetGithubRelease(component); if (githubRelease != null && githubRelease.assets != null && githubRelease.assets.Count != 0) { var localVersionString = component.localVersion; var onlineVersionString = githubRelease.name; // fix for weird things like OpenParrotx64_1.0.0.30 if (onlineVersionString.Contains(component.name)) { onlineVersionString = onlineVersionString.Split('_')[1]; } bool needsUpdate = false; // component not installed. if (localVersionString == Properties.Resources.UpdaterNotInstalled) { needsUpdate = true; } else { switch (localVersionString) { // version number is weird / unable to be formatted case "unknown": Debug.WriteLine($"{component.name} version is weird! local: {localVersionString} | online: {onlineVersionString}"); needsUpdate = localVersionString != onlineVersionString; break; default: int localNumber = GetVersionNumber(localVersionString); int onlineNumber = GetVersionNumber(onlineVersionString); needsUpdate = localNumber < onlineNumber; break; } } Debug.WriteLine($"{component.name} - local: {localVersionString} | online: {onlineVersionString} | needs update? {needsUpdate}"); if (needsUpdate) { new GitHubUpdates(component, githubRelease, localVersionString, onlineVersionString).Show(); } } else { Debug.WriteLine($"release is null? component: {component.name}"); } } catch (Exception ex) { Debug.WriteLine(ex); } }
public GitHubUpdates(UpdaterComponent componentUpdated, GithubRelease latestRelease, string local, string online) { InitializeComponent(); _componentUpdated = componentUpdated; labelUpdated.Content = componentUpdated.name; labelVersion.Content = $"{(local != Properties.Resources.UpdaterNotInstalled ? $"{local} to " : "")}{online}"; _latestRelease = latestRelease; onlineVersion = online; }
async Task <GithubRelease> GetGithubRelease(UpdaterComponent component) { using (var client = new HttpClient()) { #if DEBUG //https://github.com/settings/applications/new GET ONE HERE //MAKE SURE YOU DO NOT COMMIT THIS TOKEN IF YOU ADD IT! ONLY USE FOR DEVELOPMENT THEN REMOVE! //(bypasses retarded rate limit) string secret = string.Empty; //?client_id=CLIENT_ID_HERE&client_secret=CLIENT_SECRET_HERE" #else string secret = string.Empty; #endif //Github's API requires a user agent header, it'll 403 without it client.DefaultRequestHeaders.Add("User-Agent", "TeknoParrot"); var reponame = !string.IsNullOrEmpty(component.reponame) ? component.reponame : component.name; var url = $"https://api.github.com/repos/{(!string.IsNullOrEmpty(component.userName) ? component.userName : "******")}/{reponame}/releases/tags/{component.name}{secret}"; Debug.WriteLine($"Updater url for {component.name}: {url}"); var response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { var release = await response.Content.ReadAsAsync <GithubRelease>(); return(release); } else { // Handle github exceptions nicely string message = "Unkown exception"; string mediaType = response.Content.Headers.ContentType.MediaType; string body = await response.Content.ReadAsStringAsync(); HttpStatusCode statusCode = response.StatusCode; if (statusCode == HttpStatusCode.NotFound) { message = "Not found!"; } else if (mediaType == "text/html") { message = body.Trim(); } else if (mediaType == "application/json") { var json = JObject.Parse(body); message = json["message"]?.ToString(); if (message.Contains("API rate limit exceeded")) { message = "Update limit exceeded, try again in an hour!"; } } throw new Exception(message); } } }
public GitHubUpdates(UpdaterComponent componentUpdated, GithubRelease latestRelease, string local, string online) { InitializeComponent(); _componentUpdated = componentUpdated; if (componentUpdated.name == "TeknoParrotUI") { labelUpdated.Content = componentUpdated.name + " (Requires App Restart)"; } else { labelUpdated.Content = componentUpdated.name; } labelVersion.Content = $"{(local != Properties.Resources.UpdaterNotInstalled ? $"{local} to " : "")}{online}"; _latestRelease = latestRelease; onlineVersion = online; }