Skip to content

Commit

Permalink
Add BranchProtectionRequiredPullRequestReviews and `BranchProtectio…
Browse files Browse the repository at this point in the history
…nRequiredPullRequestReviewsUpdate` models
  • Loading branch information
M-Zuber committed Jan 17, 2017
1 parent 3c81893 commit 29c6b87
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public async Task UpdatesBranchProtection()
var repoOwner = _userRepoContext.RepositoryOwner;
var repoName = _userRepoContext.RepositoryName;
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }));
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update);

Expand All @@ -425,7 +425,7 @@ public async Task UpdatesBranchProtectionWithRepositoryId()
{
var repoId = _userRepoContext.RepositoryId;
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }));
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

var protection = await _client.UpdateBranchProtection(repoId, "master", update);

Expand All @@ -443,7 +443,7 @@ public async Task UpdatesBranchProtectionForOrgRepo()
var repoName = _orgRepoContext.RepositoryContext.RepositoryName;
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }),
new BranchProtectionPushRestrictionsUpdate());
new BranchProtectionPushRestrictionsUpdate(), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update);

Expand All @@ -461,7 +461,7 @@ public async Task UpdatesBranchProtectionForOrgRepoWithRepositoryId()
var repoId = _orgRepoContext.RepositoryContext.RepositoryId;
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }),
new BranchProtectionPushRestrictionsUpdate());
new BranchProtectionPushRestrictionsUpdate(), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

var protection = await _client.UpdateBranchProtection(repoId, "master", update);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal async static Task<RepositoryContext> CreateRepositoryWithProtectedBranc

// Protect master branch
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "build", "test" }));
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "build", "test" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

await client.Repository.Branch.UpdateBranchProtection(contextUserRepo.RepositoryOwner, contextUserRepo.RepositoryName, "master", update);

Expand All @@ -57,7 +57,7 @@ await client.Organization.Team.AddRepository(
// Protect master branch
var protection = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "build", "test" }),
new BranchProtectionPushRestrictionsUpdate(new BranchProtectionTeamCollection { contextOrgTeam.TeamName }));
new BranchProtectionPushRestrictionsUpdate(new BranchProtectionTeamCollection { contextOrgTeam.TeamName }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));
await client.Repository.Branch.UpdateBranchProtection(contextOrgRepo.RepositoryOwner, contextOrgRepo.RepositoryName, "master", protection);

return new OrganizationRepositoryWithTeamContext
Expand Down
6 changes: 3 additions & 3 deletions Octokit.Tests/Clients/RepositoryBranchesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public void RequestsTheCorrectUrl()
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryBranchesClient(connection);
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));
const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";

client.UpdateBranchProtection("owner", "repo", "branch", update);
Expand All @@ -272,7 +272,7 @@ public void RequestsTheCorrectUrlWithRepositoryId()
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryBranchesClient(connection);
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));
const string previewAcceptsHeader = "application/vnd.github.loki-preview+json";

client.UpdateBranchProtection(1, "branch", update);
Expand All @@ -286,7 +286,7 @@ public async Task EnsuresNonNullArguments()
{
var client = new RepositoryBranchesClient(Substitute.For<IApiConnection>());
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateBranchProtection(null, "repo", "branch", update));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.UpdateBranchProtection("owner", null, "branch", update));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public void RequestsTheCorrectUrl()
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableRepositoryBranchesClient(gitHubClient);
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

client.UpdateBranchProtection("owner", "repo", "branch", update);

Expand All @@ -264,7 +264,7 @@ public void RequestsTheCorrectUrlWithRepositoryId()
var gitHubClient = Substitute.For<IGitHubClient>();
var client = new ObservableRepositoryBranchesClient(gitHubClient);
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

client.UpdateBranchProtection(1, "branch", update);

Expand All @@ -277,7 +277,7 @@ public async Task EnsuresNonNullArguments()
{
var client = new ObservableRepositoryBranchesClient(Substitute.For<IGitHubClient>());
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }));
new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" }), new BranchProtectionRequiredPullRequestReviewsUpdate(false));

Assert.Throws<ArgumentNullException>(() => client.UpdateBranchProtection(null, "repo", "branch", update));
Assert.Throws<ArgumentNullException>(() => client.UpdateBranchProtection("owner", null, "branch", update));
Expand Down
60 changes: 56 additions & 4 deletions Octokit/Models/Request/BranchProtectionUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,37 @@ public class BranchProtectionSettingsUpdate
/// Create a BranchProtection update request
/// </summary>
/// <param name="requiredStatusChecks">Specifies the requested status check settings. Pass null to disable status checks</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks)
/// <param name="requiredPullRequestReviews">Specifies whether repository administrators need to pass pull requests checks before being able to merge a pull request</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks, BranchProtectionRequiredPullRequestReviewsUpdate requiredPullRequestReviews)
{
RequiredStatusChecks = requiredStatusChecks;
RequiredPullRequestReviews = requiredPullRequestReviews;
Restrictions = null;
}

/// <summary>
/// Create a BranchProtection update request
/// </summary>
/// <param name="restrictions">Specifies the requested push access restrictions (applies only to Organization owned repositories). Pass null to disable push access restrictions</param>
/// <param name="requiredPullRequestReviews">Specifies whether repository administrators need to pass pull requests checks before being able to merge a pull request</param>
public BranchProtectionSettingsUpdate(BranchProtectionPushRestrictionsUpdate restrictions, BranchProtectionRequiredPullRequestReviewsUpdate requiredPullRequestReviews)
{
RequiredStatusChecks = null;
RequiredPullRequestReviews = requiredPullRequestReviews;
Restrictions = restrictions;
}

