Add Protect/Unprotect Branch functionality

- Create BranchUpdate model
- Add ProtectBranch and UnprotectBranch calls to RepositoryClient
- Add tests
This commit is contained in:
Ryan Gribble
2015-12-13 21:09:29 +10:00
parent 2a2a4b6d22
commit d7472d6699
8 changed files with 175 additions and 1 deletions
@@ -717,5 +717,93 @@ namespace Octokit.Tests.Clients
Arg.Any<Dictionary<string, string>>());
}
}
public class TheProtectBranchMethod
{
[Fact]
public void GetsCorrectUrl()
{
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.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", ""));
}
}
}
}
+19
View File
@@ -327,5 +327,24 @@ namespace Octokit
/// <param name="update">New values to update the repository with</param>
/// <returns>The updated <see cref="T:Octokit.Repository"/></returns>
Task<Repository> Edit(string owner, string name, RepositoryUpdate update);
/// <summary>
/// Protects 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>
/// <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);
}
}
+41
View File
@@ -505,5 +505,46 @@ namespace Octokit
const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";
return ApiConnection.Get<Branch>(ApiUrls.RepoBranch(owner, repositoryName, branchName), null, previewAcceptsHeader);
}
/// <summary>
/// Protects 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>
/// <returns>The updated <see cref="T:Octokit.Branch"/></returns>
public Task<Branch> ProtectBranch(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);
}
}
}
+22
View File
@@ -0,0 +1,22 @@
using System.Diagnostics;
namespace Octokit
{
/// <summary>
/// Specifies the values used to update a <see cref="Branch"/>.
/// Note: this is a PREVIEW api: https://developer.github.com/changes/2015-11-11-protected-branches-api/
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class BranchUpdate
{
/// <summary>
/// The <see cref="BranchProtection"/> details
/// </summary>
public BranchProtection Protection { get; private set; }
public BranchUpdate()
{
Protection = new BranchProtection();
}
}
}
+1
View File
@@ -98,6 +98,7 @@
<Compile Include="Clients\IMilestonesClient.cs" />
<Compile Include="Helpers\ParameterAttribute.cs" />
<Compile Include="Helpers\ReflectionExtensions.cs" />
<Compile Include="Models\Request\BranchUpdate.cs" />
<Compile Include="Models\Request\GistFileUpdate.cs" />
<Compile Include="Models\Request\GistRequest.cs" />
<Compile Include="Models\Request\GistUpdate.cs" />
+1
View File
@@ -176,6 +176,7 @@
<Compile Include="Http\IResponse.cs" />
<Compile Include="Http\Request.cs" />
<Compile Include="Models\Request\AuthorizationUpdate.cs" />
<Compile Include="Models\Request\BranchUpdate.cs" />
<Compile Include="Models\Request\EditRepositoryHook.cs" />
<Compile Include="Models\Request\GistRequest.cs" />
<Compile Include="Models\Request\GistUpdate.cs" />
+2 -1
View File
@@ -183,6 +183,7 @@
<Compile Include="Http\IResponse.cs" />
<Compile Include="Http\Request.cs" />
<Compile Include="Models\Request\AuthorizationUpdate.cs" />
<Compile Include="Models\Request\BranchUpdate.cs" />
<Compile Include="Models\Request\GistRequest.cs" />
<Compile Include="Models\Request\GistUpdate.cs" />
<Compile Include="Models\Request\EditRepositoryHook.cs" />
@@ -430,4 +431,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
+1
View File
@@ -92,6 +92,7 @@
<Compile Include="Http\IApiInfoProvider.cs" />
<Compile Include="Http\ProductHeaderValue.cs" />
<Compile Include="Http\RequestBody.cs" />
<Compile Include="Models\Request\BranchUpdate.cs" />
<Compile Include="Models\Request\GistFileUpdate.cs" />
<Compile Include="Models\Request\NewArbitraryMarkDown.cs" />
<Compile Include="Models\Request\NewMerge.cs" />