Merge branch 'better-merge-exception-rebased' of https://github.com/elbaloo/octokit.net into elbaloo-better-merge-exception-rebased

This commit is contained in:
Haacked
2015-12-07 15:13:13 -08:00
10 changed files with 181 additions and 5 deletions

View File

@@ -227,7 +227,7 @@ public class PullRequestsClientTests : IDisposable
}
[IntegrationTest]
public async Task CannotBeMerged()
public async Task CannotBeMergedDueMismatchConflict()
{
await CreateTheWorld();
var fakeSha = new string('f', 40);
@@ -236,9 +236,27 @@ public class PullRequestsClientTests : IDisposable
var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest);
var merge = new MergePullRequest { Sha = fakeSha };
var ex = await Assert.ThrowsAsync<ApiException>(() => _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge));
var ex = await Assert.ThrowsAsync<PullRequestMismatchException>(() => _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge));
Assert.True(ex.ApiError.Message.StartsWith("Head branch was modified"));
Assert.True(ex.Message.StartsWith("Head branch was modified"));
}
[IntegrationTest]
public async Task CannotBeMergedDueNotInMergeableState()
{
await CreateTheWorld();
var master = await _github.GitDatabase.Reference.Get(Helper.UserName, _context.RepositoryName, "heads/master");
var newMasterTree = await CreateTree(new Dictionary<string, string> { { "README.md", "Hello World, we meet again!" } });
await CreateCommit("baseline for pull request", newMasterTree.Sha, master.Object.Sha);
var newPullRequest = new NewPullRequest("a pull request", branchName, "master");
var pullRequest = await _fixture.Create(Helper.UserName, _context.RepositoryName, newPullRequest);
var merge = new MergePullRequest { Sha = pullRequest.Head.Sha };
var ex = await Assert.ThrowsAsync<PullRequestNotMergeableException>(() => _fixture.Merge(Helper.UserName, _context.RepositoryName, pullRequest.Number, merge));
Assert.True(ex.Message.Equals("Pull Request is not mergeable"));
}
[IntegrationTest]

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace Octokit
@@ -115,7 +116,24 @@ namespace Octokit
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(mergePullRequest, "mergePullRequest");
return ApiConnection.Put<PullRequestMerge>(ApiUrls.MergePullRequest(owner, name, number), mergePullRequest);
try
{
return 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>

View File

@@ -0,0 +1,64 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Runtime.Serialization;
namespace Octokit
{
/// <summary>
/// Represents an error that occurs when the specified SHA
/// doesn't match the current pull request's HEAD
/// </summary>
#if !NETFX_CORE
[Serializable]
#endif
[SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors",
Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")]
public class PullRequestMismatchException : ApiException
{
/// <summary>
/// Constructs an instace of <see cref="Octokit.PullRequestMismatchException"/>.
/// </summary>
/// <param name="response"></param>
public PullRequestMismatchException(IResponse response) : this(response, null)
{
}
/// <summary>
/// Constructs an instance of <see cref="Octokit.PullRequestMismatchException"/>.
/// </summary>
/// <param name="response">The HTTP payload from the server</param>
/// <param name="innerException">The inner exception</param>
public PullRequestMismatchException(IResponse response, Exception innerException)
: base(response, innerException)
{
Debug.Assert(response != null && response.StatusCode == HttpStatusCode.Conflict,
"PullRequestMismatchException created with the wrong HTTP status code");
}
public override string Message
{
//https://developer.github.com/v3/pulls/#response-if-sha-was-provided-and-pull-request-head-did-not-match
get { return ApiErrorMessageSafe ?? "Head branch was modified. Review and try the merge again."; }
}
#if !NETFX_CORE
/// <summary>
/// Constructs an instance of <see cref="Octokit.PullRequestNotMergeableException"/>.
/// </summary>
/// <param name="info">
/// The <see cref="SerializationInfo"/> that holds the
/// serialized object data about the exception being thrown.
/// </param>
/// <param name="context">
/// The <see cref="StreamingContext"/> that contains
/// contextual information about the source or destination.
/// </param>
protected PullRequestMismatchException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Runtime.Serialization;
namespace Octokit
{
/// <summary>
/// Represents an error that occurs when the pull request is in an
/// unmergeable state
/// </summary>
#if !NETFX_CORE
[Serializable]
#endif
[SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors",
Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")]
public class PullRequestNotMergeableException : ApiException
{
/// <summary>
/// Constructs an instance of the <see cref="Octokit.PullRequestNotMergeableException"/> class.
/// </summary>
/// <param name="response">The HTTP payload from the server</param>
public PullRequestNotMergeableException(IResponse response) : this(response, null)
{
}
/// <summary>
/// Constructs an instance of the <see cref="Octokit.PullRequestNotMergeableException"/> class.
/// </summary>
/// <param name="response">The HTTP payload from the server</param>
/// <param name="innerException">The inner exception</param>
public PullRequestNotMergeableException(IResponse response, Exception innerException)
: base(response, innerException)
{
Debug.Assert(response != null && response.StatusCode == HttpStatusCode.MethodNotAllowed,
"PullRequestNotMergeableException created with the wrong HTTP status code");
}
public override string Message
{
//https://developer.github.com/v3/pulls/#response-if-merge-cannot-be-performed
get { return ApiErrorMessageSafe ?? "Pull Request is not mergeable"; }
}
#if !NETFX_CORE
/// <summary>
/// Constructs an instance of <see cref="Octokit.PullRequestNotMergeableException"/>.
/// </summary>
/// <param name="info">
/// The <see cref="SerializationInfo"/> that holds the
/// serialized object data about the exception being thrown.
/// </param>
/// <param name="context">
/// The <see cref="StreamingContext"/> that contains
/// contextual information about the source or destination.
/// </param>
protected PullRequestNotMergeableException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
}

View File

@@ -409,6 +409,8 @@
<Compile Include="Models\Request\NewRepositoryWebHook.cs" />
<Compile Include="Exceptions\RepositoryWebHookConfigException.cs" />
<Compile Include="Helpers\WebHookConfigComparer.cs" />
<Compile Include="Exceptions\PullRequestNotMergeableException.cs" />
<Compile Include="Exceptions\PullRequestMismatchException.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -416,6 +416,8 @@
<Compile Include="Models\Request\NewRepositoryWebHook.cs" />
<Compile Include="Exceptions\RepositoryWebHookConfigException.cs" />
<Compile Include="Helpers\WebHookConfigComparer.cs" />
<Compile Include="Exceptions\PullRequestNotMergeableException.cs" />
<Compile Include="Exceptions\PullRequestMismatchException.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>

View File

@@ -412,6 +412,8 @@
<Compile Include="Models\Request\NewRepositoryWebHook.cs" />
<Compile Include="Exceptions\RepositoryWebHookConfigException.cs" />
<Compile Include="Helpers\WebHookConfigComparer.cs" />
<Compile Include="Exceptions\PullRequestNotMergeableException.cs" />
<Compile Include="Exceptions\PullRequestMismatchException.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.MonoTouch.CSharp.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

View File

@@ -406,6 +406,8 @@
<Compile Include="Models\Request\NewRepositoryWebHook.cs" />
<Compile Include="Exceptions\RepositoryWebHookConfigException.cs" />
<Compile Include="Helpers\WebHookConfigComparer.cs" />
<Compile Include="Exceptions\PullRequestNotMergeableException.cs" />
<Compile Include="Exceptions\PullRequestMismatchException.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">

View File

@@ -413,6 +413,8 @@
<Compile Include="Models\Request\NewRepositoryWebHook.cs" />
<Compile Include="Exceptions\RepositoryWebHookConfigException.cs" />
<Compile Include="Helpers\WebHookConfigComparer.cs" />
<Compile Include="Exceptions\PullRequestNotMergeableException.cs" />
<Compile Include="Exceptions\PullRequestMismatchException.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
@@ -427,4 +429,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

View File

@@ -74,6 +74,8 @@
<Compile Include="Clients\RepositoryContentsClient.cs" />
<Compile Include="Exceptions\InvalidGitIgnoreTemplateException.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
<Compile Include="Exceptions\PullRequestMismatchException.cs" />
<Compile Include="Exceptions\PullRequestNotMergeableException.cs" />
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Exceptions\RepositoryFormatException.cs" />
<Compile Include="Exceptions\RepositoryWebHookConfigException.cs" />