Skip to content

Commit

Permalink
Merge pull request #377 from octokit/haacked/repository-create-except…
Browse files Browse the repository at this point in the history
…ions

Implement RepositoryExistsException
  • Loading branch information
shiftkey committed Feb 19, 2014
2 parents 4da4c39 + dae6cb2 commit 8fa98fa
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 4 deletions.
27 changes: 27 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Octokit;
using Octokit.Tests.Integration;
using Xunit;
using Octokit.Tests.Helpers;

public class RepositoriesClientTests
{
Expand Down Expand Up @@ -260,6 +261,32 @@ public async Task CreatesARepositoryWithAGitignoreTemplate()
Helper.DeleteRepo(createdRepository);
}
}

[IntegrationTest]
public async Task ThrowsRepositoryExistsExceptionForExistingRepository()
{
var github = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};
var repoName = Helper.MakeNameWithTimestamp("existing-repo");
var repository = new NewRepository { Name = repoName };
var createdRepository = await github.Repository.Create(repository);

try
{
var thrown = await AssertEx.Throws<RepositoryExistsException>(
async () => await github.Repository.Create(repository));
Assert.NotNull(thrown);
Assert.Equal(repoName, thrown.RepositoryName);
Assert.Equal(Helper.Credentials.Login, thrown.Owner);
Assert.False(thrown.OwnerIsOrganization);
}
finally
{
Helper.DeleteRepo(createdRepository);
}
}
}

public class TheCreateMethodForOrganization
Expand Down
99 changes: 97 additions & 2 deletions Octokit.Tests/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using NSubstitute;
using Octokit;
using Octokit.Tests.Helpers;
using Xunit;

Expand Down Expand Up @@ -54,7 +56,32 @@ public void TheNewRepositoryDescription()

client.Create(newRepository);

connection.Received().Post<Repository>(Arg.Any<Uri>(), newRepository);
connection.Received().Post<Repository>(Args.Uri, newRepository);
}

[Fact]
public async Task ThrowsRepositoryExistsExceptionWhenRepositoryExistsForCurrentUser()
{
var newRepository = new NewRepository { Name = "aName" };
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 already exists on this account""}]}");
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<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);
}
}

Expand Down Expand Up @@ -92,7 +119,75 @@ public async Task TheNewRepositoryDescription()

await client.Create("aLogin", newRepository);

connection.Received().Post<Repository>(Arg.Any<Uri>(), newRepository);
connection.Received().Post<Repository>(Args.Uri, newRepository);
}

[Fact]
public async Task ThrowsRepositoryExistsExceptionWhenRepositoryExistsForSpecifiedOrg()
{
var newRepository = new NewRepository { Name = "aName" };
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 already exists on this account""}]}");
var connection = Substitute.For<IApiConnection>();
connection.Connection.BaseAddress.Returns(GitHubClient.GitHubApiUrl);
connection.Post<Repository>(Args.Uri, newRepository)
.Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
var client = new RepositoriesClient(connection);

var exception = await AssertEx.Throws<RepositoryExistsException>(
async () => await client.Create("illuminati", newRepository));

Assert.True(exception.OwnerIsOrganization);
Assert.Equal("illuminati", exception.Owner);
Assert.Equal("aName", exception.RepositoryName);
Assert.Equal(new Uri("https://github.com/illuminati/aName"), exception.ExistingRepositoryWebUrl);
Assert.Equal("There is already a repository named 'aName' in the organization 'illuminati'",
exception.Message);
}

[Fact]
public async Task ThrowsValidationException()
{
var newRepository = new NewRepository { Name = "aName" };
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"":[]}");
var connection = Substitute.For<IApiConnection>();
connection.Connection.BaseAddress.Returns(GitHubClient.GitHubApiUrl);
connection.Post<Repository>(Args.Uri, newRepository)
.Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
var client = new RepositoriesClient(connection);

var exception = await AssertEx.Throws<ApiValidationException>(
async () => await client.Create("illuminati", newRepository));

Assert.Null(exception as RepositoryExistsException);
}

[Fact]
public async Task ThrowsRepositoryExistsExceptionForEnterpriseInstance()
{
var newRepository = new NewRepository { Name = "aName" };
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 already exists on this account""}]}");
var connection = Substitute.For<IApiConnection>();
connection.Connection.BaseAddress.Returns(new Uri("https://example.com"));
connection.Post<Repository>(Args.Uri, newRepository)
.Returns<Task<Repository>>(_ => { throw new ApiValidationException(response); });
var client = new RepositoriesClient(connection);

var exception = await AssertEx.Throws<RepositoryExistsException>(
async () => await client.Create("illuminati", newRepository));

Assert.Equal("aName", exception.RepositoryName);
Assert.Equal(new Uri("https://example.com/illuminati/aName"), exception.ExistingRepositoryWebUrl);
}
}

Expand Down
32 changes: 30 additions & 2 deletions Octokit/Clients/RepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Task<Repository> Create(NewRepository newRepository)
if (string.IsNullOrEmpty(newRepository.Name))
throw new ArgumentException("The new repository's name must not be null.");

return ApiConnection.Post<Repository>(ApiUrls.Repositories(), newRepository);
return Create(ApiUrls.Repositories(), null, newRepository);
}

/// <summary>
Expand All @@ -62,7 +62,35 @@ public Task<Repository> Create(string organizationLogin, NewRepository newReposi
if (string.IsNullOrEmpty(newRepository.Name))
throw new ArgumentException("The new repository's name must not be null.");

return ApiConnection.Post<Repository>(ApiUrls.OrganizationRepositories(organizationLogin), newRepository);
return Create(ApiUrls.OrganizationRepositories(organizationLogin), organizationLogin, newRepository);
}

