Skip to content
Draft
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
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