From 76df08bd84dd094e9c3e2a50e36944409bdea0a9 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Tue, 2 Jul 2019 13:20:20 -0500 Subject: [PATCH] extract AgentGitHubClient --- src/FilterLists.Agent/AppSettings/GitHub.cs | 2 ++ .../Extensions/ServiceCollectionExtensions.cs | 15 +------- .../Clients/AgentGitHubClient.cs | 34 +++++++++++++++++++ src/FilterLists.Agent/appsettings.json | 4 ++- 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs diff --git a/src/FilterLists.Agent/AppSettings/GitHub.cs b/src/FilterLists.Agent/AppSettings/GitHub.cs index e88491a2a..33c7743ca 100644 --- a/src/FilterLists.Agent/AppSettings/GitHub.cs +++ b/src/FilterLists.Agent/AppSettings/GitHub.cs @@ -4,5 +4,7 @@ public class GitHub { public string ProductHeaderValue { get; set; } public string PersonalAccessToken { get; set; } + public string RepositoryOwner { get; set; } + public string Repository { get; set; } } } \ No newline at end of file diff --git a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs index 3f3415cf9..3df8fcdf2 100644 --- a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs @@ -7,7 +7,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using Octokit; namespace FilterLists.Agent.Extensions { @@ -20,7 +19,7 @@ public static void RegisterAgentServices(this IServiceCollection services) services.AddMediatR(typeof(Program).Assembly); services.AddHttpClient(); services.AddSingleton(); - services.AddGitHubClient(); + services.AddSingleton(); services.AddTransient(); services.AddTransient(); } @@ -48,17 +47,5 @@ private static void AddLoggingCustom(this IServiceCollection services) b.AddApplicationInsights(appInsightsConfig.Value.InstrumentationKey); }); } - - private static void AddGitHubClient(this IServiceCollection services) - { - services.AddSingleton(s => - { - var gitHubConfig = s.GetService>(); - return new GitHubClient(new ProductHeaderValue(gitHubConfig.Value.ProductHeaderValue)) - { - Credentials = new Credentials(gitHubConfig.Value.PersonalAccessToken) - }; - }); - } } } \ No newline at end of file diff --git a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs new file mode 100644 index 000000000..31e5765bd --- /dev/null +++ b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using FilterLists.Agent.AppSettings; +using Microsoft.Extensions.Options; +using Octokit; + +namespace FilterLists.Agent.Infrastructure.Clients +{ + public interface IAgentGitHubClient + { + Task> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest); + } + + public class AgentGitHubClient : IAgentGitHubClient + { + private readonly GitHubClient _gitHubClient; + private readonly GitHub _gitHubOptions; + + public AgentGitHubClient(IOptions gitHubOptions) + { + _gitHubOptions = gitHubOptions.Value; + _gitHubClient = new GitHubClient(new ProductHeaderValue(_gitHubOptions.ProductHeaderValue)) + { + Credentials = new Credentials(_gitHubOptions.PersonalAccessToken) + }; + } + + public async Task> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest) + { + return await _gitHubClient.Issue.GetAllForRepository(_gitHubOptions.RepositoryOwner, + _gitHubOptions.Repository, repositoryIssueRequest); + } + } +} \ No newline at end of file diff --git a/src/FilterLists.Agent/appsettings.json b/src/FilterLists.Agent/appsettings.json index c2f265c0b..dc0313a1f 100644 --- a/src/FilterLists.Agent/appsettings.json +++ b/src/FilterLists.Agent/appsettings.json @@ -7,6 +7,8 @@ }, "GitHub": { "ProductHeaderValue": "", - "OauthToken:": "" + "OauthToken:": "", + "RepositoryOwner": "", + "Repository": "" } } \ No newline at end of file