Skip to content

Commit

Permalink
- improved branch protection support
Browse files Browse the repository at this point in the history
  • Loading branch information
jgangemi committed Aug 8, 2017
1 parent 7396395 commit 2dd660e
Show file tree
Hide file tree
Showing 8 changed files with 441 additions and 64 deletions.
21 changes: 0 additions & 21 deletions src/main/java/org/kohsuke/github/BranchProtection.java

This file was deleted.

38 changes: 11 additions & 27 deletions src/main/java/org/kohsuke/github/GHBranch.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@

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

import org.kohsuke.github.BranchProtection.RequiredStatusChecks;

import com.fasterxml.jackson.annotation.JsonProperty;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
* A branch in a repository.
*
*
* @author Yusuke Kokubo
*/
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD"}, justification = "JSON API")
public class GHBranch {
private GitHub root;
Expand All @@ -33,7 +29,7 @@ public class GHBranch {

public static class Commit {
String sha;

@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
String url;
}
Expand Down Expand Up @@ -69,6 +65,10 @@ public URL getProtectionUrl() {
return GitHub.parseURL(protection_url);
}

@Preview @Deprecated
public GHBranchProtection getProtection() throws IOException {
return root.retrieve().withPreview(LOKI).to(protection_url, GHBranchProtection.class);
}

/**
* The commit that this branch currently points to.
Expand All @@ -82,9 +82,7 @@ public String getSHA1() {
*/
@Preview @Deprecated
public void disableProtection() throws IOException {
BranchProtection bp = new BranchProtection();
bp.enabled = false;
setProtection(bp);
new Requester(root).method("DELETE").withPreview(LOKI).to(protection_url);
}

/**
Expand All @@ -93,28 +91,14 @@ public void disableProtection() throws IOException {
* @see GHCommitStatus#getContext()
*/
@Preview @Deprecated
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);
}

@Preview @Deprecated
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").withPreview(LOKI)._with("protection",bp).to(getApiRoute());
public GHBranchProtectionBuilder enableProtection() {
return new GHBranchProtectionBuilder(this);
}

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

@Override
public String toString() {
final String url = owner != null ? owner.getUrl().toString() : "unknown";
Expand Down
152 changes: 152 additions & 0 deletions src/main/java/org/kohsuke/github/GHBranchProtection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package org.kohsuke.github;

import java.util.Collection;

import com.fasterxml.jackson.annotation.JsonProperty;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
"URF_UNREAD_FIELD" }, justification = "JSON API")
public class GHBranchProtection {
@JsonProperty("enforce_admins")
private EnforceAdmins enforceAdmins;

@JsonProperty("required_pull_request_reviews")
private RequiredReviews requiredReviews;

@JsonProperty("required_status_checks")
private RequiredStatusChecks requiredStatusChecks;

@JsonProperty
private Restrictions restrictions;

@JsonProperty
private String url;

public EnforceAdmins getEnforceAdmins() {
return enforceAdmins;
}

public RequiredReviews getRequiredReviews() {
return requiredReviews;
}

public RequiredStatusChecks getRequiredStatusChecks() {
return requiredStatusChecks;
}

public Restrictions getRestrictions() {
return restrictions;
}

public String getUrl() {
return url;
}

public static class EnforceAdmins {
@JsonProperty
private boolean enabled;

@JsonProperty
private String url;

public String getUrl() {
return url;
}

public boolean isEnabled() {
return enabled;
}
}

public static class RequiredReviews {
@JsonProperty("dismissal_restrictions")
private Restrictions dismissalRestriction;

@JsonProperty("dismiss_stale_reviews")
private boolean dismissStaleReviews;

@JsonProperty("require_code_owner_reviews")
private boolean requireCodeOwnerReviews;

@JsonProperty
private String url;

public Restrictions getDismissalRestrictions() {
return dismissalRestriction;
}

public String getUrl() {
return url;
}

public boolean isDismissStaleReviews() {
return dismissStaleReviews;
}

public boolean isRequireCodeOwnerReviews() {
return requireCodeOwnerReviews;
}
}

public static class RequiredStatusChecks {
@JsonProperty
private Collection<String> contexts;

@JsonProperty
private boolean strict;

@JsonProperty
private String url;

public Collection<String> getContexts() {
return contexts;
}

public String getUrl() {
return url;
}

public boolean isRequiresBranchUpToDate() {
return strict;
}
}

public static class Restrictions {
@JsonProperty
private Collection<GHTeam> teams;

@JsonProperty("teams_url")
private String teamsUrl;

@JsonProperty
private String url;

@JsonProperty
private Collection<GHUser> users;

@JsonProperty("users_url")
private String usersUrl;

public Collection<GHTeam> getTeams() {
return teams;
}

public String getTeamsUrl() {
return teamsUrl;
}

public String getUrl() {
return url;
}

public Collection<GHUser> getUsers() {
return users;
}

public String getUsersUrl() {
return usersUrl;
}
}
}
Loading

0 comments on commit 2dd660e

Please sign in to comment.