diff --git a/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs b/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs
index 49e0a7c82d..dbf2452d0f 100644
--- a/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs
+++ b/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs
@@ -92,5 +92,70 @@ public interface IObservableRepositoryBranchesClient
/// 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
+ /// The name of the branch
+ /// 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 269da55fca..eb200bdb45 100644
--- a/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs
+++ b/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs
@@ -147,5 +147,108 @@ public IObservable Edit(int repositoryId, string branch, BranchUpdate up
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
+ /// The name of the branch
+ /// 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.Integration/Clients/RepositoryBranchesClientTests.cs b/Octokit.Tests.Integration/Clients/RepositoryBranchesClientTests.cs
index 5202fe0090..39542e6181 100644
--- a/Octokit.Tests.Integration/Clients/RepositoryBranchesClientTests.cs
+++ b/Octokit.Tests.Integration/Clients/RepositoryBranchesClientTests.cs
@@ -1,4 +1,6 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
+using System.Linq;
using System.Threading.Tasks;
using Octokit;
using Octokit.Tests.Integration;
@@ -18,11 +20,12 @@ public async Task GetsAllBranches()
Assert.NotEmpty(branches);
- // Ensure Protection attribute is deserialized
foreach (var branch in branches)
{
Assert.NotNull(branch.Protection);
}
+
+ Assert.True(branches.First(x => x.Name == "master").Protected);
}
[IntegrationTest]
@@ -33,6 +36,13 @@ public async Task GetsAllBranchesWithRepositoryId()
var branches = await github.Repository.Branch.GetAll(7528679);
Assert.NotEmpty(branches);
+
+ foreach (var branch in branches)
+ {
+ Assert.NotNull(branch.Protection);
+ }
+
+ Assert.True(branches.First(x => x.Name == "master").Protected);
}
[IntegrationTest]
@@ -179,6 +189,8 @@ public async Task GetsABranch()
Assert.NotNull(branch);
Assert.Equal("master", branch.Name);
+ Assert.NotNull(branch.Protection);
+ Assert.True(branch.Protected);
}
[IntegrationTest]
@@ -190,6 +202,9 @@ public async Task GetsABranchWithRepositoryId()
Assert.NotNull(branch);
Assert.Equal("master", branch.Name);
+
+ Assert.NotNull(branch.Protection);
+ Assert.True(branch.Protected);
}
}
@@ -290,4 +305,242 @@ public async Task UnprotectsBranch()
Assert.Equal(branch.Protection.RequiredStatusChecks.Contexts.Count, 0);
}
}
+
+ public class TheGetBranchProtectionMethod : IDisposable
+ {
+ IRepositoryBranchesClient _client;
+ RepositoryContext _userRepoContext;
+ OrganizationRepositoryWithTeamContext _orgRepoContext;
+
+ public TheGetBranchProtectionMethod()
+ {
+ var github = Helper.GetAuthenticatedClient();
+ _client = github.Repository.Branch;
+
+ _userRepoContext = github.CreateRepositoryWithProtectedBranch().Result;
+ _orgRepoContext = github.CreateOrganizationRepositoryWithProtectedBranch().Result;
+ }
+
+ [IntegrationTest]
+ public async Task GetsBranchProtection()
+ {
+ var repoOwner = _userRepoContext.RepositoryOwner;
+ var repoName = _userRepoContext.RepositoryName;
+ var protection = await _client.GetBranchProtection(repoOwner, repoName, "master");
+
+ Assert.True(protection.RequiredStatusChecks.IncludeAdmins);
+ Assert.True(protection.RequiredStatusChecks.Strict);
+ Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
+
+ Assert.Null(protection.Restrictions);
+ }
+
+ [IntegrationTest]
+ public async Task GetsBranchProtectionWithRepositoryId()
+ {
+ var repoId = _userRepoContext.RepositoryId;
+ var protection = await _client.GetBranchProtection(repoId, "master");
+
+ Assert.True(protection.RequiredStatusChecks.IncludeAdmins);
+ Assert.True(protection.RequiredStatusChecks.Strict);
+ Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
+
+ Assert.Null(protection.Restrictions);
+ }
+
+ [IntegrationTest]
+ public async Task GetsBranchProtectionForOrgRepo()
+ {
+ var repoOwner = _orgRepoContext.RepositoryContext.RepositoryOwner;
+ var repoName = _orgRepoContext.RepositoryContext.RepositoryName;
+ var protection = await _client.GetBranchProtection(repoOwner, repoName, "master");
+
+ Assert.True(protection.RequiredStatusChecks.IncludeAdmins);
+ Assert.True(protection.RequiredStatusChecks.Strict);
+ Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
+
+ Assert.Equal(1, protection.Restrictions.Teams.Count);
+ Assert.Equal(0, protection.Restrictions.Users.Count);
+ }
+
+ [IntegrationTest]
+ public async Task GetsBranchProtectionForOrgRepoWithRepositoryId()
+ {
+ var repoId = _orgRepoContext.RepositoryContext.RepositoryId;
+ var protection = await _client.GetBranchProtection(repoId, "master");
+
+ Assert.True(protection.RequiredStatusChecks.IncludeAdmins);
+ Assert.True(protection.RequiredStatusChecks.Strict);
+ Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
+
+ Assert.Equal(1, protection.Restrictions.Teams.Count);
+ Assert.Equal(0, protection.Restrictions.Users.Count);
+ }
+
+ public void Dispose()
+ {
+ if (_userRepoContext != null)
+ _userRepoContext.Dispose();
+
+ if (_orgRepoContext != null)
+ _orgRepoContext.Dispose();
+ }
+ }
+
+ public class TheUpdateBranchProtectionMethod : IDisposable
+ {
+ IRepositoryBranchesClient _client;
+ RepositoryContext _userRepoContext;
+ OrganizationRepositoryWithTeamContext _orgRepoContext;
+
+ public TheUpdateBranchProtectionMethod()
+ {
+ var github = Helper.GetAuthenticatedClient();
+ _client = github.Repository.Branch;
+
+ _userRepoContext = github.CreateRepositoryWithProtectedBranch().Result;
+ _orgRepoContext = github.CreateOrganizationRepositoryWithProtectedBranch().Result;
+ }
+
+ [IntegrationTest]
+ public async Task UpdatesBranchProtection()
+ {
+ var repoOwner = _userRepoContext.RepositoryOwner;
+ var repoName = _userRepoContext.RepositoryName;
+ var update = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }));
+
+ var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update);
+
+ Assert.False(protection.RequiredStatusChecks.IncludeAdmins);
+ Assert.False(protection.RequiredStatusChecks.Strict);
+ Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
+
+ Assert.Null(protection.Restrictions);
+ }
+
+ [IntegrationTest]
+ public async Task UpdatesBranchProtectionWithRepositoryId()
+ {
+ var repoId = _userRepoContext.RepositoryId;
+ var update = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }));
+
+ var protection = await _client.UpdateBranchProtection(repoId, "master", update);
+
+ Assert.False(protection.RequiredStatusChecks.IncludeAdmins);
+ Assert.False(protection.RequiredStatusChecks.Strict);
+ Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
+
+ Assert.Null(protection.Restrictions);
+ }
+
+ [IntegrationTest]
+ public async Task UpdatesBranchProtectionForOrgRepo()
+ {
+ var repoOwner = _orgRepoContext.RepositoryContext.RepositoryOwner;
+ var repoName = _orgRepoContext.RepositoryContext.RepositoryName;
+ var update = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }),
+ new BranchProtectionPushRestrictionsUpdate());
+
+ var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update);
+
+ Assert.False(protection.RequiredStatusChecks.IncludeAdmins);
+ Assert.False(protection.RequiredStatusChecks.Strict);
+ Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
+
+ Assert.Empty(protection.Restrictions.Teams);
+ Assert.Empty(protection.Restrictions.Users);
+ }
+
+ [IntegrationTest]
+ public async Task UpdatesBranchProtectionForOrgRepoWithRepositoryId()
+ {
+ var repoId = _orgRepoContext.RepositoryContext.RepositoryId;
+ var update = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }),
+ new BranchProtectionPushRestrictionsUpdate());
+
+ var protection = await _client.UpdateBranchProtection(repoId, "master", update);
+
+ Assert.False(protection.RequiredStatusChecks.IncludeAdmins);
+ Assert.False(protection.RequiredStatusChecks.Strict);
+ Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
+
+ Assert.Empty(protection.Restrictions.Teams);
+ Assert.Empty(protection.Restrictions.Users);
+ }
+
+ public void Dispose()
+ {
+ if (_userRepoContext != null)
+ _userRepoContext.Dispose();
+
+ if (_orgRepoContext != null)
+ _orgRepoContext.Dispose();
+ }
+ }
+
+ public class TheDeleteBranchProtectionMethod
+ {
+ IGitHubClient _github;
+ IRepositoryBranchesClient _client;
+
+ public TheDeleteBranchProtectionMethod()
+ {
+ _github = Helper.GetAuthenticatedClient();
+ _client = _github.Repository.Branch;
+ }
+
+ [IntegrationTest]
+ public async Task DeletesBranchProtection()
+ {
+ using (var context = await _github.CreateRepositoryWithProtectedBranch())
+ {
+ var repoOwner = context.RepositoryOwner;
+ var repoName = context.RepositoryName;
+ var deleted = await _client.DeleteBranchProtection(repoOwner, repoName, "master");
+
+ Assert.True(deleted);
+ }
+ }
+
+ [IntegrationTest]
+ public async Task DeletesBranchProtectionWithRepositoryId()
+ {
+ using (var context = await _github.CreateRepositoryWithProtectedBranch())
+ {
+ var repoId = context.RepositoryId;
+ var deleted = await _client.DeleteBranchProtection(repoId, "master");
+
+ Assert.True(deleted);
+ }
+ }
+
+ [IntegrationTest]
+ public async Task DeletesBranchProtectionForOrgRepo()
+ {
+ using (var context = await _github.CreateOrganizationRepositoryWithProtectedBranch())
+ {
+ var repoOwner = context.RepositoryContext.RepositoryOwner;
+ var repoName = context.RepositoryContext.RepositoryName;
+ var deleted = await _client.DeleteBranchProtection(repoOwner, repoName, "master");
+
+ Assert.True(deleted);
+ }
+ }
+
+ [IntegrationTest]
+ public async Task DeletesBranchProtectionForOrgRepoWithRepositoryId()
+ {
+ using (var context = await _github.CreateOrganizationRepositoryWithProtectedBranch())
+ {
+ var repoId = context.RepositoryContext.RepositoryId;
+ var deleted = await _client.DeleteBranchProtection(repoId, "master");
+
+ Assert.True(deleted);
+ }
+ }
+ }
}
diff --git a/Octokit.Tests.Integration/Helpers/OrganizationRepositoryWithTeamContext.cs b/Octokit.Tests.Integration/Helpers/OrganizationRepositoryWithTeamContext.cs
new file mode 100644
index 0000000000..6760060679
--- /dev/null
+++ b/Octokit.Tests.Integration/Helpers/OrganizationRepositoryWithTeamContext.cs
@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Octokit.Tests.Integration.Helpers
+{
+ internal class OrganizationRepositoryWithTeamContext : IDisposable
+ {
+ internal RepositoryContext RepositoryContext { get; set; }
+ internal TeamContext TeamContext { get; set; }
+
+ public void Dispose()
+ {
+ if (RepositoryContext != null)
+ RepositoryContext.Dispose();
+
+ if (TeamContext != null)
+ TeamContext.Dispose();
+ }
+ }
+
+ internal static class RepositoryProtectedBranchHelperExtensions
+ {
+ internal async static Task CreateRepositoryWithProtectedBranch(this IGitHubClient client)
+ {
+ // Create user owned repo
+ var userRepo = new NewRepository(Helper.MakeNameWithTimestamp("protected-repo")) { AutoInit = true };
+ var contextUserRepo = await client.CreateRepositoryContext(userRepo);
+
+ // Protect master branch
+ var update = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "build", "test" }));
+
+ await client.Repository.Branch.UpdateBranchProtection(contextUserRepo.RepositoryOwner, contextUserRepo.RepositoryName, "master", update);
+
+ return contextUserRepo;
+ }
+
+ internal async static Task CreateOrganizationRepositoryWithProtectedBranch(this IGitHubClient client)
+ {
+ // Create organization owned repo
+ var orgRepo = new NewRepository(Helper.MakeNameWithTimestamp("protected-org-repo")) { AutoInit = true };
+ var contextOrgRepo = await client.CreateRepositoryContext(Helper.Organization, orgRepo);
+
+ // Create team in org
+ var contextOrgTeam = await client.CreateTeamContext(Helper.Organization, new NewTeam(Helper.MakeNameWithTimestamp("team")));
+
+ // Grant team push access to repo
+ await client.Organization.Team.AddRepository(
+ contextOrgTeam.TeamId,
+ contextOrgRepo.RepositoryOwner,
+ contextOrgRepo.RepositoryName,
+ new RepositoryPermissionRequest(Permission.Push));
+
+ // Protect master branch
+ var protection = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "build", "test" }),
+ new BranchProtectionPushRestrictionsUpdate(new BranchProtectionTeamCollection { contextOrgTeam.TeamName }));
+ await client.Repository.Branch.UpdateBranchProtection(contextOrgRepo.RepositoryOwner, contextOrgRepo.RepositoryName, "master", protection);
+
+ return new OrganizationRepositoryWithTeamContext
+ {
+ RepositoryContext = contextOrgRepo,
+ TeamContext = contextOrgTeam
+ };
+ }
+ }
+}
diff --git a/Octokit.Tests.Integration/Helpers/RepositoryContext.cs b/Octokit.Tests.Integration/Helpers/RepositoryContext.cs
index 509cba7a3a..ca0b9f37ad 100644
--- a/Octokit.Tests.Integration/Helpers/RepositoryContext.cs
+++ b/Octokit.Tests.Integration/Helpers/RepositoryContext.cs
@@ -13,11 +13,13 @@ internal RepositoryContext(IConnection connection, Repository repo)
{
_connection = connection;
Repository = repo;
+ RepositoryId = repo.Id;
RepositoryOwner = repo.Owner.Login;
RepositoryName = repo.Name;
}
private IConnection _connection;
+ internal int RepositoryId { get; private set; }
internal string RepositoryOwner { get; private set; }
internal string RepositoryName { get; private set; }
diff --git a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
index 4c8ae8b5f5..f0176627ab 100644
--- a/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
+++ b/Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
@@ -137,6 +137,7 @@
+
diff --git a/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs b/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs
index f9164c4b34..6a1fea9652 100644
--- a/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs
+++ b/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs
@@ -201,5 +201,154 @@ public async Task EnsuresNonNullArguments()
await Assert.ThrowsAsync(() => client.Edit(1, "", update));
}
}
+
+ public class TheGetBranchProtectectionMethod
+ {
+ [Fact]
+ public void RequestsTheCorrectUrl()
+ {
+ var connection = Substitute.For();
+ var client = new RepositoryBranchesClient(connection);
+ const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";
+
+ client.GetBranchProtection("owner", "repo", "branch");
+
+ connection.Received()
+ .Get(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection"), null, previewAcceptsHeader);
+ }
+
+ [Fact]
+ public void RequestsTheCorrectUrlWithRepositoryId()
+ {
+ var connection = Substitute.For();
+ var client = new RepositoryBranchesClient(connection);
+ const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";
+
+ client.GetBranchProtection(1, "branch");
+
+ connection.Received()
+ .Get(Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection"), null, previewAcceptsHeader);
+ }
+
+ [Fact]
+ public async Task EnsuresNonNullArguments()
+ {
+ var client = new RepositoryBranchesClient(Substitute.For());
+
+ await Assert.ThrowsAsync(() => client.GetBranchProtection(null, "repo", "branch"));
+ await Assert.ThrowsAsync(() => client.GetBranchProtection("owner", null, "branch"));
+ await Assert.ThrowsAsync(() => client.GetBranchProtection("owner", "repo", null));
+
+ await Assert.ThrowsAsync(() => client.GetBranchProtection(1, null));
+
+ await Assert.ThrowsAsync(() => client.GetBranchProtection("", "repo", "branch"));
+ await Assert.ThrowsAsync(() => client.GetBranchProtection("owner", "", "branch"));
+ await Assert.ThrowsAsync(() => client.GetBranchProtection("owner", "repo", ""));
+
+ await Assert.ThrowsAsync(() => client.GetBranchProtection(1, ""));
+ }
+ }
+
+ public class TheUpdateBranchProtectionMethod
+ {
+ [Fact]
+ public void RequestsTheCorrectUrl()
+ {
+ var connection = Substitute.For();
+ var client = new RepositoryBranchesClient(connection);
+ var update = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
+ const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";
+
+ client.UpdateBranchProtection("owner", "repo", "branch", update);
+
+ connection.Received()
+ .Put(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection"), Arg.Any(), null, previewAcceptsHeader);
+ }
+
+ [Fact]
+ public void RequestsTheCorrectUrlWithRepositoryId()
+ {
+ var connection = Substitute.For();
+ var client = new RepositoryBranchesClient(connection);
+ var update = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
+ const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";
+
+ client.UpdateBranchProtection(1, "branch", update);
+
+ connection.Received()
+ .Put(Arg.Is(u => u.ToString() == "repositories/1/branches/branch/protection"), Arg.Any(), null, previewAcceptsHeader);
+ }
+
+ [Fact]
+ public async Task EnsuresNonNullArguments()
+ {
+ var client = new RepositoryBranchesClient(Substitute.For());
+ var update = new BranchProtectionSettingsUpdate(
+ new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
+
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection(null, "repo", "branch", update));
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection("owner", null, "branch", update));
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection("owner", "repo", null, update));
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection("owner", "repo", "branch", null));
+
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection(1, null, update));
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection(1, "branch", null));
+
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection("", "repo", "branch", update));
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection("owner", "", "branch", update));
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection("owner", "repo", "", update));
+
+ await Assert.ThrowsAsync(() => client.UpdateBranchProtection(1, "", update));
+ }
+ }
+
+ public class TheDeleteBranchProtectectionMethod
+ {
+ [Fact]
+ public void RequestsTheCorrectUrl()
+ {
+ var connection = Substitute.For();
+ var client = new RepositoryBranchesClient(connection);
+ const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";
+
+ client.DeleteBranchProtection("owner", "repo", "branch");
+
+ connection.Connection.Received()
+ .Delete(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/protection"), Arg.Any