Exemple #1
0
        public async Task<JarvisUpdateInfo> CheckForUpdates()
        {
            try
            {
                _log.Verbose("Getting releases from GitHub.");
                var uri = new Uri("https://api.github.com/repos/spectresystems/jarvis/releases");
                var response = await _client.GetAsync(uri);
                if (!response.IsSuccessStatusCode)
                {
                    _log.Warning($"GitHub returned status code {response.StatusCode} ({(int)response.StatusCode}) when checking for updates.");
                    return null;
                }

                var json = await response.Content.ReadAsStringAsync();

                // Get the last release.
                var result = JsonConvert.DeserializeObject<List<GitHubRelease>>(json)
                    .Where(x => !x.Draft);

                // Should we not include previews?
                if (!_settings.Get<bool>(Constants.Settings.General.IncludePreviews))
                {
                    result = result.Where(x => !x.Prerelease);
                }

                // Return the latest version.
                return result.OrderByDescending(x => x.PublishedAt)
                    .FirstOrDefault()?.ToJarvisUpdateInfo(_currentVersion);
            }
            catch (Exception ex)
            {
                _log.Error(ex, "An exception occured while checking for updates at GitHub.");
                return null;
            }
        }