/Octokit.Tests.Integration/Clients/PullRequestReviewCommentReactionsClientTests.cs

https://gitlab.com/Hexexpeck/GitHub-API-.NET · C# · 228 lines · 151 code · 65 blank · 12 comment · 0 complexity · ce0979dfcae02f398a44e28139641cc6 MD5 · raw file

  1. using System;
  2. using System.Threading.Tasks;
  3. using Octokit;
  4. using Octokit.Tests.Integration;
  5. using Octokit.Tests.Integration.Helpers;
  6. using Xunit;
  7. public class PullRequestReviewCommentReactionsClientTests : IDisposable
  8. {
  9. private readonly IGitHubClient _github;
  10. private readonly IPullRequestReviewCommentsClient _client;
  11. private readonly RepositoryContext _context;
  12. const string branchName = "new-branch";
  13. const string branchHead = "heads/" + branchName;
  14. const string branchRef = "refs/" + branchHead;
  15. const string path = "CONTRIBUTING.md";
  16. public PullRequestReviewCommentReactionsClientTests()
  17. {
  18. _github = Helper.GetAuthenticatedClient();
  19. _client = _github.PullRequest.Comment;
  20. // We'll create a pull request that can be used by most tests
  21. _context = _github.CreateRepositoryContext("test-repo").Result;
  22. }
  23. [IntegrationTest]
  24. public async Task CanListReactions()
  25. {
  26. var pullRequest = await CreatePullRequest(_context);
  27. const string body = "A review comment message";
  28. const int position = 1;
  29. var createdComment = await CreateComment(body, position, pullRequest.Sha, pullRequest.Number);
  30. var commentFromGitHub = await _client.GetComment(Helper.UserName, _context.RepositoryName, createdComment.Id);
  31. AssertComment(commentFromGitHub, body, position);
  32. var reaction = await _github.Reaction.PullRequestReviewComment.Create(_context.RepositoryOwner, _context.RepositoryName, commentFromGitHub.Id, new NewReaction(ReactionType.Heart));
  33. var reactions = await _github.Reaction.PullRequestReviewComment.GetAll(_context.RepositoryOwner, _context.RepositoryName, commentFromGitHub.Id);
  34. Assert.NotEmpty(reactions);
  35. Assert.Equal(reaction.Id, reactions[0].Id);
  36. Assert.Equal(reaction.Content, reactions[0].Content);
  37. }
  38. [IntegrationTest]
  39. public async Task CanListReactionsWithRepositoryId()
  40. {
  41. var pullRequest = await CreatePullRequest(_context);
  42. const string body = "A review comment message";
  43. const int position = 1;
  44. var createdComment = await CreateComment(body, position, pullRequest.Sha, pullRequest.Number);
  45. var commentFromGitHub = await _client.GetComment(Helper.UserName, _context.RepositoryName, createdComment.Id);
  46. AssertComment(commentFromGitHub, body, position);
  47. var reaction = await _github.Reaction.PullRequestReviewComment.Create(_context.Repository.Id, commentFromGitHub.Id, new NewReaction(ReactionType.Heart));
  48. var reactions = await _github.Reaction.PullRequestReviewComment.GetAll(_context.Repository.Id, commentFromGitHub.Id);
  49. Assert.NotEmpty(reactions);
  50. Assert.Equal(reaction.Id, reactions[0].Id);
  51. Assert.Equal(reaction.Content, reactions[0].Content);
  52. }
  53. [IntegrationTest]
  54. public async Task CanCreateReaction()
  55. {
  56. var pullRequest = await CreatePullRequest(_context);
  57. const string body = "A review comment message";
  58. const int position = 1;
  59. var createdComment = await CreateComment(body, position, pullRequest.Sha, pullRequest.Number);
  60. var commentFromGitHub = await _client.GetComment(Helper.UserName, _context.RepositoryName, createdComment.Id);
  61. AssertComment(commentFromGitHub, body, position);
  62. foreach (ReactionType reactionType in Enum.GetValues(typeof(ReactionType)))
  63. {
  64. var newReaction = new NewReaction(reactionType);
  65. var reaction = await _github.Reaction.PullRequestReviewComment.Create(_context.RepositoryOwner, _context.RepositoryName, commentFromGitHub.Id, newReaction);
  66. Assert.IsType<Reaction>(reaction);
  67. Assert.Equal(reactionType, reaction.Content);
  68. Assert.Equal(commentFromGitHub.User.Id, reaction.User.Id);
  69. }
  70. }
  71. [IntegrationTest]
  72. public async Task CanCreateReactionWithRepositoryId()
  73. {
  74. var pullRequest = await CreatePullRequest(_context);
  75. const string body = "A review comment message";
  76. const int position = 1;
  77. var createdComment = await CreateComment(body, position, pullRequest.Sha, pullRequest.Number);
  78. var commentFromGitHub = await _client.GetComment(Helper.UserName, _context.RepositoryName, createdComment.Id);
  79. AssertComment(commentFromGitHub, body, position);
  80. var pullRequestReviewCommentReaction = await _github.Reaction.PullRequestReviewComment.Create(_context.Repository.Id, commentFromGitHub.Id, new NewReaction(ReactionType.Heart));
  81. Assert.NotNull(pullRequestReviewCommentReaction);
  82. Assert.IsType<Reaction>(pullRequestReviewCommentReaction);
  83. Assert.Equal(ReactionType.Heart, pullRequestReviewCommentReaction.Content);
  84. Assert.Equal(commentFromGitHub.User.Id, pullRequestReviewCommentReaction.User.Id);
  85. }
  86. /// <summary>
  87. /// Creates the base state for testing (creates a repo, a commit in master, a branch, a commit in the branch and a pull request)
  88. /// </summary>
  89. /// <returns></returns>
  90. async Task<PullRequestData> CreatePullRequest(RepositoryContext context)
  91. {
  92. var repoName = context.RepositoryName;
  93. // Creating a commit in master
  94. var createdCommitInMaster = await CreateCommit(repoName, "Hello World!", "README.md", "heads/master", "A master commit message");
  95. // Creating a branch
  96. var newBranch = new NewReference(branchRef, createdCommitInMaster.Sha);
  97. await _github.Git.Reference.Create(Helper.UserName, repoName, newBranch);
  98. // Creating a commit in the branch
  99. var createdCommitInBranch = await CreateCommit(repoName, "Hello from the fork!", path, branchHead, "A branch commit message");
  100. // Creating a pull request
  101. var pullRequest = new NewPullRequest("Nice title for the pull request", branchName, "master");
  102. var createdPullRequest = await _github.PullRequest.Create(Helper.UserName, repoName, pullRequest);
  103. var data = new PullRequestData
  104. {
  105. Sha = createdCommitInBranch.Sha,
  106. Number = createdPullRequest.Number
  107. };
  108. return data;
  109. }
  110. async Task<Commit> CreateCommit(string repoName, string blobContent, string treePath, string reference, string commitMessage)
  111. {
  112. // Creating a blob
  113. var blob = new NewBlob
  114. {
  115. Content = blobContent,
  116. Encoding = EncodingType.Utf8
  117. };
  118. var createdBlob = await _github.Git.Blob.Create(Helper.UserName, repoName, blob);
  119. // Creating a tree
  120. var newTree = new NewTree();
  121. newTree.Tree.Add(new NewTreeItem
  122. {
  123. Type = TreeType.Blob,
  124. Mode = FileMode.File,
  125. Path = treePath,
  126. Sha = createdBlob.Sha
  127. });
  128. var createdTree = await _github.Git.Tree.Create(Helper.UserName, repoName, newTree);
  129. var treeSha = createdTree.Sha;
  130. // Creating a commit
  131. var parent = await _github.Git.Reference.Get(Helper.UserName, repoName, reference);
  132. var commit = new NewCommit(commitMessage, treeSha, parent.Object.Sha);
  133. var createdCommit = await _github.Git.Commit.Create(Helper.UserName, repoName, commit);
  134. await _github.Git.Reference.Update(Helper.UserName, repoName, reference, new ReferenceUpdate(createdCommit.Sha));
  135. return createdCommit;
  136. }
  137. async Task<PullRequestReviewComment> CreateComment(string body, int position, string commitId, int number)
  138. {
  139. return await CreateComment(body, position, _context.RepositoryName, commitId, number);
  140. }
  141. async Task<PullRequestReviewComment> CreateComment(string body, int position, string repoName, string pullRequestCommitId, int pullRequestNumber)
  142. {
  143. var comment = new PullRequestReviewCommentCreate(body, pullRequestCommitId, path, position);
  144. var createdComment = await _client.Create(Helper.UserName, repoName, pullRequestNumber, comment);
  145. AssertComment(createdComment, body, position);
  146. return createdComment;
  147. }
  148. static void AssertComment(PullRequestReviewComment comment, string body, int position)
  149. {
  150. Assert.NotNull(comment);
  151. Assert.Equal(body, comment.Body);
  152. Assert.Equal(position, comment.Position);
  153. }
  154. public void Dispose()
  155. {
  156. _context.Dispose();
  157. }
  158. class PullRequestData
  159. {
  160. public int Number { get; set; }
  161. public string Sha { get; set; }
  162. }
  163. }