Skip to content

Commit

Permalink
[feat] Adds support for making branches read-only (#2715)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefflill authored Aug 3, 2023
1 parent 729f261 commit 7fe4c4e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public async Task GetsBranchProtection()
Assert.Null(protection.Restrictions);

Assert.True(protection.EnforceAdmins.Enabled);
Assert.True(protection.LockBranch.Enabled);
Assert.True(protection.RequiredLinearHistory.Enabled);
Assert.True(protection.AllowForcePushes.Enabled);
Assert.True(protection.AllowDeletions.Enabled);
Expand Down Expand Up @@ -323,6 +324,7 @@ public async Task GetsBranchProtectionWithRepositoryId()
Assert.Null(protection.Restrictions);

Assert.True(protection.EnforceAdmins.Enabled);
Assert.True(protection.LockBranch.Enabled);
Assert.True(protection.RequiredLinearHistory.Enabled);
Assert.True(protection.AllowForcePushes.Enabled);
Assert.True(protection.AllowDeletions.Enabled);
Expand Down
31 changes: 25 additions & 6 deletions Octokit/Models/Request/BranchProtectionUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate
RequiredPullRequestReviews = null;
Restrictions = null;
EnforceAdmins = false;
LockBranch = false;
}

/// <summary>
Expand All @@ -38,6 +39,7 @@ public BranchProtectionSettingsUpdate(BranchProtectionRequiredReviewsUpdate requ
RequiredPullRequestReviews = requiredPullRequestReviews;
Restrictions = null;
EnforceAdmins = false;
LockBranch = false;
}

/// <summary>
Expand All @@ -50,18 +52,21 @@ public BranchProtectionSettingsUpdate(BranchProtectionPushRestrictionsUpdate res
RequiredPullRequestReviews = null;
Restrictions = restrictions;
EnforceAdmins = false;
LockBranch = false;
}

/// <summary>
/// Create a BranchProtection update request
/// </summary>
/// <param name="enforceAdmins">Specifies whether the protections applied to this branch also apply to repository admins</param>
public BranchProtectionSettingsUpdate(bool enforceAdmins)
/// <param name="lockBranch">Optionally specfies that the branch should be read-only.</param>
public BranchProtectionSettingsUpdate(bool enforceAdmins, bool lockBranch = false)
{
RequiredStatusChecks = null;
RequiredPullRequestReviews = null;
Restrictions = null;
EnforceAdmins = enforceAdmins;
LockBranch = lockBranch;
}

/// <summary>
Expand All @@ -70,12 +75,14 @@ public BranchProtectionSettingsUpdate(bool enforceAdmins)
/// <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="enforceAdmins">Specifies whether the protections applied to this branch also apply to repository admins</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks, BranchProtectionRequiredReviewsUpdate requiredPullRequestReviews, bool enforceAdmins)
/// <param name="lockBranch">Optionally specfies that the branch should be read-only.</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks, BranchProtectionRequiredReviewsUpdate requiredPullRequestReviews, bool enforceAdmins, bool lockBranch = false)
{
RequiredStatusChecks = requiredStatusChecks;
RequiredPullRequestReviews = requiredPullRequestReviews;
Restrictions = null;
EnforceAdmins = enforceAdmins;
LockBranch = lockBranch;
}

/// <summary>
Expand All @@ -85,15 +92,18 @@ 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>
/// <param name="lockBranch">Optionally specfies that the branch should be read-only.</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks,
BranchProtectionRequiredReviewsUpdate requiredPullRequestReviews,
BranchProtectionPushRestrictionsUpdate restrictions,
bool enforceAdmins)
bool enforceAdmins,
bool lockBranch = false)
{
RequiredStatusChecks = requiredStatusChecks;
RequiredPullRequestReviews = requiredPullRequestReviews;
Restrictions = restrictions;
EnforceAdmins = enforceAdmins;
LockBranch = lockBranch;
}

