mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-03 11:05:56 +00:00
Add Pull Request Review Request API. (#1588)
* Add Pull Request Review Request API. * Add Reactive Pull Request Review Request API. * Add PullRequestReviewRequestClient tests. * Add ObservablePullRequestReviewRequestClient tests. * Fix sub-client property naming. * Remove redundant model and update PullRequest model. * Add repositoryId based methods and missing Observable documentation. * Add missing parameter to PullRequest ctor. * Add integration tests for PullRequestReviewRequest. * Upgrade PullRequestReviewRequest integration tests. * Add integration tests for repositoryId methods and fix url bug. * Add missing unit tests and fix PR issues. * Add pagination support for PullRequestReviewRequst.GetAll and tests for it. * Revert changes on `PullRequestReviewCommentsClientTests.cs` * Small upgrades - remove unused using and compress property to expression body. * Revert use of expression body in property. * Add pagination tests for PullRequestReviewRequest.GetAll. * Change pagination tests to use 2 users. * Correct class/file name * Reword the integration test names for consistency Move the plumbing to create reviews into CreateTheWorld to clean up the actual tests * Fix DebuggerDisplay of requested reviewers * fix reviewRequestToCreate parameter to be consistent
This commit is contained in:
committed by
Ryan Gribble
parent
13593676d5
commit
97ae3cb3de
@@ -0,0 +1,296 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Octokit;
|
||||
using Octokit.Tests.Integration;
|
||||
using Octokit.Tests.Integration.Helpers;
|
||||
using Xunit;
|
||||
|
||||
public class PullRequestReviewRequestsClientTests
|
||||
{
|
||||
private static readonly string[] _collaboratorLogins = {
|
||||
"octokitnet-test1",
|
||||
"octokitnet-test2"
|
||||
};
|
||||
|
||||
public class PullRequestReviewRequestClientTestsBase : IDisposable
|
||||
{
|
||||
internal readonly IGitHubClient _github;
|
||||
internal readonly IPullRequestReviewRequestsClient _client;
|
||||
internal readonly RepositoryContext _context;
|
||||
|
||||
public PullRequestReviewRequestClientTestsBase()
|
||||
{
|
||||
_github = Helper.GetAuthenticatedClient();
|
||||
|
||||
_client = _github.PullRequest.ReviewRequest;
|
||||
|
||||
_context = _github.CreateRepositoryContext("test-repo").Result;
|
||||
|
||||
Task.WaitAll(_collaboratorLogins.Select(AddCollaborator).ToArray());
|
||||
}
|
||||
|
||||
private Task AddCollaborator(string collaborator) => _github.Repository.Collaborator.Add(_context.RepositoryOwner, _context.RepositoryName, collaborator);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_context.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public class TheGetAllMethod : PullRequestReviewRequestClientTestsBase
|
||||
{
|
||||
[IntegrationTest]
|
||||
public async Task GetsNoRequestsWhenNoneExist()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context, createReviewRequests: false);
|
||||
|
||||
var reviewRequests = await _client.GetAll(_context.RepositoryOwner, _context.RepositoryName, pullRequestId);
|
||||
|
||||
Assert.NotNull(reviewRequests);
|
||||
Assert.Empty(reviewRequests);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task GetsNoRequestsWhenNoneExistWithRepositoryId()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context, createReviewRequests: false);
|
||||
|
||||
var reviewRequests = await _client.GetAll(_context.RepositoryId, pullRequestId);
|
||||
|
||||
Assert.NotNull(reviewRequests);
|
||||
Assert.Empty(reviewRequests);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task GetsRequests()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context);
|
||||
|
||||
var reviewRequests = await _client.GetAll(_context.RepositoryOwner, _context.RepositoryName, pullRequestId);
|
||||
|
||||
Assert.Equal(_collaboratorLogins, reviewRequests.Select(rr => rr.Login));
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task GetsRequestsWithRepositoryId()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context);
|
||||
|
||||
var reviewRequests = await _client.GetAll(_context.RepositoryId, pullRequestId);
|
||||
|
||||
Assert.Equal(_collaboratorLogins, reviewRequests.Select(rr => rr.Login));
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task ReturnsCorrectCountOfReviewRequestsWithStart()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context);
|
||||
|
||||
var options = new ApiOptions
|
||||
{
|
||||
PageSize = 1,
|
||||
PageCount = 1,
|
||||
StartPage = 2
|
||||
};
|
||||
var reviewRequests = await _client.GetAll(_context.RepositoryOwner, _context.RepositoryName, pullRequestId, options);
|
||||
|
||||
Assert.Equal(1, reviewRequests.Count);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task ReturnsCorrectCountOfReviewRequestsWithStartWithRepositoryId()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context);
|
||||
|
||||
var options = new ApiOptions
|
||||
{
|
||||
PageSize = 1,
|
||||
PageCount = 2,
|
||||
StartPage = 2
|
||||
};
|
||||
var reviewRequests = await _client.GetAll(_context.RepositoryId, pullRequestId, options);
|
||||
|
||||
Assert.Equal(1, reviewRequests.Count);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task ReturnsDistinctResultsBasedOnStartPage()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context);
|
||||
|
||||
var startOptions = new ApiOptions
|
||||
{
|
||||
PageSize = 1,
|
||||
PageCount = 1
|
||||
};
|
||||
var firstPage = await _client.GetAll(_context.RepositoryOwner, _context.RepositoryName, pullRequestId, startOptions);
|
||||
|
||||
var skipStartOptions = new ApiOptions
|
||||
{
|
||||
PageSize = 1,
|
||||
PageCount = 1,
|
||||
StartPage = 2
|
||||
};
|
||||
var secondPage = await _client.GetAll(_context.RepositoryOwner, _context.RepositoryName, pullRequestId, skipStartOptions);
|
||||
|
||||
Assert.Equal(1, firstPage.Count);
|
||||
Assert.Equal(1, secondPage.Count);
|
||||
Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task ReturnsDistinctResultsBasedOnStartPageWithRepositoryId()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context);
|
||||
|
||||
var startOptions = new ApiOptions
|
||||
{
|
||||
PageSize = 1,
|
||||
PageCount = 1
|
||||
};
|
||||
var firstPage = await _client.GetAll(_context.RepositoryId, pullRequestId, startOptions);
|
||||
|
||||
var skipStartOptions = new ApiOptions
|
||||
{
|
||||
PageSize = 1,
|
||||
PageCount = 1,
|
||||
StartPage = 2
|
||||
};
|
||||
var secondPage = await _client.GetAll(_context.RepositoryId, pullRequestId, skipStartOptions);
|
||||
|
||||
Assert.Equal(1, firstPage.Count);
|
||||
Assert.Equal(1, secondPage.Count);
|
||||
Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
|
||||
}
|
||||
}
|
||||
|
||||
public class TheDeleteMethod : PullRequestReviewRequestClientTestsBase
|
||||
{
|
||||
[IntegrationTest]
|
||||
public async Task DeletesRequests()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context);
|
||||
|
||||
var reviewRequestsBeforeDelete = await _client.GetAll(_context.RepositoryOwner, _context.RepositoryName, pullRequestId);
|
||||
var reviewRequestToCreate = new PullRequestReviewRequest(_collaboratorLogins);
|
||||
await _client.Delete(_context.RepositoryOwner, _context.RepositoryName, pullRequestId, reviewRequestToCreate);
|
||||
var reviewRequestsAfterDelete = await _client.GetAll(_context.RepositoryOwner, _context.RepositoryName, pullRequestId);
|
||||
|
||||
Assert.NotEmpty(reviewRequestsBeforeDelete);
|
||||
Assert.Empty(reviewRequestsAfterDelete);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task DeletesRequestsWithRepositoryId()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context);
|
||||
|
||||
var reviewRequestsBeforeDelete = await _client.GetAll(_context.RepositoryId, pullRequestId);
|
||||
var reviewRequestToCreate = new PullRequestReviewRequest(_collaboratorLogins);
|
||||
await _client.Delete(_context.RepositoryId, pullRequestId, reviewRequestToCreate);
|
||||
var reviewRequestsAfterDelete = await _client.GetAll(_context.RepositoryId, pullRequestId);
|
||||
|
||||
Assert.NotEmpty(reviewRequestsBeforeDelete);
|
||||
Assert.Empty(reviewRequestsAfterDelete);
|
||||
}
|
||||
}
|
||||
|
||||
public class TheCreateMethod : PullRequestReviewRequestClientTestsBase, IDisposable
|
||||
{
|
||||
[IntegrationTest]
|
||||
public async Task CreatesRequests()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context, createReviewRequests: false);
|
||||
var reviewRequestToCreate = new PullRequestReviewRequest(_collaboratorLogins);
|
||||
|
||||
var pr = await _client.Create(_context.RepositoryOwner, _context.RepositoryName, pullRequestId, reviewRequestToCreate);
|
||||
|
||||
Assert.Equal(_collaboratorLogins.ToList(), pr.RequestedReviewers.Select(rr => rr.Login));
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task CreatesRequestsWithRepositoryId()
|
||||
{
|
||||
var pullRequestId = await CreateTheWorld(_github, _context, createReviewRequests: false);
|
||||
var reviewRequestToCreate = new PullRequestReviewRequest(_collaboratorLogins);
|
||||
|
||||
var pr = await _client.Create(_context.RepositoryId, pullRequestId, reviewRequestToCreate);
|
||||
|
||||
Assert.Equal(_collaboratorLogins.ToList(), pr.RequestedReviewers.Select(rr => rr.Login));
|
||||
}
|
||||
}
|
||||
|
||||
static async Task<int> CreateTheWorld(IGitHubClient github, RepositoryContext context, bool createReviewRequests = true)
|
||||
{
|
||||
var master = await github.Git.Reference.Get(context.RepositoryOwner, context.RepositoryName, "heads/master");
|
||||
|
||||
// create new commit for master branch
|
||||
var newMasterTree = await CreateTree(github, context, new Dictionary<string, string> { { "README.md", "Hello World!" } });
|
||||
var newMaster = await CreateCommit(github, context, "baseline for pull request", newMasterTree.Sha, master.Object.Sha);
|
||||
|
||||
// update master
|
||||
await github.Git.Reference.Update(context.RepositoryOwner, context.RepositoryName, "heads/master", new ReferenceUpdate(newMaster.Sha));
|
||||
|
||||
// create new commit for feature branch
|
||||
var featureBranchTree = await CreateTree(github, context, new Dictionary<string, string> { { "README.md", "I am overwriting this blob with something new" } });
|
||||
var featureBranchCommit = await CreateCommit(github, context, "this is the commit to merge into the pull request", featureBranchTree.Sha, newMaster.Sha);
|
||||
|
||||
var featureBranchTree2 = await CreateTree(github, context, new Dictionary<string, string> { { "README.md", "I am overwriting this blob with something new a 2nd time" } });
|
||||
var featureBranchCommit2 = await CreateCommit(github, context, "this is a 2nd commit to merge into the pull request", featureBranchTree2.Sha, featureBranchCommit.Sha);
|
||||
|
||||
// create branch
|
||||
await github.Git.Reference.Create(context.RepositoryOwner, context.RepositoryName, new NewReference("refs/heads/my-branch", featureBranchCommit2.Sha));
|
||||
|
||||
// create a pull request
|
||||
var pullRequest = new NewPullRequest("Nice title for the pull request", "my-branch", "master");
|
||||
var createdPullRequest = await github.PullRequest.Create(context.RepositoryOwner, context.RepositoryName, pullRequest);
|
||||
|
||||
// Create review requests (optional)
|
||||
if (createReviewRequests)
|
||||
{
|
||||
var reviewRequest = new PullRequestReviewRequest(_collaboratorLogins);
|
||||
await github.PullRequest.ReviewRequest.Create(context.RepositoryOwner, context.RepositoryName, createdPullRequest.Number, reviewRequest);
|
||||
}
|
||||
|
||||
return createdPullRequest.Number;
|
||||
}
|
||||
|
||||
static async Task<TreeResponse> CreateTree(IGitHubClient github, RepositoryContext context, IEnumerable<KeyValuePair<string, string>> treeContents)
|
||||
{
|
||||
var collection = new List<NewTreeItem>();
|
||||
|
||||
foreach (var c in treeContents)
|
||||
{
|
||||
var baselineBlob = new NewBlob
|
||||
{
|
||||
Content = c.Value,
|
||||
Encoding = EncodingType.Utf8
|
||||
};
|
||||
var baselineBlobResult = await github.Git.Blob.Create(context.RepositoryOwner, context.RepositoryName, baselineBlob);
|
||||
|
||||
collection.Add(new NewTreeItem
|
||||
{
|
||||
Type = TreeType.Blob,
|
||||
Mode = FileMode.File,
|
||||
Path = c.Key,
|
||||
Sha = baselineBlobResult.Sha
|
||||
});
|
||||
}
|
||||
|
||||
var newTree = new NewTree();
|
||||
foreach (var item in collection)
|
||||
{
|
||||
newTree.Tree.Add(item);
|
||||
}
|
||||
|
||||
return await github.Git.Tree.Create(context.RepositoryOwner, context.RepositoryName, newTree);
|
||||
}
|
||||
|
||||
static async Task<Commit> CreateCommit(IGitHubClient github, RepositoryContext context, string message, string sha, string parent)
|
||||
{
|
||||
var newCommit = new NewCommit(message, sha, parent);
|
||||
return await github.Git.Commit.Create(context.RepositoryOwner, context.RepositoryName, newCommit);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user