Skip to content

Commit

Permalink
Issue 262: added support for branch protection API
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Jun 3, 2016
1 parent 3d1bed0 commit 27e855d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/kohsuke/github/BranchProtection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.kohsuke.github;

import java.util.ArrayList;
import java.util.List;

/**
* @author Kohsuke Kawaguchi
* @see GHBranch#disableProtection()
*/
class BranchProtection {
boolean enabled;
RequiredStatusChecks requiredStatusChecks;

static class RequiredStatusChecks {
EnforcementLevel enforcement_level;
List<String> contexts = new ArrayList<String>();
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/kohsuke/github/EnforcementLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.kohsuke.github;

import java.util.Locale;

/**
* @author Kohsuke Kawaguchi
*/
public enum EnforcementLevel {
OFF, NON_ADMINS, EVERYONE;

public String toString() {
return name().toLowerCase(Locale.ENGLISH);
}
}
42 changes: 42 additions & 0 deletions src/main/java/org/kohsuke/github/GHBranch.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.BranchProtection.RequiredStatusChecks;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

/**
* A branch in a repository.
Expand Down Expand Up @@ -44,6 +49,43 @@ public String getName() {
public String getSHA1() {
return commit.sha;
}

/**
* Disables branch protection and allows anyone with push access to push changes.
*/
public void disableProtection() throws IOException {
BranchProtection bp = new BranchProtection();
bp.enabled = false;
setProtection(bp);
}

/**
* Enables branch protection to control what commit statuses are required to push.
*
* @see GHCommitStatus#getContext()
*/
public void enableProtection(EnforcementLevel level, Collection<String> contexts) throws IOException {
BranchProtection bp = new BranchProtection();
bp.enabled = true;
bp.requiredStatusChecks = new RequiredStatusChecks();
bp.requiredStatusChecks.enforcement_level = level;
bp.requiredStatusChecks.contexts.addAll(contexts);
setProtection(bp);
}

public void enableProtection(EnforcementLevel level, String... contexts) throws IOException {
enableProtection(level, Arrays.asList(contexts));
}

private void setProtection(BranchProtection bp) throws IOException {
new Requester(root).method("PATCH")
.withHeader("Accept","application/vnd.github.loki-preview+json")
._with("protection",bp).to(getApiRoute());
}

String getApiRoute() {
return owner.getApiTailUrl("/branches/"+name);
}

@Override
public String toString() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,10 @@ public Map<String,GHBranch> getBranches() throws IOException {
return r;
}

public GHBranch getBranch(String name) throws IOException {
return root.retrieve().to(getApiTailUrl("branches/"+name),GHBranch.class).wrap(this);
}

/**
* @deprecated
* Use {@link #listMilestones(GHIssueState)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public void setUp() throws Exception {
repo = gitHub.getRepository("github-api-test-org/GHContentIntegrationTest").fork();
}

@Test
public void testBranchProtection() throws Exception {
GHBranch b = repo.getBranch("master");
b.enableProtection(EnforcementLevel.NON_ADMINS, "foo/bar");
b.disableProtection();
}

@Test
public void testGetFileContent() throws Exception {
GHContent content = repo.getFileContent("ghcontent-ro/a-file-with-content");
Expand Down

0 comments on commit 27e855d

Please sign in to comment.