Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions Octokit.Tests.Integration/Clients/RepositoryBranchesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,23 +197,21 @@ 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");
var protection = await _client.GetBranchProtection(repoOwner, repoName, "main");

Assert.True(protection.RequiredStatusChecks.Strict);
Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
Expand All @@ -225,13 +223,18 @@ public async Task GetsBranchProtection()
Assert.Null(protection.Restrictions);

Assert.True(protection.EnforceAdmins.Enabled);
Assert.True(protection.RequiredLinearHistory.Enabled);
Assert.True(protection.AllowForcePushes.Enabled);
Assert.True(protection.AllowDeletions.Enabled);
Assert.False(protection.BlockCreations.Enabled);
Assert.True(protection.RequiredConversationResolution.Enabled);
}

[IntegrationTest]
public async Task GetsBranchProtectionWithRepositoryId()
{
var repoId = _userRepoContext.RepositoryId;
var protection = await _client.GetBranchProtection(repoId, "master");
var protection = await _client.GetBranchProtection(repoId, "main");

Assert.True(protection.RequiredStatusChecks.Strict);
Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
Expand All @@ -243,14 +246,39 @@ public async Task GetsBranchProtectionWithRepositoryId()
Assert.Null(protection.Restrictions);

Assert.True(protection.EnforceAdmins.Enabled);
Assert.True(protection.RequiredLinearHistory.Enabled);
Assert.True(protection.AllowForcePushes.Enabled);
Assert.True(protection.AllowDeletions.Enabled);
Assert.False(protection.BlockCreations.Enabled);
Assert.True(protection.RequiredConversationResolution.Enabled);
}

[IntegrationTest]
public void Dispose()
{
if (_userRepoContext != null)
_userRepoContext.Dispose();
}
}

public class TheGetBranchOrgProtectionMethod : IDisposable
{
IRepositoryBranchesClient _client;
OrganizationRepositoryWithTeamContext _orgRepoContext;

public TheGetBranchOrgProtectionMethod()
{
var github = Helper.GetAuthenticatedClient();
_client = github.Repository.Branch;

_orgRepoContext = github.CreateOrganizationRepositoryWithProtectedBranch().Result;
}

[OrganizationTest]
public async Task GetsBranchProtectionForOrgRepo()
{
var repoOwner = _orgRepoContext.RepositoryContext.RepositoryOwner;
var repoName = _orgRepoContext.RepositoryContext.RepositoryName;
var protection = await _client.GetBranchProtection(repoOwner, repoName, "master");
var protection = await _client.GetBranchProtection(repoOwner, repoName, "main");

Assert.True(protection.RequiredStatusChecks.Strict);
Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
Expand All @@ -266,11 +294,11 @@ public async Task GetsBranchProtectionForOrgRepo()
Assert.True(protection.EnforceAdmins.Enabled);
}

[IntegrationTest]
[OrganizationTest]
public async Task GetsBranchProtectionForOrgRepoWithRepositoryId()
{
var repoId = _orgRepoContext.RepositoryContext.RepositoryId;
var protection = await _client.GetBranchProtection(repoId, "master");
var protection = await _client.GetBranchProtection(repoId, "main");

Assert.True(protection.RequiredStatusChecks.Strict);
Assert.Equal(2, protection.RequiredStatusChecks.Contexts.Count);
Expand All @@ -288,9 +316,6 @@ public async Task GetsBranchProtectionForOrgRepoWithRepositoryId()

public void Dispose()
{
if (_userRepoContext != null)
_userRepoContext.Dispose();

if (_orgRepoContext != null)
_orgRepoContext.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ internal async static Task<RepositoryContext> CreateRepositoryWithProtectedBranc
new BranchProtectionRequiredStatusChecksUpdate(true, new[] { "build", "test" }),
new BranchProtectionRequiredReviewsUpdate(true, true, 3),
null,
true,
true,
true,
true,
false,
true);

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

return contextUserRepo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,13 @@ public class TheUpdateBranchProtectionMethod : IDisposable
{
IObservableRepositoryBranchesClient _client;
RepositoryContext _userRepoContext;
OrganizationRepositoryWithTeamContext _orgRepoContext;

public TheUpdateBranchProtectionMethod()
{
var github = Helper.GetAuthenticatedClient();
_client = new ObservableRepositoryBranchesClient(github);

_userRepoContext = github.CreateRepositoryWithProtectedBranch().Result;
_orgRepoContext = github.CreateOrganizationRepositoryWithProtectedBranch().Result;
}

[IntegrationTest]
Expand All @@ -142,7 +140,7 @@ public async Task UpdatesBranchProtection()
new BranchProtectionRequiredReviewsUpdate(false, true, 2),
false);

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

Assert.False(protection.RequiredStatusChecks.Strict);
Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
Expand All @@ -166,7 +164,7 @@ public async Task UpdatesBranchProtectionWithRepositoryId()
new BranchProtectionRequiredReviewsUpdate(false, true, 2),
false);

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

Assert.False(protection.RequiredStatusChecks.Strict);
Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
Expand All @@ -181,7 +179,27 @@ public async Task UpdatesBranchProtectionWithRepositoryId()
Assert.False(protection.EnforceAdmins.Enabled);
}

