Skip to content

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/Core/VersionChecker.cs
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")
.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('+')
? version[..version.IndexOf('+')] // Slice version string before '+'
: version ?? assembly.GetName().Version?.ToString();
}

private class NuGetVersionResponse
{
[JsonPropertyName("versions")]
public string[]? Versions { get; set; }
}
}
26 changes: 26 additions & 0 deletions src/Service.Tests/Unittests/VersionCheckTests.cs
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.");
}
}
}
8 changes: 8 additions & 0 deletions src/Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this change work for the docker scenario too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd need to test to be sure. My gut says no.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
if (!string.IsNullOrEmpty(latestVersion) && latestVersion != currentVersion)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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}");
}

if (!ValidateAspNetCoreUrls())
{
Console.Error.WriteLine("Invalid ASPNETCORE_URLS format. e.g.: ASPNETCORE_URLS=\"http://localhost:5000;https://localhost:5001\"");
Expand Down