diff --git a/src/FilterLists.Agent/Features/Urls/CreateOrUpdateGitHubIssue.cs b/src/FilterLists.Agent/Features/Urls/CreateOrUpdateGitHubIssue.cs index c26c8684f..bb4472d14 100644 --- a/src/FilterLists.Agent/Features/Urls/CreateOrUpdateGitHubIssue.cs +++ b/src/FilterLists.Agent/Features/Urls/CreateOrUpdateGitHubIssue.cs @@ -1,8 +1,9 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using FilterLists.Agent.Features.Urls.Models.ValidationResults; +using FilterLists.Agent.Infrastructure.Clients; using MediatR; using Octokit; @@ -22,16 +23,49 @@ public Command(IEnumerable dataFileUrlValidationRe public class Handler : AsyncRequestHandler { - private readonly IGitHubClient _gitHubClient; + private const string AgentBotLabel = "agent bot"; + private const string HelpWantedLabel = "help wanted"; + private const string IssueTitle = "Url Validation Errors"; + private readonly IAgentGitHubClient _gitHubClient; - public Handler(IGitHubClient gitHubClient) + public Handler(IAgentGitHubClient agentGitHubClient) { - _gitHubClient = gitHubClient; + _gitHubClient = agentGitHubClient; } protected override async Task Handle(Command request, CancellationToken cancellationToken) { - throw new NotImplementedException(); + var issue = await GetOpenIssueOrNull() ?? await CreateBaseIssue(); + await UpdateIssue(issue); + } + + private async Task GetOpenIssueOrNull() + { + var issueRequest = new RepositoryIssueRequest + { + Filter = IssueFilter.All, + State = ItemStateFilter.Open, + Labels = {AgentBotLabel} + }; + return (await _gitHubClient.GetAllIssues(issueRequest)).FirstOrDefault(); + } + + private async Task CreateBaseIssue() + { + var newIssue = new NewIssue(IssueTitle); + return await _gitHubClient.CreateIssue(newIssue); + } + + private async Task UpdateIssue(Issue issue) + { + var updateIssue = issue.ToUpdate(); + updateIssue.AddLabel(AgentBotLabel); + updateIssue.AddLabel(HelpWantedLabel); + + //TODO: set real body from request + updateIssue.Body = "Testing update of auto-issue from FilterLists.Agent."; + + await _gitHubClient.UpdateIssue(issue.Number, updateIssue); } } } diff --git a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs index 31e5765bd..90d5a14ed 100644 --- a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs +++ b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs @@ -9,6 +9,8 @@ namespace FilterLists.Agent.Infrastructure.Clients public interface IAgentGitHubClient { Task> GetAllIssues(RepositoryIssueRequest repositoryIssueRequest); + Task CreateIssue(NewIssue newIssue); + Task UpdateIssue(int issueNumber, IssueUpdate updateIssue); } public class AgentGitHubClient : IAgentGitHubClient @@ -30,5 +32,17 @@ public async Task> GetAllIssues(RepositoryIssueRequest repo return await _gitHubClient.Issue.GetAllForRepository(_gitHubOptions.RepositoryOwner, _gitHubOptions.Repository, repositoryIssueRequest); } + + public async Task CreateIssue(NewIssue newIssue) + { + return await _gitHubClient.Issue.Create(_gitHubOptions.RepositoryOwner, _gitHubOptions.Repository, + newIssue); + } + + public async Task UpdateIssue(int issueNumber, IssueUpdate updateIssue) + { + return await _gitHubClient.Issue.Update(_gitHubOptions.RepositoryOwner, _gitHubOptions.Repository, + issueNumber, updateIssue); + } } } \ No newline at end of file