mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-04 11:24:44 +00:00
Change ProtectBranch/UnprotectBranch into a single EditBranch function
Add tests for various protected/unprotected scenarios
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Branch>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch"), Arg.Any<BranchUpdate>(), previewAcceptsHeader);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EnablesProtection()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
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<Branch>(Arg.Any<Uri>(), Arg.Is<BranchUpdate>(b => b.Protection.Enabled == true), previewAcceptsHeader);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsuresNonNullArguments()
|
||||
{
|
||||
var client = new RepositoriesClient(Substitute.For<IApiConnection>());
|
||||
var update = new BranchUpdate();
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ProtectBranch(null, "repo", "branch", update));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ProtectBranch("owner", null, "branch", update));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ProtectBranch("owner", "repo", null, update));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.ProtectBranch("owner", "repo", "branch", null));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.ProtectBranch("", "repo", "branch", update));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.ProtectBranch("owner", "", "branch", update));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.ProtectBranch("owner", "repo", "", update));
|
||||
}
|
||||
}
|
||||
|
||||
public class TheUnprotectBranchMethod
|
||||
{
|
||||
[Fact]
|
||||
public void GetsCorrectUrl()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new RepositoriesClient(connection);
|
||||
const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";
|
||||
|
||||
client.UnprotectBranch("owner", "repo", "branch");
|
||||
|
||||
connection.Received()
|
||||
.Patch<Branch>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch"), Arg.Any<BranchUpdate>(), previewAcceptsHeader);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisablesProtection()
|
||||
{
|
||||
var connection = Substitute.For<IApiConnection>();
|
||||
var client = new RepositoriesClient(connection);
|
||||
const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";
|
||||
|
||||
client.UnprotectBranch("owner", "repo", "branch");
|
||||
|
||||
connection.Received()
|
||||
.Patch<Branch>(Arg.Any<Uri>(), Arg.Is<BranchUpdate>(b => b.Protection.Enabled == false), previewAcceptsHeader);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EnsuresNonNullArguments()
|
||||
{
|
||||
var client = new RepositoriesClient(Substitute.For<IApiConnection>());
|
||||
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UnprotectBranch(null, "repo", "branch"));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UnprotectBranch("owner", null, "branch"));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UnprotectBranch("owner", "repo", null));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.UnprotectBranch("", "repo", "branch"));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.UnprotectBranch("owner", "", "branch"));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.UnprotectBranch("owner", "repo", ""));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.EditBranch(null, "repo", "branch", update));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.EditBranch("owner", null, "branch", update));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.EditBranch("owner", "repo", null, update));
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(() => client.EditBranch("owner", "repo", "branch", null));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.EditBranch("", "repo", "branch", update));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.EditBranch("owner", "", "branch", update));
|
||||
await Assert.ThrowsAsync<ArgumentException>(() => client.EditBranch("owner", "repo", "", update));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,22 +329,13 @@ namespace Octokit
|
||||
Task<Repository> Edit(string owner, string name, RepositoryUpdate update);
|
||||
|
||||
/// <summary>
|
||||
/// Protects the specified branch with the values given in <paramref name="update"/>
|
||||
/// Edit the specified branch with the values given in <paramref name="update"/>
|
||||
/// </summary>
|
||||
/// <param name="owner">The owner of the repository</param>
|
||||
/// <param name="repositoryName">The name of the repository</param>
|
||||
/// <param name="branchName">The name of the branch</param>
|
||||
/// <param name="update"></param>
|
||||
/// <param name="update">New values to update the branch with</param>
|
||||
/// <returns>The updated <see cref="T:Octokit.Branch"/></returns>
|
||||
Task<Branch> ProtectBranch(string owner, string repositoryName, string branchName, BranchUpdate update);
|
||||
|
||||
/// <summary>
|
||||
/// Unprotects the specified branch
|
||||
/// </summary>
|
||||
/// <param name="owner">The owner of the repository</param>
|
||||
/// <param name="repositoryName">The name of the repository</param>
|
||||
/// <param name="branchName">The name of the branch</param>
|
||||
/// <returns>The updated <see cref="T:Octokit.Branch"/></returns>
|
||||
Task<Branch> UnprotectBranch(string owner, string repositoryName, string branchName);
|
||||
Task<Branch> EditBranch(string owner, string repositoryName, string branchName, BranchUpdate update);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -507,42 +507,20 @@ namespace Octokit
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Protects the specified branch with the values given in <paramref name="update"/>
|
||||
/// Edit the specified branch with the values given in <paramref name="update"/>
|
||||
/// </summary>
|
||||
/// <param name="owner">The owner of the repository</param>
|
||||
/// <param name="repositoryName">The name of the repository</param>
|
||||
/// <param name="branchName">The name of the branch</param>
|
||||
/// <param name="update"></param>
|
||||
/// <param name="update">New values to update the branch with</param>
|
||||
/// <returns>The updated <see cref="T:Octokit.Branch"/></returns>
|
||||
public Task<Branch> ProtectBranch(string owner, string repositoryName, string branchName, BranchUpdate update)
|
||||
public Task<Branch> 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<Branch>(ApiUrls.RepoBranch(owner, repositoryName, branchName), update, previewAcceptsHeader);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unprotects the specified branch
|
||||
/// </summary>
|
||||
/// <param name="owner">The owner of the repository</param>
|
||||
/// <param name="repositoryName">The name of the repository</param>
|
||||
/// <param name="branchName">The name of the branch</param>
|
||||
/// <returns>The updated <see cref="T:Octokit.Branch"/></returns>
|
||||
public Task<Branch> 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<Branch>(ApiUrls.RepoBranch(owner, repositoryName, branchName), update, previewAcceptsHeader);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user