async Task<Repository> Create(Uri url, string organizationLogin, NewRepository newRepository)
{
try
{
return await ApiConnection.Post<Repository>(url, newRepository);
}
catch (ApiValidationException e)
{
if (String.Equals(
"name already exists on this account",
e.ApiError.FirstErrorMessageSafe(),
StringComparison.OrdinalIgnoreCase))
{
string owner = organizationLogin ?? Connection.Credentials.Login;

var baseAddress = Connection.BaseAddress.Host != GitHubClient.GitHubApiUrl.Host
? Connection.BaseAddress
: new Uri("https://github.com/");
throw new RepositoryExistsException(
owner,
newRepository.Name,
organizationLogin != null,
baseAddress, e);
}
throw;
}
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions Octokit/Exceptions/ApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public ApiException(IResponse response, Exception innerException)
ApiError = GetApiErrorFromExceptionMessage(response);
}

protected ApiException(ApiException innerException)
{
Ensure.ArgumentNotNull(innerException, "innerException");
StatusCode = innerException.StatusCode;
ApiError = innerException.ApiError;
}

public override string Message
{
get { return ApiErrorMessageSafe ?? "An error occurred with this API request"; }
Expand Down
4 changes: 4 additions & 0 deletions Octokit/Exceptions/ApiValidationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public ApiValidationException(IResponse response, Exception innerException)
"ApiValidationException created with wrong status code");
}

protected ApiValidationException(ApiValidationException innerException) : base(innerException)
{
}

public override string Message
{
get { return ApiErrorMessageSafe ?? "Validation Failed"; }
Expand Down
112 changes: 112 additions & 0 deletions Octokit/Exceptions/RepositoryExistsException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Octokit
{
/// <summary>
/// Exception thrown when creating a repository, but it already exists on the server.
/// </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 RepositoryExistsException : ApiValidationException
{
string _message;

/// <summary>
/// Constructs an instance of RepositoryExistsException.
/// </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 RepositoryExistsException(
string owner,
string name,
bool ownerIsOrganization,
Uri baseAddress,
ApiValidationException innerException)
: base(innerException)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "repositoryName");
Ensure.ArgumentNotNull(baseAddress, "baseAddress");

Owner = owner;
RepositoryName = name;
OwnerIsOrganization = ownerIsOrganization;
var webBaseAddress = baseAddress.Host != GitHubClient.GitHubApiUrl.Host
? baseAddress
: GitHubClient.GitHubDotComUrl;
ExistingRepositoryWebUrl = new Uri(webBaseAddress, new Uri(owner + "/" + name, UriKind.Relative));
string messageFormat = ownerIsOrganization
? "There is already a repository named '{0}' in the organization '{1}'"
: "There is already a repository named '{0}' for the owner '{1}'.";

_message = String.Format(CultureInfo.InvariantCulture, messageFormat, name, owner);
}

/// <summary>
/// The Name of the repository that already exists.
/// </summary>
public string RepositoryName { get; private set; }

/// <summary>
/// The URL to the existing repository's web page on github.com (or enterprise instance).
/// </summary>
public Uri ExistingRepositoryWebUrl { get; set; }

/// <summary>
/// A useful default error message.
/// </summary>
public override string Message
{
get
{
return _message;
}
}

/// <summary>
/// The login of the owner of the repository.
/// </summary>
public string Owner { get; private set; }

/// <summary>
/// True if the owner is an organization and not the user.
/// </summary>
public bool OwnerIsOrganization { get; private set; }

#if !NETFX_CORE
protected RepositoryExistsException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
if (info == null) return;
_message = info.GetString("Message");
RepositoryName = info.GetString("RepositoryName");
Owner = info.GetString("Owner");
OwnerIsOrganization = info.GetBoolean("OwnerIsOrganization");
ExistingRepositoryWebUrl = (Uri)(info.GetValue("ExistingRepositoryWebUrl", typeof(Uri)));
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("Message", Message);
info.AddValue("RepositoryName", RepositoryName);
info.AddValue("Owner", Owner);
info.AddValue("OwnerIsOrganization", OwnerIsOrganization);
info.AddValue("ExistingRepositoryWebUrl", ExistingRepositoryWebUrl);
}
#endif
}
}
1 change: 1 addition & 0 deletions Octokit/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Octokit
public class GitHubClient : IGitHubClient
{
public static readonly Uri GitHubApiUrl = new Uri("https://api.github.com/");
internal static readonly Uri GitHubDotComUrl = new Uri("https://github.com/");

/// <summary>
/// Create a new instance of the GitHub API v3 client pointing to
Expand Down
16 changes: 16 additions & 0 deletions Octokit/Helpers/ApiErrorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Linq;

namespace Octokit
{
internal static class ApiErrorExtensions
{
public static string FirstErrorMessageSafe(this ApiError apiError)
{
if (apiError == null) return null;
if (apiError.Errors == null) return null;
var firstError = apiError.Errors.FirstOrDefault();
if (firstError == null) return null;
return firstError.Message;
}
}
}
2 changes: 2 additions & 0 deletions Octokit/Octokit-Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@
<Compile Include="Models\Response\CommitActivity.cs" />
<Compile Include="Helpers\UnixTimeStampExtensions.cs" />
<Compile Include="Models\Response\PullRequestCommit.cs" />
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
2 changes: 2 additions & 0 deletions Octokit/Octokit-MonoAndroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@
<Compile Include="Models\Response\CommitActivity.cs" />
<Compile Include="Helpers\UnixTimeStampExtensions.cs" />
<Compile Include="Models\Response\PullRequestCommit.cs" />
<Compile Include="Exceptions\RepositoryExistsException.cs" />
<Compile Include="Helpers\ApiErrorExtensions.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
Loading

0 comments on commit 8fa98fa

Please sign in to comment.