From e7377e6e422b20af4b47742fdf38b23b9fc93001 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Thu, 4 Jul 2019 21:47:07 -0500 Subject: [PATCH] resolve archives git repo from DI --- .../Extensions/ServiceCollectionExtensions.cs | 19 +++++++++++++--- .../Features/Lists/CommitLists.cs | 22 ++++++++----------- .../Clients/AgentGitHubClient.cs | 4 ++-- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs index a16d1a2d7..420e5de85 100644 --- a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs @@ -5,6 +5,7 @@ using FilterLists.Agent.Core.Interfaces; using FilterLists.Agent.Infrastructure.Clients; using FilterLists.Agent.Infrastructure.Repositories; +using LibGit2Sharp; using MediatR; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -24,6 +25,7 @@ public static void RegisterAgentServices(this IServiceCollection services) services.AddAgentHttpClient(); services.AddSingleton(); services.AddSingleton(); + services.AddArchiveRepository(); services.AddTransient(); services.AddTransient(); } @@ -59,9 +61,9 @@ private static void AddLoggingCustom(this IServiceCollection services) services.AddLogging(b => { b.AddConsole(); - var appInsightsConfig = b.Services.BuildServiceProvider() - .GetService>(); - b.AddApplicationInsights(appInsightsConfig.Value.InstrumentationKey); + var applicationInsightsSettings = b.Services.BuildServiceProvider() + .GetService>().Value; + b.AddApplicationInsights(applicationInsightsSettings.InstrumentationKey); }); } @@ -73,5 +75,16 @@ private static void AddAgentHttpClient(this IServiceCollection services) b.Build(); }); } + + private static void AddArchiveRepository(this IServiceCollection services) + { + services.AddTransient(s => + { + var archiveSettings = s.GetService>().Value; + if (!Repository.IsValid(archiveSettings.RepositoryDirectory)) + Repository.Init(archiveSettings.RepositoryDirectory); + return new Repository(archiveSettings.RepositoryDirectory); + }); + } } } \ No newline at end of file diff --git a/src/FilterLists.Agent/Features/Lists/CommitLists.cs b/src/FilterLists.Agent/Features/Lists/CommitLists.cs index 06380f619..cb0982c61 100644 --- a/src/FilterLists.Agent/Features/Lists/CommitLists.cs +++ b/src/FilterLists.Agent/Features/Lists/CommitLists.cs @@ -15,25 +15,21 @@ public class Command : IRequest public class Handler : RequestHandler { private readonly ArchiveSettings _archiveSettings; + private readonly IRepository _repository; - public Handler(IOptions archiveSettings) + public Handler(IOptions archiveOptions, IRepository repository) { - _archiveSettings = archiveSettings.Value; + _archiveSettings = archiveOptions.Value; + _repository = repository; } protected override void Handle(Command request) { - if (!Repository.IsValid(_archiveSettings.RepositoryDirectory)) - Repository.Init(_archiveSettings.RepositoryDirectory); - using (var repo = new Repository(_archiveSettings.RepositoryDirectory)) - { - Commands.Stage(repo, "*"); - var utcNow = DateTime.UtcNow; - var signature = new Signature(_archiveSettings.SignatureName, _archiveSettings.SignatureEmail, - utcNow); - repo.Commit(utcNow.ToShortDateString() + _archiveSettings.CommitMessageSuffix, signature, - signature); - } + Commands.Stage(_repository, "*"); + var utcNow = DateTime.UtcNow; + var signature = new Signature(_archiveSettings.SignatureName, _archiveSettings.SignatureEmail, utcNow); + _repository.Commit(utcNow.ToShortDateString() + _archiveSettings.CommitMessageSuffix, signature, + signature); } } } diff --git a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs index 86bf6e7e8..6f6357a11 100644 --- a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs +++ b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs @@ -23,9 +23,9 @@ public class AgentGitHubClient : IAgentGitHubClient private readonly GitHubSettings _gitHubSettings; private readonly ILogger _logger; - public AgentGitHubClient(IOptions gitHubSettings, ILogger logger) + public AgentGitHubClient(IOptions gitHubOptions, ILogger logger) { - _gitHubSettings = gitHubSettings.Value; + _gitHubSettings = gitHubOptions.Value; _logger = logger; _gitHubClient = new GitHubClient(new ProductHeaderValue(_gitHubSettings.ProductHeaderValue)) {