diff --git a/backend/src/Designer/Controllers/PreviewController.cs b/backend/src/Designer/Controllers/PreviewController.cs index 9f071258e2f..c9f8df30a86 100644 --- a/backend/src/Designer/Controllers/PreviewController.cs +++ b/backend/src/Designer/Controllers/PreviewController.cs @@ -13,6 +13,7 @@ using Altinn.Platform.Storage.Interface.Models; using Altinn.Studio.Designer.Filters; using Altinn.Studio.Designer.Helpers; +using Altinn.Studio.Designer.Helpers.Preview; using Altinn.Studio.Designer.Infrastructure.GitRepository; using Altinn.Studio.Designer.Models; using Altinn.Studio.Designer.Models.App; @@ -61,8 +62,6 @@ public class PreviewController(IHttpContextAccessor httpContextAccessor, // This value will be overridden to act as the task number for apps that use layout sets private const int PartyId = 51001; - private const string MINIMUM_NUGET_VERSION = "8.0.0.0"; - private const int MINIMUM_PREVIEW_NUGET_VERSION = 15; /// /// Default action for the preview. @@ -142,7 +141,7 @@ public async Task> ApplicationMetadata(string ApplicationMetadata applicationMetadata = await altinnAppGitRepository.GetApplicationMetadata(cancellationToken); string appNugetVersionString = _appDevelopmentService.GetAppLibVersion(AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer)).ToString(); // This property is populated at runtime by the apps, so we need to mock it here - applicationMetadata.AltinnNugetVersion = GetMockedAltinnNugetBuildFromVersion(appNugetVersionString); + applicationMetadata.AltinnNugetVersion = NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion(appNugetVersionString); applicationMetadata = SetMockedPartyTypesAllowedAsAllFalse(applicationMetadata); return Ok(applicationMetadata); } @@ -695,29 +694,6 @@ private static string ReplaceIndexToFetchCorrectOrgAppInCshtml(string originalCo return modifiedContent; } - /// - /// Method to get the mocked altinn nuget build from the version - /// We are returnning the minimum BUILD version of the nuget package that is required for app frontend to work - /// from v4 and above. - /// - /// The version of the nuget package - /// The minimum build version of the nuget package - private string GetMockedAltinnNugetBuildFromVersion(string version) - { - - string[] versionParts = version.Split('.'); - if (!IsValidSemVerVersion(versionParts)) - { - return string.Empty; - } - - if (IsPreviewVersion(versionParts) && GetPreviewVersion(versionParts) < MINIMUM_PREVIEW_NUGET_VERSION) - { - return string.Empty; - } - - return MINIMUM_NUGET_VERSION; - } /// /// Method to override the partyTypesAllowed in app metadata to bypass the check in app-frontend for a valid party during instantiation. @@ -733,19 +709,5 @@ private static ApplicationMetadata SetMockedPartyTypesAllowedAsAllFalse(Applicat return applicationMetadata; } - private bool IsValidSemVerVersion(string[] versionParts) - { - return versionParts.Length >= 3 && Convert.ToInt32(versionParts[0]) >= 8; - } - - private bool IsPreviewVersion(string[] versionParts) - { - return versionParts[2].Contains("-preview") && versionParts.Length == 4; - } - - private int GetPreviewVersion(string[] versionParts) - { - return Convert.ToInt32(versionParts[3]); - } } } diff --git a/backend/src/Designer/Helpers/Preview/NugetVersionHelper.cs b/backend/src/Designer/Helpers/Preview/NugetVersionHelper.cs new file mode 100644 index 00000000000..be914f2e997 --- /dev/null +++ b/backend/src/Designer/Helpers/Preview/NugetVersionHelper.cs @@ -0,0 +1,69 @@ +using System; +using System.Diagnostics.Contracts; +using System.Linq; + +namespace Altinn.Studio.Designer.Helpers.Preview +{ + public static class NugetVersionHelper + { + private const string MINIMUM_NUGET_VERSION = "8.0.0.0"; + private const int MINIMUM_PREVIEW_NUGET_VERSION = 15; + + /// + /// Method to get the mocked altinn nuget build from the version + /// We are returnning the minimum BUILD version of the nuget package that is required for app frontend to work + /// from v4 and above. + /// + /// The version of the nuget package + /// The minimum build version of the nuget package + public static string GetMockedAltinnNugetBuildFromVersion(string version) + { + + string[] versionParts = version.Split('.'); + if (!IsValidSemVerVersion(versionParts)) + { + return string.Empty; + } + + string normalVersion = version.Split('-').First(); + int[] numberVersion = [.. normalVersion.Split('.').Take(3).Select((split) => Convert.ToInt32(split))]; + if (IsVersionOrBelow(numberVersion, [8, 0, 0]) && IsPreviewVersion(versionParts) && GetPreviewVersion(versionParts) < MINIMUM_PREVIEW_NUGET_VERSION) + { + return string.Empty; + } + + return MINIMUM_NUGET_VERSION; + } + + private static bool IsValidSemVerVersion(string[] versionParts) + { + return versionParts.Length >= 3 && Convert.ToInt32(versionParts[0]) >= 8; + } + + // + // + private static bool IsVersionOrBelow(int[] versionParts, int[] breakpoint) + { + Contract.Requires(versionParts.Length == 3); + Contract.Requires(breakpoint.Length == 3); + for (int i = 0; i < 3; i++) + { + if (versionParts[i] > breakpoint[i]) + { + return false; + } + } + return true; + } + + private static bool IsPreviewVersion(string[] versionParts) + { + return versionParts[2].Contains("-preview") && versionParts.Length == 4; + } + + private static int GetPreviewVersion(string[] versionParts) + { + return Convert.ToInt32(versionParts[3]); + } + } +} diff --git a/backend/tests/Designer.Tests/Helpers/Preview/NugetVersionHelperTests.cs b/backend/tests/Designer.Tests/Helpers/Preview/NugetVersionHelperTests.cs new file mode 100644 index 00000000000..8801be580e2 --- /dev/null +++ b/backend/tests/Designer.Tests/Helpers/Preview/NugetVersionHelperTests.cs @@ -0,0 +1,24 @@ +using Altinn.Studio.Designer.Helpers.Preview; +using Xunit; + +namespace Designer.Tests.Helpers.Preview +{ + + public class NugetVersionHelperTests + { + [Fact] + public void GetMockedAltinnNugetBuildFromVersion_ShouldReturnEmptyStringForVersionsBelowBreakpoint() + { + Assert.Equal("", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("1.1.1")); + Assert.Equal("", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("1.1.1-preview.150")); + Assert.Equal("", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("1.1.1-preview.0")); + Assert.Equal("8.0.0.0", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("8.1.1-preview.14")); + Assert.Equal("8.0.0.0", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("8.8.8-preview.0")); + Assert.Equal("8.0.0.0", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("8.18.1238")); + Assert.Equal("8.0.0.0", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("8.18.1238")); + Assert.Equal("8.0.0.0", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("8.8.8-preview.342")); + Assert.Equal("8.0.0.0", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("8.0.0-preview.15")); + Assert.Equal("", NugetVersionHelper.GetMockedAltinnNugetBuildFromVersion("8.0.0-preview.14")); + } + } +}