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
177 lines (157 loc) · 4.36 KB
/
Copy pathGHBranch.java
File metadata and controls
177 lines (157 loc) · 4.36 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
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Objects;
/**
* A branch in a repository.
*/
@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;
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
@Deprecated
public boolean isProtected() {
return protection;
}
/**
* Gets protection url.
*
* @return API URL that deals with the protection of this branch.
*/
@Preview
@Deprecated
public URL getProtectionUrl() {
return GitHubClient.parseURL(protection_url);
}
/**
* Gets protection.
*
* @return the protection
* @throws IOException
* the io exception
*/
public GHBranchProtection getProtection() throws IOException {
return root.createRequest().withUrlPath(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").withUrlPath(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
@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;
}
}
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;
}
}