/// <summary>
/// Create a BranchProtection update request
/// </summary>
/// <param name="requiredStatusChecks">Specifies the requested status check settings. Pass null to disable status checks</param>
/// <param name="restrictions">Specifies the requested push access restrictions (applies only to Organization owned repositories). Pass null to disable push access restrictions</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks, BranchProtectionPushRestrictionsUpdate restrictions)
/// <param name="requiredPullRequestReviews">Specifies whether repository administrators need to pass pull request reviews before being able to merge a pull request</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks, BranchProtectionPushRestrictionsUpdate restrictions, BranchProtectionRequiredPullRequestReviewsUpdate requiredPullRequestReviews)
{
RequiredStatusChecks = requiredStatusChecks;
Restrictions = restrictions;
RequiredPullRequestReviews = requiredPullRequestReviews;
}

/// <summary>
Expand All @@ -43,6 +59,11 @@ public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate
[SerializeNull]
public BranchProtectionRequiredStatusChecksUpdate RequiredStatusChecks { get; protected set; }

/// <summary>
/// Pull Request review settings for the protected branch
/// </summary>
public BranchProtectionRequiredPullRequestReviewsUpdate RequiredPullRequestReviews { get; protected set; }

/// <summary>
/// Push access restrictions for the protected branch
/// </summary>
Expand All @@ -54,9 +75,10 @@ internal string DebuggerDisplay
get
{
return string.Format(CultureInfo.InvariantCulture,
"StatusChecks: {0} Restrictions: {1}",
"StatusChecks: {0} Restrictions: {1} PullRequestReviews: {2}",
RequiredStatusChecks == null ? "disabled" : RequiredStatusChecks.DebuggerDisplay,
Restrictions == null ? "disabled" : Restrictions.DebuggerDisplay);
Restrictions == null ? "disabled" : Restrictions.DebuggerDisplay,
RequiredPullRequestReviews.DebuggerDisplay);
}
}
}
Expand Down Expand Up @@ -214,4 +236,34 @@ internal string DebuggerDisplay
}
}
}

/// <summary>
/// Specifies whether repository administrators need to pass pull request reviews before being able to merge a pull request
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class BranchProtectionRequiredPullRequestReviewsUpdate
{
/// <summary>
/// Sets whether repository administrators are allowed to merge a pull request without going through the review status checks
/// </summary>
/// <param name="includeAdmins">Enforce pull request reviews for repository administrators</param>
public BranchProtectionRequiredPullRequestReviewsUpdate(bool includeAdmins)
{
IncludeAdmins = includeAdmins;
}

/// <summary>
/// Enforce pull request reviews for repository administrators
/// </summary>
public bool IncludeAdmins { get; protected set; }

internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "IncludeAdmins: {0}", IncludeAdmins);
}
}

}
}
42 changes: 38 additions & 4 deletions Octokit/Models/Response/BranchProtection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ public class BranchProtectionSettings
{
public BranchProtectionSettings() { }

public BranchProtectionSettings(BranchProtectionRequiredStatusChecks requiredStatusChecks, BranchProtectionPushRestrictions restrictions)
public BranchProtectionSettings(BranchProtectionRequiredStatusChecks requiredStatusChecks, BranchProtectionPushRestrictions restrictions, BranchProtectionRequiredPullRequestReviews requiredPullRequestReviews)
{
RequiredStatusChecks = requiredStatusChecks;
Restrictions = restrictions;
RequiredPullRequestReviews = requiredPullRequestReviews;
}

/// <summary>
Expand All @@ -124,14 +125,20 @@ public BranchProtectionSettings(BranchProtectionRequiredStatusChecks requiredSta
/// </summary>
public BranchProtectionPushRestrictions Restrictions { get; protected set; }

/// <summary>
/// Pull Request Review required settings for the protected branch
/// </summary>
public BranchProtectionRequiredPullRequestReviews RequiredPullRequestReviews { get; protected set; }

internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"StatusChecks: {0} Restrictions: {1}",
RequiredStatusChecks == null ? "disabled" : RequiredStatusChecks.DebuggerDisplay,
Restrictions == null ? "disabled" : Restrictions.DebuggerDisplay);
"StatusChecks: {0} Restrictions: {1} PullRequestReviews: {2}",
RequiredStatusChecks == null ? "disabled" : RequiredStatusChecks.DebuggerDisplay,
Restrictions == null ? "disabled" : Restrictions.DebuggerDisplay,
RequiredPullRequestReviews.DebuggerDisplay);
}
}
}
Expand Down Expand Up @@ -214,4 +221,31 @@ internal string DebuggerDisplay
}
}
}

/// <summary>
/// Specifies if pull request reviews are required before merging a pull request made by a repository administrator.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class BranchProtectionRequiredPullRequestReviews
{
public BranchProtectionRequiredPullRequestReviews() { }

public BranchProtectionRequiredPullRequestReviews(bool includeAdmins)
{
IncludeAdmins = includeAdmins;
}

/// <summary>
/// Enforce pull request reviews for repository administrators
/// </summary>
public bool IncludeAdmins { get; protected set; }

internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "IncludeAdmins: {0}", IncludeAdmins);
}
}
}
}
8 changes: 8 additions & 0 deletions Octokit/Octokit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@
<Reference Include="System.Net" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.1.0.3179, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\SolutionInfo.cs">
Expand Down
4 changes: 3 additions & 1 deletion Octokit/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SimpleJson" version="0.38.0" targetFramework="net45" developmentDependency="true" />
</packages>
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
</packages>

0 comments on commit 29c6b87

Please sign in to comment.