From db57a92a7e216b3984e3bb333f9c8664b088c9a4 Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Tue, 16 Jun 2015 22:29:02 +0930 Subject: [PATCH] sketching out the exception necessary when raising specific merge exceptions --- Octokit/Clients/PullRequestsClient.cs | 19 ++++++- .../PullRequestMismatchException.cs | 52 +++++++++++++++++++ .../PullRequestNotMergeableException.cs | 48 +++++++++++++++++ Octokit/Octokit-Mono.csproj | 2 + Octokit/Octokit-MonoAndroid.csproj | 4 +- Octokit/Octokit-Monotouch.csproj | 2 + Octokit/Octokit-Portable.csproj | 2 + Octokit/Octokit-netcore45.csproj | 4 +- Octokit/Octokit.csproj | 2 + 9 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 Octokit/Exceptions/PullRequestMismatchException.cs create mode 100644 Octokit/Exceptions/PullRequestNotMergeableException.cs diff --git a/Octokit/Clients/PullRequestsClient.cs b/Octokit/Clients/PullRequestsClient.cs index 3cce9f30..fc1aeb35 100644 --- a/Octokit/Clients/PullRequestsClient.cs +++ b/Octokit/Clients/PullRequestsClient.cs @@ -115,7 +115,24 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(mergePullRequest, "mergePullRequest"); - return ApiConnection.Put(ApiUrls.MergePullRequest(owner, name, number), mergePullRequest); + try + { + return ApiConnection.Put(ApiUrls.MergePullRequest(owner, name, number), mergePullRequest); + } + catch (ApiException ex) + { + if (ex.StatusCode == HttpStatusCode.MethodNotAllowed) + { + throw new PullRequestNotMergeableException(); + } + + if (ex.StatusCode == HttpStatusCode.Conflict) + { + throw new PullRequestMismatchException(); + } + + throw; + } } /// diff --git a/Octokit/Exceptions/PullRequestMismatchException.cs b/Octokit/Exceptions/PullRequestMismatchException.cs new file mode 100644 index 00000000..8a77d6dc --- /dev/null +++ b/Octokit/Exceptions/PullRequestMismatchException.cs @@ -0,0 +1,52 @@ +using System; +using System.Runtime.Serialization; + +namespace Octokit +{ + /// + /// Represents an error that occurs when the specified SHA + /// doesn't match the current pull request's HEAD + /// +#if !NETFX_CORE + [Serializable] +#endif + public class PullRequestMismatchException : Exception + { + public PullRequestMismatchException() + : base("The merge operation specified a SHA which didn't match " + + "the SHA of the pull request's HEAD") + { + } + + public PullRequestMismatchException(string message) + : base(message) + { + } + + + public PullRequestMismatchException(string message, Exception innerException) + : base(message, innerException) + { + } + +#if !NETFX_CORE + /// + /// Constructs an instance of PullRequestNotMergeableException. + /// + /// + /// The that holds the + /// serialized object data about the exception being thrown. + /// + /// + /// The that contains + /// contextual information about the source or destination. + /// + protected PullRequestMismatchException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } +#endif + } + + +} diff --git a/Octokit/Exceptions/PullRequestNotMergeableException.cs b/Octokit/Exceptions/PullRequestNotMergeableException.cs new file mode 100644 index 00000000..e24c2d36 --- /dev/null +++ b/Octokit/Exceptions/PullRequestNotMergeableException.cs @@ -0,0 +1,48 @@ +using System; +using System.Runtime.Serialization; + +namespace Octokit +{ + /// + /// Represents an error that occurs when the pull request is in an + /// unmergeable state + /// +#if !NETFX_CORE + [Serializable] +#endif + public class PullRequestNotMergeableException : Exception + { + public PullRequestNotMergeableException() + : base("The pull request is not in a mergeable state") + { + } + + public PullRequestNotMergeableException(string message) + : base(message) + { + } + + public PullRequestNotMergeableException(string message, Exception innerException) + : base(message, innerException) + { + } + +#if !NETFX_CORE + /// + /// Constructs an instance of PullRequestNotMergeableException. + /// + /// + /// The that holds the + /// serialized object data about the exception being thrown. + /// + /// + /// The that contains + /// contextual information about the source or destination. + /// + protected PullRequestNotMergeableException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } +#endif + } +} diff --git a/Octokit/Octokit-Mono.csproj b/Octokit/Octokit-Mono.csproj index 3d6206fc..7c25f716 100644 --- a/Octokit/Octokit-Mono.csproj +++ b/Octokit/Octokit-Mono.csproj @@ -403,6 +403,8 @@ + + diff --git a/Octokit/Octokit-MonoAndroid.csproj b/Octokit/Octokit-MonoAndroid.csproj index ea68c7d3..97b56f7b 100644 --- a/Octokit/Octokit-MonoAndroid.csproj +++ b/Octokit/Octokit-MonoAndroid.csproj @@ -411,6 +411,8 @@ + + - \ No newline at end of file + diff --git a/Octokit/Octokit-Monotouch.csproj b/Octokit/Octokit-Monotouch.csproj index 7b228d6d..cd135963 100644 --- a/Octokit/Octokit-Monotouch.csproj +++ b/Octokit/Octokit-Monotouch.csproj @@ -407,6 +407,8 @@ + + diff --git a/Octokit/Octokit-Portable.csproj b/Octokit/Octokit-Portable.csproj index 28280e54..c5f57eb6 100644 --- a/Octokit/Octokit-Portable.csproj +++ b/Octokit/Octokit-Portable.csproj @@ -400,6 +400,8 @@ + + diff --git a/Octokit/Octokit-netcore45.csproj b/Octokit/Octokit-netcore45.csproj index 6f98071f..8bf0101a 100644 --- a/Octokit/Octokit-netcore45.csproj +++ b/Octokit/Octokit-netcore45.csproj @@ -404,6 +404,8 @@ + + @@ -418,4 +420,4 @@ --> - \ No newline at end of file + diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index 3708c95b..46462ba3 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -72,6 +72,8 @@ + +