-
Notifications
You must be signed in to change notification settings - Fork 259
Adding version check to startup #2567
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
d273971
a458873
7ed7a11
5635d3f
0f57e40
9448942
66e37ca
da3d997
5b0736c
358b111
52e6f2a
141a0f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Net.Http.Json; | ||
using System.Reflection; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Azure.DataApiBuilder.Core; | ||
|
||
public static class VersionChecker | ||
{ | ||
public static void GetVersions(out string? latestVersion, out string? currentVersion) | ||
{ | ||
latestVersion = FetchLatestNuGetVersion(); | ||
currentVersion = GetCurrentVersionFromAssembly(Assembly.GetCallingAssembly()); | ||
} | ||
|
||
private static string? FetchLatestNuGetVersion() | ||
{ | ||
try | ||
{ | ||
using HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(2) }; | ||
NuGetVersionResponse? versionData = httpClient | ||
.GetFromJsonAsync<NuGetVersionResponse>("https://api.nuget.org/v3-flatcontainer/microsoft.dataapibuilder/index.json") | ||
JerryNixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.GetAwaiter().GetResult(); | ||
|
||
return versionData?.Versions | ||
?.Where(version => !version.Contains("-rc")) // Filter out pre-release versions | ||
.Select(version => new Version(version)) // Convert to Version objects | ||
.Max()?.ToString(); // Get the latest | ||
} | ||
catch (Exception) | ||
{ | ||
return null; // Assume no update available on failure | ||
} | ||
} | ||
|
||
private static string? GetCurrentVersionFromAssembly(Assembly assembly) | ||
{ | ||
string? version = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion; | ||
|
||
return version is { Length: > 0 } && version.Contains('+') | ||
JerryNixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? version[..version.IndexOf('+')] // Slice version string before '+' | ||
: version ?? assembly.GetName().Version?.ToString(); | ||
} | ||
|
||
private class NuGetVersionResponse | ||
{ | ||
[JsonPropertyName("versions")] | ||
public string[]? Versions { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.DataApiBuilder.Core; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Cli.Tests | ||
{ | ||
[TestClass] | ||
public class VersionCheckTests | ||
{ | ||
[TestMethod] | ||
public void GetVersions_LatestVersionNotNull() | ||
{ | ||
VersionChecker.GetVersions(out string latestVersion, out string _); | ||
Assert.IsNotNull(latestVersion, "Latest version should not be null."); | ||
} | ||
|
||
[TestMethod] | ||
public void GetVersions_CurrentVersionNotNull() | ||
{ | ||
VersionChecker.GetVersions(out string _, out string currentVersion); | ||
Assert.IsNotNull(currentVersion, "Current version should not be null."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Azure.DataApiBuilder.Config; | ||
using Azure.DataApiBuilder.Core; | ||
using Azure.DataApiBuilder.Service.Exceptions; | ||
using Microsoft.ApplicationInsights; | ||
using Microsoft.AspNetCore; | ||
|
@@ -27,6 +28,13 @@ public class Program | |
|
||
public static void Main(string[] args) | ||
{ | ||
// Compare current version of DAB with latest (non-rc) version in NuGet. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this change work for the docker scenario too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd need to test to be sure. My gut says no. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it fails to reach NuGet it does not emit a warning. |
||
VersionChecker.GetVersions(out string? latestVersion, out string? currentVersion); | ||
JerryNixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!string.IsNullOrEmpty(latestVersion) && latestVersion != currentVersion) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if people clone the repo, their currentVersion might be > latestVersion. Saying newer version is available doesn't make sense in that scenario, we should check for latestVersion > currentVersion |
||
{ | ||
Console.Error.WriteLine($"A newer version of Data API builder is available. {currentVersion} -> {latestVersion}"); | ||
JerryNixon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (!ValidateAspNetCoreUrls()) | ||
{ | ||
Console.Error.WriteLine("Invalid ASPNETCORE_URLS format. e.g.: ASPNETCORE_URLS=\"http://localhost:5000;https://localhost:5001\""); | ||
|
Uh oh!
There was an error while loading. Please reload this page.