[IntegrationTest]
public void Dispose()
{
if (_userRepoContext != null)
_userRepoContext.Dispose();
}
}

public class TheUpdateOrgBranchProtectionMethod : IDisposable
{
IObservableRepositoryBranchesClient _client;
OrganizationRepositoryWithTeamContext _orgRepoContext;

public TheUpdateOrgBranchProtectionMethod()
{
var github = Helper.GetAuthenticatedClient();
_client = new ObservableRepositoryBranchesClient(github);

_orgRepoContext = github.CreateOrganizationRepositoryWithProtectedBranch().Result;
}

[OrganizationTest]
public async Task UpdatesBranchProtectionForOrgRepo()
{
var repoOwner = _orgRepoContext.RepositoryContext.RepositoryOwner;
Expand All @@ -190,9 +208,14 @@ public async Task UpdatesBranchProtectionForOrgRepo()
new BranchProtectionRequiredStatusChecksUpdate(false, new[] { "new" }),
new BranchProtectionRequiredReviewsUpdate(new BranchProtectionRequiredReviewsDismissalRestrictionsUpdate(false), false, false, 2),
new BranchProtectionPushRestrictionsUpdate(),
false);
false,
true,
true,
true,
true,
true);

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

Assert.False(protection.RequiredStatusChecks.Strict);
Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
Expand All @@ -206,19 +229,29 @@ public async Task UpdatesBranchProtectionForOrgRepo()
Assert.Empty(protection.Restrictions.Users);

Assert.False(protection.EnforceAdmins.Enabled);
Assert.True(protection.RequiredLinearHistory.Enabled);
Assert.True(protection.AllowForcePushes.Enabled);
Assert.True(protection.AllowDeletions.Enabled);
Assert.True(protection.BlockCreations.Enabled);
Assert.True(protection.RequiredConversationResolution.Enabled);
}

[IntegrationTest]
[OrganizationTest]
public async Task UpdatesBranchProtectionForOrgRepoWithRepositoryId()
{
var repoId = _orgRepoContext.RepositoryContext.RepositoryId;
var update = new BranchProtectionSettingsUpdate(
new BranchProtectionRequiredStatusChecksUpdate(false, new[] { "new" }),
new BranchProtectionRequiredReviewsUpdate(new BranchProtectionRequiredReviewsDismissalRestrictionsUpdate(false), false, false, 2),
new BranchProtectionPushRestrictionsUpdate(),
false);
false,
true,
true,
true,
true,
true);

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

Assert.False(protection.RequiredStatusChecks.Strict);
Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count);
Expand All @@ -232,13 +265,15 @@ public async Task UpdatesBranchProtectionForOrgRepoWithRepositoryId()
Assert.Empty(protection.Restrictions.Users);

