using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; namespace Octokit { /// /// 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 { public BranchProtectionSettings() { } public BranchProtectionSettings(BranchProtectionRequiredStatusChecks requiredStatusChecks, BranchProtectionPushRestrictions restrictions, BranchProtectionRequiredReviews requiredPullRequestReviews, EnforceAdmins enforceAdmins) { RequiredStatusChecks = requiredStatusChecks; Restrictions = restrictions; RequiredPullRequestReviews = requiredPullRequestReviews; EnforceAdmins = enforceAdmins; } /// /// Status check settings for the protected branch /// public BranchProtectionRequiredStatusChecks RequiredStatusChecks { get; protected set; } /// /// Required review settings for the protected branch /// public BranchProtectionRequiredReviews RequiredPullRequestReviews { get; protected set; } /// /// Push access restrictions for the protected branch /// public BranchProtectionPushRestrictions Restrictions { get; protected set; } /// /// Specifies whether the protections applied to this branch also apply to repository admins /// public EnforceAdmins EnforceAdmins { get; protected set; } internal string DebuggerDisplay { get { return string.Format(CultureInfo.InvariantCulture, "RequiredStatusChecks: {0} RequiredPullRequestReviews {1} Restrictions: {2} EnforceAdmins: {3}", RequiredStatusChecks?.DebuggerDisplay ?? "disabled", RequiredPullRequestReviews?.DebuggerDisplay ?? "disabled", Restrictions?.DebuggerDisplay ?? "disabled", EnforceAdmins?.DebuggerDisplay ?? "disabled"); } } } /// /// Specifies whether the protections applied to this branch also apply to repository admins /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class EnforceAdmins { public EnforceAdmins() { } public EnforceAdmins(bool enabled) { Enabled = enabled; } public bool Enabled { get; protected set; } internal string DebuggerDisplay { get { return string.Format(CultureInfo.InvariantCulture, "Enabled: {0}", Enabled); } } } /// /// Specifies settings for status checks which must pass before branches can be merged into the protected branch /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class BranchProtectionRequiredStatusChecks { public BranchProtectionRequiredStatusChecks() { } public BranchProtectionRequiredStatusChecks(bool strict, IReadOnlyList contexts) { Strict = strict; Contexts = contexts; } /// /// 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, "Strict: {0} Contexts: {1}", Strict, Contexts == null ? "" : string.Join(",", Contexts)); } } } /// /// Specifies people or teams allowed to push to the protected branch. Required status checks will still prevent these people from merging if the checks fail /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class BranchProtectionPushRestrictions { public BranchProtectionPushRestrictions() { } public BranchProtectionPushRestrictions(IReadOnlyList teams, IReadOnlyList users) { Teams = teams; 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 { get { return string.Format(CultureInfo.InvariantCulture, "Teams: {0} Users: {1}", Teams == null ? "" : String.Join(",", Teams.Select(x => x.Name)), Users == null ? "" : String.Join(",", Users.Select(x => x.Login))); } } } /// /// Specifies if pull request reviews are required before merging a pull request. Can optionally enforce the policy on repository administrators also. /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class BranchProtectionRequiredReviews { public BranchProtectionRequiredReviews() { } public BranchProtectionRequiredReviews(BranchProtectionRequiredReviewsDismissalRestrictions dismissalRestrictions, bool dismissStaleReviews, bool requireCodeOwnerReviews, int requiredApprovingReviewCount) { DismissalRestrictions = dismissalRestrictions; DismissStaleReviews = dismissStaleReviews; RequireCodeOwnerReviews = requireCodeOwnerReviews; RequiredApprovingReviewCount = requiredApprovingReviewCount; } /// /// Specify which users and teams can dismiss pull request reviews. /// public BranchProtectionRequiredReviewsDismissalRestrictions DismissalRestrictions { get; protected set; } /// /// Dismiss approved reviews automatically when a new commit is pushed. /// public bool DismissStaleReviews { get; protected set; } /// /// Blocks merge until code owners have reviewed. /// public bool RequireCodeOwnerReviews { get; protected set; } /// /// Specify the number of reviewers required to approve pull requests. Use a number between 1 and 6. /// public int RequiredApprovingReviewCount { get; protected set; } internal string DebuggerDisplay { get { return string.Format(CultureInfo.InvariantCulture, "DismissalRestrictions: {0} DismissStaleReviews: {1} RequireCodeOwnerReviews: {2} RequiredApprovingReviewCount: {3}", DismissalRestrictions?.DebuggerDisplay ?? "disabled", DismissStaleReviews, RequireCodeOwnerReviews, RequiredApprovingReviewCount); } } } /// /// Specifies people or teams allowed to push to the protected branch. Required status checks will still prevent these people from merging if the checks fail /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class BranchProtectionRequiredReviewsDismissalRestrictions { public BranchProtectionRequiredReviewsDismissalRestrictions() { } public BranchProtectionRequiredReviewsDismissalRestrictions(IReadOnlyList teams, IReadOnlyList users) { Teams = teams; Users = users; } /// /// The specified Teams that can dismiss reviews /// public IReadOnlyList Teams { get; private set; } /// /// The specified Users who can dismiss reviews /// public IReadOnlyList Users { get; private set; } internal string DebuggerDisplay { get { return string.Format(CultureInfo.InvariantCulture, "Teams: {0} Users: {1}", Teams == null ? "" : String.Join(",", Teams.Select(x => x.Name)), Users == null ? "" : String.Join(",", Users.Select(x => x.Login))); } } } }