Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 23cd51a

Browse filesBrowse files
committed
- improved branch protection support
1 parent 7396395 commit 23cd51a
Copy full SHA for 23cd51a

File tree

Expand file treeCollapse file tree

8 files changed

+441
-64
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+441
-64
lines changed

‎src/main/java/org/kohsuke/github/BranchProtection.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/BranchProtection.java
-21Lines changed: 0 additions & 21 deletions
This file was deleted.

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

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHBranch.java
+11-27Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@
44

55
import java.io.IOException;
66
import java.net.URL;
7-
import java.util.Arrays;
8-
import java.util.Collection;
9-
10-
import org.kohsuke.github.BranchProtection.RequiredStatusChecks;
117

128
import com.fasterxml.jackson.annotation.JsonProperty;
139

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

1612
/**
1713
* A branch in a repository.
18-
*
14+
*
1915
* @author Yusuke Kokubo
2016
*/
21-
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
17+
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
2218
"NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD"}, justification = "JSON API")
2319
public class GHBranch {
2420
private GitHub root;
@@ -33,7 +29,7 @@ public class GHBranch {
3329

3430
public static class Commit {
3531
String sha;
36-
32+
3733
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
3834
String url;
3935
}
@@ -69,6 +65,10 @@ public URL getProtectionUrl() {
6965
return GitHub.parseURL(protection_url);
7066
}
7167

68+
@Preview @Deprecated
69+
public GHBranchProtection getProtection() throws IOException {
70+
return root.retrieve().withPreview(LOKI).to(protection_url, GHBranchProtection.class);
71+
}
7272

7373
/**
7474
* The commit that this branch currently points to.
@@ -82,9 +82,7 @@ public String getSHA1() {
8282
*/
8383
@Preview @Deprecated
8484
public void disableProtection() throws IOException {
85-
BranchProtection bp = new BranchProtection();
86-
bp.enabled = false;
87-
setProtection(bp);
85+
new Requester(root).method("DELETE").withPreview(LOKI).to(protection_url);
8886
}
8987

9088
/**
@@ -93,28 +91,14 @@ public void disableProtection() throws IOException {
9391
* @see GHCommitStatus#getContext()
9492
*/
9593
@Preview @Deprecated
96-
public void enableProtection(EnforcementLevel level, Collection<String> contexts) throws IOException {
97-
BranchProtection bp = new BranchProtection();
98-
bp.enabled = true;
99-
bp.requiredStatusChecks = new RequiredStatusChecks();
100-
bp.requiredStatusChecks.enforcement_level = level;
101-
bp.requiredStatusChecks.contexts.addAll(contexts);
102-
setProtection(bp);
103-
}
104-
105-
@Preview @Deprecated
106-
public void enableProtection(EnforcementLevel level, String... contexts) throws IOException {
107-
enableProtection(level, Arrays.asList(contexts));
108-
}
109-
110-
private void setProtection(BranchProtection bp) throws IOException {
111-
new Requester(root).method("PATCH").withPreview(LOKI)._with("protection",bp).to(getApiRoute());
94+
public GHBranchProtectionBuilder enableProtection() {
95+
return new GHBranchProtectionBuilder(this);
11296
}
11397

11498
String getApiRoute() {
11599
return owner.getApiTailUrl("/branches/"+name);
116100
}
117-
101+
118102
@Override
119103
public String toString() {
120104
final String url = owner != null ? owner.getUrl().toString() : "unknown";
+152Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package org.kohsuke.github;
2+
3+
import java.util.Collection;
4+
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
8+
9+
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
10+
"URF_UNREAD_FIELD" }, justification = "JSON API")
11+
public class GHBranchProtection {
12+
@JsonProperty("enforce_admins")
13+
private EnforceAdmins enforceAdmins;
14+
15+
@JsonProperty("required_pull_request_reviews")
16+
private RequiredReviews requiredReviews;
17+
18+
@JsonProperty("required_status_checks")
19+
private RequiredStatusChecks requiredStatusChecks;
20+
21+
@JsonProperty
22+
private Restrictions restrictions;
23+
24+
@JsonProperty
25+
private String url;
26+
27+
public EnforceAdmins getEnforceAdmins() {
28+
return enforceAdmins;
29+
}
30+
31+
public RequiredReviews getRequiredReviews() {
32+
return requiredReviews;
33+
}
34+
35+
public RequiredStatusChecks getRequiredStatusChecks() {
36+
return requiredStatusChecks;
37+
}
38+
39+
public Restrictions getRestrictions() {
40+
return restrictions;
41+
}
42+
43+
public String getUrl() {
44+
return url;
45+
}
46+
47+
public static class EnforceAdmins {
48+
@JsonProperty
49+
private boolean enabled;
50+
51+
@JsonProperty
52+
private String url;
53+
54+
public String getUrl() {
55+
return url;
56+
}
57+
58+
public boolean isEnabled() {
59+
return enabled;
60+
}
61+
}
62+
63+
public static class RequiredReviews {
64+
@JsonProperty("dismissal_restrictions")
65+
private Restrictions dismissalRestriction;
66+
67+
@JsonProperty("dismiss_stale_reviews")
68+
private boolean dismissStaleReviews;
69+
70+
@JsonProperty("require_code_owner_reviews")
71+
private boolean requireCodeOwnerReviews;
72+
73+
@JsonProperty
74+
private String url;
75+
76+
public Restrictions getDismissalRestrictions() {
77+
return dismissalRestriction;
78+
}
79+
80+
public String getUrl() {
81+
return url;
82+
}
83+
84+
public boolean isDismissStaleReviews() {
85+
return dismissStaleReviews;
86+
}
87+
88+
public boolean isRequireCodeOwnerReviews() {
89+
return requireCodeOwnerReviews;
90+
}
91+
}
92+
93+
public static class RequiredStatusChecks {
94+
@JsonProperty
95+
private Collection<String> contexts;
96+
97+
@JsonProperty
98+
private boolean strict;
99+
100+
@JsonProperty
101+
private String url;
102+
103+
public Collection<String> getContexts() {
104+
return contexts;
105+
}
106+
107+
public String getUrl() {
108+
return url;
109+
}
110+
111+
public boolean isRequiresBranchUpToDate() {
112+
return strict;
113+
}
114+
}
115+
116+
public static class Restrictions {
117+
@JsonProperty
118+
private Collection<GHTeam> teams;
119+
120+
@JsonProperty("teams_url")
121+
private String teamsUrl;
122+
123+
@JsonProperty
124+
private String url;
125+
126+
@JsonProperty
127+
private Collection<GHUser> users;
128+
129+
@JsonProperty("users_url")
130+
private String usersUrl;
131+
132+
public Collection<GHTeam> getTeams() {
133+
return teams;
134+
}
135+
136+
public String getTeamsUrl() {
137+
return teamsUrl;
138+
}
139+
140+
public String getUrl() {
141+
return url;
142+
}
143+
144+
public Collection<GHUser> getUsers() {
145+
return users;
146+
}
147+
148+
public String getUsersUrl() {
149+
return usersUrl;
150+
}
151+
}
152+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.