mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-04 11:24:44 +00:00
RepositoryExistsException has now two constructors (one for account and one for organization)
This commit is contained in:
@@ -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<RepositoryExistsException>(
|
||||
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<RepositoryExistsException>(
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -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'.",
|
||||
|
||||
@@ -5,22 +5,30 @@ namespace Octokit.Tests.Exceptions
|
||||
{
|
||||
public class RepositoryExistsExceptionTests
|
||||
{
|
||||
[Fact]
|
||||
public void WhenOrganizationIsNullShouldThrowArgumentNullException()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => 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",
|
||||
|
||||
@@ -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/");
|
||||
|
||||
@@ -18,36 +18,49 @@ namespace Octokit
|
||||
readonly string _message;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs an instance of RepositoryExistsException.
|
||||
/// Constructs an instance of RepositoryExistsException for an organization.
|
||||
/// </summary>
|
||||
/// <param name="owner">The login of the owner of the existing repository</param>
|
||||
/// <param name="organization">The name of the organization of the existing repository</param>
|
||||
/// <param name="name">The name of the existing repository</param>
|
||||
/// <param name="baseAddress">The base address of the repository.</param>
|
||||
/// <param name="innerException">The inner validation exception.</param>
|
||||
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);
|
||||
/// <summary>
|
||||
/// Constructs an instance of RepositoryExistsException for an account.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the existing repository</param>
|
||||
/// <param name="innerException">The inner validation exception</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -72,9 +85,9 @@ namespace Octokit
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The login of the owner of the repository.
|
||||
/// The login of the organization of the repository.
|
||||
/// </summary>
|
||||
public string Owner { get; private set; }
|
||||
public string Organization { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user