Assert.False(protection.EnforceAdmins.Enabled);
Assert.True(protection.RequiredLinearHistory.Enabled);
Assert.True(protection.AllowForcePushes.Enabled);
Assert.True(protection.AllowDeletions.Enabled);
Assert.True(protection.BlockCreations.Enabled);
Assert.True(protection.RequiredConversationResolution.Enabled);
}

public void Dispose()
{
if (_userRepoContext != null)
_userRepoContext.Dispose();

if (_orgRepoContext != null)
_orgRepoContext.Dispose();
}
Expand Down
64 changes: 63 additions & 1 deletion Octokit/Models/Request/BranchProtectionUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,50 @@ public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate
/// <param name="requiredPullRequestReviews">Specifies if reviews are required to merge the pull request. Pass null to disable required reviews</param>
/// <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="enforceAdmins">Specifies whether the protections applied to this branch also apply to repository admins</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks, BranchProtectionRequiredReviewsUpdate requiredPullRequestReviews, BranchProtectionPushRestrictionsUpdate restrictions, bool enforceAdmins)
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks,
BranchProtectionRequiredReviewsUpdate requiredPullRequestReviews,
BranchProtectionPushRestrictionsUpdate restrictions,
bool enforceAdmins)
{
RequiredStatusChecks = requiredStatusChecks;
RequiredPullRequestReviews = requiredPullRequestReviews;
Restrictions = restrictions;
EnforceAdmins = enforceAdmins;
}

/// <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="requiredPullRequestReviews">Specifies if reviews are required to merge the pull request. Pass null to disable required reviews</param>
/// <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="enforceAdmins">Specifies whether the protections applied to this branch also apply to repository admins</param>
/// <param name="requiredLinearHistory">Enforces a linear commit Git history</param>
/// <param name="allowForcePushes">Permits force pushes to the protected branch</param>
/// <param name="allowDeletions">Allows deletion of the protected branch</param>
/// <param name="blockCreations">The restrictions branch protection settings will also block pushes which create new branches</param>
/// <param name="requiredConversationResolution">Requires all conversations on code to be resolved before a pull request can be merged</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks,
BranchProtectionRequiredReviewsUpdate requiredPullRequestReviews,
BranchProtectionPushRestrictionsUpdate restrictions,
bool enforceAdmins,
bool requiredLinearHistory,
bool? allowForcePushes,
bool allowDeletions,
bool blockCreations,
bool requiredConversationResolution)
{
RequiredStatusChecks = requiredStatusChecks;
RequiredPullRequestReviews = requiredPullRequestReviews;
Restrictions = restrictions;
EnforceAdmins = enforceAdmins;
RequiredLinearHistory = requiredLinearHistory;
AllowForcePushes = allowForcePushes;
AllowDeletions = allowDeletions;
BlockCreations = blockCreations;
RequiredConversationResolution = requiredConversationResolution;
}

/// <summary>
/// Status check settings for the protected branch
/// </summary>
Expand All @@ -116,6 +152,32 @@ public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate
/// </summary>
public bool EnforceAdmins { get; set; }

/// <summary>
/// Enforces a linear commit Git history. Default is false.
/// </summary>
public bool RequiredLinearHistory { get; set; }

/// <summary>
/// Permits force pushes to the protected branch by anyone with write access to the repository. Default is false.
/// </summary>
public bool? AllowForcePushes { get; set; }

/// <summary>
/// Allows deletion of the protected branch by anyone with write access to the repository. Default is false.
/// </summary>
public bool AllowDeletions { get; set; }

/// <summary>
/// If set to true, the restrictions branch protection settings which limits who can push will also block pushes which create new branches, unless the push is initiated by a user, team, or app which has the ability to push. Default is false.
/// </summary>
public bool BlockCreations { get; set; }

/// <summary>
/// Requires all conversations on code to be resolved before a pull request can be merged. Default is false.
/// </summary>
public bool RequiredConversationResolution { get; set; }


internal string DebuggerDisplay
{
get
Expand Down
Loading