Add exception for exceeding the private quota

This commit is contained in:
Haacked
2014-02-18 22:41:19 -08:00
parent 8fa98fa99a
commit 34ac57ccab
9 changed files with 133 additions and 3 deletions
@@ -9,7 +9,7 @@ using Octokit.Tests.Helpers;
public class RepositoriesClientTests
{
public class TheCreateMethodForUser
public class TheCreateMethodForUser : IDisposable
{
[IntegrationTest]
public async Task CreatesANewPublicRepository()
@@ -287,6 +287,45 @@ public class RepositoriesClientTests
Helper.DeleteRepo(createdRepository);
}
}
[IntegrationTest]
public async Task ThrowsPrivateRepositoryQuotaExceededExceptionWhenOverQuota()
{
var github = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};
for (int i = 0; i < 5; i++)
{
var repoName = Helper.MakeNameWithTimestamp("private-repo" + i);
var repository = new NewRepository { Name = repoName, Private = true };
await github.Repository.Create(repository);
}
var thrown = await AssertEx.Throws<PrivateRepositoryQuotaExceededException>(
async () => await github.Repository.Create(new NewRepository { Name = "x-private", Private = true }));
Assert.NotNull(thrown);
}
// Clean up the repos.
public void Dispose()
{
var github = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};
var repositories = github.Repository.GetAllForCurrent().Result;
foreach (var repository in repositories)
{
try
{
github.Repository.Delete(repository.Owner.Login, repository.Name).Wait();
}
catch (Exception) { }
}
}
}
public class TheCreateMethodForOrganization
@@ -76,13 +76,38 @@ namespace Octokit.Tests.Clients
.Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
var client = new RepositoriesClient(connection);
var exception = await AssertEx.Throws<RepositoryExistsException>(async () => await client.Create(newRepository));
var exception = await AssertEx.Throws<RepositoryExistsException>(
async () => await client.Create(newRepository));
Assert.False(exception.OwnerIsOrganization);
Assert.Equal("haacked", exception.Owner);
Assert.Equal("aName", exception.RepositoryName);
Assert.Equal(new Uri("https://github.com/haacked/aName"), exception.ExistingRepositoryWebUrl);
}
[Fact]
public async Task ThrowsExceptionWhenPrivateRepositoryQuotaExceeded()
{
var newRepository = new NewRepository { Name = "aName", Private = true };
var response = Substitute.For<IResponse>();
response.StatusCode.Returns((HttpStatusCode)422);
response.Body.Returns(@"{""message"":""Validation Failed"",""documentation_url"":"
+ @"""http://developer.github.com/v3/repos/#create"",""errors"":[{""resource"":""Repository"","
+ @"""code"":""custom"",""field"":""name"",""message"":"
+ @"""name can't be private. You are over your quota.""}]}");
var credentials = new Credentials("haacked", "pwd");
var connection = Substitute.For<IApiConnection>();
connection.Connection.BaseAddress.Returns(GitHubClient.GitHubApiUrl);
connection.Connection.Credentials.Returns(credentials);
connection.Post<Repository>(Args.Uri, newRepository)
.Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
var client = new RepositoriesClient(connection);
var exception = await AssertEx.Throws<PrivateRepositoryQuotaExceededException>(
async () => await client.Create(newRepository));
Assert.NotNull(exception);
}
}
public class TheCreateMethodForOrganization
+10 -1
View File
@@ -73,9 +73,11 @@ namespace Octokit
}
catch (ApiValidationException e)
{
string errorMessage = e.ApiError.FirstErrorMessageSafe();
if (String.Equals(
"name already exists on this account",
e.ApiError.FirstErrorMessageSafe(),
errorMessage,
StringComparison.OrdinalIgnoreCase))
{
string owner = organizationLogin ?? Connection.Credentials.Login;
@@ -89,6 +91,13 @@ namespace Octokit
organizationLogin != null,
baseAddress, e);
}
if (String.Equals(
"name can't be private. You are over your quota.",
errorMessage,
StringComparison.OrdinalIgnoreCase))
{
throw new PrivateRepositoryQuotaExceededException(e);
}
throw;
}
}
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Octokit
{
/// <summary>
/// Exception thrown when creating a private repository, but the user's private quota is or would be exceeded
/// by creating it.
/// </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 PrivateRepositoryQuotaExceededException : ApiValidationException
{
/// <summary>
/// Constructs an instance of PrivateRepositoryQuotaExceededException.
/// </summary>
/// <param name="owner">The login of the owner of the existing repository</param>
/// <param name="name">The name of the existing repository</param>
/// <param name="ownerIsOrganization">True if the owner is an organization</param>
/// <param name="baseAddress">The base address of the repository.</param>
/// <param name="innerException">The inner validation exception.</param>
public PrivateRepositoryQuotaExceededException(ApiValidationException innerException)
: base(innerException)
{
}
public override string Message
{
get
{
// TODO: Would be nice to show the actual numbers, but that requires another request.
return "You are currently at your limit of private repositories. Either delete a private repository "
+ "you no longer use or upgrade your account to a plan that allows for more private repositories.";
}
}
#if !NETFX_CORE
protected PrivateRepositoryQuotaExceededException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
}
+1
View File
@@ -292,6 +292,7 @@
<Compile Include="Models\Response\PullRequestCommit.cs" />
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+1
View File
@@ -303,6 +303,7 @@
<Compile Include="Models\Response\PullRequestCommit.cs" />
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
+1
View File
@@ -298,6 +298,7 @@
<Compile Include="Models\Response\PullRequestCommit.cs" />
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+1
View File
@@ -290,6 +290,7 @@
<Compile Include="Models\Response\PullRequestCommit.cs" />
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
+1
View File
@@ -66,6 +66,7 @@
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\ActivitiesClient.cs" />
<Compile Include="Exceptions\PrivateRepositoryQuotaExceededException.cs" />
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
<Compile Include="Models\Response\DeploymentStatus.cs" />