diff --git a/src/main/java/org/kohsuke/github/GHBranchProtection.java b/src/main/java/org/kohsuke/github/GHBranchProtection.java index 697686ce2d..834544944f 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; @@ -59,6 +80,42 @@ public void disableSignedCommits() throws IOException { requester().method("DELETE").withUrlPath(url + REQUIRE_SIGNATURES_URI).send(); } + /** + * Gets allow deletions. + * + * @return the enforce admins + */ + public AllowDeletions getAllowDeletions() { + return allowDeletions; + } + + /** + * Gets allow force pushes. + * + * @return the enforce admins + */ + public AllowForcePushes getAllowForcePushes() { + return allowForcePushes; + } + + /** + * Gets allow fork syncing. + * + * @return the enforce admins + */ + public AllowForkSyncing getAllowForkSyncing() { + return allowForkSyncing; + } + + /** + * Gets block creations. + * + * @return the enforce admins + */ + public BlockCreations getBlockCreations() { + return blockCreations; + } + /** * Gets enforce admins. * @@ -68,6 +125,33 @@ public EnforceAdmins getEnforceAdmins() { return enforceAdmins; } + /** + * Gets lock branch. + * + * @return the enforce admins + */ + public LockBranch getLockBranch() { + return lockBranch; + } + + /** + * Gets required conversation resolution. + * + * @return the enforce admins + */ + public RequiredConversationResolution getRequiredConversationResolution() { + return requiredConversationResolution; + } + + /** + * Gets required linear history. + * + * @return the enforce admins + */ + public RequiredLinearHistory getRequiredLinearHistory() { + return requiredLinearHistory; + } + /** * Gets required reviews. * @@ -120,6 +204,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 +301,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 +359,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 +410,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..ec21b3168c 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,73 @@ 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. * diff --git a/src/test/java/org/kohsuke/github/GHBranchProtectionTest.java b/src/test/java/org/kohsuke/github/GHBranchProtectionTest.java index 3624068ee5..5254b3a707 100755 --- a/src/test/java/org/kohsuke/github/GHBranchProtectionTest.java +++ b/src/test/java/org/kohsuke/github/GHBranchProtectionTest.java @@ -2,7 +2,14 @@ import org.junit.Before; import org.junit.Test; +import org.kohsuke.github.GHBranchProtection.AllowDeletions; +import org.kohsuke.github.GHBranchProtection.AllowForcePushes; +import org.kohsuke.github.GHBranchProtection.AllowForkSyncing; +import org.kohsuke.github.GHBranchProtection.BlockCreations; import org.kohsuke.github.GHBranchProtection.EnforceAdmins; +import org.kohsuke.github.GHBranchProtection.LockBranch; +import org.kohsuke.github.GHBranchProtection.RequiredConversationResolution; +import org.kohsuke.github.GHBranchProtection.RequiredLinearHistory; import org.kohsuke.github.GHBranchProtection.RequiredReviews; import org.kohsuke.github.GHBranchProtection.RequiredStatusChecks; @@ -45,9 +52,17 @@ public void testEnableBranchProtections() throws Exception { .addRequiredChecks("test-status-check") .requireBranchIsUpToDate() .requireCodeOwnReviews() + .requireLastPushApproval() .dismissStaleReviews() .requiredReviewers(2) + .allowDeletions() + .allowForcePushes() + .allowForkSyncing() + .blockCreations() .includeAdmins() + .lockBranch() + .requiredConversationResolution() + .requiredLinearHistory() .enable(); verifyBranchProtection(protection); @@ -67,11 +82,40 @@ private void verifyBranchProtection(GHBranchProtection protection) { assertThat(requiredReviews, notNullValue()); assertThat(requiredReviews.isDismissStaleReviews(), is(true)); assertThat(requiredReviews.isRequireCodeOwnerReviews(), is(true)); + assertThat(requiredReviews.isRequireLastPushApproval(), is(true)); assertThat(requiredReviews.getRequiredReviewers(), equalTo(2)); + AllowDeletions allowDeletions = protection.getAllowDeletions(); + assertThat(allowDeletions, notNullValue()); + assertThat(allowDeletions.isEnabled(), is(true)); + + AllowForcePushes allowForcePushes = protection.getAllowForcePushes(); + assertThat(allowForcePushes, notNullValue()); + assertThat(allowForcePushes.isEnabled(), is(true)); + + AllowForkSyncing allowForkSyncing = protection.getAllowForkSyncing(); + assertThat(allowForkSyncing, notNullValue()); + assertThat(allowForkSyncing.isEnabled(), is(true)); + + BlockCreations blockCreations = protection.getBlockCreations(); + assertThat(blockCreations, notNullValue()); + assertThat(blockCreations.isEnabled(), is(true)); + EnforceAdmins enforceAdmins = protection.getEnforceAdmins(); assertThat(enforceAdmins, notNullValue()); assertThat(enforceAdmins.isEnabled(), is(true)); + + LockBranch lockBranch = protection.getLockBranch(); + assertThat(lockBranch, notNullValue()); + assertThat(lockBranch.isEnabled(), is(true)); + + RequiredConversationResolution requiredConversationResolution = protection.getRequiredConversationResolution(); + assertThat(requiredConversationResolution, notNullValue()); + assertThat(requiredConversationResolution.isEnabled(), is(true)); + + RequiredLinearHistory requiredLinearHistory = protection.getRequiredLinearHistory(); + assertThat(requiredLinearHistory, notNullValue()); + assertThat(requiredLinearHistory.isEnabled(), is(true)); } /** diff --git a/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/__files/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-4.json b/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/__files/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-4.json index ee226bab75..a793b4cefa 100644 --- a/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/__files/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-4.json +++ b/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/__files/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-4.json @@ -12,19 +12,32 @@ "url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/main/protection/required_pull_request_reviews", "dismiss_stale_reviews": true, "require_code_owner_reviews": true, - "required_approving_review_count": 2 + "required_approving_review_count": 2, + "require_last_push_approval": true + }, + "allow_deletions": { + "enabled": true + }, + "allow_force_pushes": { + "enabled": true + }, + "allow_fork_syncing": { + "enabled": true + }, + "block_creations": { + "enabled": true }, "enforce_admins": { "url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/main/protection/enforce_admins", "enabled": true }, - "required_linear_history": { - "enabled": false + "lock_branch": { + "enabled": true }, - "allow_force_pushes": { - "enabled": false + "required_conversation_resolution": { + "enabled": true }, - "allow_deletions": { - "enabled": false + "required_linear_history": { + "enabled": true } -} \ No newline at end of file +} diff --git a/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/__files/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-5.json b/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/__files/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-5.json index ee226bab75..a793b4cefa 100644 --- a/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/__files/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-5.json +++ b/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/__files/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-5.json @@ -12,19 +12,32 @@ "url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/main/protection/required_pull_request_reviews", "dismiss_stale_reviews": true, "require_code_owner_reviews": true, - "required_approving_review_count": 2 + "required_approving_review_count": 2, + "require_last_push_approval": true + }, + "allow_deletions": { + "enabled": true + }, + "allow_force_pushes": { + "enabled": true + }, + "allow_fork_syncing": { + "enabled": true + }, + "block_creations": { + "enabled": true }, "enforce_admins": { "url": "https://api.github.com/repos/hub4j-test-org/temp-testEnableBranchProtections/branches/main/protection/enforce_admins", "enabled": true }, - "required_linear_history": { - "enabled": false + "lock_branch": { + "enabled": true }, - "allow_force_pushes": { - "enabled": false + "required_conversation_resolution": { + "enabled": true }, - "allow_deletions": { - "enabled": false + "required_linear_history": { + "enabled": true } -} \ No newline at end of file +} diff --git a/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/mappings/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-4.json b/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/mappings/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-4.json index 31a205713a..3dfbbcf036 100644 --- a/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/mappings/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-4.json +++ b/src/test/resources/org/kohsuke/github/GHBranchProtectionTest/wiremock/testEnableBranchProtections/mappings/repos_hub4j-test-org_temp-testenablebranchprotections_branches_main_protection-4.json @@ -11,7 +11,7 @@ }, "bodyPatterns": [ { - "equalToJson": "{\"required_pull_request_reviews\":{\"required_approving_review_count\":2,\"require_code_owner_reviews\":true,\"dismiss_stale_reviews\":true},\"required_status_checks\":{\"contexts\":[\"test-status-check\"],\"strict\":true},\"restrictions\":null,\"enforce_admins\":true}", + "equalToJson": "{\"required_pull_request_reviews\":{\"required_approving_review_count\":2,\"require_code_owner_reviews\":true,\"dismiss_stale_reviews\":true,\"require_last_push_approval\":true},\"required_status_checks\":{\"contexts\":[\"test-status-check\"],\"strict\":true},\"restrictions\":null,\"allow_deletions\":true,\"allow_force_pushes\":true,\"allow_fork_syncing\":true,\"block_creations\":true,\"enforce_admins\":true,\"lock_branch\":true,\"required_conversation_resolution\":true,\"required_linear_history\":true}", "ignoreArrayOrder": true, "ignoreExtraElements": false } @@ -49,4 +49,4 @@ "uuid": "3cdd44cf-a62c-43f6-abad-de7c8b65bfe3", "persistent": true, "insertionIndex": 4 -} \ No newline at end of file +}