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

fix: add helper method to check if version is below or equal to a specific version #14812

Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 2 additions & 40 deletions backend/src/Designer/Controllers/PreviewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/// <summary>
/// Default action for the preview.
Expand Down Expand Up @@ -142,7 +141,7 @@ public async Task<ActionResult<ApplicationMetadata>> 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);
}
Expand Down Expand Up @@ -695,29 +694,6 @@ private static string ReplaceIndexToFetchCorrectOrgAppInCshtml(string originalCo
return modifiedContent;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="version">The version of the nuget package</param>
/// <returns>The minimum build version of the nuget package</returns>
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;
}

/// <summary>
/// Method to override the partyTypesAllowed in app metadata to bypass the check in app-frontend for a valid party during instantiation.
Expand All @@ -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]);
}
}
}
69 changes: 69 additions & 0 deletions backend/src/Designer/Helpers/Preview/NugetVersionHelper.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// 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.
/// </summary>
/// <param name="version">The version of the nuget package</param>
/// <returns>The minimum build version of the nuget package</returns>
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;
}

// <exception cref="FormatException"></exception>
// <exception cref="OverflowException"></exception>
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]);
}
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}