diff --git a/Octokit.Reactive/Clients/IObservableRepositoryForksClient.cs b/Octokit.Reactive/Clients/IObservableRepositoryForksClient.cs new file mode 100644 index 00000000..ed73fc72 --- /dev/null +++ b/Octokit.Reactive/Clients/IObservableRepositoryForksClient.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Octokit.Reactive +{ + public interface IObservableRepositoryForksClient + { + /// + /// Gets the list of forks defined for a repository + /// + /// See API documentation for more information. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", Justification = "This is ok; we're matching HTTP verbs not keyworks")] + IObservable> Get(string owner, string repositoryName); + + /// + /// Creates a fork for a repository. Specify organization in the fork parameter to create for an organization. + /// + /// See API documentation for more information. + /// + IObservable Create(string owner, string repositoryName, NewRepositoryFork fork); + } +} diff --git a/Octokit.Reactive/Clients/ObservableRepositoryForksClient.cs b/Octokit.Reactive/Clients/ObservableRepositoryForksClient.cs new file mode 100644 index 00000000..1e07afc1 --- /dev/null +++ b/Octokit.Reactive/Clients/ObservableRepositoryForksClient.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Reactive.Threading.Tasks; +using System.Threading.Tasks; + +namespace Octokit.Reactive +{ + public class ObservableRepositoryForksClient : IObservableRepositoryForksClient + { + readonly IRepositoryForksClient _client; + + /// + /// Initializes a new GitHub Repos Fork API client. + /// + /// + public ObservableRepositoryForksClient(IGitHubClient client) + { + Ensure.ArgumentNotNull(client, "client"); + _client = client.Repository.Forks; + } + + /// + /// Gets the list of forks defined for a repository + /// + /// See API documentation for more information. + /// + public IObservable> Get(string owner, string repositoryName) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + + return _client.Get(owner, repositoryName).ToObservable(); + } + + /// + /// Creates a fork for a repository. Specify organization in the fork parameter to create for an organization. + /// + /// See API documentation for more information. + /// + public IObservable Create(string owner, string repositoryName, NewRepositoryFork fork) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + Ensure.ArgumentNotNull(fork, "fork"); + + return _client.Create(owner, repositoryName, fork).ToObservable(); + } + } +} \ No newline at end of file diff --git a/Octokit.Tests/Clients/RepositoryForksClientTests.cs b/Octokit.Tests/Clients/RepositoryForksClientTests.cs new file mode 100644 index 00000000..d640ef88 --- /dev/null +++ b/Octokit.Tests/Clients/RepositoryForksClientTests.cs @@ -0,0 +1,71 @@ +using System; +using System.Threading.Tasks; +using NSubstitute; +using Octokit.Tests.Helpers; +using Xunit; + +namespace Octokit.Tests.Clients +{ + public class RepositoryForksClientTests + { + public class TheGetMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + + client.Forks.Get("fake", "repo"); + + connection.Received().GetAll(Arg.Is(u => u.ToString() == "repos/fake/repo/forks")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new RepositoriesClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.Forks.Get(null, "name")); + await AssertEx.Throws(async () => await client.Forks.Get("owner", null)); + } + } + + public class TheCreateMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + var newRepositoryFork = new NewRepositoryFork(); + + client.Forks.Create("fake", "repo", newRepositoryFork); + + connection.Received().Post(Arg.Is(u => u.ToString() == "repos/fake/repo/forks"), newRepositoryFork); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new RepositoriesClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.Forks.Create(null, "name", new NewRepositoryFork())); + await AssertEx.Throws(async () => await client.Forks.Create("owner", null, new NewRepositoryFork())); + await AssertEx.Throws(async () => await client.Forks.Create("owner", "name", null)); + } + + [Fact] + public void UsesTheSuppliedHook() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + var newRepositoryFork = new NewRepositoryFork { Organization = "aName" }; + + client.Forks.Create("owner", "repo", newRepositoryFork); + + connection.Received().Post(Arg.Any(), newRepositoryFork); + } + } + } +} \ No newline at end of file diff --git a/Octokit/Clients/IRepositoryForksClient.cs b/Octokit/Clients/IRepositoryForksClient.cs new file mode 100644 index 00000000..28756aef --- /dev/null +++ b/Octokit/Clients/IRepositoryForksClient.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Octokit +{ + public interface IRepositoryForksClient + { + /// + /// Gets the list of forks defined for a repository + /// + /// See API documentation for more information. + /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", Justification = "This is ok; we're matching HTTP verbs not keyworks")] + Task> Get(string owner, string repositoryName); + + /// + /// Creates a fork for a repository. Specify organization in the fork parameter to create for an organization. + /// + /// See API documentation for more information. + /// + Task Create(string owner, string repositoryName, NewRepositoryFork fork); + } +} diff --git a/Octokit/Clients/RepositoryForksClient.cs b/Octokit/Clients/RepositoryForksClient.cs new file mode 100644 index 00000000..70af7f5f --- /dev/null +++ b/Octokit/Clients/RepositoryForksClient.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Octokit +{ + public class RepositoryForksClient : ApiClient, IRepositoryForksClient + { + /// + /// Initializes a new GitHub Repos Fork API client. + /// + /// An API connection. + public RepositoryForksClient(IApiConnection apiConnection) : base(apiConnection) + { + } + + /// + /// Gets the list of forks defined for a repository + /// + /// See API documentation for more information. + /// + public Task> Get(string owner, string repositoryName) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + + return ApiConnection.GetAll(ApiUrls.RepositoryForks(owner, repositoryName)); + } + + /// + /// Creates a fork for a repository. Specify organization in the fork parameter to create for an organization. + /// + /// See API documentation for more information. + /// + public Task Create(string owner, string repositoryName, NewRepositoryFork fork) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + Ensure.ArgumentNotNull(fork, "fork"); + + return ApiConnection.Post(ApiUrls.RepositoryForks(owner, repositoryName), fork); + } + } +} \ No newline at end of file diff --git a/Octokit/Models/Request/NewRepositoryFork.cs b/Octokit/Models/Request/NewRepositoryFork.cs new file mode 100644 index 00000000..86d97ba8 --- /dev/null +++ b/Octokit/Models/Request/NewRepositoryFork.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; + +namespace Octokit +{ + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class NewRepositoryFork + { + public string Organization { get; set; } + + internal string DebuggerDisplay + { + get + { + return String.Format(CultureInfo.InvariantCulture, + "Repository Hook: Organization: {0}", Organization); + } + } + } +} \ No newline at end of file