/// <summary>
Expand All @@ -108,6 +118,7 @@ public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate
/// <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>
/// <param name="lockBranch">Optionally specfies that the branch should be read-only.</param>
public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate requiredStatusChecks,
BranchProtectionRequiredReviewsUpdate requiredPullRequestReviews,
BranchProtectionPushRestrictionsUpdate restrictions,
Expand All @@ -116,12 +127,14 @@ public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate
bool? allowForcePushes,
bool allowDeletions,
bool blockCreations,
bool requiredConversationResolution)
bool requiredConversationResolution,
bool lockBranch = false)
{
RequiredStatusChecks = requiredStatusChecks;
RequiredPullRequestReviews = requiredPullRequestReviews;
Restrictions = restrictions;
EnforceAdmins = enforceAdmins;
LockBranch = lockBranch;
RequiredLinearHistory = requiredLinearHistory;
AllowForcePushes = allowForcePushes;
AllowDeletions = allowDeletions;
Expand Down Expand Up @@ -152,6 +165,11 @@ public BranchProtectionSettingsUpdate(BranchProtectionRequiredStatusChecksUpdate
/// </summary>
public bool EnforceAdmins { get; set; }

/// <summary>
/// Specifies whether this branch should be read-only.
/// </summary>
public bool LockBranch { get; set; }

/// <summary>
/// Enforces a linear commit Git history. Default is false.
/// </summary>
Expand Down Expand Up @@ -183,11 +201,12 @@ internal string DebuggerDisplay
get
{
return string.Format(CultureInfo.InvariantCulture,
"RequiredStatusChecks: {0} RequiredPullRequestReviews: {1} Restrictions: {2} EnforceAdmins: {3}",
"RequiredStatusChecks: {0} RequiredPullRequestReviews: {1} Restrictions: {2} EnforceAdmins: {3} LockBranch: {4}",
RequiredStatusChecks?.DebuggerDisplay ?? "disabled",
RequiredPullRequestReviews?.DebuggerDisplay ?? "disabled",
Restrictions?.DebuggerDisplay ?? "disabled",
EnforceAdmins);
EnforceAdmins,
LockBranch);
}
}
}
Expand Down
35 changes: 32 additions & 3 deletions Octokit/Models/Response/BranchProtection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public BranchProtectionSettings(BranchProtectionRequiredStatusChecks requiredSta
BranchProtectionEnabledCommon allowDeletions,
BranchProtectionEnabledCommon blockCreations,
BranchProtectionEnabledCommon requiredConversationResolution,
BranchProtectionEnabledCommon requiredSignatures)
BranchProtectionEnabledCommon requiredSignatures,
EnforceLock lockBranch = null)
{
RequiredStatusChecks = requiredStatusChecks;
RequiredPullRequestReviews = requiredPullRequestReviews;
Expand All @@ -35,10 +36,9 @@ public BranchProtectionSettings(BranchProtectionRequiredStatusChecks requiredSta
BlockCreations = blockCreations;
RequiredConversationResolution = requiredConversationResolution;
RequiredSignatures = requiredSignatures;
LockBranch = lockBranch != null ? lockBranch : new EnforceLock(false);
}



/// <summary>
/// Status check settings for the protected branch
/// </summary>
Expand All @@ -59,6 +59,11 @@ public BranchProtectionSettings(BranchProtectionRequiredStatusChecks requiredSta
/// </summary>
public EnforceAdmins EnforceAdmins { get; private set; }

/// <summary>
/// Indicates whether this branch is read-only.
/// </summary>
public EnforceLock LockBranch { get; private set; }

/// <summary>
/// Specifies whether a linear history is required
/// </summary>
Expand Down Expand Up @@ -127,6 +132,30 @@ internal string DebuggerDisplay
}
}

/// <summary>
/// Specifies whether the this branch also should be read-only.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class EnforceLock
{
public EnforceLock() { }

public EnforceLock(bool enabled)
{
Enabled = enabled;
}

public bool Enabled { get; private set; }

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

/// <summary>
/// Specifies settings for status checks which must pass before branches can be merged into the protected branch
/// </summary>
Expand Down
84 changes: 42 additions & 42 deletions Octokit/Octokit.csproj
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>An async-based GitHub API client library for .NET and .NET Core</Description>
<AssemblyTitle>Octokit</AssemblyTitle>
<Authors>GitHub</Authors>
<Version>0.0.0-dev</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Octokit</AssemblyName>
<PackageId>Octokit</PackageId>
<DebugType>embedded</DebugType>
<RepositoryUrl>https://github.com/octokit/octokit.net</RepositoryUrl>
<PackageProjectUrl>https://github.com/octokit/octokit.net</PackageProjectUrl>
<PackageIconUrl>https://f.cloud.github.com/assets/19977/1510987/64af2b26-4a9d-11e3-89fc-96a185171c75.png</PackageIconUrl>
<PackageIcon>octokit.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>GitHub API Octokit linqpad-samples dotnetcore</PackageTags>
<Copyright>Copyright GitHub 2017</Copyright>
</PropertyGroup>
<PropertyGroup>
<Description>An async-based GitHub API client library for .NET and .NET Core</Description>
<AssemblyTitle>Octokit</AssemblyTitle>
<Authors>GitHub</Authors>
<Version>0.0.0-dev</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Octokit</AssemblyName>
<PackageId>Octokit</PackageId>
<DebugType>embedded</DebugType>
<RepositoryUrl>https://github.com/octokit/octokit.net</RepositoryUrl>
<PackageProjectUrl>https://github.com/octokit/octokit.net</PackageProjectUrl>
<PackageIconUrl>https://f.cloud.github.com/assets/19977/1510987/64af2b26-4a9d-11e3-89fc-96a185171c75.png</PackageIconUrl>
<PackageIcon>octokit.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>GitHub API Octokit linqpad-samples dotnetcore</PackageTags>
<Copyright>Copyright GitHub 2017</Copyright>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);SIMPLE_JSON_INTERNAL;SIMPLE_JSON_OBJARRAYINTERNAL;SIMPLE_JSON_READONLY_COLLECTIONS;SIMPLE_JSON_TYPEINFO</DefineConstants>
<NoWarn>$(NoWarn);1591;1701;1702;1705</NoWarn>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);SIMPLE_JSON_INTERNAL;SIMPLE_JSON_OBJARRAYINTERNAL;SIMPLE_JSON_READONLY_COLLECTIONS;SIMPLE_JSON_TYPEINFO</DefineConstants>
<NoWarn>$(NoWarn);1591;1701;1702;1705</NoWarn>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<PropertyGroup Label="Source Link">
<!-- Optional: Declare that the Repository URL can be published to NuSpec -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Optional: Embed source files that are not tracked by the source control manager to the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- Optional: Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<PropertyGroup Label="Source Link">
<!-- Optional: Declare that the Repository URL can be published to NuSpec -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Optional: Embed source files that are not tracked by the source control manager to the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- Optional: Include PDB in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<ItemGroup>
<None Include="images\octokit.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<None Include="images\octokit.png" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Label="InternalsVisibleTo attributes">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Octokit.Tests$(StrongNameSuffix)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup Label="InternalsVisibleTo attributes">
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Octokit.Tests$(StrongNameSuffix)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>

0 comments on commit 7fe4c4e

Please sign in to comment.