diff --git a/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs index e0609d17..deb3a225 100644 --- a/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Linq; using System.Threading.Tasks; using Octokit; @@ -253,7 +254,7 @@ public class RepositoriesClientTests } } - [IntegrationTest(Skip="this test is bollocks")] + [IntegrationTest] public async Task ThrowsRepositoryExistsExceptionForExistingRepository() { var github = Helper.GetAuthenticatedClient(); @@ -263,11 +264,14 @@ public class RepositoriesClientTests try { + var message = string.Format(CultureInfo.InvariantCulture, "There is already a repository named '{0}' for the current account.", repoName); + var thrown = await AssertEx.Throws( async () => await github.Repository.Create(repository)); + Assert.NotNull(thrown); Assert.Equal(repoName, thrown.RepositoryName); - Assert.Equal(Helper.Credentials.Login, thrown.Owner); + Assert.Equal(message, thrown.Message); Assert.False(thrown.OwnerIsOrganization); } finally @@ -347,6 +351,37 @@ public class RepositoriesClientTests } } + [IntegrationTest] + public async Task ThrowsRepositoryExistsExceptionForExistingRepository() + { + var github = Helper.GetAuthenticatedClient(); + + var repoName = Helper.MakeNameWithTimestamp("existing-org-repo"); + + var repository = new NewRepository { Name = repoName }; + var createdRepository = await github.Repository.Create(Helper.Organization, repository); + + try + { + var repositoryUrl = string.Format(CultureInfo.InvariantCulture, "https://github.com/{0}/{1}", Helper.Organization, repository.Name); + var message = string.Format(CultureInfo.InvariantCulture, "There is already a repository named '{0}' in the organization '{1}'.", repository.Name, Helper.Organization); + + var thrown = await AssertEx.Throws( + async () => await github.Repository.Create(Helper.Organization, repository)); + + Assert.NotNull(thrown); + Assert.Equal(repoName, thrown.RepositoryName); + Assert.Equal(message, thrown.Message); + Assert.True(thrown.OwnerIsOrganization); + Assert.Equal(Helper.Organization, thrown.Organization); + Assert.Equal(repositoryUrl, thrown.ExistingRepositoryWebUrl.ToString()); + } + finally + { + Helper.DeleteRepo(createdRepository); + } + } + // TODO: Add a test for the team_id param once an overload that takes an oranization is added } diff --git a/Octokit.Tests/Clients/RepositoriesClientTests.cs b/Octokit.Tests/Clients/RepositoriesClientTests.cs index 2599af3c..b0ed782e 100644 --- a/Octokit.Tests/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoriesClientTests.cs @@ -1,10 +1,8 @@ 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; @@ -80,7 +78,7 @@ namespace Octokit.Tests.Clients async () => await client.Create(newRepository)); Assert.False(exception.OwnerIsOrganization); - Assert.Null(exception.Owner); + Assert.Null(exception.Organization); Assert.Equal("aName", exception.RepositoryName); Assert.Null(exception.ExistingRepositoryWebUrl); } @@ -166,7 +164,7 @@ namespace Octokit.Tests.Clients async () => await client.Create("illuminati", newRepository)); Assert.True(exception.OwnerIsOrganization); - Assert.Equal("illuminati", exception.Owner); + Assert.Equal("illuminati", exception.Organization); 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'.", diff --git a/Octokit.Tests/Exceptions/RepositoryExistsExceptionTests.cs b/Octokit.Tests/Exceptions/RepositoryExistsExceptionTests.cs index 591fe37f..8d4530db 100644 --- a/Octokit.Tests/Exceptions/RepositoryExistsExceptionTests.cs +++ b/Octokit.Tests/Exceptions/RepositoryExistsExceptionTests.cs @@ -5,22 +5,30 @@ namespace Octokit.Tests.Exceptions { public class RepositoryExistsExceptionTests { + [Fact] + public void WhenOrganizationIsNullShouldThrowArgumentNullException() + { + Assert.Throws(() => new RepositoryExistsException( + null, + "some-repo", + GitHubClient.GitHubDotComUrl, + new ApiValidationException())); + } + public class TheMessageProperty { [Fact] - public void WhenOwnerIsNullDoNotMentionInMessage() + public void WhenOwnerIsAccountDoNotMentionOwnerNameInMessage() { var exception = new RepositoryExistsException( - null, "some-repo", - GitHubClient.GitHubDotComUrl, new ApiValidationException()); Assert.Equal("There is already a repository named 'some-repo' for the current account.", exception.Message); } [Fact] - public void WhenOwnerIsNotNullMentionInMessage() + public void WhenOwnerIsOrganizationMentionInMessage() { var exception = new RepositoryExistsException( "some-org", @@ -35,19 +43,17 @@ namespace Octokit.Tests.Exceptions public class TheOwnerIsOrganizationProperty { [Fact] - public void WhenOwnerIsNullReturnsFalse() + public void WhenOwnerIsAccountReturnsFalse() { var exception = new RepositoryExistsException( - null, "some-repo", - GitHubClient.GitHubDotComUrl, new ApiValidationException()); Assert.False(exception.OwnerIsOrganization); } [Fact] - public void WhenOwnerIsNotNullReturnsTrue() + public void WhenOwnerIsOrganizationReturnsTrue() { var exception = new RepositoryExistsException( "some-org", @@ -62,19 +68,17 @@ namespace Octokit.Tests.Exceptions public class TheExistingRepositoryWebUrlProperty { [Fact] - public void WhenOwnerIsNullDoNotSetUrl() + public void WhenOwnerIsAccountDoNotSetUrl() { var exception = new RepositoryExistsException( - null, "some-repo", - GitHubClient.GitHubDotComUrl, new ApiValidationException()); Assert.Null(exception.ExistingRepositoryWebUrl); } [Fact] - public void WhenOwnerIsNotNullSetUrl() + public void WhenOwnerIsOrganizationSetUrl() { var exception = new RepositoryExistsException( "some-org", diff --git a/Octokit/Clients/RepositoriesClient.cs b/Octokit/Clients/RepositoriesClient.cs index f3b2cdae..6022ff55 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -86,6 +86,11 @@ namespace Octokit errorMessage, StringComparison.OrdinalIgnoreCase)) { + if (string.IsNullOrEmpty(organizationLogin)) + { + throw new RepositoryExistsException(newRepository.Name, e); + } + var baseAddress = Connection.BaseAddress.Host != GitHubClient.GitHubApiUrl.Host ? Connection.BaseAddress : new Uri("https://github.com/"); diff --git a/Octokit/Exceptions/RepositoryExistsException.cs b/Octokit/Exceptions/RepositoryExistsException.cs index 3a0de556..b843b33a 100644 --- a/Octokit/Exceptions/RepositoryExistsException.cs +++ b/Octokit/Exceptions/RepositoryExistsException.cs @@ -18,36 +18,49 @@ namespace Octokit readonly string _message; /// - /// Constructs an instance of RepositoryExistsException. + /// Constructs an instance of RepositoryExistsException for an organization. /// - /// The login of the owner of the existing repository + /// The name of the organization of the existing repository /// The name of the existing repository /// The base address of the repository. /// The inner validation exception. public RepositoryExistsException( - string owner, + string organization, string name, Uri baseAddress, ApiValidationException innerException) : base(innerException) { - Ensure.ArgumentNotNullOrEmptyString(name, "repositoryName"); + Ensure.ArgumentNotNullOrEmptyString(organization, "organization"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(baseAddress, "baseAddress"); - Owner = owner; + Organization = organization; RepositoryName = name; - OwnerIsOrganization = !String.IsNullOrWhiteSpace(owner); + OwnerIsOrganization = true; var webBaseAddress = baseAddress.Host != GitHubClient.GitHubApiUrl.Host ? baseAddress : GitHubClient.GitHubDotComUrl; - ExistingRepositoryWebUrl = OwnerIsOrganization - ? new Uri(webBaseAddress, new Uri(owner + "/" + name, UriKind.Relative)) - : null; - string messageFormat = OwnerIsOrganization - ? "There is already a repository named '{0}' in the organization '{1}'." - : "There is already a repository named '{0}' for the current account."; + ExistingRepositoryWebUrl = new Uri(webBaseAddress, new Uri(organization + "/" + name, UriKind.Relative)); + + _message = string.Format(CultureInfo.InvariantCulture, "There is already a repository named '{0}' in the organization '{1}'.", name, organization); + } - _message = String.Format(CultureInfo.InvariantCulture, messageFormat, name, owner); + /// + /// Constructs an instance of RepositoryExistsException for an account. + /// + /// The name of the existing repository + /// The inner validation exception + public RepositoryExistsException( + string name, + ApiValidationException innerException) + : base(innerException) + { + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + + RepositoryName = name; + + _message = String.Format(CultureInfo.InvariantCulture, "There is already a repository named '{0}' for the current account.", name); } /// @@ -72,9 +85,9 @@ namespace Octokit } /// - /// The login of the owner of the repository. + /// The login of the organization of the repository. /// - public string Owner { get; private set; } + public string Organization { get; private set; } /// /// True if the owner is an organization and not the user. @@ -99,7 +112,7 @@ namespace Octokit if (info == null) return; _message = info.GetString("Message"); RepositoryName = info.GetString("RepositoryName"); - Owner = info.GetString("Owner"); + Organization = info.GetString("Organization"); OwnerIsOrganization = info.GetBoolean("OwnerIsOrganization"); ExistingRepositoryWebUrl = (Uri)(info.GetValue("ExistingRepositoryWebUrl", typeof(Uri))); } @@ -109,7 +122,7 @@ namespace Octokit base.GetObjectData(info, context); info.AddValue("Message", Message); info.AddValue("RepositoryName", RepositoryName); - info.AddValue("Owner", Owner); + info.AddValue("Organization", Organization); info.AddValue("OwnerIsOrganization", OwnerIsOrganization); info.AddValue("ExistingRepositoryWebUrl", ExistingRepositoryWebUrl); }