forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHBranch.java
More file actions
240 lines (215 loc) · 6.3 KB
/
GHBranch.java
File metadata and controls
240 lines (215 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.internal.Previews;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Objects;
import javax.annotation.CheckForNull;
/**
* A branch in a repository.
*
* @author Yusuke Kokubo
*/
@SuppressFBWarnings(
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD",
"URF_UNREAD_FIELD" },
justification = "JSON API")
public class GHBranch extends GitHubInteractiveObject {
private GHRepository owner;
private String name;
private Commit commit;
@JsonProperty("protected")
private boolean protection;
private String protection_url;
@JsonCreator
GHBranch(@JsonProperty(value = "name", required = true) String name) throws Exception {
Objects.requireNonNull(name);
this.name = name;
}
/**
* The type Commit.
*/
public static class Commit {
String sha;
@SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "We don't provide it in API now")
String url;
}
/**
* Gets root.
*
* @return the root
*/
public GitHub getRoot() {
return root;
}
/**
* Gets owner.
*
* @return the repository that this branch is in.
*/
public GHRepository getOwner() {
return owner;
}
/**
* Gets name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Is protected boolean.
*
* @return true if the push to this branch is restricted via branch protection.
*/
@Preview(Previews.LUKE_CAGE)
@Deprecated
public boolean isProtected() {
return protection;
}
/**
* Gets protection url.
*
* @return API URL that deals with the protection of this branch.
*/
@Preview(Previews.LUKE_CAGE)
@Deprecated
public URL getProtectionUrl() {
return GitHubClient.parseURL(protection_url);
}
/**
* Gets protection.
*
* @return the protection
* @throws IOException
* the io exception
*/
@Preview(Previews.LUKE_CAGE)
@Deprecated
public GHBranchProtection getProtection() throws IOException {
return root.createRequest()
.withPreview(Previews.LUKE_CAGE)
.setRawUrlPath(protection_url)
.fetch(GHBranchProtection.class)
.wrap(this);
}
/**
* Gets sha 1.
*
* @return The SHA1 of the commit that this branch currently points to.
*/
public String getSHA1() {
return commit.sha;
}
/**
* Disables branch protection and allows anyone with push access to push changes.
*
* @throws IOException
* if disabling protection fails
*/
public void disableProtection() throws IOException {
root.createRequest().method("DELETE").setRawUrlPath(protection_url).send();
}
/**
* Enables branch protection to control what commit statuses are required to push.
*
* @return GHBranchProtectionBuilder for enabling protection
* @see GHCommitStatus#getContext() GHCommitStatus#getContext()
*/
@Preview(Previews.LUKE_CAGE)
@Deprecated
public GHBranchProtectionBuilder enableProtection() {
return new GHBranchProtectionBuilder(this);
}
/**
* Enable protection.
*
* @param level
* the level
* @param contexts
* the contexts
* @throws IOException
* the io exception
*/
// backward compatibility with previous signature
@Deprecated
public void enableProtection(EnforcementLevel level, Collection<String> contexts) throws IOException {
switch (level) {
case OFF :
disableProtection();
break;
case NON_ADMINS :
case EVERYONE :
enableProtection().addRequiredChecks(contexts)
.includeAdmins(level == EnforcementLevel.EVERYONE)
.enable();
break;
}
}
/**
* Merge a branch into this branch.
*
* @param headBranch
* the branch whose head will be merged
*
* @param commitMessage
* the commit message
*
* @return the merge {@link GHCommit} created, or {@code null} if the base already contains the head (nothing to
* merge).
*
* @throws IOException
* if merging fails
*/
@CheckForNull
public GHCommit merge(GHBranch headBranch, String commitMessage) throws IOException {
return merge(headBranch.getName(), commitMessage);
}
/**
* Merge a ref into this branch.
*
* @param head
* the ref name that will be merged into this branch. Follows the usual ref naming rules, could be a
* branch name, tag, or commit sha.
*
* @param commitMessage
* the commit message
*
* @return the merge {@link GHCommit} created, or {@code null} if the base already contains the head (nothing to
* merge).
*
* @throws IOException
* if merging fails
*/
@CheckForNull
public GHCommit merge(String head, String commitMessage) throws IOException {
GHCommit result = root.createRequest()
.withUrlPath(owner.getApiTailUrl("merges"))
.method("POST")
.with("commit_message", commitMessage)
.with("base", this.name)
.with("head", head)
.fetch(GHCommit.class);
if (result != null) {
result.wrapUp(owner);
}
return result;
}
String getApiRoute() {
return owner.getApiTailUrl("/branches/" + name);
}
@Override
public String toString() {
final String url = owner != null ? owner.getUrl().toString() : "unknown";
return "Branch:" + name + " in " + url;
}
GHBranch wrap(GHRepository repo) {
this.owner = repo;
this.root = repo.root;
return this;
}
}