| | 1 | | using System; |
| | 2 | | using System.Threading.Tasks; |
| | 3 | | using ApplicationCore.Model; |
| | 4 | | using Octokit; |
| | 5 | | using YamlDotNet.Serialization; |
| | 6 | |
|
| | 7 | | namespace ApplicationCore |
| | 8 | | { |
| | 9 | | public class PullRequestService : IPullRequestService |
| | 10 | | { |
| | 11 | | private readonly GitHubConfig _gitHubConfig; |
| | 12 | | private readonly CommentConfig _commentConfig; |
| | 13 | | private readonly ISerializer _serializer; |
| | 14 | | private readonly IGitHubClient _github; |
| | 15 | |
|
| 5 | 16 | | public PullRequestService( |
| 5 | 17 | | GitHubConfig gitHubConfig, |
| 5 | 18 | | CommentConfig commentConfig, |
| 5 | 19 | | ISerializer serializer, |
| 5 | 20 | | IGitHubClient github) |
| 5 | 21 | | { |
| 5 | 22 | | _gitHubConfig = gitHubConfig; |
| 5 | 23 | | _commentConfig = commentConfig; |
| 5 | 24 | | _serializer = serializer; |
| 5 | 25 | | _github = github; |
| 5 | 26 | | } |
| | 27 | |
|
| | 28 | | public async Task<PullRequestResult> TryCreatePullRequestAsync(Comment comment) |
| 3 | 29 | | { |
| 3 | 30 | | if (comment is null) |
| 1 | 31 | | { |
| 1 | 32 | | throw new ArgumentNullException(nameof(comment)); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | // Get a reference to our GitHub repository |
| 2 | 36 | | var repoOwnerName = _gitHubConfig.PullRequestRepository.Split('/'); |
| | 37 | | Repository repository; |
| | 38 | |
|
| | 39 | | try |
| 2 | 40 | | { |
| 2 | 41 | | repository = await _github.Repository.Get(repoOwnerName[0], repoOwnerName[1]) |
| 2 | 42 | | .ConfigureAwait(false); |
| 1 | 43 | | } |
| 1 | 44 | | catch (ApiException e) |
| 1 | 45 | | { |
| 1 | 46 | | return new PullRequestResult(e.Message); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | // Create a new branch from the default branch |
| 1 | 50 | | var defaultBranch = await _github.Repository.Branch.Get(repository.Id, repository.DefaultBranch) |
| 1 | 51 | | .ConfigureAwait(false); |
| 1 | 52 | | var newBranch = await _github.Git.Reference.Create( |
| 1 | 53 | | repository.Id, |
| 1 | 54 | | new NewReference($"refs/heads/comment-{comment.Id}", defaultBranch.Commit.Sha)) |
| 1 | 55 | | .ConfigureAwait(false); |
| | 56 | |
|
| | 57 | | // Create a new file with the comments in it |
| 1 | 58 | | var fileRequest = new CreateFileRequest( |
| 1 | 59 | | $"Comment by {comment.Name} on {comment.PostId}", |
| 1 | 60 | | _serializer.Serialize(comment), |
| 1 | 61 | | newBranch.Ref) // TODO: NEWBRANCH REF NULL |
| 1 | 62 | | { |
| 1 | 63 | | Committer = new Committer( |
| 1 | 64 | | comment.Name, |
| 1 | 65 | | comment.Email ?? _commentConfig.FallbackCommitEmailAddress.ToString(), |
| 1 | 66 | | comment.Date) |
| 1 | 67 | | }; |
| 1 | 68 | | await _github.Repository.Content.CreateFile( |
| 1 | 69 | | repository.Id, |
| 1 | 70 | | $"_data/comments/{comment.PostId}/{comment.Id}.yml", |
| 1 | 71 | | fileRequest) |
| 1 | 72 | | .ConfigureAwait(false); |
| | 73 | |
|
| | 74 | | // Create a pull request for the new branch and file |
| 1 | 75 | | await _github.Repository.PullRequest.Create( |
| 1 | 76 | | repository.Id, |
| 1 | 77 | | new NewPullRequest(fileRequest.Message, newBranch.Ref, defaultBranch.Name) |
| 1 | 78 | | { |
| 1 | 79 | | Body = $"avatar: <img src=\"{comment.Avatar}\" width=\"64\" height=\"64\" />" + |
| 1 | 80 | | $"\n\nScore: {comment.Score}" + |
| 1 | 81 | | $"\n\n{comment.Message}" |
| 1 | 82 | | }).ConfigureAwait(false); |
| | 83 | |
|
| 1 | 84 | | return new PullRequestResult(); |
| 2 | 85 | | } |
| | 86 | | } |
| | 87 | | } |