From 63708d14108dee207d2cfdd0b30727a7fa70b8d7 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Fri, 20 Oct 2023 16:40:19 +0100 Subject: [PATCH] Add missing branch protection fields --- .../kohsuke/github/GHBranchProtection.java | 154 +++++++++++++++ .../github/GHBranchProtectionBuilder.java | 187 +++++++++++++++++- 2 files changed, 338 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHBranchProtection.java b/src/main/java/org/kohsuke/github/GHBranchProtection.java index 697686ce2d..70c837678f 100644 --- a/src/main/java/org/kohsuke/github/GHBranchProtection.java +++ b/src/main/java/org/kohsuke/github/GHBranchProtection.java @@ -22,9 +22,30 @@ public class GHBranchProtection extends GitHubInteractiveObject { private static final String REQUIRE_SIGNATURES_URI = "/required_signatures"; + @JsonProperty + private AllowDeletions allowDeletions; + + @JsonProperty + private AllowForcePushes allowForcePushes; + + @JsonProperty + private AllowForkSyncing allowForkSyncing; + + @JsonProperty + private BlockCreations blockCreations; + @JsonProperty private EnforceAdmins enforceAdmins; + @JsonProperty + private LockBranch lockBranch; + + @JsonProperty + private RequiredConversationResolution requiredConversationResolution; + + @JsonProperty + private RequiredLinearHistory requiredLinearHistory; + @JsonProperty("required_pull_request_reviews") private RequiredReviews requiredReviews; @@ -120,6 +141,74 @@ private Requester requester() { return root().createRequest().withPreview(ZZZAX); } + /** + * The type AllowDeletions. + */ + public static class AllowDeletions { + @JsonProperty + private boolean enabled; + + /** + * Is enabled boolean. + * + * @return the boolean + */ + public boolean isEnabled() { + return enabled; + } + } + + /** + * The type AllowForcePushes. + */ + public static class AllowForcePushes { + @JsonProperty + private boolean enabled; + + /** + * Is enabled boolean. + * + * @return the boolean + */ + public boolean isEnabled() { + return enabled; + } + } + + /** + * The type AllowForkSyncing. + */ + public static class AllowForkSyncing { + @JsonProperty + private boolean enabled; + + /** + * Is enabled boolean. + * + * @return the boolean + */ + public boolean isEnabled() { + return enabled; + } + } + + /** + * The type BlockCreations. + */ + public static class BlockCreations { + @JsonProperty + private boolean enabled; + + /** + * Is enabled boolean. + * + * @return the boolean + */ + public boolean isEnabled() { + return enabled; + } + } + /** * The type EnforceAdmins. */ @@ -149,6 +238,57 @@ public boolean isEnabled() { } } + /** + * The type LockBranch. + */ + public static class LockBranch { + @JsonProperty + private boolean enabled; + + /** + * Is enabled boolean. + * + * @return the boolean + */ + public boolean isEnabled() { + return enabled; + } + } + + /** + * The type RequiredConversationResolution. + */ + public static class RequiredConversationResolution { + @JsonProperty + private boolean enabled; + + /** + * Is enabled boolean. + * + * @return the boolean + */ + public boolean isEnabled() { + return enabled; + } + } + + /** + * The type RequiredLinearHistory. + */ + public static class RequiredLinearHistory { + @JsonProperty + private boolean enabled; + + /** + * Is enabled boolean. + * + * @return the boolean + */ + public boolean isEnabled() { + return enabled; + } + } + /** * The type RequiredReviews. */ @@ -156,10 +296,15 @@ public static class RequiredReviews { @JsonProperty("dismissal_restrictions") private Restrictions dismissalRestriction; + @JsonProperty private boolean dismissStaleReviews; + @JsonProperty private boolean requireCodeOwnerReviews; + @JsonProperty + private boolean requireLastPushApproval; + @JsonProperty("required_approving_review_count") private int requiredReviewers; @@ -202,6 +347,15 @@ public boolean isRequireCodeOwnerReviews() { return requireCodeOwnerReviews; } + /** + * Is require last push approval boolean. + * + * @return the boolean + */ + public boolean isRequireLastPushApproval() { + return requireLastPushApproval; + } + /** * Gets required reviewers. * diff --git a/src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java b/src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java index 9d22703914..70a614894a 100644 --- a/src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java +++ b/src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java @@ -27,7 +27,7 @@ public class GHBranchProtectionBuilder { private final GHBranch branch; - private boolean enforceAdmins; + private Map fields = new HashMap(); private Map prReviews; private Restrictions restrictions; private StatusChecks statusChecks; @@ -40,6 +40,7 @@ public class GHBranchProtectionBuilder { */ GHBranchProtectionBuilder(GHBranch branch) { this.branch = branch; + includeAdmins(false); } /** @@ -66,6 +67,95 @@ public GHBranchProtectionBuilder addRequiredChecks(String... checks) { return this; } + /** + * Allow deletion of the protected branch. + * + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder allowDeletions() { + return allowDeletions(true); + } + + /** + * Allows deletion of the protected branch by anyone with write access to the repository. + * Set to true to allow deletion of the protected branch. Default: false. + * + * @param v + * the v + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder allowDeletions(boolean v) { + fields.put("allow_deletions", v); + return this; + } + + /** + * Permits force pushes. + * + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder allowForcePushes() { + return allowForcePushes(true); + } + + /** + * Permits force pushes to the protected branch by anyone with write access to the repository. + * Set to true to allow force pushes. Set to false to block force pushes. Default: false. + * + * @param v + * the v + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder allowForcePushes(boolean v) { + fields.put("allow_force_pushes", v); + return this; + } + + /** + * Allow fork syncing. + * + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder allowForkSyncing() { + return allowForkSyncing(true); + } + + /** + * Whether users can pull changes from upstream when the branch is locked. Set to true to allow fork syncing. + * Set to true to enable. Default: false. + * + * @param v + * the v + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder allowForkSyncing(boolean v) { + fields.put("allow_fork_syncing", v); + return this; + } + + /** + * Restrict new branch creation. + * + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder blockCreations() { + return blockCreations(true); + } + + /** + * 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. + * Set to true to restrict new branch creation. Default: false. + * + * @param v + * the v + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder blockCreations(boolean v) { + fields.put("block_creations", v); + return this; + } + /** * Dismiss stale reviews gh branch protection builder. * @@ -96,10 +186,10 @@ public GHBranchProtectionBuilder dismissStaleReviews(boolean v) { */ public GHBranchProtection enable() throws IOException { return requester().method("PUT") + .with(fields) .withNullable("required_status_checks", statusChecks) .withNullable("required_pull_request_reviews", prReviews) .withNullable("restrictions", restrictions) - .withNullable("enforce_admins", enforceAdmins) .withUrlPath(branch.getProtectionUrl().toString()) .fetch(GHBranchProtection.class); } @@ -121,7 +211,29 @@ public GHBranchProtectionBuilder includeAdmins() { * @return the gh branch protection builder */ public GHBranchProtectionBuilder includeAdmins(boolean v) { - enforceAdmins = v; + fields.put("enforce_admins", v); + return this; + } + + /** + * Set the branch as read-only. + * + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder lockBranch() { + return lockBranch(true); + } + + /** + * Whether to set the branch as read-only. If this is true, users will not be able to push to the branch. + * Set to true to enable. Default: false. + * + * @param v + * the v + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder lockBranch(boolean v) { + fields.put("lock_branch", v); return this; } @@ -179,6 +291,75 @@ public GHBranchProtectionBuilder requireCodeOwnReviews(boolean v) { return this; } + /** + * Enable the most recent push must be approved by someone other than the person who pushed it. + * + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder requireLastPushApproval() { + return requireLastPushApproval(true); + } + + /** + * Whether the most recent push must be approved by someone other than the person who pushed it. Default: false. + * + * @param v + * the v + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder requireLastPushApproval(boolean v) { + getPrReviews().put("require_last_push_approval", v); + return this; + } + + /** + * Require all conversations on code to be resolved before a pull request can be merged into a branch that + * matches this rule. + * + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder requiredConversationResolution() { + return requiredConversationResolution(true); + } + + /** + * Require all conversations on code to be resolved before a pull request can be merged into a branch that + * matches this rule. + * Set to true to enable. Default: false. + * + * @param v + * the v + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder requiredConversationResolution(boolean v) { + fields.put("required_conversation_resolution", v); + return this; + } + + /** + * Enforce a linear commit Git history. + * + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder requiredLinearHistory() { + return requiredLinearHistory(true); + } + + /** + * Enforces a linear commit Git history, which prevents anyone from pushing merge commits to a branch. Set to true + * to enforce a linear commit history. Set to false to disable a linear commit Git history. Your repository must + * allow squash merging or rebase merging before you can enable a linear commit history. + * Default: false. + * + * @param v + * the v + * @return the gh branch protection builder + */ + public GHBranchProtectionBuilder requiredLinearHistory(boolean v) { + fields.put("required_linear_history", v); + return this; + } + /** * Require reviews gh branch protection builder. *