< Summary

Class:ApplicationCore.PullRequestService
Assembly:ApplicationCore
File(s):C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Services\PullRequest\PullRequestService.cs
Covered lines:54
Uncovered lines:0
Coverable lines:54
Total lines:87
Line coverage:100% (54 of 54)
Covered branches:5
Total branches:6
Branch coverage:83.3% (5 of 6)

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)10100%100%
TryCreatePullRequestAsync()60100%83.33%

File(s)

C:\Users\Teknikaali\Source\Repos\jekyll-blog-comments\ApplicationCore\Services\PullRequest\PullRequestService.cs

#LineLine coverage
 1using System;
 2using System.Threading.Tasks;
 3using ApplicationCore.Model;
 4using Octokit;
 5using YamlDotNet.Serialization;
 6
 7namespace 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
 516        public PullRequestService(
 517            GitHubConfig gitHubConfig,
 518            CommentConfig commentConfig,
 519            ISerializer serializer,
 520            IGitHubClient github)
 521        {
 522            _gitHubConfig = gitHubConfig;
 523            _commentConfig = commentConfig;
 524            _serializer = serializer;
 525            _github = github;
 526        }
 27
 28        public async Task<PullRequestResult> TryCreatePullRequestAsync(Comment comment)
 329        {
 330            if (comment is null)
 131            {
 132                throw new ArgumentNullException(nameof(comment));
 33            }
 34
 35            // Get a reference to our GitHub repository
 236            var repoOwnerName = _gitHubConfig.PullRequestRepository.Split('/');
 37            Repository repository;
 38
 39            try
 240            {
 241                repository = await _github.Repository.Get(repoOwnerName[0], repoOwnerName[1])
 242                    .ConfigureAwait(false);
 143            }
 144            catch (ApiException e)
 145            {
 146                return new PullRequestResult(e.Message);
 47            }
 48
 49            // Create a new branch from the default branch
 150            var defaultBranch = await _github.Repository.Branch.Get(repository.Id, repository.DefaultBranch)
 151                .ConfigureAwait(false);
 152            var newBranch = await _github.Git.Reference.Create(
 153                repository.Id,
 154                new NewReference($"refs/heads/comment-{comment.Id}", defaultBranch.Commit.Sha))
 155                .ConfigureAwait(false);
 56
 57            // Create a new file with the comments in it
 158            var fileRequest = new CreateFileRequest(
 159                $"Comment by {comment.Name} on {comment.PostId}",
 160                _serializer.Serialize(comment),
 161                newBranch.Ref) // TODO: NEWBRANCH REF NULL
 162            {
 163                Committer = new Committer(
 164                    comment.Name,
 165                    comment.Email ?? _commentConfig.FallbackCommitEmailAddress.ToString(),
 166                    comment.Date)
 167            };
 168            await _github.Repository.Content.CreateFile(
 169                repository.Id,
 170                $"_data/comments/{comment.PostId}/{comment.Id}.yml",
 171                fileRequest)
 172                .ConfigureAwait(false);
 73
 74            // Create a pull request for the new branch and file
 175            await _github.Repository.PullRequest.Create(
 176                repository.Id,
 177                new NewPullRequest(fileRequest.Message, newBranch.Ref, defaultBranch.Name)
 178                {
 179                    Body = $"avatar: <img src=\"{comment.Avatar}\" width=\"64\" height=\"64\" />" +
 180                    $"\n\nScore: {comment.Score}" +
 181                    $"\n\n{comment.Message}"
 182                }).ConfigureAwait(false);
 83
 184            return new PullRequestResult();
 285        }
 86    }
 87}