-
Notifications
You must be signed in to change notification settings - Fork 606
/
GhprbRepository.java
309 lines (272 loc) · 11.3 KB
/
GhprbRepository.java
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package org.jenkinsci.plugins.ghprb;
import com.google.common.annotations.VisibleForTesting;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import org.kohsuke.github.*;
import org.kohsuke.github.GHEventPayload.IssueComment;
import org.kohsuke.github.GHEventPayload.PullRequest;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Honza Brázdil <jbrazdil@redhat.com>
*/
public class GhprbRepository {
private static final Logger logger = Logger.getLogger(GhprbRepository.class.getName());
private static final EnumSet<GHEvent> HOOK_EVENTS = EnumSet.of(GHEvent.ISSUE_COMMENT, GHEvent.PULL_REQUEST);
private final String reponame;
private final ConcurrentMap<Integer, GhprbPullRequest> pulls;
private GHRepository ghRepository;
private Ghprb helper;
public GhprbRepository(String user, String repository, Ghprb helper, ConcurrentMap<Integer, GhprbPullRequest> pulls) {
this.reponame = user + "/" + repository;
this.helper = helper;
this.pulls = pulls;
}
public void init() {
for (GhprbPullRequest pull : pulls.values()) {
pull.init(helper, this);
}
// make the initial check call to populate our data structures
initGhRepository();
}
private boolean initGhRepository() {
GitHub gitHub = null;
try {
gitHub = helper.getGitHub().get();
if (gitHub.getRateLimit().remaining == 0) {
return false;
}
} catch (FileNotFoundException ex) {
logger.log(Level.INFO, "Rate limit API not found.");
} catch (IOException ex) {
logger.log(Level.SEVERE, "Error while accessing rate limit API", ex);
return false;
}
if (ghRepository == null) {
try {
ghRepository = gitHub.getRepository(reponame);
} catch (IOException ex) {
logger.log(Level.SEVERE, "Could not retrieve GitHub repository named " + reponame + " (Do you have properly set 'GitHub project' field in job configuration?)", ex);
return false;
}
}
return true;
}
public void check() {
if (!initGhRepository()) {
return;
}
if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Project is disabled, not checking github state");
return;
}
List<GHPullRequest> openPulls;
try {
openPulls = ghRepository.getPullRequests(GHIssueState.OPEN);
} catch (IOException ex) {
logger.log(Level.SEVERE, "Could not retrieve open pull requests.", ex);
return;
}
Set<Integer> closedPulls = new HashSet<Integer>(pulls.keySet());
for (GHPullRequest pr : openPulls) {
if (pr.getHead() == null) {
try {
pr = ghRepository.getPullRequest(pr.getNumber());
} catch (IOException ex) {
logger.log(Level.SEVERE, "Could not retrieve pr " + pr.getNumber(), ex);
return;
}
}
check(pr);
closedPulls.remove(pr.getNumber());
}
// remove closed pulls so we don't check them again
for (Integer id : closedPulls) {
pulls.remove(id);
}
}
private void check(GHPullRequest pr) {
final Integer id = pr.getNumber();
GhprbPullRequest pull;
if (pulls.containsKey(id)) {
pull = pulls.get(id);
} else {
pulls.putIfAbsent(id, new GhprbPullRequest(pr, helper, this));
pull = pulls.get(id);
}
pull.check(pr);
}
public void createCommitStatus(AbstractBuild<?, ?> build, GHCommitState state, String message, int id, String context, PrintStream stream) {
String sha1 = build.getCause(GhprbCause.class).getCommit();
createCommitStatus(build, sha1, state, Jenkins.getInstance().getRootUrl() + build.getUrl(), message, id, context, stream);
}
public void createCommitStatus(String sha1, GHCommitState state, String url, String message, int id, String context) {
createCommitStatus(null, sha1, state, url, message, id, context, null);
}
public void createCommitStatus(AbstractBuild<?, ?> build, String sha1, GHCommitState state, String url, String message, int id, String context, PrintStream stream) {
String newMessage = String.format("Setting status of %s to %s with url %s and message: %s", sha1, state, url, message);
if (stream != null) {
stream.println(newMessage);
} else {
logger.log(Level.INFO, newMessage);
}
try {
if (context != null && !context.isEmpty()) {
ghRepository.createCommitStatus(sha1, state, url, message, context);
} else {
ghRepository.createCommitStatus(sha1, state, url, message);
}
} catch (IOException ex) {
if (ex instanceof FileNotFoundException) {
newMessage = "FileNotFoundException means that the credentials Jenkins is using is probably wrong. Or the user account does not have write access to the repo.";
} else {
newMessage = "Could not update commit status of the Pull Request on GitHub.";
}
if (stream != null) {
stream.println(newMessage);
ex.printStackTrace(stream);
} else {
logger.log(Level.INFO, newMessage, ex);
}
if (GhprbTrigger.getDscp().getUseComments()) {
if (state == GHCommitState.SUCCESS) {
message = message + " " + GhprbTrigger.getDscp().getMsgSuccess(build);
} else if (state == GHCommitState.FAILURE) {
message = message + " " + GhprbTrigger.getDscp().getMsgFailure(build);
}
if (GhprbTrigger.getDscp().getUseDetailedComments() || (state == GHCommitState.SUCCESS || state == GHCommitState.FAILURE)) {
logger.log(Level.INFO, "Trying to send comment.", ex);
addComment(id, message);
}
} else {
logger.log(Level.SEVERE, "Could not update commit status of the Pull Request on GitHub.");
}
}
}
public String getName() {
return reponame;
}
public void addComment(int id, String comment) {
addComment(id, comment, null, null);
}
public void addComment(int id, String comment, AbstractBuild<?, ?> build, TaskListener listener) {
if (comment.trim().isEmpty())
return;
if (build != null && listener != null) {
try {
comment = build.getEnvironment(listener).expand(comment);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error", e);
}
}
try {
ghRepository.getPullRequest(id).comment(comment);
} catch (IOException ex) {
logger.log(Level.SEVERE, "Couldn't add comment to pull request #" + id + ": '" + comment + "'", ex);
}
}
public void closePullRequest(int id) {
try {
ghRepository.getPullRequest(id).close();
} catch (IOException ex) {
logger.log(Level.SEVERE, "Couldn't close the pull request #" + id + ": '", ex);
}
}
private boolean hookExist() throws IOException {
for (GHHook h : ghRepository.getHooks()) {
if (!"web".equals(h.getName())) {
continue;
}
if (!getHookUrl().equals(h.getConfig().get("url"))) {
continue;
}
return true;
}
return false;
}
public boolean createHook() {
if (ghRepository == null) {
logger.log(Level.INFO, "Repository not available, cannot set pull request hook for repository {0}", reponame);
return false;
}
try {
if (hookExist()) {
return true;
}
Map<String, String> config = new HashMap<String, String>();
config.put("url", new URL(getHookUrl()).toExternalForm());
config.put("insecure_ssl", "1");
ghRepository.createHook("web", config, HOOK_EVENTS, true);
return true;
} catch (IOException ex) {
logger.log(Level.SEVERE, "Couldn''t create web hook for repository {0}. Does the user (from global configuration) have admin rights to the repository?", reponame);
return false;
}
}
private static String getHookUrl() {
return Jenkins.getInstance().getRootUrl() + GhprbRootAction.URL + "/";
}
public GHPullRequest getPullRequest(int id) throws IOException {
return ghRepository.getPullRequest(id);
}
void onIssueCommentHook(IssueComment issueComment) throws IOException {
if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Not checking comments since build is disabled");
return;
}
int id = issueComment.getIssue().getNumber();
logger.log(Level.FINER, "Comment on issue #{0} from {1}: {2}", new Object[] { id, issueComment.getComment().getUser(), issueComment.getComment().getBody() });
if (!"created".equals(issueComment.getAction())) {
return;
}
if (ghRepository == null) {
init();
}
GhprbPullRequest pull = pulls.get(id);
if (pull == null) {
pull = new GhprbPullRequest(ghRepository.getPullRequest(id), helper, this);
pulls.put(id, pull);
}
pull.check(issueComment.getComment());
GhprbTrigger.getDscp().save();
}
void onPullRequestHook(PullRequest pr) {
if ("closed".equals(pr.getAction())) {
pulls.remove(pr.getNumber());
} else if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Not processing Pull request since the build is disabled");
} else if ("opened".equals(pr.getAction()) || "reopened".equals(pr.getAction())) {
GhprbPullRequest pull = pulls.get(pr.getNumber());
if (pull == null) {
pulls.putIfAbsent(pr.getNumber(), new GhprbPullRequest(pr.getPullRequest(), helper, this));
pull = pulls.get(pr.getNumber());
}
pull.check(pr.getPullRequest());
} else if ("synchronize".equals(pr.getAction())) {
GhprbPullRequest pull = pulls.get(pr.getNumber());
if (pull == null) {
pulls.putIfAbsent(pr.getNumber(), new GhprbPullRequest(pr.getPullRequest(), helper, this));
pull = pulls.get(pr.getNumber());
}
if (pull == null) {
logger.log(Level.SEVERE, "Pull Request #{0} doesn''t exist", pr.getNumber());
return;
}
pull.check(pr.getPullRequest());
} else {
logger.log(Level.WARNING, "Unknown Pull Request hook action: {0}", pr.getAction());
}
GhprbTrigger.getDscp().save();
}
@VisibleForTesting
void setHelper(Ghprb helper) {
this.helper = helper;
}
}