Files
octokit.net/Octokit/Clients/PullRequestsClient.cs
T

197 lines
9.2 KiB
C#

using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace Octokit
{
public class PullRequestsClient : ApiClient, IPullRequestsClient
{
public PullRequestsClient(IApiConnection apiConnection) : base(apiConnection)
{
Comment = new PullRequestReviewCommentsClient(apiConnection);
}
/// <summary>
/// Client for managing comments.
/// </summary>
public IPullRequestReviewCommentsClient Comment { get; private set; }
/// <summary>
/// Get a pull request by number.
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/pulls/#get-a-single-pull-request
/// </remarks>
/// <returns>A <see cref="PullRequest"/> result</returns>
public Task<PullRequest> Get(string owner, string name, int number)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.Get<PullRequest>(ApiUrls.PullRequest(owner, name, number));
}
/// <summary>
/// Get all open pull requests for the repository.
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/pulls/#list-pull-requests
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns>A <see cref="IReadOnlyList{PullRequest}"/> of <see cref="PullRequest"/>s which are currently open</returns>
public Task<IReadOnlyList<PullRequest>> GetAllForRepository(string owner, string name)
{
return GetAllForRepository(owner, name, new PullRequestRequest());
}
/// <summary>
/// Query pull requests for the repository based on criteria
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/pulls/#list-pull-requests
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="request">Used to filter and sort the list of pull requests returned</param>
/// <returns>A <see cref="IReadOnlyList{PullRequest}"/> of <see cref="PullRequest"/>s which match the criteria</returns>
public Task<IReadOnlyList<PullRequest>> GetAllForRepository(string owner, string name, PullRequestRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(request, "request");
return ApiConnection.GetAll<PullRequest>(ApiUrls.PullRequests(owner, name),
request.ToParametersDictionary());
}
/// <summary>
/// Create a pull request for the specified repository.
/// </summary>
/// <remarks>http://developer.github.com/v3/pulls/#create-a-pull-request</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="newPullRequest">A <see cref="NewPullRequest"/> instance describing the new PullRequest to create</param>
/// <returns>A <see cref="PullRequest"/> result which was created on the server</returns>
public Task<PullRequest> Create(string owner, string name, NewPullRequest newPullRequest)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(newPullRequest, "newPullRequest");
return ApiConnection.Post<PullRequest>(ApiUrls.PullRequests(owner, name), newPullRequest);
}
/// <summary>
/// Create a pull request for the specified repository.
/// </summary>
/// <remarks>http://developer.github.com/v3/pulls/#update-a-pull-request</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The PullRequest number</param>
/// <param name="pullRequestUpdate">An <see cref="PullRequestUpdate"/> instance describing the changes to make to the PullRequest
/// </param>
/// <returns>An updated <see cref="PullRequest"/> result</returns>
public Task<PullRequest> Update(string owner, string name, int number, PullRequestUpdate pullRequestUpdate)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(pullRequestUpdate, "pullRequestUpdate");
return ApiConnection.Patch<PullRequest>(ApiUrls.PullRequest(owner, name, number), pullRequestUpdate);
}
/// <summary>
/// Merge a pull request.
/// </summary>
/// <remarks>http://developer.github.com/v3/pulls/#merge-a-pull-request-merge-buttontrade</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The pull request number</param>
/// <param name="mergePullRequest">A <see cref="MergePullRequest"/> instance describing a pull request merge</param>
/// <returns>An <see cref="PullRequestMerge"/> result which indicates the merge result</returns>
public async Task<PullRequestMerge> Merge(string owner, string name, int number, MergePullRequest mergePullRequest)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(mergePullRequest, "mergePullRequest");
try
{
return await ApiConnection.Put<PullRequestMerge>(ApiUrls.MergePullRequest(owner, name, number), mergePullRequest);
}
catch (ApiException ex)
{
if (ex.StatusCode == HttpStatusCode.MethodNotAllowed)
{
throw new PullRequestNotMergeableException(ex.HttpResponse);
}
if (ex.StatusCode == HttpStatusCode.Conflict)
{
throw new PullRequestMismatchException(ex.HttpResponse);
}
throw;
}
}
/// <summary>
/// Get the pull request merge status.
/// </summary>
/// <remarks>http://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The pull request number</param>
/// <returns>True if the operation has been merged, false otherwise</returns>
public async Task<bool> Merged(string owner, string name, int number)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
try
{
var response = await Connection.Get<object>(ApiUrls.MergePullRequest(owner, name, number), null, null)
.ConfigureAwait(false);
return response.HttpResponse.IsTrue();
}
catch (NotFoundException)
{
return false;
}
}
/// <summary>
/// Get the list of commits on a pull request.
/// </summary>
/// <remarks>http://developer.github.com/v3/pulls/#list-commits-on-a-pull-request</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The pull request number</param>
/// <returns>A <see cref="IReadOnlyList{PullRequestCommit}"/> of <see cref="Commit"/>s which are part of this pull request</returns>
public Task<IReadOnlyList<PullRequestCommit>> Commits(string owner, string name, int number)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetAll<PullRequestCommit>(ApiUrls.PullRequestCommits(owner, name, number));
}
/// <summary>
/// Get the list of files on a pull request.
/// </summary>
/// <remarks>https://developer.github.com/v3/pulls/#list-pull-requests-files</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The pull request number</param>
/// <returns>A <see cref="IReadOnlyList{PullRequestFile}"/> which are part of this pull request</returns>
public Task<IReadOnlyList<PullRequestFile>> Files(string owner, string name, int number)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetAll<PullRequestFile>(ApiUrls.PullRequestFiles(owner, name, number));
}
}
}