diff --git a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs index 549d99984..d0b80cccf 100644 --- a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs +++ b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using FilterLists.Agent.AppSettings; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Octokit; @@ -19,10 +20,12 @@ public class AgentGitHubClient : IAgentGitHubClient { private readonly GitHubClient _gitHubClient; private readonly GitHub _gitHubOptions; + private readonly ILogger _logger; - public AgentGitHubClient(IOptions gitHubOptions) + public AgentGitHubClient(IOptions gitHubOptions, ILogger logger) { _gitHubOptions = gitHubOptions.Value; + _logger = logger; _gitHubClient = new GitHubClient(new ProductHeaderValue(_gitHubOptions.ProductHeaderValue)) { Credentials = new Credentials(_gitHubOptions.PersonalAccessToken) @@ -31,20 +34,44 @@ public AgentGitHubClient(IOptions gitHubOptions) public async Task> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest) { - return await _gitHubClient.Issue.GetAllForRepository(_gitHubOptions.RepositoryOwner, - _gitHubOptions.Repository, repositoryIssueRequest); + try + { + return await _gitHubClient.Issue.GetAllForRepository(_gitHubOptions.RepositoryOwner, + _gitHubOptions.Repository, repositoryIssueRequest); + } + catch (ApiException ex) + { + _logger.LogError(ex, "Failed getting all Issues from the GitHub API."); + return null; + } } public async Task CreateIssue(NewIssue newIssue) { - return await _gitHubClient.Issue.Create(_gitHubOptions.RepositoryOwner, _gitHubOptions.Repository, - newIssue); + try + { + return await _gitHubClient.Issue.Create(_gitHubOptions.RepositoryOwner, _gitHubOptions.Repository, + newIssue); + } + catch (ApiException ex) + { + _logger.LogError(ex, "Failed creating Issue with the GitHub API."); + return null; + } } public async Task UpdateIssue(int issueNumber, IssueUpdate updateIssue) { - return await _gitHubClient.Issue.Update(_gitHubOptions.RepositoryOwner, _gitHubOptions.Repository, - issueNumber, updateIssue); + try + { + return await _gitHubClient.Issue.Update(_gitHubOptions.RepositoryOwner, _gitHubOptions.Repository, + issueNumber, updateIssue); + } + catch (ApiException ex) + { + _logger.LogError(ex, "Failed updating Issue with the GitHub API."); + return null; + } } } } \ No newline at end of file