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 5 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
51 changes: 51 additions & 0 deletions src/Product/VersionChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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<NuGetVersionResponse>(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<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
return !string.IsNullOrEmpty(version) ? version.Split('+')[0] : assembly.GetName().Version?.ToString();
}

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

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
Contributor

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