From d273971f451561ffa0562220e1e61db284cbd18b Mon Sep 17 00:00:00 2001 From: Jerry Nixon <1749983+JerryNixon@users.noreply.github.com> Date: Thu, 13 Feb 2025 11:46:34 -0700 Subject: [PATCH 01/12] Adding version check to startup --- src/Product/VersionChecker.cs | 51 +++++++++++++++++++++++++++++++++++ src/Service/Program.cs | 7 +++++ 2 files changed, 58 insertions(+) create mode 100644 src/Product/VersionChecker.cs diff --git a/src/Product/VersionChecker.cs b/src/Product/VersionChecker.cs new file mode 100644 index 0000000000..5c926809ef --- /dev/null +++ b/src/Product/VersionChecker.cs @@ -0,0 +1,51 @@ +using System; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Reflection; +using System.Text.Json.Serialization; + +namespace Azure.DataApiBuilder.Product; + +public static class VersionChecker +{ + private const string PackageName = "Microsoft.DataApiBuilder"; + private const string NuGetApiUrl = "https://api.nuget.org/v3-flatcontainer/{0}/index.json"; + + public static (string? LatestVersion, string? CurrentVersion) GetVersions() + { + var latestVersion = FetchLatestNuGetVersion(); + var currentVersion = GetCurrentVersionFromAssembly(Assembly.GetExecutingAssembly()); + return (latestVersion, currentVersion); + } + + private static string? FetchLatestNuGetVersion() + { + try + { + using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) }; + var url = string.Format(NuGetApiUrl, PackageName.ToLower()); + var versionData = httpClient.GetFromJsonAsync(url).GetAwaiter().GetResult(); + + return versionData?.Versions + ?.Where(v => !v.Contains("-rc")) + .Max(); // Get the latest stable version + } + catch + { + return null; // Assume no update available on failure + } + } + + private static string? GetCurrentVersionFromAssembly(Assembly assembly) + { + var version = assembly.GetCustomAttribute()?.InformationalVersion; + return !string.IsNullOrEmpty(version) ? version.Split('+')[0] : assembly.GetName().Version?.ToString(); + } + + private class NuGetVersionResponse + { + [JsonPropertyName("versions")] + public string[]? Versions { get; set; } + } +} diff --git a/src/Service/Program.cs b/src/Service/Program.cs index a0d6fe01d4..d059acf367 100644 --- a/src/Service/Program.cs +++ b/src/Service/Program.cs @@ -27,6 +27,13 @@ public class Program public static void Main(string[] args) { + // Compare current version of DAB with latest (non-rc) version in NuGet. + var (latestVersion, currentVersion) = await Azure.DataApiBuilder.Product.VersionChecker.GetVersionsAsync(); + if (!string.IsNullOrEmpty(latestVersion) && latestVersion != currentVersion) + { + Console.Error.WriteLine("A newer version of Data API builder is available. {currentVersion} -> {latestVersion}"); + } + if (!ValidateAspNetCoreUrls()) { Console.Error.WriteLine("Invalid ASPNETCORE_URLS format. e.g.: ASPNETCORE_URLS=\"http://localhost:5000;https://localhost:5001\""); From a4588732e6c7c7434bfe8b88df46f3ac0a549b25 Mon Sep 17 00:00:00 2001 From: Jerry Nixon <1749983+JerryNixon@users.noreply.github.com> Date: Thu, 13 Feb 2025 11:59:34 -0700 Subject: [PATCH 02/12] Updated to not use vars --- src/Product/VersionChecker.cs | 21 +++++++++------------ src/Service/Program.cs | 4 ++-- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Product/VersionChecker.cs b/src/Product/VersionChecker.cs index 5c926809ef..268a1bd2a9 100644 --- a/src/Product/VersionChecker.cs +++ b/src/Product/VersionChecker.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Net; using System.Net.Http; using System.Reflection; using System.Text.Json.Serialization; @@ -9,26 +8,24 @@ namespace Azure.DataApiBuilder.Product; public static class VersionChecker { - private const string PackageName = "Microsoft.DataApiBuilder"; - private const string NuGetApiUrl = "https://api.nuget.org/v3-flatcontainer/{0}/index.json"; + private const string NuGetApiUrl = "https://api.nuget.org/v3-flatcontainer/Microsoft.DataApiBuilder/index.json"; - public static (string? LatestVersion, string? CurrentVersion) GetVersions() + public static void GetVersions(out string? latestVersion, out string? currentVersion) { - var latestVersion = FetchLatestNuGetVersion(); - var currentVersion = GetCurrentVersionFromAssembly(Assembly.GetExecutingAssembly()); - return (latestVersion, currentVersion); + latestVersion = FetchLatestNuGetVersion(); + currentVersion = GetCurrentVersionFromAssembly(Assembly.GetExecutingAssembly()); } private static string? FetchLatestNuGetVersion() { try { - using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) }; - var url = string.Format(NuGetApiUrl, PackageName.ToLower()); - var versionData = httpClient.GetFromJsonAsync(url).GetAwaiter().GetResult(); + using HttpClient httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) }; + NuGetVersionResponse? versionData = httpClient.GetFromJsonAsync(NuGetApiUrl) + .GetAwaiter().GetResult(); return versionData?.Versions - ?.Where(v => !v.Contains("-rc")) + ?.Where(version => !version.Contains("-rc")) .Max(); // Get the latest stable version } catch @@ -39,7 +36,7 @@ public static (string? LatestVersion, string? CurrentVersion) GetVersions() private static string? GetCurrentVersionFromAssembly(Assembly assembly) { - var version = assembly.GetCustomAttribute()?.InformationalVersion; + string? version = assembly.GetCustomAttribute()?.InformationalVersion; return !string.IsNullOrEmpty(version) ? version.Split('+')[0] : assembly.GetName().Version?.ToString(); } diff --git a/src/Service/Program.cs b/src/Service/Program.cs index d059acf367..a050b3b733 100644 --- a/src/Service/Program.cs +++ b/src/Service/Program.cs @@ -28,10 +28,10 @@ public class Program public static void Main(string[] args) { // Compare current version of DAB with latest (non-rc) version in NuGet. - var (latestVersion, currentVersion) = await Azure.DataApiBuilder.Product.VersionChecker.GetVersionsAsync(); + VersionChecker.GetVersions(out string? latestVersion, out string? currentVersion); if (!string.IsNullOrEmpty(latestVersion) && latestVersion != currentVersion) { - Console.Error.WriteLine("A newer version of Data API builder is available. {currentVersion} -> {latestVersion}"); + Console.Error.WriteLine($"A newer version of Data API builder is available. {currentVersion} -> {latestVersion}"); } if (!ValidateAspNetCoreUrls()) From 7ed7a11a3c82f5e696da6e96d3c6e42f6691eb7b Mon Sep 17 00:00:00 2001 From: Jerry Nixon <1749983+JerryNixon@users.noreply.github.com> Date: Thu, 13 Feb 2025 17:23:52 -0700 Subject: [PATCH 03/12] Refactor version filtering and conversion logic --- src/Product/VersionChecker.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Product/VersionChecker.cs b/src/Product/VersionChecker.cs index 268a1bd2a9..4b160f5aa7 100644 --- a/src/Product/VersionChecker.cs +++ b/src/Product/VersionChecker.cs @@ -25,9 +25,10 @@ public static void GetVersions(out string? latestVersion, out string? currentVer .GetAwaiter().GetResult(); return versionData?.Versions - ?.Where(version => !version.Contains("-rc")) - .Max(); // Get the latest stable version - } + ?.Where(version => !version.Contains("-rc")) // Filter out pre-release versions + .Select(version => new Version(version)) // Convert to Version objects + .Max() // Get the latest + ?.ToString(); // Convert to string catch { return null; // Assume no update available on failure From 5635d3f542d904c6abaa71fc1616d485845a55d7 Mon Sep 17 00:00:00 2001 From: Jerry Nixon <1749983+JerryNixon@users.noreply.github.com> Date: Thu, 13 Feb 2025 17:26:00 -0700 Subject: [PATCH 04/12] Remove redundant line breaks in VersionChecker.cs --- src/Product/VersionChecker.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Product/VersionChecker.cs b/src/Product/VersionChecker.cs index 4b160f5aa7..25368a1a65 100644 --- a/src/Product/VersionChecker.cs +++ b/src/Product/VersionChecker.cs @@ -27,8 +27,11 @@ public static void GetVersions(out string? latestVersion, out string? currentVer return versionData?.Versions ?.Where(version => !version.Contains("-rc")) // Filter out pre-release versions .Select(version => new Version(version)) // Convert to Version objects - .Max() // Get the latest - ?.ToString(); // Convert to string + .Max()?.ToString(); // Get the latest + .Max() // Get the latest + + ?.ToString(); // Convert to string + catch { return null; // Assume no update available on failure From 0f57e401dde83be7f64be7079ac1c49fde18bf6c Mon Sep 17 00:00:00 2001 From: Jerry Nixon <1749983+JerryNixon@users.noreply.github.com> Date: Thu, 13 Feb 2025 17:26:46 -0700 Subject: [PATCH 05/12] Refactor version filtering and conversion logic --- src/Product/VersionChecker.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Product/VersionChecker.cs b/src/Product/VersionChecker.cs index 25368a1a65..7f4c612726 100644 --- a/src/Product/VersionChecker.cs +++ b/src/Product/VersionChecker.cs @@ -28,7 +28,6 @@ public static void GetVersions(out string? latestVersion, out string? currentVer ?.Where(version => !version.Contains("-rc")) // Filter out pre-release versions .Select(version => new Version(version)) // Convert to Version objects .Max()?.ToString(); // Get the latest - .Max() // Get the latest ?.ToString(); // Convert to string From 944894264271b1a9801c7606eac78c1deb7a82c8 Mon Sep 17 00:00:00 2001 From: Jerry Nixon Date: Thu, 13 Feb 2025 17:34:20 -0700 Subject: [PATCH 06/12] Fixing whatever weird thing was happening in the web editor. --- src/Product/VersionChecker.cs | 100 +++++++++++++++++----------------- 1 file changed, 49 insertions(+), 51 deletions(-) diff --git a/src/Product/VersionChecker.cs b/src/Product/VersionChecker.cs index 7f4c612726..57d84a94d8 100644 --- a/src/Product/VersionChecker.cs +++ b/src/Product/VersionChecker.cs @@ -1,51 +1,49 @@ -using System; -using System.Linq; -using System.Net.Http; -using System.Reflection; -using System.Text.Json.Serialization; - -namespace Azure.DataApiBuilder.Product; - -public static class VersionChecker -{ - private const string NuGetApiUrl = "https://api.nuget.org/v3-flatcontainer/Microsoft.DataApiBuilder/index.json"; - - public static void GetVersions(out string? latestVersion, out string? currentVersion) - { - latestVersion = FetchLatestNuGetVersion(); - currentVersion = GetCurrentVersionFromAssembly(Assembly.GetExecutingAssembly()); - } - - private static string? FetchLatestNuGetVersion() - { - try - { - using HttpClient httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) }; - NuGetVersionResponse? versionData = httpClient.GetFromJsonAsync(NuGetApiUrl) - .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 - - ?.ToString(); // Convert to string - - catch - { - return null; // Assume no update available on failure - } - } - - private static string? GetCurrentVersionFromAssembly(Assembly assembly) - { - string? version = assembly.GetCustomAttribute()?.InformationalVersion; - return !string.IsNullOrEmpty(version) ? version.Split('+')[0] : assembly.GetName().Version?.ToString(); - } - - private class NuGetVersionResponse - { - [JsonPropertyName("versions")] - public string[]? Versions { get; set; } - } -} +using System; +using System.Linq; +using System.Net.Http; +using System.Reflection; +using System.Text.Json.Serialization; + +namespace Azure.DataApiBuilder.Product; + +public static class VersionChecker +{ + private const string NuGetApiUrl = "https://api.nuget.org/v3-flatcontainer/Microsoft.DataApiBuilder/index.json"; + + public static void GetVersions(out string? latestVersion, out string? currentVersion) + { + latestVersion = FetchLatestNuGetVersion(); + currentVersion = GetCurrentVersionFromAssembly(Assembly.GetExecutingAssembly()); + } + + private static string? FetchLatestNuGetVersion() + { + try + { + using HttpClient httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) }; + NuGetVersionResponse? versionData = httpClient.GetFromJsonAsync(NuGetApiUrl) + .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 + { + return null; // Assume no update available on failure + } + } + + private static string? GetCurrentVersionFromAssembly(Assembly assembly) + { + string? version = assembly.GetCustomAttribute()?.InformationalVersion; + return !string.IsNullOrEmpty(version) ? version.Split('+')[0] : assembly.GetName().Version?.ToString(); + } + + private class NuGetVersionResponse + { + [JsonPropertyName("versions")] + public string[]? Versions { get; set; } + } +} From 66e37caf7ed3e04fad505b14eb55c428b66845ea Mon Sep 17 00:00:00 2001 From: Jerry Nixon Date: Thu, 13 Feb 2025 18:32:25 -0700 Subject: [PATCH 07/12] Refactor VersionChecker: move to Core namespace and add unit tests --- src/{Product => Core}/VersionChecker.cs | 22 ++++++++-------- .../Unittests/VersionCheckTests.cs | 26 +++++++++++++++++++ src/Service/Program.cs | 1 + 3 files changed, 38 insertions(+), 11 deletions(-) rename src/{Product => Core}/VersionChecker.cs (66%) create mode 100644 src/Service.Tests/Unittests/VersionCheckTests.cs diff --git a/src/Product/VersionChecker.cs b/src/Core/VersionChecker.cs similarity index 66% rename from src/Product/VersionChecker.cs rename to src/Core/VersionChecker.cs index 57d84a94d8..43ba61d3e7 100644 --- a/src/Product/VersionChecker.cs +++ b/src/Core/VersionChecker.cs @@ -1,27 +1,24 @@ -using System; -using System.Linq; -using System.Net.Http; +using System.Net.Http.Json; using System.Reflection; using System.Text.Json.Serialization; -namespace Azure.DataApiBuilder.Product; +namespace Azure.DataApiBuilder.Core; public static class VersionChecker { - private const string NuGetApiUrl = "https://api.nuget.org/v3-flatcontainer/Microsoft.DataApiBuilder/index.json"; - public static void GetVersions(out string? latestVersion, out string? currentVersion) { latestVersion = FetchLatestNuGetVersion(); - currentVersion = GetCurrentVersionFromAssembly(Assembly.GetExecutingAssembly()); + currentVersion = GetCurrentVersionFromAssembly(Assembly.GetCallingAssembly()); } private static string? FetchLatestNuGetVersion() { try { - using HttpClient httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) }; - NuGetVersionResponse? versionData = httpClient.GetFromJsonAsync(NuGetApiUrl) + using HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(2) }; + NuGetVersionResponse? versionData = httpClient + .GetFromJsonAsync("https://api.nuget.org/v3-flatcontainer/microsoft.dataapibuilder/index.json") .GetAwaiter().GetResult(); return versionData?.Versions @@ -29,7 +26,7 @@ public static void GetVersions(out string? latestVersion, out string? currentVer .Select(version => new Version(version)) // Convert to Version objects .Max()?.ToString(); // Get the latest } - catch + catch (Exception) { return null; // Assume no update available on failure } @@ -38,7 +35,10 @@ public static void GetVersions(out string? latestVersion, out string? currentVer private static string? GetCurrentVersionFromAssembly(Assembly assembly) { string? version = assembly.GetCustomAttribute()?.InformationalVersion; - return !string.IsNullOrEmpty(version) ? version.Split('+')[0] : assembly.GetName().Version?.ToString(); + + return version is { Length: > 0 } && version.Contains('+') + ? version[..version.IndexOf('+')] // Slice version string before '+' + : version ?? assembly.GetName().Version?.ToString(); } private class NuGetVersionResponse diff --git a/src/Service.Tests/Unittests/VersionCheckTests.cs b/src/Service.Tests/Unittests/VersionCheckTests.cs new file mode 100644 index 0000000000..c5af229809 --- /dev/null +++ b/src/Service.Tests/Unittests/VersionCheckTests.cs @@ -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."); + } + } +} diff --git a/src/Service/Program.cs b/src/Service/Program.cs index a050b3b733..dcd48523a8 100644 --- a/src/Service/Program.cs +++ b/src/Service/Program.cs @@ -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; From da3d997226242e6ece10915baa3a58246bde06ff Mon Sep 17 00:00:00 2001 From: Jerry Nixon Date: Fri, 14 Feb 2025 09:08:57 -0700 Subject: [PATCH 08/12] Refactor VersionChecker: extract NuGet URL to a constant --- src/Core/VersionChecker.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Core/VersionChecker.cs b/src/Core/VersionChecker.cs index 43ba61d3e7..14884b3508 100644 --- a/src/Core/VersionChecker.cs +++ b/src/Core/VersionChecker.cs @@ -6,6 +6,8 @@ namespace Azure.DataApiBuilder.Core; public static class VersionChecker { + private const string NUGETURL = "https://api.nuget.org/v3/registration5-semver1/azure.dataapibuilder/index.json"; + public static void GetVersions(out string? latestVersion, out string? currentVersion) { latestVersion = FetchLatestNuGetVersion(); @@ -18,7 +20,7 @@ public static void GetVersions(out string? latestVersion, out string? currentVer { using HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(2) }; NuGetVersionResponse? versionData = httpClient - .GetFromJsonAsync("https://api.nuget.org/v3-flatcontainer/microsoft.dataapibuilder/index.json") + .GetFromJsonAsync(NUGETURL) .GetAwaiter().GetResult(); return versionData?.Versions From 5b0736c9d709cb0fd56ac372344f7cbaeefae902 Mon Sep 17 00:00:00 2001 From: Jerry Nixon Date: Fri, 14 Feb 2025 11:24:39 -0700 Subject: [PATCH 09/12] Rearranged as discussed. --- src/Cli.Tests/VersionCheckTests.cs | 33 +++++++++++++++++++ .../Configurations/RuntimeConfigValidator.cs | 5 +++ src/Core/VersionChecker.cs | 23 +++++-------- .../Unittests/VersionCheckTests.cs | 26 --------------- src/Service/Program.cs | 8 ----- 5 files changed, 46 insertions(+), 49 deletions(-) create mode 100644 src/Cli.Tests/VersionCheckTests.cs delete mode 100644 src/Service.Tests/Unittests/VersionCheckTests.cs diff --git a/src/Cli.Tests/VersionCheckTests.cs b/src/Cli.Tests/VersionCheckTests.cs new file mode 100644 index 0000000000..99fc56e939 --- /dev/null +++ b/src/Cli.Tests/VersionCheckTests.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +using Azure.DataApiBuilder.Core; + +namespace Cli.Tests; + +[TestClass] +public class VersionCheckTests +{ + public TestContext TestContext { get; set; } = null!; + + [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}"); + } +} diff --git a/src/Core/Configurations/RuntimeConfigValidator.cs b/src/Core/Configurations/RuntimeConfigValidator.cs index 5620ed352e..ddf8ef6340 100644 --- a/src/Core/Configurations/RuntimeConfigValidator.cs +++ b/src/Core/Configurations/RuntimeConfigValidator.cs @@ -141,6 +141,11 @@ public async Task TryValidateConfig( string configFilePath, ILoggerFactory loggerFactory) { + if (!VersionChecker.IsCurrentVersion(out string? nugetVersion, out string? localVersion)) + { + _logger.LogWarning("A newer version of Data API builder is available. Update {LocalVersion} -> {NugetVersion}.", localVersion, nugetVersion); + } + RuntimeConfig? runtimeConfig; if (!_runtimeConfigProvider.TryGetConfig(out runtimeConfig)) diff --git a/src/Core/VersionChecker.cs b/src/Core/VersionChecker.cs index 14884b3508..23bc5c95d1 100644 --- a/src/Core/VersionChecker.cs +++ b/src/Core/VersionChecker.cs @@ -1,17 +1,19 @@ +using System.Diagnostics; using System.Net.Http.Json; -using System.Reflection; using System.Text.Json.Serialization; +using Azure.DataApiBuilder.Product; namespace Azure.DataApiBuilder.Core; public static class VersionChecker { - private const string NUGETURL = "https://api.nuget.org/v3/registration5-semver1/azure.dataapibuilder/index.json"; + private const string NUGETURL = "https://api.nuget.org/v3-flatcontainer/microsoft.dataapibuilder/index.json"; - public static void GetVersions(out string? latestVersion, out string? currentVersion) + public static bool IsCurrentVersion(out string? nugetVersion, out string? localVersion) { - latestVersion = FetchLatestNuGetVersion(); - currentVersion = GetCurrentVersionFromAssembly(Assembly.GetCallingAssembly()); + nugetVersion = FetchLatestNuGetVersion(); + localVersion = ProductInfo.GetProductVersion(false); + return string.IsNullOrEmpty(nugetVersion) || nugetVersion == localVersion; } private static string? FetchLatestNuGetVersion() @@ -20,7 +22,7 @@ public static void GetVersions(out string? latestVersion, out string? currentVer { using HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(2) }; NuGetVersionResponse? versionData = httpClient - .GetFromJsonAsync(NUGETURL) + .GetFromJsonAsync(new Uri(NUGETURL).ToString()) .GetAwaiter().GetResult(); return versionData?.Versions @@ -34,15 +36,6 @@ public static void GetVersions(out string? latestVersion, out string? currentVer } } - private static string? GetCurrentVersionFromAssembly(Assembly assembly) - { - string? version = assembly.GetCustomAttribute()?.InformationalVersion; - - return version is { Length: > 0 } && version.Contains('+') - ? version[..version.IndexOf('+')] // Slice version string before '+' - : version ?? assembly.GetName().Version?.ToString(); - } - private class NuGetVersionResponse { [JsonPropertyName("versions")] diff --git a/src/Service.Tests/Unittests/VersionCheckTests.cs b/src/Service.Tests/Unittests/VersionCheckTests.cs deleted file mode 100644 index c5af229809..0000000000 --- a/src/Service.Tests/Unittests/VersionCheckTests.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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."); - } - } -} diff --git a/src/Service/Program.cs b/src/Service/Program.cs index dcd48523a8..a0d6fe01d4 100644 --- a/src/Service/Program.cs +++ b/src/Service/Program.cs @@ -7,7 +7,6 @@ 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; @@ -28,13 +27,6 @@ public class Program public static void Main(string[] args) { - // Compare current version of DAB with latest (non-rc) version in NuGet. - VersionChecker.GetVersions(out string? latestVersion, out string? currentVersion); - if (!string.IsNullOrEmpty(latestVersion) && latestVersion != currentVersion) - { - Console.Error.WriteLine($"A newer version of Data API builder is available. {currentVersion} -> {latestVersion}"); - } - if (!ValidateAspNetCoreUrls()) { Console.Error.WriteLine("Invalid ASPNETCORE_URLS format. e.g.: ASPNETCORE_URLS=\"http://localhost:5000;https://localhost:5001\""); From 358b111917a856980537d4bee80f49a94949e67a Mon Sep 17 00:00:00 2001 From: Jerry Nixon Date: Fri, 14 Feb 2025 11:28:00 -0700 Subject: [PATCH 10/12] Remove unused TestContext property from VersionCheckTests --- src/Cli.Tests/VersionCheckTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Cli.Tests/VersionCheckTests.cs b/src/Cli.Tests/VersionCheckTests.cs index 99fc56e939..2d14fe36c2 100644 --- a/src/Cli.Tests/VersionCheckTests.cs +++ b/src/Cli.Tests/VersionCheckTests.cs @@ -8,8 +8,6 @@ namespace Cli.Tests; [TestClass] public class VersionCheckTests { - public TestContext TestContext { get; set; } = null!; - [TestMethod] public void GetVersions_LatestVersionNotNull() { From 52e6f2a0faf87244de4afb8275c6adf2f0d24506 Mon Sep 17 00:00:00 2001 From: Jerry Nixon Date: Fri, 14 Feb 2025 11:39:18 -0700 Subject: [PATCH 11/12] Enhance VersionChecker documentation: add XML comments for class and method --- src/Core/VersionChecker.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Core/VersionChecker.cs b/src/Core/VersionChecker.cs index 23bc5c95d1..0ca4921a40 100644 --- a/src/Core/VersionChecker.cs +++ b/src/Core/VersionChecker.cs @@ -1,14 +1,28 @@ -using System.Diagnostics; using System.Net.Http.Json; using System.Text.Json.Serialization; using Azure.DataApiBuilder.Product; namespace Azure.DataApiBuilder.Core; +/// +/// A class to test the local version of the engine against the NuGet version. +/// +/// +/// This is used in startup to suggest upgrading the engine. +/// public static class VersionChecker { private const string NUGETURL = "https://api.nuget.org/v3-flatcontainer/microsoft.dataapibuilder/index.json"; + /// + /// Checks if the current local version of the product matches the latest version available on NuGet. + /// + /// Outputs the latest version available on NuGet. + /// Outputs the current local version of the product. + /// + /// Returns true if the local version matches the latest NuGet version or if the NuGet version is not available; + /// otherwise, returns false. + /// public static bool IsCurrentVersion(out string? nugetVersion, out string? localVersion) { nugetVersion = FetchLatestNuGetVersion(); From 141a0f8b431f52c9dead4c5b0c095ad3b329aeb0 Mon Sep 17 00:00:00 2001 From: Jerry Nixon Date: Fri, 14 Feb 2025 13:50:31 -0700 Subject: [PATCH 12/12] Enhance VersionChecker documentation: add remarks for IsCurrentVersion method --- src/Core/VersionChecker.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Core/VersionChecker.cs b/src/Core/VersionChecker.cs index 0ca4921a40..28d25b513a 100644 --- a/src/Core/VersionChecker.cs +++ b/src/Core/VersionChecker.cs @@ -23,6 +23,11 @@ public static class VersionChecker /// Returns true if the local version matches the latest NuGet version or if the NuGet version is not available; /// otherwise, returns false. /// + /// + // 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 null and + // this method will return true. This is mostly because this check is a user courtesy. + // public static bool IsCurrentVersion(out string? nugetVersion, out string? localVersion) { nugetVersion = FetchLatestNuGetVersion();