diff --git a/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs b/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs index 49e0a7c8..8b960ec3 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs @@ -92,5 +92,69 @@ namespace Octokit.Reactive /// New values to update the branch with [Obsolete("BranchProtection preview functionality in the GitHub API has had breaking changes. This existing implementation will cease to work when the preview period ends.")] IObservable Edit(int repositoryId, string branch, BranchUpdate update); + + /// + /// Get the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch + IObservable GetBranchProtection(string owner, string name, string branch); + + /// + /// Get the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// The name of the branch + IObservable GetBranchProtection(int repositoryId, string branch); + + /// + /// Update the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch + /// Branch protection settings + IObservable UpdateBranchProtection(string owner, string name, string branch, BranchProtectionSettingsUpdate update); + + /// + /// Update the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// Branch protection settings + IObservable UpdateBranchProtection(int repositoryId, string branch, BranchProtectionSettingsUpdate update); + + /// + /// Remove the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch + IObservable DeleteBranchProtection(string owner, string name, string branch); + + /// + /// Remove the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// The name of the branch + IObservable DeleteBranchProtection(int repositoryId, string branch); } } diff --git a/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs b/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs index 269da55f..7aec5e28 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs @@ -147,5 +147,107 @@ namespace Octokit.Reactive return _client.Edit(repositoryId, branch, update).ToObservable(); } + + /// + /// Get the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch + public IObservable GetBranchProtection(string owner, string name, string branch) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); + + return _client.GetBranchProtection(owner, name, branch).ToObservable(); + } + + /// + /// Get the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// The name of the branch + public IObservable GetBranchProtection(int repositoryId, string branch) + { + Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); + + return _client.GetBranchProtection(repositoryId, branch).ToObservable(); + } + + /// + /// Update the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch + /// Branch protection settings + public IObservable UpdateBranchProtection(string owner, string name, string branch, BranchProtectionSettingsUpdate update) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); + Ensure.ArgumentNotNull(update, "update"); + + return _client.UpdateBranchProtection(owner, name, branch, update).ToObservable(); + } + + /// + /// Update the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// Branch protection settings + public IObservable UpdateBranchProtection(int repositoryId, string branch, BranchProtectionSettingsUpdate update) + { + Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); + Ensure.ArgumentNotNull(update, "update"); + + return _client.UpdateBranchProtection(repositoryId, branch, update).ToObservable(); + } + + /// + /// Remove the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch + public IObservable DeleteBranchProtection(string owner, string name, string branch) + { + Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); + Ensure.ArgumentNotNullOrEmptyString(name, "name"); + Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); + + return _client.DeleteBranchProtection(owner, name, branch).ToObservable(); + } + + /// + /// Remove the branch protection settings for the specified branch /> + /// + /// + /// See the API documentation for more details + /// + /// The Id of the repository + /// The name of the branch + public IObservable DeleteBranchProtection(int repositoryId, string branch) + { + Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); + + return _client.DeleteBranchProtection(repositoryId, branch).ToObservable(); + } } } diff --git a/Octokit.Tests/Reactive/ObservableRepositoryBranchesClientTests.cs b/Octokit.Tests/Reactive/ObservableRepositoryBranchesClientTests.cs index 91811fd1..5a8e6338 100644 --- a/Octokit.Tests/Reactive/ObservableRepositoryBranchesClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableRepositoryBranchesClientTests.cs @@ -152,7 +152,7 @@ namespace Octokit.Tests.Reactive public class TheEditMethod { [Fact] - public void PatchsTheCorrectUrl() + public void RequestsTheCorrectUrl() { var github = Substitute.For(); var client = new ObservableRepositoryBranchesClient(github); @@ -164,7 +164,7 @@ namespace Octokit.Tests.Reactive } [Fact] - public void PatchsTheCorrectUrlWithRepositoryId() + public void RequestsTheCorrectUrlWithRepositoryId() { var github = Substitute.For(); var client = new ObservableRepositoryBranchesClient(github); @@ -196,5 +196,148 @@ namespace Octokit.Tests.Reactive Assert.Throws(() => client.Edit(1, "", update)); } } + + public class TheGetBranchProtectectionMethod + { + [Fact] + public void RequestsTheCorrectUrl() + { + var gitHubClient = Substitute.For(); + var client = new ObservableRepositoryBranchesClient(gitHubClient); + + client.GetBranchProtection("owner", "repo", "branch"); + + gitHubClient.Repository.Branch.Received() + .GetBranchProtection("owner", "repo", "branch"); + } + + [Fact] + public void RequestsTheCorrectUrlWithRepositoryId() + { + var gitHubClient = Substitute.For(); + var client = new ObservableRepositoryBranchesClient(gitHubClient); + + client.GetBranchProtection(1, "branch"); + + gitHubClient.Repository.Branch.Received() + .GetBranchProtection(1, "branch"); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new ObservableRepositoryBranchesClient(Substitute.For()); + + Assert.Throws(() => client.GetBranchProtection(null, "repo", "branch")); + Assert.Throws(() => client.GetBranchProtection("owner", null, "branch")); + Assert.Throws(() => client.GetBranchProtection("owner", "repo", null)); + + Assert.Throws(() => client.GetBranchProtection(1, null)); + + Assert.Throws(() => client.GetBranchProtection("", "repo", "branch")); + Assert.Throws(() => client.GetBranchProtection("owner", "", "branch")); + Assert.Throws(() => client.GetBranchProtection("owner", "repo", "")); + + Assert.Throws(() => client.GetBranchProtection(1, "")); + } + } + + public class TheUpdateBranchProtectionMethod + { + [Fact] + public void RequestsTheCorrectUrl() + { + var gitHubClient = Substitute.For(); + var client = new ObservableRepositoryBranchesClient(gitHubClient); + var update = new BranchProtectionSettingsUpdate( + new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); + + client.UpdateBranchProtection("owner", "repo", "branch", update); + + gitHubClient.Repository.Branch.Received() + .UpdateBranchProtection("owner", "repo", "branch", update); + } + + [Fact] + public void RequestsTheCorrectUrlWithRepositoryId() + { + var gitHubClient = Substitute.For(); + var client = new ObservableRepositoryBranchesClient(gitHubClient); + var update = new BranchProtectionSettingsUpdate( + new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); + + client.UpdateBranchProtection(1, "branch", update); + + gitHubClient.Repository.Branch.Received() + .UpdateBranchProtection(1, "branch", update); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new ObservableRepositoryBranchesClient(Substitute.For()); + var update = new BranchProtectionSettingsUpdate( + new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); + + Assert.Throws(() => client.UpdateBranchProtection(null, "repo", "branch", update)); + Assert.Throws(() => client.UpdateBranchProtection("owner", null, "branch", update)); + Assert.Throws(() => client.UpdateBranchProtection("owner", "repo", null, update)); + Assert.Throws(() => client.UpdateBranchProtection("owner", "repo", "branch", null)); + + Assert.Throws(() => client.UpdateBranchProtection(1, null, update)); + Assert.Throws(() => client.UpdateBranchProtection(1, "branch", null)); + + Assert.Throws(() => client.UpdateBranchProtection("", "repo", "branch", update)); + Assert.Throws(() => client.UpdateBranchProtection("owner", "", "branch", update)); + Assert.Throws(() => client.UpdateBranchProtection("owner", "repo", "", update)); + + Assert.Throws(() => client.UpdateBranchProtection(1, "", update)); + } + } + + public class TheDeleteBranchProtectectionMethod + { + [Fact] + public void RequestsTheCorrectUrl() + { + var gitHubClient = Substitute.For(); + var client = new ObservableRepositoryBranchesClient(gitHubClient); + + client.DeleteBranchProtection("owner", "repo", "branch"); + + gitHubClient.Repository.Branch.Received() + .DeleteBranchProtection("owner", "repo", "branch"); + } + + [Fact] + public void RequestsTheCorrectUrlWithRepositoryId() + { + var gitHubClient = Substitute.For(); + var client = new ObservableRepositoryBranchesClient(gitHubClient); + + client.DeleteBranchProtection(1, "branch"); + + gitHubClient.Repository.Branch.Received() + .DeleteBranchProtection(1, "branch"); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new ObservableRepositoryBranchesClient(Substitute.For()); + + Assert.Throws(() => client.DeleteBranchProtection(null, "repo", "branch")); + Assert.Throws(() => client.DeleteBranchProtection("owner", null, "branch")); + Assert.Throws(() => client.DeleteBranchProtection("owner", "repo", null)); + + Assert.Throws(() => client.DeleteBranchProtection(1, null)); + + Assert.Throws(() => client.DeleteBranchProtection("", "repo", "branch")); + Assert.Throws(() => client.DeleteBranchProtection("owner", "", "branch")); + Assert.Throws(() => client.DeleteBranchProtection("owner", "repo", "")); + + Assert.Throws(() => client.DeleteBranchProtection(1, "")); + } + } } }