From 1af5f3b1b92d7cbcc1420ff2035fca2b1450e088 Mon Sep 17 00:00:00 2001 From: Ryan Gribble Date: Sun, 13 Dec 2015 22:33:55 +1000 Subject: [PATCH] Change ProtectBranch/UnprotectBranch into a single EditBranch function Add tests for various protected/unprotected scenarios --- .../Clients/BranchesClientTests.cs | 114 +++++++++++++++--- .../Clients/RepositoriesClientTests.cs | 74 ++---------- Octokit/Clients/IRepositoriesClient.cs | 15 +-- Octokit/Clients/RepositoriesClient.cs | 28 +---- 4 files changed, 115 insertions(+), 116 deletions(-) diff --git a/Octokit.Tests.Integration/Clients/BranchesClientTests.cs b/Octokit.Tests.Integration/Clients/BranchesClientTests.cs index ab61a236..00b18beb 100644 --- a/Octokit.Tests.Integration/Clients/BranchesClientTests.cs +++ b/Octokit.Tests.Integration/Clients/BranchesClientTests.cs @@ -16,20 +16,6 @@ public class BranchesClientTests { var github = Helper.GetAuthenticatedClient(); - using (var context = await github.CreateRepositoryContext("public-repo")) - { - var branches = await github.Repository.GetAllBranches(context.Repository.Owner.Login, context.Repository.Name); - - Assert.NotEmpty(branches); - Assert.Equal(branches[0].Name, "master"); - } - } - - [IntegrationTest] - public async Task ReturnsBranchesWithProtectionStatus() - { - var github = Helper.GetAuthenticatedClient(); - using (var context = await github.CreateRepositoryContext("public-repo")) { var branches = await github.Repository.GetAllBranches(context.Repository.Owner.Login, context.Repository.Name); @@ -40,4 +26,104 @@ public class BranchesClientTests } } } + + public class TheEditBranchesMethod + { + private readonly IRepositoriesClient _fixture; + private readonly RepositoryContext _context; + + public TheEditBranchesMethod() + { + var github = Helper.GetAuthenticatedClient(); + _context = github.CreateRepositoryContext("source-repo").Result; + _fixture = github.Repository; + } + + public async Task CreateTheWorld() + { + // Set master branch to be protected, with some status checks + var update = new BranchUpdate(); + update.Protection.Enabled = true; + update.Protection.RequiredStatusChecks.EnforcementLevel = EnforcementLevel.Everyone; + update.Protection.RequiredStatusChecks.AddContext("check1"); + update.Protection.RequiredStatusChecks.AddContext("check2"); + + var newBranch = await _fixture.EditBranch(_context.Repository.Owner.Login, _context.Repository.Name, "master", update); + } + + [IntegrationTest] + public async Task ProtectsBranch() + { + // Set master branch to be protected, with some status checks + var update = new BranchUpdate(); + update.Protection.Enabled = true; + update.Protection.RequiredStatusChecks.EnforcementLevel = EnforcementLevel.Everyone; + update.Protection.RequiredStatusChecks.AddContext("check1"); + update.Protection.RequiredStatusChecks.AddContext("check2"); + update.Protection.RequiredStatusChecks.AddContext("check3"); + + var branch = await _fixture.EditBranch(_context.Repository.Owner.Login, _context.Repository.Name, "master", update); + + // Ensure a branch object was returned + Assert.NotNull(branch); + + // Retrieve master branch + branch = await _fixture.GetBranch(_context.Repository.Owner.Login, _context.Repository.Name, "master"); + + // Assert the changes were made + Assert.Equal(branch.Protection.Enabled, true); + Assert.Equal(branch.Protection.RequiredStatusChecks.EnforcementLevel, EnforcementLevel.Everyone); + Assert.Equal(branch.Protection.RequiredStatusChecks.Contexts.Count, 3); + } + + [IntegrationTest] + public async Task RemoveStatusCheckEnforcement() + { + await CreateTheWorld(); + + // Clear status checks + var update = new BranchUpdate(); + update.Protection.Enabled = true; + update.Protection.RequiredStatusChecks.EnforcementLevel = EnforcementLevel.Off; + update.Protection.RequiredStatusChecks.AddContext("check1"); + var branch = await _fixture.EditBranch(_context.Repository.Owner.Login, _context.Repository.Name, "master", update); + + // Ensure a branch object was returned + Assert.NotNull(branch); + + // Retrieve master branch + branch = await _fixture.GetBranch(_context.Repository.Owner.Login, _context.Repository.Name, "master"); + + // Assert the changes were made + Assert.Equal(branch.Protection.Enabled, true); + Assert.Equal(branch.Protection.RequiredStatusChecks.EnforcementLevel, EnforcementLevel.Off); + Assert.Equal(branch.Protection.RequiredStatusChecks.Contexts.Count, 1); + } + + [IntegrationTest] + public async Task UnprotectsBranch() + { + await CreateTheWorld(); + + // Unprotect branch + var update = new BranchUpdate(); + update.Protection.Enabled = false; + + // Deliberately set Enforcement and Contexts to some values (these should be ignored) + update.Protection.RequiredStatusChecks.EnforcementLevel = EnforcementLevel.Everyone; + update.Protection.RequiredStatusChecks.AddContext("check1"); + var branch = await _fixture.EditBranch(_context.Repository.Owner.Login, _context.Repository.Name, "master", update); + + // Ensure a branch object was returned + Assert.NotNull(branch); + + // Retrieve master branch + branch = await _fixture.GetBranch(_context.Repository.Owner.Login, _context.Repository.Name, "master"); + + // Assert the branch is unprotected, and enforcement/contexts are cleared + Assert.Equal(branch.Protection.Enabled, false); + Assert.Equal(branch.Protection.RequiredStatusChecks.EnforcementLevel, EnforcementLevel.Off); + Assert.Equal(branch.Protection.RequiredStatusChecks.Contexts.Count, 0); + } + } } \ No newline at end of file diff --git a/Octokit.Tests/Clients/RepositoriesClientTests.cs b/Octokit.Tests/Clients/RepositoriesClientTests.cs index 65ed8274..2328ada6 100644 --- a/Octokit.Tests/Clients/RepositoriesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoriesClientTests.cs @@ -718,7 +718,7 @@ namespace Octokit.Tests.Clients } } - public class TheProtectBranchMethod + public class TheEditBranchMethod { [Fact] public void GetsCorrectUrl() @@ -728,81 +728,25 @@ namespace Octokit.Tests.Clients var update = new BranchUpdate(); const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; - client.ProtectBranch("owner", "repo", "branch", update); + client.EditBranch("owner", "repo", "branch", update); connection.Received() .Patch(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch"), Arg.Any(), previewAcceptsHeader); } - [Fact] - public void EnablesProtection() - { - var connection = Substitute.For(); - var client = new RepositoriesClient(connection); - var update = new BranchUpdate(); - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; - - client.ProtectBranch("owner", "repo", "branch", update); - - connection.Received() - .Patch(Arg.Any(), Arg.Is(b => b.Protection.Enabled == true), previewAcceptsHeader); - } - [Fact] public async Task EnsuresNonNullArguments() { var client = new RepositoriesClient(Substitute.For()); var update = new BranchUpdate(); - await Assert.ThrowsAsync(() => client.ProtectBranch(null, "repo", "branch", update)); - await Assert.ThrowsAsync(() => client.ProtectBranch("owner", null, "branch", update)); - await Assert.ThrowsAsync(() => client.ProtectBranch("owner", "repo", null, update)); - await Assert.ThrowsAsync(() => client.ProtectBranch("owner", "repo", "branch", null)); - await Assert.ThrowsAsync(() => client.ProtectBranch("", "repo", "branch", update)); - await Assert.ThrowsAsync(() => client.ProtectBranch("owner", "", "branch", update)); - await Assert.ThrowsAsync(() => client.ProtectBranch("owner", "repo", "", update)); - } - } - - public class TheUnprotectBranchMethod - { - [Fact] - public void GetsCorrectUrl() - { - var connection = Substitute.For(); - var client = new RepositoriesClient(connection); - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; - - client.UnprotectBranch("owner", "repo", "branch"); - - connection.Received() - .Patch(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch"), Arg.Any(), previewAcceptsHeader); - } - - [Fact] - public void DisablesProtection() - { - var connection = Substitute.For(); - var client = new RepositoriesClient(connection); - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; - - client.UnprotectBranch("owner", "repo", "branch"); - - connection.Received() - .Patch(Arg.Any(), Arg.Is(b => b.Protection.Enabled == false), previewAcceptsHeader); - } - - [Fact] - public async Task EnsuresNonNullArguments() - { - var client = new RepositoriesClient(Substitute.For()); - - await Assert.ThrowsAsync(() => client.UnprotectBranch(null, "repo", "branch")); - await Assert.ThrowsAsync(() => client.UnprotectBranch("owner", null, "branch")); - await Assert.ThrowsAsync(() => client.UnprotectBranch("owner", "repo", null)); - await Assert.ThrowsAsync(() => client.UnprotectBranch("", "repo", "branch")); - await Assert.ThrowsAsync(() => client.UnprotectBranch("owner", "", "branch")); - await Assert.ThrowsAsync(() => client.UnprotectBranch("owner", "repo", "")); + await Assert.ThrowsAsync(() => client.EditBranch(null, "repo", "branch", update)); + await Assert.ThrowsAsync(() => client.EditBranch("owner", null, "branch", update)); + await Assert.ThrowsAsync(() => client.EditBranch("owner", "repo", null, update)); + await Assert.ThrowsAsync(() => client.EditBranch("owner", "repo", "branch", null)); + await Assert.ThrowsAsync(() => client.EditBranch("", "repo", "branch", update)); + await Assert.ThrowsAsync(() => client.EditBranch("owner", "", "branch", update)); + await Assert.ThrowsAsync(() => client.EditBranch("owner", "repo", "", update)); } } } diff --git a/Octokit/Clients/IRepositoriesClient.cs b/Octokit/Clients/IRepositoriesClient.cs index 67954bf4..a82880ea 100644 --- a/Octokit/Clients/IRepositoriesClient.cs +++ b/Octokit/Clients/IRepositoriesClient.cs @@ -329,22 +329,13 @@ namespace Octokit Task Edit(string owner, string name, RepositoryUpdate update); /// - /// Protects the specified branch with the values given in + /// Edit the specified branch with the values given in /// /// The owner of the repository /// The name of the repository /// The name of the branch - /// + /// New values to update the branch with /// The updated - Task ProtectBranch(string owner, string repositoryName, string branchName, BranchUpdate update); - - /// - /// Unprotects the specified branch - /// - /// The owner of the repository - /// The name of the repository - /// The name of the branch - /// The updated - Task UnprotectBranch(string owner, string repositoryName, string branchName); + Task EditBranch(string owner, string repositoryName, string branchName, BranchUpdate update); } } diff --git a/Octokit/Clients/RepositoriesClient.cs b/Octokit/Clients/RepositoriesClient.cs index 46f42730..cf5cbae8 100644 --- a/Octokit/Clients/RepositoriesClient.cs +++ b/Octokit/Clients/RepositoriesClient.cs @@ -507,42 +507,20 @@ namespace Octokit } /// - /// Protects the specified branch with the values given in + /// Edit the specified branch with the values given in /// /// The owner of the repository /// The name of the repository /// The name of the branch - /// + /// New values to update the branch with /// The updated - public Task ProtectBranch(string owner, string repositoryName, string branchName, BranchUpdate update) + public Task EditBranch(string owner, string repositoryName, string branchName, BranchUpdate update) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName"); Ensure.ArgumentNotNull(update, "update"); - update.Protection.Enabled = true; - - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; - return ApiConnection.Patch(ApiUrls.RepoBranch(owner, repositoryName, branchName), update, previewAcceptsHeader); - } - - /// - /// Unprotects the specified branch - /// - /// The owner of the repository - /// The name of the repository - /// The name of the branch - /// The updated - public Task UnprotectBranch(string owner, string repositoryName, string branchName) - { - Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); - Ensure.ArgumentNotNullOrEmptyString(repositoryName, "repositoryName"); - Ensure.ArgumentNotNullOrEmptyString(branchName, "branchName"); - - var update = new BranchUpdate(); - update.Protection.Enabled = false; - const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; return ApiConnection.Patch(ApiUrls.RepoBranch(owner, repositoryName, branchName), update, previewAcceptsHeader); }