Skip to content

Commit

Permalink
Merge pull request #435
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Aug 30, 2018
2 parents 3fa70ac + 5e36377 commit 9afd1c5
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 65 deletions.
6 changes: 2 additions & 4 deletions src/main/java/org/kohsuke/github/GHBranch.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ 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);
return root.retrieve().to(protection_url, GHBranchProtection.class).wrap(this);
}

/**
Expand All @@ -80,9 +79,8 @@ public String getSHA1() {
/**
* Disables branch protection and allows anyone with push access to push changes.
*/
@Preview @Deprecated
public void disableProtection() throws IOException {
new Requester(root).method("DELETE").withPreview(LOKI).to(protection_url);
new Requester(root).method("DELETE").to(protection_url);
}

/**
Expand Down
155 changes: 106 additions & 49 deletions src/main/java/org/kohsuke/github/GHBranchProtection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,59 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import static org.kohsuke.github.Previews.ZZZAX;

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

@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
"URF_UNREAD_FIELD" }, justification = "JSON API")
@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;
private static final String REQUIRE_SIGNATURES_URI = "/required_signatures";

@JsonProperty("enforce_admins")
private EnforceAdmins enforceAdmins;

private GitHub root;

@JsonProperty("required_pull_request_reviews")
private RequiredReviews requiredReviews;

@JsonProperty("required_pull_request_reviews")
private RequiredReviews requiredReviews;
@JsonProperty("required_status_checks")
private RequiredStatusChecks requiredStatusChecks;

@JsonProperty("required_status_checks")
private RequiredStatusChecks requiredStatusChecks;
@JsonProperty
private Restrictions restrictions;

@JsonProperty
private Restrictions restrictions;
@JsonProperty
private String url;

@Preview @Deprecated
public void enabledSignedCommits() throws IOException {
requester().method("POST")
.to(url + REQUIRE_SIGNATURES_URI, RequiredSignatures.class);
}

@Preview @Deprecated
public void disableSignedCommits() throws IOException {
requester().method("DELETE")
.to(url + REQUIRE_SIGNATURES_URI);
}

@JsonProperty
private String url;

public EnforceAdmins getEnforceAdmins() {
public EnforceAdmins getEnforceAdmins() {
return enforceAdmins;
}

public RequiredReviews getRequiredReviews() {
return requiredReviews;
}

@Preview @Deprecated
public boolean getRequiredSignatures() throws IOException {
return requester().method("GET")
.to(url + REQUIRE_SIGNATURES_URI, RequiredSignatures.class).enabled;
}

public RequiredStatusChecks getRequiredStatusChecks() {
return requiredStatusChecks;
}
Expand All @@ -43,12 +68,21 @@ public String getUrl() {
return url;
}

GHBranchProtection wrap(GHBranch branch) {
this.root = branch.getRoot();
return this;
}

private Requester requester() {
return new Requester(root).withPreview(ZZZAX);
}

public static class EnforceAdmins {
@JsonProperty
private boolean enabled;
@JsonProperty
private boolean enabled;

@JsonProperty
private String url;
@JsonProperty
private String url;

public String getUrl() {
return url;
Expand All @@ -57,20 +91,23 @@ public String getUrl() {
public boolean isEnabled() {
return enabled;
}
}
}

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

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

@JsonProperty("dismiss_stale_reviews")
private boolean dismissStaleReviews;
@JsonProperty("require_code_owner_reviews")
private boolean requireCodeOwnerReviews;

@JsonProperty("require_code_owner_reviews")
private boolean requireCodeOwnerReviews;
@JsonProperty("required_approving_review_count")
private int requiredReviewers;

@JsonProperty
private String url;
@JsonProperty
private String url;

public Restrictions getDismissalRestrictions() {
return dismissalRestriction;
Expand All @@ -87,17 +124,37 @@ public boolean isDismissStaleReviews() {
public boolean isRequireCodeOwnerReviews() {
return requireCodeOwnerReviews;
}
}

public static class RequiredStatusChecks {
@JsonProperty
private Collection<String> contexts;
public int getRequiredReviewers() {
return requiredReviewers;
}
}

private static class RequiredSignatures {
@JsonProperty
private boolean enabled;

@JsonProperty
private String url;

public String getUrl() {
return url;
}

@JsonProperty
private boolean strict;
public boolean isEnabled() {
return enabled;
}
}

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

@JsonProperty
private boolean strict;

@JsonProperty
private String url;

public Collection<String> getContexts() {
return contexts;
Expand All @@ -110,23 +167,23 @@ public String getUrl() {
public boolean isRequiresBranchUpToDate() {
return strict;
}
}
}

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

@JsonProperty("teams_url")
private String teamsUrl;
@JsonProperty("teams_url")
private String teamsUrl;

@JsonProperty
private String url;
@JsonProperty
private String url;

@JsonProperty
private Collection<GHUser> users;
@JsonProperty
private Collection<GHUser> users;

@JsonProperty("users_url")
private String usersUrl;
@JsonProperty("users_url")
private String usersUrl;

public Collection<GHTeam> getTeams() {
return teams;
Expand All @@ -147,5 +204,5 @@ public Collection<GHUser> getUsers() {
public String getUsersUrl() {
return usersUrl;
}
}
}
}
33 changes: 24 additions & 9 deletions src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public GHBranchProtectionBuilder addRequiredChecks(String... checks) {
}

public GHBranchProtectionBuilder dismissStaleReviews() {
getPrReviews().put("dismiss_stale_reviews", true);
return dismissStaleReviews(true);
}

public GHBranchProtectionBuilder dismissStaleReviews(boolean v) {
getPrReviews().put("dismiss_stale_reviews", v);
return this;
}

Expand All @@ -54,7 +58,8 @@ public GHBranchProtection enable() throws IOException {
.withNullable("required_pull_request_reviews", prReviews)
.withNullable("restrictions", restrictions)
.withNullable("enforce_admins", enforceAdmins)
.to(branch.getProtectionUrl().toString(), GHBranchProtection.class);
.to(branch.getProtectionUrl().toString(), GHBranchProtection.class)
.wrap(branch);
}

public GHBranchProtectionBuilder includeAdmins() {
Expand All @@ -66,6 +71,11 @@ public GHBranchProtectionBuilder includeAdmins(boolean v) {
return this;
}

public GHBranchProtectionBuilder requiredReviewers(int v) {
getPrReviews().put("required_approving_review_count", v);
return this;
}

public GHBranchProtectionBuilder requireBranchIsUpToDate() {
return requireBranchIsUpToDate(true);
}
Expand All @@ -89,6 +99,16 @@ public GHBranchProtectionBuilder requireReviews() {
return this;
}

public GHBranchProtectionBuilder restrictReviewDismissals() {
getPrReviews();

if (!prReviews.containsKey("dismissal_restrictions")) {
prReviews.put("dismissal_restrictions", new Restrictions());
}

return this;
}

public GHBranchProtectionBuilder restrictPushAccess() {
getRestrictions();
return this;
Expand Down Expand Up @@ -151,12 +171,7 @@ public GHBranchProtectionBuilder userReviewDismissals(GHUser... users) {
}

private void addReviewRestriction(String restriction, boolean isTeam) {
getPrReviews();

if (!prReviews.containsKey("dismissal_restrictions")) {
prReviews.put("dismissal_restrictions", new Restrictions());
}

restrictReviewDismissals();
Restrictions restrictions = (Restrictions) prReviews.get("dismissal_restrictions");

if (isTeam) {
Expand Down Expand Up @@ -188,7 +203,7 @@ private StatusChecks getStatusChecks() {
}

private Requester requester() {
return new Requester(branch.getRoot()).withPreview(LOKI);
return new Requester(branch.getRoot()).withPreview(LUKE_CAGE);
}

private static class Restrictions {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -1304,15 +1304,15 @@ public boolean remove(Object url) {
*/
public Map<String,GHBranch> getBranches() throws IOException {
Map<String,GHBranch> r = new TreeMap<String,GHBranch>();
for (GHBranch p : root.retrieve().withPreview(LOKI).to(getApiTailUrl("branches"), GHBranch[].class)) {
for (GHBranch p : root.retrieve().to(getApiTailUrl("branches"), GHBranch[].class)) {
p.wrap(this);
r.put(p.getName(),p);
}
return r;
}

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

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/kohsuke/github/Previews.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* @author Kohsuke Kawaguchi
*/
/*package*/ class Previews {
static final String LOKI = "application/vnd.github.loki-preview+json";
static final String LUKE_CAGE = "application/vnd.github.luke-cage-preview+json";
static final String DRAX = "application/vnd.github.drax-preview+json";
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
static final String CLOAK = "application/vnd.github.cloak-preview";
static final String ZZZAX = "application/vnd.github.zzzax-preview+json";
}
21 changes: 21 additions & 0 deletions src/test/java/org/kohsuke/github/GHBranchProtectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void setUp() throws Exception {
branch = repo.getBranch(BRANCH);

if (branch.isProtected()) {
GHBranchProtection protection = branch.getProtection();
if (protection.getRequiredSignatures()) {
protection.disableSignedCommits();
}

assertFalse(protection.getRequiredSignatures());
branch.disableProtection();
}

Expand All @@ -47,6 +53,7 @@ public void testEnableBranchProtections() throws Exception {
.requireBranchIsUpToDate()
.requireCodeOwnReviews()
.dismissStaleReviews()
.requiredReviewers(2)
.includeAdmins()
.enable();

Expand All @@ -59,6 +66,7 @@ public void testEnableBranchProtections() throws Exception {
assertNotNull(requiredReviews);
assertTrue(requiredReviews.isDismissStaleReviews());
assertTrue(requiredReviews.isRequireCodeOwnerReviews());
assertEquals(2, requiredReviews.getRequiredReviewers());

EnforceAdmins enforceAdmins = protection.getEnforceAdmins();
assertNotNull(enforceAdmins);
Expand All @@ -79,4 +87,17 @@ public void testEnableRequireReviewsOnly() throws Exception {

assertNotNull(protection.getRequiredReviews());
}

@Test
public void testSignedCommits() throws Exception {
GHBranchProtection protection = branch.enableProtection().enable();

assertFalse(protection.getRequiredSignatures());

protection.enabledSignedCommits();
assertTrue(protection.getRequiredSignatures());

protection.disableSignedCommits();
assertFalse(protection.getRequiredSignatures());
}
}

0 comments on commit 9afd1c5

Please sign in to comment.