Finalized integration testing for review comments

This commit is contained in:
Gabriel Weyer
2014-05-04 10:46:35 +10:00
parent 7223518239
commit f23ddf81f9
11 changed files with 279 additions and 91 deletions

View File

@@ -5,6 +5,11 @@ namespace Octokit.Reactive
{
public interface IObservablePullRequestsClient
{
/// <summary>
/// Client for managing comments.
/// </summary>
IObservablePullRequestReviewCommentsClient Comment { get; }
/// <summary>
/// Gets a single Pull Request by number.
/// </summary>

View File

@@ -9,12 +9,18 @@ namespace Octokit.Reactive
readonly IPullRequestsClient _client;
readonly IConnection _connection;
/// <summary>
/// Client for managing comments.
/// </summary>
public IObservablePullRequestReviewCommentsClient Comment { get; private set; }
public ObservablePullRequestsClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");
_client = client.Repository.PullRequest;
_connection = client.Connection;
Comment = new ObservablePullRequestReviewCommentsClient(client);
}
/// <summary>

View File

@@ -10,6 +10,7 @@
IObservableMiscellaneousClient Miscellaneous { get; }
IObservableOauthClient Oauth { get; }
IObservableOrganizationsClient Organization { get; }
IObservablePullRequestsClient PullRequest { get; }
IObservableRepositoriesClient Repository { get; }
IObservableGistsClient Gist { get; }
IObservableReleasesClient Release { get; }

View File

@@ -38,6 +38,7 @@ namespace Octokit.Reactive
Notification = new ObservableNotificationsClient(gitHubClient);
Oauth = new ObservableOauthClient(gitHubClient);
Organization = new ObservableOrganizationsClient(gitHubClient);
PullRequest = new ObservablePullRequestsClient(gitHubClient);
Repository = new ObservableRepositoriesClient(gitHubClient);
SshKey = new ObservableSshKeysClient(gitHubClient);
User = new ObservableUsersClient(gitHubClient);
@@ -58,6 +59,7 @@ namespace Octokit.Reactive
public IObservableMiscellaneousClient Miscellaneous { get; private set; }
public IObservableOauthClient Oauth { get; private set; }
public IObservableOrganizationsClient Organization { get; private set; }
public IObservablePullRequestsClient PullRequest { get; private set; }
public IObservableRepositoriesClient Repository { get; private set; }
public IObservableGistsClient Gist { get; private set; }
public IObservableReleasesClient Release { get; private set; }

View File

@@ -11,16 +11,15 @@ using Xunit;
public class PullRequestReviewCommentsClientTests : IDisposable
{
IGitHubClient _gitHubClient;
Repository _repository;
List<Repository> _repositoriesToDelete = new List<Repository>();
IPullRequestReviewCommentsClient _client;
string _ownerName;
string _repoName;
string _branchName;
const string _branchName = "heads/new-branch";
const string _path = "CONTRIBUTING.md";
string _repoName;
int _pullRequestNumber;
string _pullRequestCommitId;
string _path;
public PullRequestReviewCommentsClientTests()
{
@@ -29,109 +28,276 @@ public class PullRequestReviewCommentsClientTests : IDisposable
Credentials = Helper.Credentials
};
// Creating a repository
_client = _gitHubClient.PullRequest.Comment;
_repoName = Helper.MakeNameWithTimestamp("public-repo");
_repository = _gitHubClient.Repository.Create(new NewRepository { Name = _repoName }).Result;
_ownerName = _repository.Owner.Name;
// We'll create a pull request that can be used by most tests
var pullRequestData = CreatePullRequest().Result;
_repositoriesToDelete.Add(pullRequestData.Repository);
_repoName = pullRequestData.RepoName;
_pullRequestNumber = pullRequestData.PullRequestNumber;
_pullRequestCommitId = pullRequestData.PullRequestCommitId;
}
[IntegrationTest]
public async Task CanCreateAndRetrieveComment()
{
var body = "A review comment message";
var position = 1;
var createdCommentId = await CreateComment(body, position);
var commentFromGitHub = await _client.GetComment(Helper.UserName, _repoName, createdCommentId);
AssertComment(commentFromGitHub, body, position);
}
[IntegrationTest]
public async Task CanEditComment()
{
var body = "A new review comment message";
var position = 1;
var createdCommentId = await CreateComment(body, position);
var edit = new PullRequestReviewCommentEdit("Edited Comment");
var editedComment = await _client.Edit(Helper.UserName, _repoName, createdCommentId, edit);
var commentFromGitHub = await _client.GetComment(Helper.UserName, _repoName, editedComment.Id);
AssertComment(commentFromGitHub, edit.Body, position);
}
[IntegrationTest]
public async Task CanDeleteComment()
{
var body = "A new review comment message";
var position = 1;
var createdCommentId = await CreateComment(body, position);
var edit = new PullRequestReviewCommentEdit("Edited Comment");
Assert.DoesNotThrow(async () => { await _client.Delete(Helper.UserName, _repoName, createdCommentId); });
}
[IntegrationTest]
public async Task CanCreateReply()
{
var body = "Reply me!";
var position = 1;
var createdCommentId = await CreateComment(body, position);
var reply = new PullRequestReviewCommentReplyCreate("Replied", createdCommentId);
var createdReply = await _client.CreateReply(Helper.UserName, _repoName, _pullRequestNumber, reply);
var createdReplyFromGitHub = await _client.GetComment(Helper.UserName, _repoName, createdReply.Id);
AssertComment(createdReplyFromGitHub, reply.Body, position);
}
[IntegrationTest]
public async Task CanGetForPullRequest()
{
var pullRequest = await CreatePullRequest();
_repositoriesToDelete.Add(pullRequest.Repository);
var position = 1;
List<string> commentsToCreate = new List<string> { "Comment 1", "Comment 2", "Comment 3" };
await CreateComments(commentsToCreate, position, pullRequest.RepoName, pullRequest.PullRequestCommitId, pullRequest.PullRequestNumber);
var pullRequestComments = await _client.GetForPullRequest(Helper.UserName, pullRequest.RepoName, pullRequest.PullRequestNumber);
AssertComments(pullRequestComments, commentsToCreate, position);
}
[IntegrationTest]
public async Task CanGetForRepository()
{
var pullRequest = await CreatePullRequest();
_repositoriesToDelete.Add(pullRequest.Repository);
var position = 1;
List<string> commentsToCreate = new List<string> { "Comment One", "Comment Two" };
await CreateComments(commentsToCreate, position, pullRequest.RepoName, pullRequest.PullRequestCommitId, pullRequest.PullRequestNumber);
var pullRequestComments = await _client.GetForRepository(Helper.UserName, pullRequest.RepoName);
AssertComments(pullRequestComments, commentsToCreate, position);
}
[IntegrationTest]
public async Task CanGetForRepositoryAscendingSort()
{
var pullRequest = await CreatePullRequest();
_repositoriesToDelete.Add(pullRequest.Repository);
var position = 1;
List<string> commentsToCreate = new List<string> { "Comment One", "Comment Two", "Comment Three", "Comment Four" };
await CreateComments(commentsToCreate, position, pullRequest.RepoName, pullRequest.PullRequestCommitId, pullRequest.PullRequestNumber);
var pullRequestComments = await _client.GetForRepository(Helper.UserName, pullRequest.RepoName, new PullRequestReviewCommentRequest { Direction = SortDirection.Ascending });
AssertComments(pullRequestComments, commentsToCreate, position);
}
[IntegrationTest]
public async Task CanGetForRepositoryDescendingSort()
{
var pullRequest = await CreatePullRequest();
_repositoriesToDelete.Add(pullRequest.Repository);
var position = 1;
List<string> commentsToCreate = new List<string> { "Comment One", "Comment Two", "Comment Three", "Comment Four" };
await CreateComments(commentsToCreate, position, pullRequest.RepoName, pullRequest.PullRequestCommitId, pullRequest.PullRequestNumber);
var pullRequestComments = await _client.GetForRepository(Helper.UserName, pullRequest.RepoName, new PullRequestReviewCommentRequest { Direction = SortDirection.Descending });
commentsToCreate.Reverse();
AssertComments(pullRequestComments, commentsToCreate, position);
}
public void Dispose()
{
foreach (var repo in _repositoriesToDelete)
{
Helper.DeleteRepo(repo);
}
}
private async Task<int> CreateComment(string body, int position)
{
return await CreateComment(body, position, _repoName, _pullRequestCommitId, _pullRequestNumber);
}
private async Task<int> CreateComment(string body, int position, string repoName, string pullRequestCommitId, int pullRequestNumber)
{
var comment = new PullRequestReviewCommentCreate(body, pullRequestCommitId, _path, position);
var createdComment = await _client.Create(Helper.UserName, repoName, pullRequestNumber, comment);
AssertComment(createdComment, body, position);
return createdComment.Id;
}
private async Task CreateComments(List<string> comments, int position, string repoName, string pullRequestCommitId, int pullRequestNumber)
{
foreach (var comment in comments)
{
await CreateComment(comment, position, repoName, pullRequestCommitId, pullRequestNumber);
}
}
private void AssertComment(PullRequestReviewComment comment, string body, int position)
{
Assert.NotNull(comment);
Assert.Equal(body, comment.Body);
Assert.Equal(position, comment.Position);
}
private void AssertComments(IReadOnlyList<PullRequestReviewComment> comments, List<string> bodies, int position)
{
Assert.Equal(bodies.Count, comments.Count);
for (int i = 0; i < bodies.Count; i = i + 1)
{
AssertComment(comments[i], bodies[i], position);
}
}
private async Task<Repository> CreateRepository(string repoName)
{
return await _gitHubClient.Repository.Create(new NewRepository { Name = repoName, AutoInit = true });
}
/// <summary>
/// Creates the base state for testing (creates a repo, a commit in master, a branch, a commit in the branch and a pull request)
/// </summary>
/// <returns></returns>
private async Task<PullRequestData> CreatePullRequest()
{
var repoName = Helper.MakeNameWithTimestamp("test-repo");
var repository = await CreateRepository(repoName);
// Creating a commit in master
var createdCommitInMaster = await CreateCommit(repoName, "Hello World!", "README.md", "heads/master", "A master commit message");
// Creating a branch
var newBranch = new NewReference("refs/" + _branchName, createdCommitInMaster.Sha);
var branchReference = await _gitHubClient.GitDatabase.Reference.Create(Helper.UserName, repoName, newBranch);
// Creating a commit in the branch
var createdCommitInBranch = await CreateCommit(repoName, "Hello from the fork!", _path, _branchName, "A branch commit message");
// Creating a pull request
var pullRequest = new NewPullRequest("Nice title for the pull request", _branchName, "master");
var createdPullRequest = await _gitHubClient.PullRequest.Create(Helper.UserName, repoName, pullRequest);
var data = new PullRequestData
{
PullRequestCommitId = createdCommitInBranch.Sha,
PullRequestNumber = createdPullRequest.Number,
RepoName = repoName,
Repository = repository,
};
return data;
}
private async Task<Commit> CreateCommit(string repoName, string blobContent, string treePath, string reference, string commitMessage)
{
// Creating a blob
var blob = new NewBlob
{
Content = "Hello World!",
Content = blobContent,
Encoding = EncodingType.Utf8
};
var createdBlob = _gitHubClient.GitDatabase.Blob.Create(_ownerName, _repoName, blob).Result;
var createdBlob = await _gitHubClient.GitDatabase.Blob.Create(Helper.UserName, repoName, blob);
// Creating a tree
var treeSha = createdBlob.Sha;
var newTree = new NewTree();
newTree.Tree.Add(new NewTreeItem
{
Type = TreeType.Blob,
Mode = FileMode.File,
Path = "README.md",
Sha = treeSha,
Path = treePath,
Sha = createdBlob.Sha,
});
var createdTree = _gitHubClient.GitDatabase.Tree.Create(_ownerName, _repoName, newTree).Result;
var createdTree = await _gitHubClient.GitDatabase.Tree.Create(Helper.UserName, repoName, newTree);
var treeSha = createdTree.Sha;
// Creating a commit
var commit = new NewCommit("A nice commit message", treeSha, Enumerable.Empty<string>());
var parent = await _gitHubClient.GitDatabase.Reference.Get(Helper.UserName, repoName, reference);
var commit = new NewCommit(commitMessage, treeSha, parent.Object.Sha);
var createdCommit = _gitHubClient.GitDatabase.Commit.Create(_ownerName, _repoName, commit).Result;
var createdCommit = await _gitHubClient.GitDatabase.Commit.Create(Helper.UserName, repoName, commit);
await _gitHubClient.GitDatabase.Reference.Update(Helper.UserName, repoName, reference, new ReferenceUpdate(createdCommit.Sha));
// Creating a branch
var newBranch = new NewReference("refs/heads/new-branch", createdCommit.Sha);
var reference = _gitHubClient.GitDatabase.Reference.Create(_ownerName, _repoName, newBranch).Result;
_branchName = reference.Ref;
// Creating a blob in the branch
var pullRequestBlob = new NewBlob
{
Content = "Hello from the fork!",
Encoding = EncodingType.Utf8
};
var createdPullRequestBlob = _gitHubClient.GitDatabase.Blob.Create(_ownerName, _branchName, pullRequestBlob).Result;
// Creating a tree in the branch
var pullRequestTreeSha = createdPullRequestBlob.Sha;
_path = "CONTRIBUTING.md";
var pullRequestTree = new NewTree();
pullRequestTree.Tree.Add(new NewTreeItem
{
Type = TreeType.Blob,
Mode = FileMode.File,
Path = _path,
Sha = pullRequestTreeSha,
});
var createdPullRequestTree = _gitHubClient.GitDatabase.Tree.Create(_ownerName, _branchName, pullRequestTree).Result;
// Creating a commit in the branch
var pullRequestcommit = new NewCommit("A pull request commit message", pullRequestTreeSha, Enumerable.Empty<string>());
var createdPullRequestCommit = _gitHubClient.GitDatabase.Commit.Create(_ownerName, _branchName, pullRequestcommit).Result;
_pullRequestCommitId = createdPullRequestCommit.Sha;
// Creating a pull request
// TODO Not Implemented: http://developer.github.com/v3/pulls/#create-a-pull-request
_pullRequestNumber = 0;
_client = _gitHubClient.PullRequest.Comment;
}
[IntegrationTest(Skip = "Requires Pull Request Api implementation")]
public async Task CanCreateAndRetrieveReviewComment()
{
var pullRequestReviewComment = new PullRequestReviewCommentCreate("A review comment message", _pullRequestCommitId, _path, 1);
var createdComment = await _client.Create(_ownerName, _repoName, _pullRequestNumber, pullRequestReviewComment);
Assert.NotNull(createdComment);
Assert.Equal(pullRequestReviewComment.Body, createdComment.Body);
var commentFromGitHub = await _client.GetComment(_ownerName, _repoName, createdComment.Id);
Assert.NotNull(commentFromGitHub);
Assert.Equal(pullRequestReviewComment.Body, commentFromGitHub.Body);
}
public void Dispose()
{
Helper.DeleteRepo(_repository);
// TODO Ensure that it deletes the branch too
return createdCommit;
}
}
class PullRequestData
{
public Repository Repository { get; set; }
public int PullRequestNumber { get; set; }
public string PullRequestCommitId { get; set; }
public string RepoName { get; set; }
}

