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

- implemented argument variable provider #257

Merged
merged 2 commits into from
Jun 25, 2024
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
4 changes: 2 additions & 2 deletions src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ WORKDIR /app
EXPOSE 80
EXPOSE 443

RUN apt-get -y update
RUN apt-get install -y libsecret-1-0
#RUN apt-get -y update
#RUN apt-get install -y libsecret-1-0

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public NoxActionMetaData Discover()

public Task BeginAsync(IDictionary<string,object> inputs)
{
var connection = inputs.Value<VssConnection>("connection");
_server = inputs.Value<string>("server");
_pat = inputs.Value<string>("personal-access-token");
_projectId = inputs.Value<Guid>("project-id");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public static bool CopyFile(string sourcePath, string targetPath, bool isOverwri
var createDate = System.IO.File.GetCreationTime(targetPath);
System.IO.File.Delete(targetPath);
System.IO.File.Copy(sourcePath, targetPath);
System.IO.File.SetCreationTime(targetPath, createDate);
System.IO.File.SetCreationTime(targetPath, createDate);
return true;
}

return false;
Expand Down
1 change: 1 addition & 0 deletions src/Nox.Cli.Server/Nox.Cli.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
<Content Include=".config\dotnet-tools.json" />
</ItemGroup>
</Project>
17 changes: 17 additions & 0 deletions src/Nox.Cli.Server/appsettings.Production.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,22 @@
"Microsoft": "Debug",
"Microsoft.AspNetCore": "Debug"
}
},
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "88155c28-f750-4013-91d3-8347ddb3daa7",
"ClientId": "750b96e1-e772-48f8-b6b3-84bac1961d9b",
"Scopes": "access_as_user"
},
"ServerSecretResolver": {
"TenantId": "88155c28-f750-4013-91d3-8347ddb3daa7",
"ClientId": "750b96e1-e772-48f8-b6b3-84bac1961d9b",
"ClientSecret": "DoM8Q~xfHyKasa~MHTuPGXoPZbYj.B1pIWOvzc7K"
},
"NoxScriptsUrl": "https://noxorg.dev",
"ElasticApm": {
"ServerUrl": "http://localhost:8200",
"SecretToken": "",
"TransactionSampleRate": "1.0"
}
}
25 changes: 24 additions & 1 deletion src/Nox.Cli.Variables/ArgumentVariableResolver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
using Spectre.Console.Cli;

namespace Nox.Cli.Variables;

public class ArgumentVariableResolver
public static class ArgumentVariableResolver
{
public static void ResolveNoxArgumentVariables(this IDictionary<string, object?> variables, IRemainingArguments arguments)
{
var keys = variables
.Where(kv => kv.Value == null)
.Select(kv => kv.Key)
.Where(e => e.StartsWith("args.", StringComparison.OrdinalIgnoreCase))
.Select(e => e[5..])
.ToArray();



foreach (var key in keys)
{
var value = ResolveArgumentValue(key, arguments);
variables[$"args.{key}"] = value;
}
}

private static bool ResolveArgumentValue(string argument, IRemainingArguments arguments)
{
return arguments.Parsed.Any(a => a.Key == "--" + argument);
}
}
7 changes: 6 additions & 1 deletion src/Nox.Cli.Variables/ClientVariableProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@
using Nox.Cli.Configuration;
using Nox.Cli.Variables.Secrets;
using Nox.Solution;
using Spectre.Console.Cli;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace Nox.Cli.Variables;

public class ClientVariableProvider: IClientVariableProvider
{
private readonly Regex _variableRegex = new(@"\$\{\{\s*(?<variable>\b(vars|solution|steps|server|env|runner|cache)[\w\.\-_:]+)\s*\}\}", RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));
private readonly Regex _variableRegex = new(@"\$\{\{\s*(?<variable>\b(vars|solution|steps|server|env|runner|cache|args)[\w\.\-_:]+)\s*\}\}", RegexOptions.Compiled | RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));

private readonly Dictionary<string, object?> _variables;
private readonly IOrgSecretResolver _orgSecretResolver;
private NoxSolution? _projectConfig;
private readonly INoxCliCache? _cache;
private readonly ILocalTaskExecutorConfiguration? _lteConfig;
private readonly List<string> _serverVariables;
private readonly IRemainingArguments _arguments;

public ClientVariableProvider(
WorkflowConfiguration workflow,
IOrgSecretResolver orgSecretResolver,
IRemainingArguments arguments,
NoxSolution? projectConfig = null,
ILocalTaskExecutorConfiguration? lteConfig = null,
INoxCliCache? cache = null)
Expand All @@ -35,6 +38,7 @@ public ClientVariableProvider(
_projectConfig = projectConfig;
_lteConfig = lteConfig;
_cache = cache;
_arguments = arguments;
Initialize(workflow);
}

