From 3bd7323044c31cf52403b296ec9f2687ad335f42 Mon Sep 17 00:00:00 2001 From: Ryan Gribble Date: Mon, 8 Aug 2016 23:25:52 +1000 Subject: [PATCH] Add comments, move request classes to new file --- .../Clients/RepositoryBranchesClientTests.cs | 12 +- Octokit/Clients/IRepositoryBranchesClient.cs | 30 +++++ Octokit/Clients/RepositoryBranchesClient.cs | 57 +++++++- .../Models/Request/BranchProtectionUpdate.cs | 122 ++++++++++++++++++ Octokit/Models/Response/BranchProtection.cs | 76 ++++------- Octokit/Octokit-Mono.csproj | 1 + Octokit/Octokit-MonoAndroid.csproj | 1 + Octokit/Octokit-Monotouch.csproj | 1 + Octokit/Octokit-Portable.csproj | 1 + Octokit/Octokit-netcore45.csproj | 1 + Octokit/Octokit.csproj | 1 + 11 files changed, 247 insertions(+), 56 deletions(-) create mode 100644 Octokit/Models/Request/BranchProtectionUpdate.cs diff --git a/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs b/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs index 6a370ebf..8d1192f7 100644 --- a/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs @@ -256,7 +256,9 @@ namespace Octokit.Tests.Clients { var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); - var update = new BranchProtectionSettingsUpdate(); + var update = new BranchProtectionSettingsUpdate( + new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), + new ProtectedBranchRestrictionsUpdate(null, null)); const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.UpdateBranchProtection("owner", "repo", "branch", update); @@ -270,7 +272,9 @@ namespace Octokit.Tests.Clients { var connection = Substitute.For(); var client = new RepositoryBranchesClient(connection); - var update = new BranchProtectionSettingsUpdate(); + var update = new BranchProtectionSettingsUpdate( + new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), + new ProtectedBranchRestrictionsUpdate(null, null)); const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.UpdateBranchProtection(1, "branch", update); @@ -283,7 +287,9 @@ namespace Octokit.Tests.Clients public async Task EnsuresNonNullArguments() { var client = new RepositoryBranchesClient(Substitute.For()); - var update = new BranchProtectionSettingsUpdate(); + var update = new BranchProtectionSettingsUpdate( + new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), + new ProtectedBranchRestrictionsUpdate(null, null)); await Assert.ThrowsAsync(() => client.UpdateBranchProtection(null, "repo", "branch", update)); await Assert.ThrowsAsync(() => client.UpdateBranchProtection("owner", null, "branch", update)); diff --git a/Octokit/Clients/IRepositoryBranchesClient.cs b/Octokit/Clients/IRepositoryBranchesClient.cs index 9f4f73b0..5ec0111d 100644 --- a/Octokit/Clients/IRepositoryBranchesClient.cs +++ b/Octokit/Clients/IRepositoryBranchesClient.cs @@ -109,6 +109,16 @@ namespace Octokit /// The name of the branch Task 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 + Task GetBranchProtection(int repositoryId, string branch); + /// /// Update the branch protection settings for the specified branch /> /// @@ -121,6 +131,16 @@ namespace Octokit /// Branch protection settings Task 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 + Task UpdateBranchProtection(int repositoryId, string branch, BranchProtectionSettingsUpdate update); + /// /// Remove the branch protection settings for the specified branch /> /// @@ -131,5 +151,15 @@ namespace Octokit /// The name of the repository /// The name of the branch Task 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 + Task DeleteBranchProtection(int repositoryId, string branch); } } diff --git a/Octokit/Clients/RepositoryBranchesClient.cs b/Octokit/Clients/RepositoryBranchesClient.cs index a2d6faef..9ce6ed9a 100644 --- a/Octokit/Clients/RepositoryBranchesClient.cs +++ b/Octokit/Clients/RepositoryBranchesClient.cs @@ -1,9 +1,6 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; using System.Net; -using System.Text; using System.Threading.Tasks; namespace Octokit @@ -153,6 +150,15 @@ namespace Octokit return ApiConnection.Patch(ApiUrls.RepoBranch(repositoryId, branch), update, AcceptHeaders.ProtectedBranchesApiPreview); } + /// + /// 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 Task GetBranchProtection(string owner, string name, string branch) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); @@ -162,6 +168,14 @@ namespace Octokit return ApiConnection.Get(ApiUrls.RepoBranchProtection(owner, name, branch), null, AcceptHeaders.ProtectedBranchesApiPreview); } + /// + /// 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 Task GetBranchProtection(int repositoryId, string branch) { Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); @@ -169,16 +183,34 @@ namespace Octokit return ApiConnection.Get(ApiUrls.RepoBranchProtection(repositoryId, branch), null, AcceptHeaders.ProtectedBranchesApiPreview); } + /// + /// 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 Task 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 ApiConnection.Put(ApiUrls.RepoBranchProtection(owner, name, branch), update, AcceptHeaders.ProtectedBranchesApiPreview); } + /// + /// 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 Task UpdateBranchProtection(int repositoryId, string branch, BranchProtectionSettingsUpdate update) { Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); @@ -187,6 +219,15 @@ namespace Octokit return ApiConnection.Put(ApiUrls.RepoBranchProtection(repositoryId, branch), update, AcceptHeaders.ProtectedBranchesApiPreview); } + /// + /// 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 async Task DeleteBranchProtection(string owner, string name, string branch) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); @@ -205,6 +246,14 @@ namespace Octokit } } + /// + /// 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 async Task DeleteBranchProtection(int repositoryId, string branch) { Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); diff --git a/Octokit/Models/Request/BranchProtectionUpdate.cs b/Octokit/Models/Request/BranchProtectionUpdate.cs new file mode 100644 index 00000000..fc198474 --- /dev/null +++ b/Octokit/Models/Request/BranchProtectionUpdate.cs @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; + +namespace Octokit +{ + /// + /// Specifies the requested settings for branch protection + /// + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class BranchProtectionSettingsUpdate + { + /// + /// Create a BranchProtection update request + /// + /// Specifies the requested status check settings + /// Specifies the requested push access restrictions (applies only to Organization owned repositories) + public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks, ProtectedBranchRestrictionsUpdate restrictions) + { + RequiredStatusChecks = requiredStatusChecks; + Restrictions = restrictions; + } + + /// + /// Status check settings for protected branch + /// + public BranchProtectionRequiredStatusChecksUpdate RequiredStatusChecks { get; protected set; } + + /// + /// Push access restrictions for the protected branch + /// + public ProtectedBranchRestrictionsUpdate Restrictions { get; protected set; } + + internal string DebuggerDisplay + { + get + { + return String.Format(CultureInfo.InvariantCulture, "StatusChecks: {0} Restrictions: {1}", RequiredStatusChecks, Restrictions); + } + } + } + + /// + /// Specifies the requested status check settings for branch protection + /// + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class BranchProtectionRequiredStatusChecksUpdate + { + /// + /// Status check settings for branch protection + /// + /// Enforce required status checks for repository administrators + /// Require branches to be up to date before merging + /// Require status checks to pass before merging + public BranchProtectionRequiredStatusChecksUpdate(bool includeAdmins, bool strict, IReadOnlyList contexts) + { + IncludeAdmins = includeAdmins; + Strict = strict; + Contexts = contexts; + } + + /// + /// Enforce required status checks for repository administrators + /// + public bool IncludeAdmins { get; protected set; } + + /// + /// Require branches to be up to date before merging + /// + public bool Strict { get; protected set; } + + /// + /// Require status checks to pass before merging + /// + public IReadOnlyList Contexts { get; private set; } + + internal string DebuggerDisplay + { + get + { + return String.Format(CultureInfo.InvariantCulture, "IncludeAdmins: {0} Strict: {1} Contexts: {2}", IncludeAdmins, Strict, String.Join(",", Contexts)); + } + } + } + + /// + /// Specifies people or teams allowed to push to this branch. Required status checks will still prevent these people from merging if the checks fail. + /// + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class ProtectedBranchRestrictionsUpdate + { + /// + /// Specify people or teams allowed to push to this branch. Required status checks will still prevent these people from merging if the checks fail. + /// + /// Teams allowed to push to this branch + /// Users allowed to push to this branch + public ProtectedBranchRestrictionsUpdate(IReadOnlyList teams, IReadOnlyList users) + { + Teams = teams; + Users = users; + } + + /// + /// Teams allowed to push to this branch + /// + public IReadOnlyList Teams { get; private set; } + + /// + /// Users allowed to push to this branch + /// + public IReadOnlyList Users { get; private set; } + + internal string DebuggerDisplay + { + get + { + return String.Format(CultureInfo.InvariantCulture, "Teams: {0} Users: {1}", String.Join(",", Teams), String.Join(",", Users)); + } + } + } +} diff --git a/Octokit/Models/Response/BranchProtection.cs b/Octokit/Models/Response/BranchProtection.cs index ed71ad6a..3968ec1e 100644 --- a/Octokit/Models/Response/BranchProtection.cs +++ b/Octokit/Models/Response/BranchProtection.cs @@ -95,6 +95,10 @@ namespace Octokit Everyone } + /// + /// Protection details for a . + /// Note: this is a PREVIEW api: https://developer.github.com/changes/2016-06-27-protected-branches-api-update/ + /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class BranchProtectionSettings { @@ -106,7 +110,14 @@ namespace Octokit Restrictions = restrictions; } + /// + /// Status check settings for the protected branch + /// public BranchProtectionRequiredStatusChecks RequiredStatusChecks { get; protected set; } + + /// + /// Push access restrictions for the protected branch + /// public ProtectedBranchRestrictions Restrictions { get; protected set; } internal string DebuggerDisplay @@ -118,30 +129,6 @@ namespace Octokit } } - [DebuggerDisplay("{DebuggerDisplay,nq}")] - public class BranchProtectionSettingsUpdate - { - public BranchProtectionSettingsUpdate() { } - - public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecks requiredStatusChecks, ProtectedBranchRestrictionsUpdate restrictions) - { - RequiredStatusChecks = requiredStatusChecks; - Restrictions = restrictions; - } - - public BranchProtectionRequiredStatusChecks RequiredStatusChecks { get; protected set; } - - public ProtectedBranchRestrictionsUpdate Restrictions { get; protected set; } - - internal string DebuggerDisplay - { - get - { - return String.Format(CultureInfo.InvariantCulture, "StatusChecks: {0} Restrictions: {1}", RequiredStatusChecks, Restrictions); - } - } - } - [DebuggerDisplay("{DebuggerDisplay,nq}")] public class BranchProtectionRequiredStatusChecks { @@ -154,10 +141,19 @@ namespace Octokit Contexts = contexts; } + /// + /// Enforce required status checks for repository administrators + /// public bool IncludeAdmins { get; protected set; } + /// + /// Require branches to be up to date before merging + /// public bool Strict { get; protected set; } + /// + /// Require status checks to pass before merging + /// public IReadOnlyList Contexts { get; private set; } internal string DebuggerDisplay @@ -172,7 +168,7 @@ namespace Octokit [DebuggerDisplay("{DebuggerDisplay,nq}")] public class ProtectedBranchRestrictions { - public ProtectedBranchRestrictions() { } + protected ProtectedBranchRestrictions() { } public ProtectedBranchRestrictions(IReadOnlyList teams, IReadOnlyList users) { @@ -180,8 +176,14 @@ namespace Octokit Users = users; } + /// + /// Push access is restricted to the specified Teams + /// public IReadOnlyList Teams { get; private set; } + /// + /// Push access is restricted to the specified Users + /// public IReadOnlyList Users { get; private set; } internal string DebuggerDisplay @@ -192,28 +194,4 @@ namespace Octokit } } } - - [DebuggerDisplay("{DebuggerDisplay,nq}")] - public class ProtectedBranchRestrictionsUpdate - { - public ProtectedBranchRestrictionsUpdate() { } - - public ProtectedBranchRestrictionsUpdate(IReadOnlyList teams, IReadOnlyList users) - { - Teams = teams; - Users = users; - } - - public IReadOnlyList Teams { get; private set; } - - public IReadOnlyList Users { get; private set; } - - internal string DebuggerDisplay - { - get - { - return String.Format(CultureInfo.InvariantCulture, "Teams: {0} Users: {1}", String.Join(",", Teams), String.Join(",", Users)); - } - } - } } diff --git a/Octokit/Octokit-Mono.csproj b/Octokit/Octokit-Mono.csproj index 06fe2de9..10f624f6 100644 --- a/Octokit/Octokit-Mono.csproj +++ b/Octokit/Octokit-Mono.csproj @@ -491,6 +491,7 @@ + \ No newline at end of file diff --git a/Octokit/Octokit-MonoAndroid.csproj b/Octokit/Octokit-MonoAndroid.csproj index 2f1544ef..7dc3e9b2 100644 --- a/Octokit/Octokit-MonoAndroid.csproj +++ b/Octokit/Octokit-MonoAndroid.csproj @@ -502,6 +502,7 @@ + \ No newline at end of file diff --git a/Octokit/Octokit-Monotouch.csproj b/Octokit/Octokit-Monotouch.csproj index d78c1075..cfc5f43d 100644 --- a/Octokit/Octokit-Monotouch.csproj +++ b/Octokit/Octokit-Monotouch.csproj @@ -498,6 +498,7 @@ + diff --git a/Octokit/Octokit-Portable.csproj b/Octokit/Octokit-Portable.csproj index 17048e23..04a7fbc1 100644 --- a/Octokit/Octokit-Portable.csproj +++ b/Octokit/Octokit-Portable.csproj @@ -488,6 +488,7 @@ + diff --git a/Octokit/Octokit-netcore45.csproj b/Octokit/Octokit-netcore45.csproj index 6529d0ac..1d515df1 100644 --- a/Octokit/Octokit-netcore45.csproj +++ b/Octokit/Octokit-netcore45.csproj @@ -185,6 +185,7 @@ + diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index fd8d6e44..9f5f9761 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -136,6 +136,7 @@ +