<commit_before>﻿using System.Collections.Generic;
using System.Threading.Tasks;
using FilterLists.Agent.AppSettings;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Octokit;

namespace FilterLists.Agent.Infrastructure.Clients
{
    public interface IAgentGitHubClient
    {
        Task<IReadOnlyList<Issue>> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest);

        Task<Issue> CreateIssue(NewIssue newIssue);

        Task<Issue> UpdateIssue(int issueNumber, IssueUpdate issueUpdate);
    }

    public class AgentGitHubClient : IAgentGitHubClient
    {
        private const string ExceptionMessageSuffix = " from the GitHub API.";
        private readonly GitHubClient _gitHubClient;
        private readonly GitHubSettings _gitHubSettings;
        private readonly ILogger<AgentGitHubClient> _logger;

        public AgentGitHubClient(IOptions<GitHubSettings> gitHubOptions, ILogger<AgentGitHubClient> logger)
        {
            _gitHubSettings = gitHubOptions.Value;
            _logger = logger;
            _gitHubClient = new GitHubClient(new ProductHeaderValue(_gitHubSettings.ProductHeaderValue))
            {
                Credentials = new Credentials(_gitHubSettings.PersonalAccessToken)
            };
        }

        public async Task<IReadOnlyList<Issue>> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest)
        {
            try
            {
                return await _gitHubClient.Issue.GetAllForRepository(_gitHubSettings.RepositoryOwner,
                    _gitHubSettings.Repository, repositoryIssueRequest);
            }
            catch (ApiException ex)
            {
                _logger.LogError(ex, $"Failed getting all Issues{ExceptionMessageSuffix}");
                return null;
            }
        }

        public async Task<Issue> CreateIssue(NewIssue newIssue)
        {
            try
            {
                return await _gitHubClient.Issue.Create(_gitHubSettings.RepositoryOwner, _gitHubSettings.Repository,
                    newIssue);
            }
            catch (ApiException ex)
            {
                _logger.LogError(ex, $"Failed creating Issue{ExceptionMessageSuffix}");
                return null;
            }
        }

        public async Task<Issue> UpdateIssue(int issueNumber, IssueUpdate issueUpdate)
        {
            try
            {
                return await _gitHubClient.Issue.Update(_gitHubSettings.RepositoryOwner, _gitHubSettings.Repository,
                    issueNumber, issueUpdate);
            }
            catch (ApiException ex)
            {
                _logger.LogError(ex, $"Failed updating Issue{ExceptionMessageSuffix}");
                return null;
            }
        }
    }
}<commit_msg>resolve CS8603 possible null ref return<commit_after>﻿using System.Collections.Generic;
using System.Threading.Tasks;
using FilterLists.Agent.AppSettings;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Octokit;

namespace FilterLists.Agent.Infrastructure.Clients
{
    public interface IAgentGitHubClient
    {
        Task<IReadOnlyList<Issue>> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest);

        Task<Issue> CreateIssue(NewIssue newIssue);

        Task<Issue> UpdateIssue(int issueNumber, IssueUpdate issueUpdate);
    }

    public class AgentGitHubClient : IAgentGitHubClient
    {
        private const string ExceptionMessageSuffix = " from the GitHub API.";
        private readonly GitHubClient _gitHubClient;
        private readonly GitHubSettings _gitHubSettings;
        private readonly ILogger<AgentGitHubClient> _logger;

        public AgentGitHubClient(IOptions<GitHubSettings> gitHubOptions, ILogger<AgentGitHubClient> logger)
        {
            _gitHubSettings = gitHubOptions.Value;
            _logger = logger;
            _gitHubClient = new GitHubClient(new ProductHeaderValue(_gitHubSettings.ProductHeaderValue))
            {
                Credentials = new Credentials(_gitHubSettings.PersonalAccessToken)
            };
        }

        public async Task<IReadOnlyList<Issue>> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest)
        {
            try
            {
                return await _gitHubClient.Issue.GetAllForRepository(_gitHubSettings.RepositoryOwner,
                    _gitHubSettings.Repository, repositoryIssueRequest);
            }
            catch (ApiException ex)
            {
                _logger.LogError(ex, $"Failed getting all Issues{ExceptionMessageSuffix}");
                return new List<Issue>();
            }
        }

        public async Task<Issue> CreateIssue(NewIssue newIssue)
        {
            try
            {
                return await _gitHubClient.Issue.Create(_gitHubSettings.RepositoryOwner, _gitHubSettings.Repository,
                    newIssue);
            }
            catch (ApiException ex)
            {
                _logger.LogError(ex, $"Failed creating Issue{ExceptionMessageSuffix}");
                return new Issue();
            }
        }

        public async Task<Issue> UpdateIssue(int issueNumber, IssueUpdate issueUpdate)
        {
            try
            {
                return await _gitHubClient.Issue.Update(_gitHubSettings.RepositoryOwner, _gitHubSettings.Repository,
                    issueNumber, issueUpdate);
            }
            catch (ApiException ex)
            {
                _logger.LogError(ex, $"Failed updating Issue{ExceptionMessageSuffix}");
                return new Issue();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.0672957094534449, 'fraction_numerical': 0.0007121239095602635, 'mean_word_length': 2.18300283286119, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3956122', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1321, 'n_words': 272}