From 29f86758beae8274bf6294ac3c97831a4d0a85c7 Mon Sep 17 00:00:00 2001 From: Jasmin Date: Mon, 5 Feb 2024 15:36:53 +0100 Subject: [PATCH] SIANXSVC-1203: Ensure library_version has correct value (#8) Several things had to get fixed in order for the library version to appear in the metadata. First and foremost, the GitHub Actions workflow was setting the PackageVersion (which is used for the NuGet package), but not the AssemblyVersion. This is now fixed by passing the tag via /p:Version. Second, the current retrieved version included the commit hash, which would have ended up to conflict with the database constraints of E5E which limit the version to 20 characters. In order to circumvent this problem, the hash is trimmed from the version. Because I had the time to work on this part of the code, I took the opportunity and made the E5ERuntimeMetadata sealed. Technically, this is a breaking change, however I don't expect that anyone is affected by it, so it's fine to put it in a patch release. Closes SIANXSVC-1203 --- .github/workflows/publish.yml | 2 +- .../Integration/StartupTests.cs | 2 +- .../Serialization/SerializationTests.cs | 9 +++++---- src/Anexia.E5E/Anexia.E5E.csproj | 1 - src/Anexia.E5E/Hosting/E5EHostWrapper.cs | 4 ++-- src/Anexia.E5E/Runtime/E5ERuntimeMetadata.cs | 17 +++++++++++++---- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index b4b5d8b..b303971 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -23,7 +23,7 @@ jobs: - run: dotnet restore - - run: dotnet pack --no-restore --include-symbols -o . /p:PackageVersion=${VERSION} + - run: dotnet pack --no-restore --include-symbols -o . /p:Version=${VERSION} - name: Deploy to NuGet run: dotnet nuget push *.nupkg --api-key $NUGET_AUTH_TOKEN --source https://api.nuget.org/v3/index.json diff --git a/src/Anexia.E5E.Tests/Integration/StartupTests.cs b/src/Anexia.E5E.Tests/Integration/StartupTests.cs index c52cc30..4e6bb53 100644 --- a/src/Anexia.E5E.Tests/Integration/StartupTests.cs +++ b/src/Anexia.E5E.Tests/Integration/StartupTests.cs @@ -25,7 +25,7 @@ public StartupTests(ITestOutputHelper outputHelper) : base(outputHelper) public async Task OutputMatches() { await Host.StartAsync(); - var expected = JsonSerializer.Serialize(new E5ERuntimeMetadata(), E5EJsonSerializerOptions.Default); + var expected = JsonSerializer.Serialize(E5ERuntimeMetadata.Current, E5EJsonSerializerOptions.Default); var stdout = await Host.GetStdoutAsync(); Assert.Equal(expected, stdout); } diff --git a/src/Anexia.E5E.Tests/Serialization/SerializationTests.cs b/src/Anexia.E5E.Tests/Serialization/SerializationTests.cs index 4653a76..187d4f4 100644 --- a/src/Anexia.E5E.Tests/Serialization/SerializationTests.cs +++ b/src/Anexia.E5E.Tests/Serialization/SerializationTests.cs @@ -100,7 +100,7 @@ public void ResponseSerializationRecognisesCorrectType() Assert.Equal(E5EResponseType.Binary, E5EResponse.From("test"u8.ToArray()).Type); Assert.Equal(E5EResponseType.Binary, E5EResponse.From("test"u8.ToArray().AsEnumerable()).Type); Assert.Equal(E5EResponseType.Binary, E5EResponse.From(new E5EFileData("something"u8.ToArray())).Type); - Assert.Equal(E5EResponseType.StructuredObject, E5EResponse.From(new E5ERuntimeMetadata()).Type); + Assert.Equal(E5EResponseType.StructuredObject, E5EResponse.From(E5ERuntimeMetadata.Current).Type); } [Theory] @@ -125,7 +125,7 @@ public void EnumsAreProperSerialized(object type, string expected) [Fact] public void MetadataIsProperSerialized() { - var json = JsonSerializer.Serialize(new E5ERuntimeMetadata(), _options); + var json = JsonSerializer.Serialize(E5ERuntimeMetadata.Current, _options); var deserialized = JsonSerializer.Deserialize(json); var sut = deserialized.EnumerateObject().ToDictionary(x => x.Name, x => x.Value); @@ -135,7 +135,8 @@ public void MetadataIsProperSerialized() () => Assert.Contains("runtime", sut), () => Assert.Contains("features", sut), () => Assert.Contains("library_version", sut), - () => Assert.Equal(1, sut["features"].GetArrayLength()) + () => Assert.Equal(1, sut["features"].GetArrayLength()), + () => Assert.Equal("1.0.0", sut["library_version"].GetString()) ); } @@ -158,7 +159,7 @@ private class SerializationTestsData : IEnumerable { new E5EContext("generic", DateTimeOffset.FromUnixTimeSeconds(0), true), new E5ERequestParameters(), - new E5ERuntimeMetadata(), + E5ERuntimeMetadata.Current, new E5EFileData("data"u8.ToArray()), }; diff --git a/src/Anexia.E5E/Anexia.E5E.csproj b/src/Anexia.E5E/Anexia.E5E.csproj index a3a0fa7..ea42ca4 100644 --- a/src/Anexia.E5E/Anexia.E5E.csproj +++ b/src/Anexia.E5E/Anexia.E5E.csproj @@ -8,7 +8,6 @@ Anexia.E5E - 1.0.0 A helper library to help you build serverless functions on top of the Anexia Engine. anexia ANEXIA Internetdienstleistungs GmbH diff --git a/src/Anexia.E5E/Hosting/E5EHostWrapper.cs b/src/Anexia.E5E/Hosting/E5EHostWrapper.cs index 37f8154..0feab72 100644 --- a/src/Anexia.E5E/Hosting/E5EHostWrapper.cs +++ b/src/Anexia.E5E/Hosting/E5EHostWrapper.cs @@ -39,10 +39,10 @@ public async Task StartAsync(CancellationToken cancellationToken = default) } #if NET8_0_OR_GREATER - var metadata = JsonSerializer.Serialize(new E5ERuntimeMetadata(), + var metadata = JsonSerializer.Serialize(E5ERuntimeMetadata.Current, E5ESerializationContext.Default.E5ERuntimeMetadata); #else - var metadata = JsonSerializer.Serialize(new E5ERuntimeMetadata(), E5EJsonSerializerOptions.Default); + var metadata = JsonSerializer.Serialize(E5ERuntimeMetadata.Current, E5EJsonSerializerOptions.Default); #endif _console.Open(); diff --git a/src/Anexia.E5E/Runtime/E5ERuntimeMetadata.cs b/src/Anexia.E5E/Runtime/E5ERuntimeMetadata.cs index f02ac1a..05d77ea 100644 --- a/src/Anexia.E5E/Runtime/E5ERuntimeMetadata.cs +++ b/src/Anexia.E5E/Runtime/E5ERuntimeMetadata.cs @@ -8,8 +8,19 @@ namespace Anexia.E5E.Runtime; /// [SuppressMessage("ReSharper", "UnusedMember.Global")] [SuppressMessage("Performance", "CA1822:Mark members as static")] -public record E5ERuntimeMetadata +public sealed record E5ERuntimeMetadata { + private E5ERuntimeMetadata() + { + // We try to fetch the informational version from the generated AssemblyInfo. Because SourceLink appends the + // commit hash, we have to trim it afterwards. + var version = typeof(E5ERuntimeMetadata).Assembly + .GetCustomAttribute() + ?.InformationalVersion ?? "0.0.0+unknown"; + + LibraryVersion = version[..version.IndexOf('+')]; + } + /// /// The current instance of the metadata. /// @@ -18,9 +29,7 @@ public record E5ERuntimeMetadata /// /// The installed version of this NuGet library. /// - public string LibraryVersion => - typeof(E5ERuntimeMetadata).Assembly.GetCustomAttribute()?.Version ?? - "0.0.0-unrecognized"; + public string LibraryVersion { get; } /// /// The runtime this function is running in.