|
1 | 1 | package org.kohsuke.github; |
2 | 2 |
|
3 | 3 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 4 | +import org.kohsuke.github.BranchProtection.RequiredStatusChecks; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collection; |
4 | 9 |
|
5 | 10 | /** |
6 | 11 | * A branch in a repository. |
@@ -44,6 +49,43 @@ public String getName() { |
44 | 49 | public String getSHA1() { |
45 | 50 | return commit.sha; |
46 | 51 | } |
| 52 | + |
| 53 | + /** |
| 54 | + * Disables branch protection and allows anyone with push access to push changes. |
| 55 | + */ |
| 56 | + public void disableProtection() throws IOException { |
| 57 | + BranchProtection bp = new BranchProtection(); |
| 58 | + bp.enabled = false; |
| 59 | + setProtection(bp); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Enables branch protection to control what commit statuses are required to push. |
| 64 | + * |
| 65 | + * @see GHCommitStatus#getContext() |
| 66 | + */ |
| 67 | + public void enableProtection(EnforcementLevel level, Collection<String> contexts) throws IOException { |
| 68 | + BranchProtection bp = new BranchProtection(); |
| 69 | + bp.enabled = true; |
| 70 | + bp.requiredStatusChecks = new RequiredStatusChecks(); |
| 71 | + bp.requiredStatusChecks.enforcement_level = level; |
| 72 | + bp.requiredStatusChecks.contexts.addAll(contexts); |
| 73 | + setProtection(bp); |
| 74 | + } |
| 75 | + |
| 76 | + public void enableProtection(EnforcementLevel level, String... contexts) throws IOException { |
| 77 | + enableProtection(level, Arrays.asList(contexts)); |
| 78 | + } |
| 79 | + |
| 80 | + private void setProtection(BranchProtection bp) throws IOException { |
| 81 | + new Requester(root).method("PATCH") |
| 82 | + .withHeader("Accept","application/vnd.github.loki-preview+json") |
| 83 | + ._with("protection",bp).to(getApiRoute()); |
| 84 | + } |
| 85 | + |
| 86 | + String getApiRoute() { |
| 87 | + return owner.getApiTailUrl("/branches/"+name); |
| 88 | + } |
47 | 89 |
|
48 | 90 | @Override |
49 | 91 | public String toString() { |
|
0 commit comments