Skip to content

Commit 27e855d

Browse files
committed
Issue 262: added support for branch protection API
1 parent 3d1bed0 commit 27e855d

5 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.kohsuke.github;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* @author Kohsuke Kawaguchi
8+
* @see GHBranch#disableProtection()
9+
*/
10+
class BranchProtection {
11+
boolean enabled;
12+
RequiredStatusChecks requiredStatusChecks;
13+
14+
static class RequiredStatusChecks {
15+
EnforcementLevel enforcement_level;
16+
List<String> contexts = new ArrayList<String>();
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.kohsuke.github;
2+
3+
import java.util.Locale;
4+
5+
/**
6+
* @author Kohsuke Kawaguchi
7+
*/
8+
public enum EnforcementLevel {
9+
OFF, NON_ADMINS, EVERYONE;
10+
11+
public String toString() {
12+
return name().toLowerCase(Locale.ENGLISH);
13+
}
14+
}

src/main/java/org/kohsuke/github/GHBranch.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package org.kohsuke.github;
22

33
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;
49

510
/**
611
* A branch in a repository.
@@ -44,6 +49,43 @@ public String getName() {
4449
public String getSHA1() {
4550
return commit.sha;
4651
}
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+
}
4789

4890
@Override
4991
public String toString() {

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,10 @@ public Map<String,GHBranch> getBranches() throws IOException {
11031103
return r;
11041104
}
11051105

1106+
public GHBranch getBranch(String name) throws IOException {
1107+
return root.retrieve().to(getApiTailUrl("branches/"+name),GHBranch.class).wrap(this);
1108+
}
1109+
11061110
/**
11071111
* @deprecated
11081112
* Use {@link #listMilestones(GHIssueState)}

src/test/java/org/kohsuke/github/GHContentIntegrationTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public void setUp() throws Exception {
2020
repo = gitHub.getRepository("github-api-test-org/GHContentIntegrationTest").fork();
2121
}
2222

23+
@Test
24+
public void testBranchProtection() throws Exception {
25+
GHBranch b = repo.getBranch("master");
26+
b.enableProtection(EnforcementLevel.NON_ADMINS, "foo/bar");
27+
b.disableProtection();
28+
}
29+
2330
@Test
2431
public void testGetFileContent() throws Exception {
2532
GHContent content = repo.getFileContent("ghcontent-ro/a-file-with-content");

0 commit comments

Comments
 (0)