This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
RedhatPullRequest.java
354 lines (296 loc) · 12.3 KB
/
RedhatPullRequest.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.pull.shared.connectors;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.egit.github.core.Comment;
import org.eclipse.egit.github.core.Label;
import org.eclipse.egit.github.core.Milestone;
import org.eclipse.egit.github.core.PullRequest;
import org.eclipse.egit.github.core.User;
import org.jboss.pull.shared.BuildResult;
import org.jboss.pull.shared.Constants;
import org.jboss.pull.shared.connectors.bugzilla.BZHelper;
import org.jboss.pull.shared.connectors.bugzilla.Bug;
import org.jboss.pull.shared.connectors.common.Issue;
import org.jboss.pull.shared.connectors.github.GithubHelper;
import org.jboss.pull.shared.connectors.jira.JiraHelper;
import org.jboss.pull.shared.connectors.jira.JiraIssue;
public class RedhatPullRequest {
private PullRequest pullRequest;
private List<Issue> bugs = null;
private List<Issue> jiraIssues = null;
private List<RedhatPullRequest> relatedPullRequests = null;
private IssueHelper bzHelper;
private IssueHelper jiraHelper;
private GithubHelper ghHelper;
public RedhatPullRequest(PullRequest pullRequest, IssueHelper bzHelper, IssueHelper jiraHelper, GithubHelper ghHelper) {
this.pullRequest = pullRequest;
if (bzHelper instanceof BZHelper && jiraHelper instanceof JiraHelper) {
this.bzHelper = bzHelper;
this.jiraHelper = jiraHelper;
} else {
throw new IllegalArgumentException("The first IssueHelper parameter has to be an instance of BZHelper and"
+ " the second IssueHelper parameter must be an instance of JiraHelper.");
}
this.ghHelper = ghHelper;
this.bugs = getBugsFromDescription();
this.jiraIssues = getJiraIssuesFromDescription();
// Can't call getPRFromDescription here. If two PR's reference each other a loop occurs.
}
/**
* Returns true if BZ link is in the PR description. Does not verify BZ exists.
*
* @return
*/
public boolean hasBZLinkInDescription() {
return extractURLs(Constants.BUGZILLA_BASE_ID, Constants.BUGZILLA_ID_PATTERN).size() > 0;
}
private List<Issue> getBugsFromDescription() {
final List<URL> urls = extractURLs(Constants.BUGZILLA_BASE_ID, Constants.BUGZILLA_ID_PATTERN);
final ArrayList<Issue> bugs = new ArrayList<Issue>();
for (URL url : urls) {
if (bzHelper.accepts(url)) {
Bug bug = (Bug) bzHelper.findIssue(url);
if (bug != null) {
bugs.add(bug);
}
}
}
return bugs;
}
/**
* Returns true if JIRA link is in the PR description. Does not verify JIRA exists.
*
* @return
*/
public boolean hasJiraLinkInDescription() {
return extractURLs(Constants.JIRA_BASE_BROWSE, Constants.RELATED_JIRA_PATTERN).size() > 0;
}
private List<Issue> getJiraIssuesFromDescription() {
final List<URL> urls = extractURLs(Constants.JIRA_BASE_BROWSE, Constants.RELATED_JIRA_PATTERN);
final List<Issue> jiraIssues = new ArrayList<Issue>();
for (URL url : urls) {
if (jiraHelper.accepts(url)) {
JiraIssue jiraIssue = (JiraIssue) jiraHelper.findIssue(url);
if (jiraIssue != null) {
jiraIssues.add(jiraIssue);
}
}
}
return jiraIssues;
}
private List<URL> extractURLs(String urlBase, Pattern toMatch) {
final List<URL> urls = new ArrayList<URL>();
final Matcher matcher = toMatch.matcher(pullRequest.getBody());
while (matcher.find()) {
try {
urls.add(new URL(urlBase + matcher.group(1)));
} catch (NumberFormatException ignore) {
System.err.printf("Invalid bug number: %s.\n", ignore);
} catch (MalformedURLException malformed) {
System.err.printf("Invalid URL formed: %s. \n", malformed);
}
}
return urls;
}
public boolean hasBugLinkInDescription() {
return (hasBZLinkInDescription() || hasJiraLinkInDescription());
}
/**
* Returns a merged list of both Bugzilla and Jira Issues found in the body of a Pull Request.
*
* @return
*/
public List<Issue> getIssues() {
List<Issue> toReturn = new ArrayList<Issue>(bugs.size() + jiraIssues.size());
toReturn.addAll(bugs);
toReturn.addAll(jiraIssues);
return toReturn;
}
/**
* Returns true if PR link is in the description
*
* @return
*/
public boolean hasRelatedPullRequestInDescription() {
boolean retVal = false;
if (Constants.RELATED_PR_PATTERN.matcher(getGithubDescription()).find()) {
retVal = true;
}
if (Constants.ABBREVIATED_RELATED_PR_PATTERN.matcher(getGithubDescription()).find()) {
retVal = true;
}
if (Constants.COMMIT_RELATED_PR_PATTERN.matcher(getGithubDescription()).find()) {
retVal = true;
}
return retVal;
}
public List<RedhatPullRequest> getRelatedPullRequests() {
if (relatedPullRequests != null) {
return relatedPullRequests;
} else {
return relatedPullRequests = getPRFromDescription();
}
}
public boolean isUpstreamRequired() {
return !Constants.UPSTREAM_NOT_REQUIRED.matcher(pullRequest.getBody()).find();
}
private List<RedhatPullRequest> getPRFromDescription() {
Matcher matcher = Constants.RELATED_PR_PATTERN.matcher(getGithubDescription());
List<RedhatPullRequest> relatedPullRequests = new ArrayList<RedhatPullRequest>();
while (matcher.find()) {
PullRequest relatedPullRequest = ghHelper.getPullRequest(matcher.group(1), matcher.group(2),
Integer.valueOf(matcher.group(3)));
if (relatedPullRequest != null) {
relatedPullRequests.add(new RedhatPullRequest(relatedPullRequest, bzHelper, jiraHelper, ghHelper));
}
}
Matcher abbreviatedMatcher = Constants.ABBREVIATED_RELATED_PR_PATTERN.matcher(getGithubDescription());
while (abbreviatedMatcher.find()) {
String match = abbreviatedMatcher.group();
System.out.println("Match: " + match);
Matcher abbreviatedExternalMatcher = Constants.ABBREVIATED_RELATED_PR_PATTERN_EXTERNAL_REPO.matcher(match);
if (abbreviatedExternalMatcher.find()) {
System.out.println("Attempting External Match: " + match);
PullRequest relatedPullRequest = ghHelper.getPullRequest(abbreviatedExternalMatcher.group(1),
abbreviatedExternalMatcher.group(2), Integer.valueOf(abbreviatedExternalMatcher.group(3)));
if (relatedPullRequest != null) {
System.out.println("External Match Found: " + match);
relatedPullRequests.add(new RedhatPullRequest(relatedPullRequest, bzHelper, jiraHelper, ghHelper));
continue;
}
}
System.out.println("Attempting Internal Match: " + match);
PullRequest relatedPullRequest = ghHelper.getPullRequest(getOrganization(), getRepository(),
Integer.valueOf(abbreviatedMatcher.group(2)));
if (relatedPullRequest != null) {
System.out.println("Internal Match Found: " + match);
relatedPullRequests.add(new RedhatPullRequest(relatedPullRequest, bzHelper, jiraHelper, ghHelper));
}
}
return relatedPullRequests;
}
public int getNumber() {
return pullRequest.getNumber();
}
public void postGithubComment(String comment) {
ghHelper.postGithubComment(pullRequest, comment);
}
public Milestone getMilestone() {
return pullRequest.getMilestone();
}
public void setMilestone(Milestone milestone) {
org.eclipse.egit.github.core.Issue issue = ghHelper.getIssue(pullRequest);
issue.setMilestone(milestone);
ghHelper.editIssue(issue);
}
public String getTargetBranchTitle() {
return pullRequest.getBase().getRef();
}
public String getSourceBranchSha() {
return pullRequest.getHead().getSha();
}
public User getGithubUser() {
return pullRequest.getUser();
}
public List<Comment> getGithubComments() {
return ghHelper.getComments(pullRequest);
}
public void postGithubStatus(String targetUrl, String status) {
ghHelper.postGithubStatus(pullRequest, targetUrl, status);
}
public String getGithubDescription() {
return pullRequest.getBody();
}
public Date getGithubUpdatedAt() {
return pullRequest.getUpdatedAt();
}
public Comment getLastMatchingGithubComment(Pattern pattern) {
return ghHelper.getLastMatchingComment(pullRequest, pattern);
}
public String getState() {
return pullRequest.getState();
}
public String getHtmlUrl() {
return pullRequest.getHtmlUrl();
}
public boolean isMerged() {
return ghHelper.isMerged(pullRequest);
}
public BuildResult getBuildResult() {
BuildResult buildResult = BuildResult.UNKNOWN;
Comment comment = ghHelper.getLastMatchingComment(pullRequest, Constants.BUILD_OUTCOME);
if (comment != null) {
Matcher matcher = Constants.BUILD_OUTCOME.matcher(comment.getBody());
while (matcher.find()) {
buildResult = BuildResult.valueOf(matcher.group(2));
}
}
return buildResult;
}
public String getOrganization() {
Matcher matcher = Constants.RELATED_PR_PATTERN.matcher(pullRequest.getUrl());
if (matcher.matches()) {
return matcher.group(1);
}
return null;
}
public String getRepository() {
Matcher matcher = Constants.RELATED_PR_PATTERN.matcher(pullRequest.getUrl());
if (matcher.matches()) {
return matcher.group(2);
}
return null;
}
public boolean updateStatus(Issue issue, Enum status) throws IllegalArgumentException {
if (issue instanceof Bug) {
// Do BZ stuff
if (bzHelper.accepts(issue.getUrl()))
return bzHelper.updateStatus(issue.getUrl(), status);
} else if (issue instanceof JiraIssue) {
// Do Jira stuff
if (jiraHelper.accepts(issue.getUrl()))
return jiraHelper.updateStatus(issue.getUrl(), status);
} else {
throw new IllegalArgumentException("Your issue implementation has to be an instance of a Bug or " + "JiraIssue");
}
return false;
}
public boolean isGithubMilestoneNullOrDefault() {
return (pullRequest.getMilestone() == null || pullRequest.getMilestone().getTitle().contains("x"));
}
public List<Label> getGithubLabels() {
return ghHelper.getLabels(pullRequest);
}
public void addLabel(Label newLabel) {
ghHelper.addLabel(pullRequest, newLabel);
}
public void removeLabel(Label newLabel) {
ghHelper.removeLabel(pullRequest, newLabel);
}
}