|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Net.Http.Headers; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Text.Json.Serialization; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Microsoft.Graph.DeveloperProxy { |
| 13 | + internal class ReleaseInfo { |
| 14 | + [JsonPropertyName("name")] |
| 15 | + public string? Version { get; set; } |
| 16 | + [JsonPropertyName("html_url")] |
| 17 | + public string? Url { get; set; } |
| 18 | + |
| 19 | + public ReleaseInfo() { |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + internal static class UpdateNotification { |
| 24 | + private static readonly string releasesUrl = "https://api.github.com/repos/microsoftgraph/msgraph-developer-proxy/releases"; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Checks if a new version of the proxy is available. |
| 28 | + /// </summary> |
| 29 | + /// <returns>Instance of ReleaseInfo if a new version is available and null if the current version is the latest</returns> |
| 30 | + public static async Task<ReleaseInfo?> CheckForNewVersion() { |
| 31 | + try { |
| 32 | + var latestRelease = await GetLatestRelease(); |
| 33 | + if (latestRelease == null || latestRelease.Version == null) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + var latestReleaseVersion = new Version(latestRelease.Version.Substring(1)); // remove leading v |
| 38 | + var currentVersion = GetCurrentVersion(); |
| 39 | + |
| 40 | + if (latestReleaseVersion > currentVersion) { |
| 41 | + return latestRelease; |
| 42 | + } |
| 43 | + else { |
| 44 | + return null; |
| 45 | + } |
| 46 | + } |
| 47 | + catch { |
| 48 | + return null; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static Version GetCurrentVersion() { |
| 53 | + var assembly = Assembly.GetExecutingAssembly(); |
| 54 | + var fvi = FileVersionInfo.GetVersionInfo(assembly.Location); |
| 55 | + var currentVersion = new Version(fvi.ProductVersion); |
| 56 | + |
| 57 | + return currentVersion; |
| 58 | + } |
| 59 | + |
| 60 | + private static async Task<ReleaseInfo?> GetLatestRelease() { |
| 61 | + var http = new HttpClient(); |
| 62 | + // GitHub API requires user agent to be set |
| 63 | + http.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("msgraph-developer-proxy", "1.0")); |
| 64 | + var response = await http.GetStringAsync(releasesUrl); |
| 65 | + var releases = JsonSerializer.Deserialize<ReleaseInfo[]>(response); |
| 66 | + |
| 67 | + if (releases == null) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + // we assume releases are sorted descending by their creation date |
| 72 | + foreach (var release in releases) { |
| 73 | + // skip preview releases |
| 74 | + if (release.Version == null || |
| 75 | + release.Version.Contains("-")) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + return release; |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments