mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-01 18:35:35 +00:00
Implement RepositoryExistsException
This exception is thrown when we try to create a repository but it already exists on the server.
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using Octokit;
|
||||
using Octokit.Tests.Integration;
|
||||
using Xunit;
|
||||
using Octokit.Tests.Helpers;
|
||||
|
||||
public class RepositoriesClientTests
|
||||
{
|
||||
@@ -260,6 +261,32 @@ public class RepositoriesClientTests
|
||||
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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -54,7 +56,32 @@ namespace Octokit.Tests.Clients
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +119,75 @@ namespace Octokit.Tests.Clients
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Octokit
|
||||
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>
|
||||
@@ -62,7 +62,35 @@ namespace Octokit
|
||||
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>
|
||||
|
||||
@@ -44,6 +44,13 @@ namespace Octokit
|
||||
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"; }
|
||||
|
||||
@@ -33,6 +33,10 @@ namespace Octokit
|
||||
"ApiValidationException created with wrong status code");
|
||||
}
|
||||
|
||||
protected ApiValidationException(ApiValidationException innerException) : base(innerException)
|
||||
{
|
||||
}
|
||||
|
||||
public override string Message
|
||||
{
|
||||
get { return ApiErrorMessageSafe ?? "Validation Failed"; }
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -296,6 +296,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>
|
||||
@@ -288,6 +288,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>
|
||||
<ItemGroup>
|
||||
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
|
||||
|
||||
@@ -66,6 +66,8 @@
|
||||
<Link>Properties\SolutionInfo.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Clients\ActivitiesClient.cs" />
|
||||
<Compile Include="Exceptions\RepositoryExistsException.cs" />
|
||||
<Compile Include="Helpers\ApiErrorExtensions.cs" />
|
||||
<Compile Include="Models\Response\DeploymentStatus.cs" />
|
||||
<Compile Include="Clients\DeploymentStatusClient.cs" />
|
||||
<Compile Include="Clients\IDeploymentStatusClient.cs" />
|
||||
|
||||
Reference in New Issue
Block a user