From 1f44ab367b21de1785520190cd18c1e8c3734c4e Mon Sep 17 00:00:00 2001 From: Coen van den Munckhof Date: Mon, 3 Oct 2022 21:36:35 +0200 Subject: [PATCH] implemented loading multiple env files. Added logging. --- ...stomEnvironmentVariableVariableProvider.cs | 22 ++++++------ .../ExpressionEvaluator/RepositoryContext.cs | 26 ++++++++++++++ .../RepositoryExpressionEvaluator.cs | 20 ----------- .../RepositorySpecificConfiguration.cs | 35 ++++++++++++++----- .../RepositorySpecificConfigurationTest.cs | 13 ++++--- 5 files changed, 72 insertions(+), 44 deletions(-) create mode 100644 src/RepoM.Api/IO/ExpressionEvaluator/RepositoryContext.cs diff --git a/src/RepoM.Api/IO/CustomEnvironmentVariableVariableProvider.cs b/src/RepoM.Api/IO/CustomEnvironmentVariableVariableProvider.cs index 7031eee8..3465218e 100644 --- a/src/RepoM.Api/IO/CustomEnvironmentVariableVariableProvider.cs +++ b/src/RepoM.Api/IO/CustomEnvironmentVariableVariableProvider.cs @@ -29,20 +29,20 @@ public static Dictionary Get(Repository _) { return _envVars.Value ?? new Dictionary(0); } -} - -public class ExecuteOnDisposed : IDisposable -{ - private readonly Func>? _func; - public ExecuteOnDisposed(Func>? func) + private sealed class ExecuteOnDisposed : IDisposable { - _func = func; - } + private readonly Func>? _func; - public void Dispose() - { - _func?.Invoke(); + public ExecuteOnDisposed(Func>? func) + { + _func = func; + } + + public void Dispose() + { + _func?.Invoke(); + } } } diff --git a/src/RepoM.Api/IO/ExpressionEvaluator/RepositoryContext.cs b/src/RepoM.Api/IO/ExpressionEvaluator/RepositoryContext.cs new file mode 100644 index 00000000..2c399e08 --- /dev/null +++ b/src/RepoM.Api/IO/ExpressionEvaluator/RepositoryContext.cs @@ -0,0 +1,26 @@ +namespace RepoM.Api.IO.ExpressionEvaluator; + +using System; +using System.Collections.Generic; +using System.Linq; +using RepoM.Api.Git; + +public class RepositoryContext +{ + public RepositoryContext() + { + Repositories = Array.Empty(); + } + + public RepositoryContext(params Repository[] repositories) + { + Repositories = repositories.ToArray(); + } + + public RepositoryContext(IEnumerable repositories) + { + Repositories = repositories.ToArray(); + } + + public Repository[] Repositories { get; } +} \ No newline at end of file diff --git a/src/RepoM.Api/IO/ExpressionEvaluator/RepositoryExpressionEvaluator.cs b/src/RepoM.Api/IO/ExpressionEvaluator/RepositoryExpressionEvaluator.cs index a60c8500..13e25a9b 100644 --- a/src/RepoM.Api/IO/ExpressionEvaluator/RepositoryExpressionEvaluator.cs +++ b/src/RepoM.Api/IO/ExpressionEvaluator/RepositoryExpressionEvaluator.cs @@ -8,26 +8,6 @@ namespace RepoM.Api.IO.ExpressionEvaluator; using ExpressionStringEvaluator.VariableProviders; using RepoM.Api.Git; -public class RepositoryContext -{ - public RepositoryContext() - { - Repositories = Array.Empty(); - } - - public RepositoryContext(params Repository[] repositories) - { - Repositories = repositories.ToArray(); - } - - public RepositoryContext(IEnumerable repositories) - { - Repositories = repositories.ToArray(); - } - - public Repository[] Repositories { get; } -} - public class RepositoryExpressionEvaluator { private readonly ExpressionExecutor _expressionExecutor; diff --git a/src/RepoM.Api/IO/ModuleBasedRepositoryActionProvider/RepositorySpecificConfiguration.cs b/src/RepoM.Api/IO/ModuleBasedRepositoryActionProvider/RepositorySpecificConfiguration.cs index e6dea49a..6060f94c 100644 --- a/src/RepoM.Api/IO/ModuleBasedRepositoryActionProvider/RepositorySpecificConfiguration.cs +++ b/src/RepoM.Api/IO/ModuleBasedRepositoryActionProvider/RepositorySpecificConfiguration.cs @@ -8,6 +8,7 @@ namespace RepoM.Api.IO.ModuleBasedRepositoryActionProvider; using System.Text; using DotNetEnv; using ExpressionStringEvaluator.Methods; +using Microsoft.Extensions.Logging; using RepoM.Api.Common; using RepoM.Api.Git; using RepoM.Api.IO.ExpressionEvaluator; @@ -25,6 +26,7 @@ public class RepositoryConfigurationReader private readonly JsonDynamicRepositoryActionDeserializer _jsonAppSettingsDeserializer; private readonly YamlDynamicRepositoryActionDeserializer _yamlAppSettingsDeserializer; private readonly RepositoryExpressionEvaluator _repoExpressionEvaluator; + private readonly ILogger _logger; private const string FILENAME = "RepositoryActions."; public const string FILENAME_JSON = FILENAME + "json"; @@ -34,13 +36,15 @@ public RepositoryConfigurationReader( IFileSystem fileSystem, JsonDynamicRepositoryActionDeserializer jsonAppSettingsDeserializer, YamlDynamicRepositoryActionDeserializer yamlAppSettingsDeserializer, - RepositoryExpressionEvaluator repoExpressionEvaluator) + RepositoryExpressionEvaluator repoExpressionEvaluator, + ILogger logger) { _appDataPathProvider = appDataPathProvider ?? throw new ArgumentNullException(nameof(appDataPathProvider)); _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); _jsonAppSettingsDeserializer = jsonAppSettingsDeserializer ?? throw new ArgumentNullException(nameof(jsonAppSettingsDeserializer)); _yamlAppSettingsDeserializer = yamlAppSettingsDeserializer ?? throw new ArgumentNullException(nameof(yamlAppSettingsDeserializer)); _repoExpressionEvaluator = repoExpressionEvaluator ?? throw new ArgumentNullException(nameof(repoExpressionEvaluator)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } private string GetRepositoryActionsFilename(string basePath) @@ -116,6 +120,7 @@ private string GetRepositoryActionsFilename(string basePath) } catch (Exception e) { + _logger.LogWarning(e, "Could not read and deserialize file '{file}'", filename); throw new InvalidConfigurationException(filename, e.Message, e); } } @@ -149,11 +154,6 @@ List EvaluateVariables(IEnumerable? vars) // load repo specific environment variables foreach (FileReference fileRef in rootFile.RepositorySpecificEnvironmentFiles.Where(fileRef => fileRef != null)) { - if (envVars != null) - { - continue; - } - if (!IsEnabled(fileRef.When, true, repository)) { continue; @@ -168,11 +168,28 @@ List EvaluateVariables(IEnumerable? vars) try { - envVars = DotNetEnv.Env.Load(f, new DotNetEnv.LoadOptions(setEnvVars: false)).ToDictionary(); + IEnumerable>? currentEnvVars = Env.Load(f, new LoadOptions(setEnvVars: false)); + if (envVars == null || !envVars.Any()) + { + envVars = currentEnvVars.ToDictionary(); + continue; + } + + foreach (KeyValuePair item in currentEnvVars) + { + if (!envVars.ContainsKey(item.Key)) + { + envVars.Add(item.Key, item.Value); + } + else + { + _logger.LogTrace("Environment key was '{Key}' already set.", item.Key); + } + } } - catch (Exception) + catch (Exception e) { - // log warning + _logger.LogWarning(e, "Something went wrong loading an environment file"); } } } diff --git a/tests/RepoM.Api.Tests/IO/ModuleBasedRepositoryActionProvider/RepositorySpecificConfigurationTest.cs b/tests/RepoM.Api.Tests/IO/ModuleBasedRepositoryActionProvider/RepositorySpecificConfigurationTest.cs index 5cf0e51c..223fcc62 100644 --- a/tests/RepoM.Api.Tests/IO/ModuleBasedRepositoryActionProvider/RepositorySpecificConfigurationTest.cs +++ b/tests/RepoM.Api.Tests/IO/ModuleBasedRepositoryActionProvider/RepositorySpecificConfigurationTest.cs @@ -18,6 +18,7 @@ namespace RepoM.Api.Tests.IO.ModuleBasedRepositoryActionProvider; using ExpressionStringEvaluator.VariableProviders.DateTime; using FakeItEasy; using FluentAssertions; +using Microsoft.Extensions.Logging.Abstractions; using RepoM.Api.Common; using RepoM.Api.Git; using RepoM.Api.IO; @@ -145,7 +146,8 @@ public async Task Create_ShouldRespectMultiSelectRepos() _fileSystem, _jsonAppsettingsDeserializer, _yamlAppsettingsDeserializer, - _repositoryExpressionEvaluator)); + _repositoryExpressionEvaluator, + NullLogger.Instance)); // act IEnumerable result = sut.CreateActions(new Repository(), new Repository()); @@ -171,7 +173,8 @@ public async Task Create_ShouldNotCareAboutMultiSelectRepos_WhenSingleRepo() _fileSystem, _jsonAppsettingsDeserializer, _yamlAppsettingsDeserializer, - _repositoryExpressionEvaluator)); + _repositoryExpressionEvaluator, + NullLogger.Instance)); // act IEnumerable result = sut.CreateActions(new Repository()); @@ -197,7 +200,8 @@ public async Task Create_ShouldOnlyProcessActiveItems() _fileSystem, _jsonAppsettingsDeserializer, _yamlAppsettingsDeserializer, - _repositoryExpressionEvaluator)); + _repositoryExpressionEvaluator, + NullLogger.Instance)); // act IEnumerable result = sut.CreateActions(new Repository()); @@ -223,7 +227,8 @@ public async Task Create_ShouldProcessSeparator1() _fileSystem, _jsonAppsettingsDeserializer, _yamlAppsettingsDeserializer, - _repositoryExpressionEvaluator)); + _repositoryExpressionEvaluator, + NullLogger.Instance)); // act IEnumerable result = sut.CreateActions(new Repository());