Skip to content

Commit 1145941

Browse files
committed
- add support for signed commits
- add support for required number of reviews
1 parent d61697a commit 1145941

File tree

5 files changed

+114
-11
lines changed

5 files changed

+114
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public URL getProtectionUrl() {
6767

6868
@Preview @Deprecated
6969
public GHBranchProtection getProtection() throws IOException {
70-
return root.retrieve().withPreview(LOKI).to(protection_url, GHBranchProtection.class);
70+
return root.retrieve().withPreview(LOKI).to(protection_url, GHBranchProtection.class).wrap(this);
7171
}
7272

7373
/**
@@ -82,7 +82,7 @@ public String getSHA1() {
8282
*/
8383
@Preview @Deprecated
8484
public void disableProtection() throws IOException {
85-
new Requester(root).method("DELETE").withPreview(LOKI).to(protection_url);
85+
new Requester(root).method("DELETE").to(protection_url);
8686
}
8787

8888
/**

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
55

6+
import static org.kohsuke.github.Previews.ZZZAX;
7+
8+
import java.io.IOException;
69
import java.util.Collection;
710

811
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
912
"URF_UNREAD_FIELD" }, justification = "JSON API")
1013
public class GHBranchProtection {
14+
private static final String REQUIRE_SIGNATURES_URI = "/required_signatures";
15+
1116
@JsonProperty("enforce_admins")
1217
private EnforceAdmins enforceAdmins;
1318

19+
private GitHub root;
20+
1421
@JsonProperty("required_pull_request_reviews")
1522
private RequiredReviews requiredReviews;
1623

@@ -23,6 +30,18 @@ public class GHBranchProtection {
2330
@JsonProperty
2431
private String url;
2532

33+
@Preview @Deprecated
34+
public RequiredSignatures enabledSignedCommits() throws IOException {
35+
return requester().method("POST")
36+
.to(url + REQUIRE_SIGNATURES_URI, RequiredSignatures.class);
37+
}
38+
39+
@Preview @Deprecated
40+
public void disableSignedCommits() throws IOException {
41+
requester().method("DELETE")
42+
.to(url + REQUIRE_SIGNATURES_URI);
43+
}
44+
2645
public EnforceAdmins getEnforceAdmins() {
2746
return enforceAdmins;
2847
}
@@ -31,6 +50,12 @@ public RequiredReviews getRequiredReviews() {
3150
return requiredReviews;
3251
}
3352

53+
@Preview @Deprecated
54+
public RequiredSignatures getRequiredSignatures() throws IOException {
55+
return requester().method("GET")
56+
.to(url + REQUIRE_SIGNATURES_URI, RequiredSignatures.class);
57+
}
58+
3459
public RequiredStatusChecks getRequiredStatusChecks() {
3560
return requiredStatusChecks;
3661
}
@@ -43,6 +68,15 @@ public String getUrl() {
4368
return url;
4469
}
4570

71+
GHBranchProtection wrap(GHBranch branch) {
72+
this.root = branch.getRoot();
73+
return this;
74+
}
75+
76+
private Requester requester() {
77+
return new Requester(root).withPreview(ZZZAX);
78+
}
79+
4680
public static class EnforceAdmins {
4781
@JsonProperty
4882
private boolean enabled;
@@ -69,6 +103,9 @@ public static class RequiredReviews {
69103
@JsonProperty("require_code_owner_reviews")
70104
private boolean requireCodeOwnerReviews;
71105

106+
@JsonProperty("required_approving_review_count")
107+
private int requiredReviewers;
108+
72109
@JsonProperty
73110
private String url;
74111

@@ -87,6 +124,27 @@ public boolean isDismissStaleReviews() {
87124
public boolean isRequireCodeOwnerReviews() {
88125
return requireCodeOwnerReviews;
89126
}
127+
128+
public int getRequiredReviewers()
129+
{
130+
return requiredReviewers;
131+
}
132+
}
133+
134+
public static class RequiredSignatures {
135+
@JsonProperty
136+
private boolean enabled;
137+
138+
@JsonProperty
139+
private String url;
140+
141+
public String getUrl() {
142+
return url;
143+
}
144+
145+
public boolean isEnabled() {
146+
return enabled;
147+
}
90148
}
91149

92150
public static class RequiredStatusChecks {

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ public GHBranchProtectionBuilder addRequiredChecks(String... checks) {
4444
}
4545

4646
public GHBranchProtectionBuilder dismissStaleReviews() {
47-
getPrReviews().put("dismiss_stale_reviews", true);
47+
dismissStaleReviews(true);
48+
return this;
49+
}
50+
51+
public GHBranchProtectionBuilder dismissStaleReviews(boolean v) {
52+
getPrReviews().put("dismiss_stale_reviews", v);
4853
return this;
4954
}
5055

@@ -54,7 +59,8 @@ public GHBranchProtection enable() throws IOException {
5459
.withNullable("required_pull_request_reviews", prReviews)
5560
.withNullable("restrictions", restrictions)
5661
.withNullable("enforce_admins", enforceAdmins)
57-
.to(branch.getProtectionUrl().toString(), GHBranchProtection.class);
62+
.to(branch.getProtectionUrl().toString(), GHBranchProtection.class)
63+
.wrap(branch);
5864
}
5965

6066
public GHBranchProtectionBuilder includeAdmins() {
@@ -66,6 +72,11 @@ public GHBranchProtectionBuilder includeAdmins(boolean v) {
6672
return this;
6773
}
6874

75+
public GHBranchProtectionBuilder requiredReviewers(int v) {
76+
getPrReviews().put("required_approving_review_count", v);
77+
return this;
78+
}
79+
6980
public GHBranchProtectionBuilder requireBranchIsUpToDate() {
7081
return requireBranchIsUpToDate(true);
7182
}
@@ -89,6 +100,16 @@ public GHBranchProtectionBuilder requireReviews() {
89100
return this;
90101
}
91102

103+
public GHBranchProtectionBuilder restrictReviewDismissals() {
104+
getPrReviews();
105+
106+
if (!prReviews.containsKey("dismissal_restrictions")) {
107+
prReviews.put("dismissal_restrictions", new Restrictions());
108+
}
109+
110+
return this;
111+
}
112+
92113
public GHBranchProtectionBuilder restrictPushAccess() {
93114
getRestrictions();
94115
return this;
@@ -151,12 +172,7 @@ public GHBranchProtectionBuilder userReviewDismissals(GHUser... users) {
151172
}
152173

153174
private void addReviewRestriction(String restriction, boolean isTeam) {
154-
getPrReviews();
155-
156-
if (!prReviews.containsKey("dismissal_restrictions")) {
157-
prReviews.put("dismissal_restrictions", new Restrictions());
158-
}
159-
175+
restrictReviewDismissals();
160176
Restrictions restrictions = (Restrictions) prReviews.get("dismissal_restrictions");
161177

162178
if (isTeam) {
@@ -188,7 +204,7 @@ private StatusChecks getStatusChecks() {
188204
}
189205

190206
private Requester requester() {
191-
return new Requester(branch.getRoot()).withPreview(LOKI);
207+
return new Requester(branch.getRoot()).withPreview(LUKE_CAGE);
192208
}
193209

194210
private static class Restrictions {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66
/*package*/ class Previews {
77
static final String LOKI = "application/vnd.github.loki-preview+json";
8+
static final String LUKE_CAGE = "application/vnd.github.luke-cage-preview+json";
89
static final String DRAX = "application/vnd.github.drax-preview+json";
910
static final String SQUIRREL_GIRL = "application/vnd.github.squirrel-girl-preview";
1011
static final String CLOAK = "application/vnd.github.cloak-preview";
12+
static final String ZZZAX = "application/vnd.github.zzzax-preview+json";
1113
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Test;
55
import org.kohsuke.github.GHBranchProtection.EnforceAdmins;
66
import org.kohsuke.github.GHBranchProtection.RequiredReviews;
7+
import org.kohsuke.github.GHBranchProtection.RequiredSignatures;
78
import org.kohsuke.github.GHBranchProtection.RequiredStatusChecks;
89

910
import java.io.FileNotFoundException;
@@ -32,6 +33,12 @@ public void setUp() throws Exception {
3233
branch = repo.getBranch(BRANCH);
3334

3435
if (branch.isProtected()) {
36+
GHBranchProtection protection = branch.getProtection();
37+
if (protection.getRequiredSignatures().isEnabled()) {
38+
protection.disableSignedCommits();
39+
}
40+
41+
assertFalse(protection.getRequiredSignatures().isEnabled());
3542
branch.disableProtection();
3643
}
3744

@@ -47,6 +54,7 @@ public void testEnableBranchProtections() throws Exception {
4754
.requireBranchIsUpToDate()
4855
.requireCodeOwnReviews()
4956
.dismissStaleReviews()
57+
.requiredReviewers(2)
5058
.includeAdmins()
5159
.enable();
5260

@@ -59,6 +67,7 @@ public void testEnableBranchProtections() throws Exception {
5967
assertNotNull(requiredReviews);
6068
assertTrue(requiredReviews.isDismissStaleReviews());
6169
assertTrue(requiredReviews.isRequireCodeOwnerReviews());
70+
assertEquals(2, requiredReviews.getRequiredReviewers());
6271

6372
EnforceAdmins enforceAdmins = protection.getEnforceAdmins();
6473
assertNotNull(enforceAdmins);
@@ -79,4 +88,22 @@ public void testEnableRequireReviewsOnly() throws Exception {
7988

8089
assertNotNull(protection.getRequiredReviews());
8190
}
91+
92+
@Test
93+
public void testSignedCommits() throws Exception {
94+
GHBranchProtection protection = branch.enableProtection().enable();
95+
96+
RequiredSignatures signatures = protection.getRequiredSignatures();
97+
assertNotNull(signatures);
98+
assertFalse(signatures.isEnabled());
99+
100+
signatures = protection.enabledSignedCommits();
101+
assertNotNull(signatures);
102+
assertTrue(signatures.isEnabled());
103+
104+
protection.disableSignedCommits();
105+
signatures = protection.getRequiredSignatures();
106+
assertNotNull(signatures);
107+
assertFalse(signatures.isEnabled());
108+
}
82109
}

0 commit comments

Comments
 (0)