diff --git a/pom.xml b/pom.xml index 399b8b56e4..eccf95f7fd 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.75 + 1.76 GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java @@ -16,7 +16,7 @@ scm:git:git@github.com/kohsuke/${project.artifactId}.git scm:git:ssh://git@github.com/kohsuke/${project.artifactId}.git http://${project.artifactId}.kohsuke.org/ - github-api-1.75 + github-api-1.76 diff --git a/src/main/java/org/kohsuke/github/BranchProtection.java b/src/main/java/org/kohsuke/github/BranchProtection.java new file mode 100644 index 0000000000..4ae03b162a --- /dev/null +++ b/src/main/java/org/kohsuke/github/BranchProtection.java @@ -0,0 +1,21 @@ +package org.kohsuke.github; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Kohsuke Kawaguchi + * @see GHBranch#disableProtection() + */ +@SuppressFBWarnings(value = {"UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD"}, justification = "JSON API") +class BranchProtection { + boolean enabled; + RequiredStatusChecks requiredStatusChecks; + + static class RequiredStatusChecks { + EnforcementLevel enforcement_level; + List contexts = new ArrayList(); + } +} diff --git a/src/main/java/org/kohsuke/github/EnforcementLevel.java b/src/main/java/org/kohsuke/github/EnforcementLevel.java new file mode 100644 index 0000000000..1b29195299 --- /dev/null +++ b/src/main/java/org/kohsuke/github/EnforcementLevel.java @@ -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); + } +} diff --git a/src/main/java/org/kohsuke/github/GHBranch.java b/src/main/java/org/kohsuke/github/GHBranch.java index 272c643f47..1de47a156e 100644 --- a/src/main/java/org/kohsuke/github/GHBranch.java +++ b/src/main/java/org/kohsuke/github/GHBranch.java @@ -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. @@ -8,7 +13,7 @@ * @author Yusuke Kokubo */ @SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", - "NP_UNWRITTEN_FIELD"}, justification = "JSON API") + "NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD"}, justification = "JSON API") public class GHBranch { private GitHub root; private GHRepository owner; @@ -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 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() { diff --git a/src/main/java/org/kohsuke/github/GHCommitStatus.java b/src/main/java/org/kohsuke/github/GHCommitStatus.java index c9241442bf..7b876fc5a0 100644 --- a/src/main/java/org/kohsuke/github/GHCommitStatus.java +++ b/src/main/java/org/kohsuke/github/GHCommitStatus.java @@ -1,8 +1,6 @@ package org.kohsuke.github; -import java.io.IOException; import java.net.URL; -import java.util.Date; /** * Represents a status of a commit. @@ -10,6 +8,7 @@ * @author Kohsuke Kawaguchi * @see GHRepository#getLastCommitStatus(String) * @see GHCommit#getLastStatus() + * @see GHRepository#createCommitStatus(String, GHCommitState, String, String) */ public class GHCommitStatus extends GHObject { String state; diff --git a/src/main/java/org/kohsuke/github/GHEvent.java b/src/main/java/org/kohsuke/github/GHEvent.java index e8e79bb456..b33cb991eb 100644 --- a/src/main/java/org/kohsuke/github/GHEvent.java +++ b/src/main/java/org/kohsuke/github/GHEvent.java @@ -5,10 +5,9 @@ /** * Hook event type. * - * See http://developer.github.com/v3/events/types/ - * * @author Kohsuke Kawaguchi * @see GHEventInfo + * @see Event type reference */ public enum GHEvent { COMMIT_COMMENT, diff --git a/src/main/java/org/kohsuke/github/GHIssue.java b/src/main/java/org/kohsuke/github/GHIssue.java index 39e5c24b70..a5afad6d2e 100644 --- a/src/main/java/org/kohsuke/github/GHIssue.java +++ b/src/main/java/org/kohsuke/github/GHIssue.java @@ -54,6 +54,7 @@ public class GHIssue extends GHObject { protected int number; protected String closed_at; protected int comments; + @SkipFromToString protected String body; // for backward compatibility with < 1.63, this collection needs to hold instances of Label, not GHLabel protected List