-
Notifications
You must be signed in to change notification settings - Fork 235
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
Adding version check to startup #2567
base: main
Are you sure you want to change the base?
Changes from all 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,31 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Azure.DataApiBuilder.Core; | ||
|
||
namespace Cli.Tests; | ||
|
||
[TestClass] | ||
public class VersionCheckTests | ||
{ | ||
[TestMethod] | ||
public void GetVersions_LatestVersionNotNull() | ||
{ | ||
VersionChecker.IsCurrentVersion(out string? nugetVersion, out string? _); | ||
Assert.IsNotNull(nugetVersion, "Nuget version should not be null."); | ||
} | ||
|
||
[TestMethod] | ||
public void GetVersions_CurrentVersionNotNull() | ||
{ | ||
VersionChecker.IsCurrentVersion(out string? _, out string? localVersion); | ||
Assert.IsNotNull(localVersion, "Local version should not be null."); | ||
} | ||
|
||
[TestMethod] | ||
public void GetVersions_IsNotInNuGet() | ||
{ | ||
bool result = VersionChecker.IsCurrentVersion(out string? nugetVersion, out string? localVersion); | ||
Assert.IsFalse(result, $"Should not be in NuGet. {localVersion} -> {nugetVersion}"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Net.Http.Json; | ||
using System.Text.Json.Serialization; | ||
using Azure.DataApiBuilder.Product; | ||
|
||
namespace Azure.DataApiBuilder.Core; | ||
|
||
/// <summary> | ||
/// A class to test the local version of the engine against the NuGet version. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is used in startup to suggest upgrading the engine. | ||
/// </remarks> | ||
public static class VersionChecker | ||
{ | ||
private const string NUGETURL = "https://api.nuget.org/v3-flatcontainer/microsoft.dataapibuilder/index.json"; | ||
|
||
/// <summary> | ||
/// Checks if the current local version of the product matches the latest version available on NuGet. | ||
/// </summary> | ||
/// <param name="nugetVersion">Outputs the latest version available on NuGet.</param> | ||
/// <param name="localVersion">Outputs the current local version of the product.</param> | ||
/// <returns> | ||
/// Returns <c>true</c> if the local version matches the latest NuGet version or if the NuGet version is not available; | ||
/// otherwise, returns <c>false</c>. | ||
/// </returns> | ||
/// <remarks> | ||
// If the internet is unavailable or NuGet is down or the HTTP request fails for any reason | ||
/// (there is a 2 second Timeout on the request), then the NuGet version will be <c>null</c> and | ||
// this method will return <c>true</c>. This is mostly because this check is a user courtesy. | ||
// </remarks> | ||
public static bool IsCurrentVersion(out string? nugetVersion, out string? localVersion) | ||
{ | ||
nugetVersion = FetchLatestNuGetVersion(); | ||
localVersion = ProductInfo.GetProductVersion(false); | ||
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. nit: name the argument that is passed to GetProductVersion with value |
||
return string.IsNullOrEmpty(nugetVersion) || nugetVersion == localVersion; | ||
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 the localVersion is > than the nugetVersion? Could happen when people clone our repo and we are in the process of developing 1.5, But nuget version will be 1.4 in that timeframe. |
||
} | ||
|
||
private static string? FetchLatestNuGetVersion() | ||
{ | ||
try | ||
{ | ||
using HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(2) }; | ||
NuGetVersionResponse? versionData = httpClient | ||
.GetFromJsonAsync<NuGetVersionResponse>(new Uri(NUGETURL).ToString()) | ||
.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 class NuGetVersionResponse | ||
{ | ||
[JsonPropertyName("versions")] | ||
public string[]? Versions { get; set; } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.