Expand Down Expand Up @@ -146,6 +150,7 @@ public void SetProjectConfiguration(NoxSolution projectConfig)
public async Task ResolveAll()
{
_variables.ResolveRunnerVariables();
_variables.ResolveNoxArgumentVariables(_arguments);
await ResolveForServer();
}

Expand Down
6 changes: 4 additions & 2 deletions src/Nox.Cli/Actions/NoxWorkflowContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Nox.Cli.Variables.Secrets;
using Nox.Secrets.Abstractions;
using Nox.Solution;
using Spectre.Console.Cli;
using Environment = System.Environment;

namespace Nox.Cli.Actions;
Expand Down Expand Up @@ -45,11 +46,12 @@ public NoxWorkflowContext(
IOrgSecretResolver orgSecretResolver,
INoxCliCacheManager cacheManager,
ILocalTaskExecutorConfiguration? lteConfig,
INoxSecretsResolver? secretsResolver)
INoxSecretsResolver? secretsResolver,
IRemainingArguments arguments)
{
WorkflowId = Guid.NewGuid();
_workflow = workflow;
_varProvider = new ClientVariableProvider(workflow, orgSecretResolver, projectConfig, lteConfig, cacheManager.Cache);
_varProvider = new ClientVariableProvider(workflow, orgSecretResolver, arguments, projectConfig, lteConfig, cacheManager.Cache);
_cacheManager = cacheManager;
_secretsResolver = secretsResolver;
_jobs = ParseWorkflow();
Expand Down
3 changes: 1 addition & 2 deletions src/Nox.Cli/Actions/NoxWorkflowExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task<bool> Execute(WorkflowConfiguration workflow, IRemainingArgume

var workflowCtx = _console.Status()
.Spinner(Spinner.Known.Clock)
.Start("Verifying the workflow script...", _ => new NoxWorkflowContext(workflow, _noxConfig, _orgSecretResolver, _cacheManager, _lteConfig, _noxSecretsResolver));
.Start("Verifying the workflow script...", _ => new NoxWorkflowContext(workflow, _noxConfig, _orgSecretResolver, _cacheManager, _lteConfig, _noxSecretsResolver, arguments));

while (workflowCtx.CurrentJob != null)
{
Expand Down Expand Up @@ -454,7 +454,6 @@ private async Task<bool> ProcessForEachJob(NoxWorkflowContext context, bool forc
}
return true;
}

}


Expand Down
2 changes: 1 addition & 1 deletion src/Nox.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Nox.Cli": {
"commandName": "Project",
"commandLineArgs": "new dop",
"commandLineArgs": "sync ado",
"workingDirectory": "/home/jan/demo/CliDemo",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
2 changes: 1 addition & 1 deletion tests/Plugin.Core.Tests/SnakeNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
var result = await plugin.ProcessAsync(ctx);
Assert.Single(result);

Check failure on line 34 in tests/Plugin.Core.Tests/SnakeNameTests.cs

View workflow job for this annotation

GitHub Actions / Tests Results

Plugin.Core.Tests.SnakeNameTests ► Can_Convert_To_Snake_Name(sourceValue: "Hello.World", expectedValue: "hello_world")

Failed test found in: tests/Plugin.Core.Tests/TestResults/test-results.trx Error: Assert.Single() Failure: The collection contained 3 items Collection: [["result"] = "Hello_World", ["result-lower"] = "hello_world", ["result-upper"] = "HELLO_WORLD"]
Raw output
Assert.Single() Failure: The collection contained 3 items
Collection: [["result"] = "Hello_World", ["result-lower"] = "hello_world", ["result-upper"] = "HELLO_WORLD"]
   at Plugin.Core.Tests.SnakeNameTests.Can_Convert_To_Snake_Name(String sourceValue, String expectedValue) in /home/runner/work/Nox.Cli/Nox.Cli/tests/Plugin.Core.Tests/SnakeNameTests.cs:line 34
--- End of stack trace from previous location ---

Check failure on line 34 in tests/Plugin.Core.Tests/SnakeNameTests.cs

View workflow job for this annotation

GitHub Actions / Tests Results

Plugin.Core.Tests.SnakeNameTests ► Can_Convert_To_Snake_Name(sourceValue: "HELLO.World", expectedValue: "hello_world")

Failed test found in: tests/Plugin.Core.Tests/TestResults/test-results.trx Error: Assert.Single() Failure: The collection contained 3 items Collection: [["result"] = "HELLO_World", ["result-lower"] = "hello_world", ["result-upper"] = "HELLO_WORLD"]
Raw output
Assert.Single() Failure: The collection contained 3 items
Collection: [["result"] = "HELLO_World", ["result-lower"] = "hello_world", ["result-upper"] = "HELLO_WORLD"]
   at Plugin.Core.Tests.SnakeNameTests.Can_Convert_To_Snake_Name(String sourceValue, String expectedValue) in /home/runner/work/Nox.Cli/Nox.Cli/tests/Plugin.Core.Tests/SnakeNameTests.cs:line 34
--- End of stack trace from previous location ---

Check failure on line 34 in tests/Plugin.Core.Tests/SnakeNameTests.cs

View workflow job for this annotation

GitHub Actions / Tests Results

Plugin.Core.Tests.SnakeNameTests ► Can_Convert_To_Snake_Name(sourceValue: "HelloWorld", expectedValue: "helloworld")

Failed test found in: tests/Plugin.Core.Tests/TestResults/test-results.trx Error: Assert.Single() Failure: The collection contained 3 items Collection: [["result"] = "HelloWorld", ["result-lower"] = "helloworld", ["result-upper"] = "HELLOWORLD"]
Raw output
Assert.Single() Failure: The collection contained 3 items
Collection: [["result"] = "HelloWorld", ["result-lower"] = "helloworld", ["result-upper"] = "HELLOWORLD"]
   at Plugin.Core.Tests.SnakeNameTests.Can_Convert_To_Snake_Name(String sourceValue, String expectedValue) in /home/runner/work/Nox.Cli/Nox.Cli/tests/Plugin.Core.Tests/SnakeNameTests.cs:line 34
--- End of stack trace from previous location ---
Assert.Equal(expectedValue, result["result"]);
await plugin.EndAsync();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Plugin.File.Tests/CopyFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task Can_copy_a_File_if_destination_folder_does_not_exist()
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
await plugin.ProcessAsync(ctx);
Assert.True(System.IO.File.Exists(Path.Combine(path, "new-target/Sample.txt")));
}
Expand All @@ -60,7 +60,7 @@ public async Task Must_not_copy_if_destination_folder_exists_and_overwrite_not_s
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
await plugin.ProcessAsync(ctx);
await plugin.EndAsync();
Assert.True(System.IO.File.Exists(Path.Combine(path, "new-target/Sample.txt")));
Expand Down Expand Up @@ -91,7 +91,7 @@ public async Task Can_copy_a_File_if_destination_folder_exists_and_overwrite_spe
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
await plugin.ProcessAsync(ctx);
await plugin.EndAsync();
Assert.True(System.IO.File.Exists(Path.Combine(path, "new-target/Sample.txt")));
Expand Down
2 changes: 1 addition & 1 deletion tests/Plugin.File.Tests/CopyFolderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task Can_copy_a_File_if_destination_folder_does_not_exist()
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
await plugin.ProcessAsync(ctx);
Assert.True(System.IO.File.Exists(Path.Combine(path, "target/Sample.txt")));
Assert.True(System.IO.File.Exists(Path.Combine(path, "target/child/Sample.txt")));
Expand Down
2 changes: 1 addition & 1 deletion tests/Plugin.File.Tests/FileRenameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task Can_Rename_a_File()
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
await plugin.ProcessAsync(ctx);
Assert.True(System.IO.File.Exists(Path.Combine(path, "Sample-After.txt")));
Assert.False(System.IO.File.Exists(Path.Combine(path, "Sample-Before.txt")));
Expand Down
2 changes: 1 addition & 1 deletion tests/Plugin.File.Tests/FolderRenameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task Can_Rename_a_Folder()
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
await plugin.ProcessAsync(ctx);
Assert.True(System.IO.Directory.Exists(Path.Combine(path, "Sample-After")));
Assert.False(System.IO.Directory.Exists(Path.Combine(path, "Sample-Before")));
Expand Down
6 changes: 3 additions & 3 deletions tests/Plugin.Git.Tests/InitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task Can_initialize_a_local_git_repo()
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
ctx.SetCurrentActionForTests();
await plugin.ProcessAsync(ctx);
Assert.Equal(ActionState.Success, ctx.CurrentAction!.State);
Expand All @@ -59,7 +59,7 @@ public async Task Must_get_warning_if_initializing_more_than_once()
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
ctx.SetCurrentActionForTests();
await plugin.ProcessAsync(ctx);
Assert.Equal(ActionState.Success, ctx.CurrentAction!.State);
Expand Down Expand Up @@ -90,7 +90,7 @@ public async Task Must_not_get_warning_if_initializing_more_than_once_and_suppre
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
ctx.SetCurrentActionForTests();
await plugin.ProcessAsync(ctx);
Assert.Equal(ActionState.Success, ctx.CurrentAction!.State);
Expand Down
2 changes: 1 addition & 1 deletion tests/Plugin.Powershell.Tests/PwshTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task Can_copy_a_File_if_destination_folder_does_not_exist()
var cacheMan = Mock.Of<INoxCliCacheManager>();
var lteConfig = Mock.Of<LocalTaskExecutorConfiguration>();
var secretsResolver = Mock.Of<INoxSecretsResolver>();
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver);
var ctx = new NoxWorkflowContext(wfConfig, sln, orgResolver, cacheMan, lteConfig, secretsResolver, null!);
await plugin.ProcessAsync(ctx);
Assert.True(Directory.Exists(".git"));
FileHelpers.PurgeFolderRecursive(".git", false);
Expand Down
Loading