View File

@@ -1,7 +1,9 @@
using Octokit.Internal;
using System.Diagnostics;
using Octokit.Internal;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PullRequestReviewCommentCreate : RequestParameters
{
/// <summary>
@@ -42,7 +44,5 @@ namespace Octokit
/// The line index in the diff to comment on.
/// </summary>
public int Position { get; private set; }
}
}

View File

@@ -1,6 +1,8 @@

using System.Diagnostics;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PullRequestReviewCommentEdit : RequestParameters
{
/// <summary>

View File

@@ -1,7 +1,9 @@
using Octokit.Internal;
using System.Diagnostics;
using Octokit.Internal;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PullRequestReviewCommentReplyCreate : RequestParameters
{
/// <summary>

View File

@@ -1,7 +1,9 @@
using System;
using System.Diagnostics;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PullRequestReviewCommentRequest : RequestParameters
{
public PullRequestReviewCommentRequest()

View File

@@ -1,7 +1,9 @@
using System;
using System.Diagnostics;
namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class PullRequestReviewComment
{
/// <summary>

View File

@@ -87,7 +87,7 @@ Target "IntegrationTests" (fun _ ->
XmlOutput = true
Verbose = false
OutputDir = testResultsDir
TimeOut = TimeSpan.FromMinutes 10.0 })
TimeOut = TimeSpan.FromMinutes 20.0 })
else
"The integration tests were skipped because the OCTOKIT_GITHUBUSERNAME and OCTOKIT_GITHUBPASSWORD environment variables are not set. " +
"Please configure these environment variables for a GitHub test account (DO NOT USE A \"REAL\" ACCOUNT)."