diff --git a/Octokit.Tests/Clients/RepositoryHooksClientTest.cs b/Octokit.Tests/Clients/RepositoryHooksClientTest.cs index bc00f414..212403e3 100644 --- a/Octokit.Tests/Clients/RepositoryHooksClientTest.cs +++ b/Octokit.Tests/Clients/RepositoryHooksClientTest.cs @@ -53,5 +53,137 @@ namespace Octokit.Tests.Clients await AssertEx.Throws(async () => await client.Hooks.GetById("owner", null, 123)); } } + + public class TheCreateMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + var hook = new NewRepositoryHook(); + + client.Hooks.Create("fake", "repo", hook); + + connection.Received().Post(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks"), hook); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new RepositoriesClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.Hooks.Create(null, "name", new NewRepositoryHook())); + await AssertEx.Throws(async () => await client.Hooks.Create("owner", null, new NewRepositoryHook())); + await AssertEx.Throws(async () => await client.Hooks.Create("owner", "name", null)); + } + + [Fact] + public void UsesTheSuppliedHook() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + var newRepositoryHook = new NewRepositoryHook { Name = "aName" }; + + client.Hooks.Create("owner", "repo", newRepositoryHook); + + connection.Received().Post(Arg.Any(), newRepositoryHook); + } + } + + public class TheEditMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + var hook = new EditRepositoryHook(); + + client.Hooks.Edit("fake", "repo", 12345678, hook); + + connection.Received().Patch(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks/12345678"), hook); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new RepositoriesClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.Hooks.Edit(null, "name", 12345678, new EditRepositoryHook())); + await AssertEx.Throws(async () => await client.Hooks.Edit("owner", null, 12345678, new EditRepositoryHook())); + await AssertEx.Throws(async () => await client.Hooks.Edit("owner", "name", 12345678, null)); + } + + [Fact] + public void UsesTheSuppliedHook() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + var editRepositoryHook = new EditRepositoryHook() { Active = false }; + + client.Hooks.Edit("owner", "repo", 12345678, editRepositoryHook); + + connection.Received().Patch(Arg.Any(), editRepositoryHook); + } + } + + + public class TheTestMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + + client.Hooks.Test("fake", "repo", 12345678); + + connection.Received().Post(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks/12345678/tests"), null); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new RepositoriesClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.Hooks.Test(null, "name", 12345678)); + await AssertEx.Throws(async () => await client.Hooks.Test("owner", null, 12345678)); + } + + [Fact] + public void CallsPost() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + + client.Hooks.Test("owner", "repo", 12345678); + + connection.Received().Post(Arg.Any(), Arg.Is(o => o == null)); + } + } + + public class TheDeleteMethod + { + [Fact] + public void RequestsCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoriesClient(connection); + + client.Hooks.Delete("fake", "repo", 12345678); + + connection.Received().Delete(Arg.Is(u => u.ToString() == "repos/fake/repo/hooks/12345678")); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new RepositoriesClient(Substitute.For()); + + await AssertEx.Throws(async () => await client.Hooks.Delete(null, "name", 12345678)); + await AssertEx.Throws(async () => await client.Hooks.Delete("owner", null, 12345678)); + } + } } } diff --git a/Octokit/Clients/IRepositoryHooksClient.cs b/Octokit/Clients/IRepositoryHooksClient.cs index bb46186c..18ea51e9 100644 --- a/Octokit/Clients/IRepositoryHooksClient.cs +++ b/Octokit/Clients/IRepositoryHooksClient.cs @@ -8,7 +8,7 @@ namespace Octokit /// /// Gets the list of hooks defined for a repository /// - /// See API documentation for more information. + /// 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); @@ -23,12 +23,37 @@ namespace Octokit /// See API documentation for more information. Task GetById(string owner, string repositoryName, int hookId); + /// + /// Creates a hook for a repository + /// + /// See API documentation for more information. + /// Task Create(string owner, string repositoryName, NewRepositoryHook hook); - Task Edit(string owner, string repositoryName, string hookId, EditRepositoryHook hook); + /// + /// Edits a hook for a repository + /// + /// See API documentation for more information. + /// + Task Edit(string owner, string repositoryName, int hookId, EditRepositoryHook hook); - Task Test(string owner, string repositoryName, string hookId); + /// + /// Tests a hook for a repository + /// + /// See API documentation for more information. + /// This will trigger the hook with the latest push to the current repository if the hook is subscribed to push events. If the hook + /// is not subscribed to push events, the server will respond with 204 but no test POST will be generated. + /// + Task Test(string owner, string repositoryName, int hookId); - Task Delete(string owner, string repositoryName, string hookId); + /// + /// Deletes a hook for a repository + /// + /// + /// + /// + /// See API documentation for more information. + /// + Task Delete(string owner, string repositoryName, int hookId); } } diff --git a/Octokit/Clients/RepositoryHooksClient.cs b/Octokit/Clients/RepositoryHooksClient.cs index a8d46533..a508e124 100644 --- a/Octokit/Clients/RepositoryHooksClient.cs +++ b/Octokit/Clients/RepositoryHooksClient.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Octokit @@ -39,7 +40,7 @@ namespace Octokit Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); - return ApiConnection.Get(ApiUrls.RepositoryHooksById(owner, repositoryName, hookId)); + return ApiConnection.Get(ApiUrls.RepositoryHookById(owner, repositoryName, hookId)); } /// @@ -49,22 +50,58 @@ namespace Octokit /// public Task Create(string owner, string repositoryName, NewRepositoryHook hook) { - throw new System.NotImplementedException(); + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + Ensure.ArgumentNotNull(hook, "hook"); + + return ApiConnection.Post(ApiUrls.RepositoryHooks(owner, repositoryName), hook); } - public Task Edit(string owner, string repositoryName, string hookId, EditRepositoryHook hook) + /// + /// Edits a hook for a repository + /// + /// See API documentation for more information. + /// + public Task Edit(string owner, string repositoryName, int hookId, EditRepositoryHook hook) { - throw new System.NotImplementedException(); + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + Ensure.ArgumentNotNull(hook, "hook"); + + return ApiConnection.Patch(ApiUrls.RepositoryHookById(owner, repositoryName, hookId), hook); } - public Task Test(string owner, string repositoryName, string hookId) + /// + /// Tests a hook for a repository + /// + /// See API documentation for more information. + /// This will trigger the hook with the latest push to the current repository if the hook is subscribed to push events. If the hook + /// is not subscribed to push events, the server will respond with 204 but no test POST will be generated. + /// + public Task Test(string owner, string repositoryName, int hookId) { - throw new System.NotImplementedException(); + throw new NotImplementedException(); + + //Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + //Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + + //return ApiConnection.Post(ApiUrls.RepositoryHookTest(owner, repositoryName, hookId), null); } - public Task Delete(string owner, string repositoryName, string hookId) + /// + /// Deletes a hook for a repository + /// + /// + /// + /// + /// See API documentation for more information. + /// + public Task Delete(string owner, string repositoryName, int hookId) { - throw new System.NotImplementedException(); + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); + + return ApiConnection.Delete(ApiUrls.RepositoryHookById(owner, repositoryName, hookId)); } } } \ No newline at end of file diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index ffe0ab57..e254160f 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -265,9 +265,21 @@ namespace Octokit /// The name of the repository /// The identifier of the repository hook /// - public static Uri RepositoryHooksById(string owner, string repositoryName, int hookId) + public static Uri RepositoryHookById(string owner, string repositoryName, int hookId) { return "repos/{0}/{1}/hooks/{2}".FormatUri(owner, repositoryName, hookId); } + + /// + /// Returns the that can tests a specified repository hook + /// + /// The owner of the repository + /// The name of the repository + /// The identifier of the repository hook + /// + public static Uri RepositoryHookTest(string owner, string repositoryName, int hookId) + { + return "repos/{0}/{1}/hooks/{2}/tests".FormatUri(owner, repositoryName, hookId); + } } }