Skip to content

Commit f68f3d7

Browse files
Adds update notifications. Closes #24 (#25)
1 parent d9e1044 commit f68f3d7

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

msgraph-developer-proxy/ProxyCommandHandler.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public async Task<int> InvokeAsync(InvocationContext context) {
3737
Configuration.AllowedErrors = allowedErrors;
3838
if (cloud is not null)
3939
Configuration.Cloud = cloud;
40+
41+
var newReleaseInfo = await UpdateNotification.CheckForNewVersion();
42+
if (newReleaseInfo != null) {
43+
var originalColor = Console.ForegroundColor;
44+
Console.ForegroundColor = ConsoleColor.Green;
45+
Console.Error.WriteLine($"New version {newReleaseInfo.Version} of the Graph Developer Proxy is available.");
46+
Console.Error.WriteLine($"See {newReleaseInfo.Url} for more information.");
47+
Console.Error.WriteLine();
48+
Console.ForegroundColor = originalColor;
49+
}
4050

4151
try {
4252
await new ChaosEngine(Configuration).Run(cancellationToken);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)