/
IssueNotifier.java
262 lines (236 loc) · 11.3 KB
/
IssueNotifier.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
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bots.notify.issue;
import org.openjdk.skara.bots.notify.*;
import org.openjdk.skara.email.EmailAddress;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.issuetracker.*;
import org.openjdk.skara.jcheck.JCheckConfiguration;
import org.openjdk.skara.json.JSON;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.vcs.openjdk.CommitMessageParsers;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.*;
import java.util.logging.Logger;
class IssueNotifier implements Notifier, PullRequestListener, RepositoryListener {
private final IssueProject issueProject;
private final boolean reviewLink;
private final URI reviewIcon;
private final boolean commitLink;
private final URI commitIcon;
private final boolean setFixVersion;
private final Map<String, String> fixVersions;
private final JbsBackport jbsBackport;
private final boolean prOnly;
private final Logger log = Logger.getLogger("org.openjdk.skara.bots.notify");
IssueNotifier(IssueProject issueProject, boolean reviewLink, URI reviewIcon, boolean commitLink, URI commitIcon,
boolean setFixVersion, Map<String, String> fixVersions, JbsBackport jbsBackport, boolean prOnly) {
this.issueProject = issueProject;
this.reviewLink = reviewLink;
this.reviewIcon = reviewIcon;
this.commitLink = commitLink;
this.commitIcon = commitIcon;
this.setFixVersion = setFixVersion;
this.fixVersions = fixVersions;
this.jbsBackport = jbsBackport;
this.prOnly = prOnly;
}
static IssueNotifierBuilder newBuilder() {
return new IssueNotifierBuilder();
}
private Optional<String> findIssueUsername(Commit commit) {
return findIssueUsername(new CommitMetadata(commit.hash(), commit.parents(), commit.author(),
commit.authored(), commit.committer(), commit.committed(),
commit.message()));
}
private Optional<String> findIssueUsername(CommitMetadata commit) {
var authorEmail = EmailAddress.from(commit.author().email());
if (authorEmail.domain().equals("openjdk.org")) {
return Optional.of(authorEmail.localPart());
}
var committerEmail = EmailAddress.from(commit.committer().email());
if (!committerEmail.domain().equals("openjdk.org")) {
log.severe("Cannot determine issue tracker user name from committer email: " + committerEmail);
return Optional.empty();
}
return Optional.of(committerEmail.localPart());
}
@Override
public void attachTo(Emitter e) {
e.registerPullRequestListener(this);
if (!prOnly) {
e.registerRepositoryListener(this);
}
}
@Override
public void onIntegratedPullRequest(PullRequest pr, Hash hash) {
var repository = pr.repository();
var commit = repository.commitMetadata(hash).orElseThrow(() ->
new IllegalStateException("Integrated commit " + hash +
" not present in repository " + repository.webUrl())
);
var commitMessage = CommitMessageParsers.v1.parse(commit);
for (var commitIssue : commitMessage.issues()) {
var optionalIssue = issueProject.issue(commitIssue.shortId());
if (optionalIssue.isEmpty()) {
log.severe("Cannot update issue " + commitIssue.id() + " with commit " + commit.hash().abbreviate()
+ " - issue not found in issue project");
continue;
}
var issue = optionalIssue.get();
if (commitLink) {
var linkBuilder = Link.create(repository.webUrl(hash), "Commit")
.summary(repository.name() + "/" + hash.abbreviate());
if (commitIcon != null) {
linkBuilder.iconTitle("Commit");
linkBuilder.iconUrl(commitIcon);
}
issue.addLink(linkBuilder.build());
}
// If prOnly is false, this is instead done when processing commits
if (prOnly) {
if (issue.state() == Issue.State.OPEN) {
issue.setState(Issue.State.RESOLVED);
if (issue.assignees().isEmpty()) {
var username = findIssueUsername(commit);
if (username.isPresent()) {
var assignee = issueProject.issueTracker().user(username.get());
assignee.ifPresent(hostUser -> issue.setAssignees(List.of(hostUser)));
}
}
}
}
}
}
@Override
public void onNewIssue(PullRequest pr, org.openjdk.skara.vcs.openjdk.Issue issue) {
var realIssue = issueProject.issue(issue.shortId());
if (realIssue.isEmpty()) {
log.warning("Pull request " + pr + " added unknown issue: " + issue.id());
return;
}
if (reviewLink) {
var linkBuilder = Link.create(pr.webUrl(), "Review")
.summary(pr.repository().name() + "/" + pr.id());
if (reviewIcon != null) {
linkBuilder.iconTitle("Review");
linkBuilder.iconUrl(reviewIcon);
}
realIssue.get().addLink(linkBuilder.build());
}
}
@Override
public void onRemovedIssue(PullRequest pr, org.openjdk.skara.vcs.openjdk.Issue issue) {
var realIssue = issueProject.issue(issue.shortId());
if (realIssue.isEmpty()) {
log.warning("Pull request " + pr + " removed unknown issue: " + issue.id());
return;
}
var link = Link.create(pr.webUrl(), "").build();
realIssue.get().removeLink(link);
}
@Override
public void onNewCommits(HostedRepository repository, Repository localRepository, List<Commit> commits, Branch branch) {
for (var commit : commits) {
var commitNotification = CommitFormatters.toTextBrief(repository, commit);
var commitMessage = CommitMessageParsers.v1.parse(commit);
var username = findIssueUsername(commit);
for (var commitIssue : commitMessage.issues()) {
var optionalIssue = issueProject.issue(commitIssue.shortId());
if (optionalIssue.isEmpty()) {
log.severe("Cannot update issue " + commitIssue.id() + " with commit " + commit.hash().abbreviate()
+ " - issue not found in issue project");
continue;
}
var issue = optionalIssue.get();
var mainIssue = Backports.findMainIssue(issue);
if (mainIssue.isEmpty()) {
log.severe("Issue " + issue.id() + " is not the main issue - bot no corresponding main issue found");
continue;
} else {
if (!mainIssue.get().id().equals(issue.id())) {
log.warning("Issue " + issue.id() + " is not the main issue - using " + mainIssue.get().id() + " instead");;
issue = mainIssue.get();
}
}
String requestedVersion = null;
// The actual issue to be updated can change depending on the fix version
if (setFixVersion) {
requestedVersion = fixVersions != null ? fixVersions.getOrDefault(branch.name(), null) : null;
if (requestedVersion == null) {
try {
var conf = localRepository.lines(Path.of(".jcheck/conf"), commit.hash());
if (conf.isPresent()) {
var parsed = JCheckConfiguration.parse(conf.get());
var version = parsed.general().version();
requestedVersion = version.orElse(null);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (requestedVersion != null) {
var fixVersion = JdkVersion.parse(requestedVersion);
var existing = Backports.findIssue(issue, fixVersion);
if (existing.isEmpty()) {
issue = jbsBackport.createBackport(issue, requestedVersion, username.orElse(null));
} else {
issue = existing.get();
}
}
}
var existingComments = issue.comments();
var hashUrl = repository.webUrl(commit.hash()).toString();
var alreadyPostedComment = existingComments.stream()
.filter(comment -> comment.author().equals(issueProject.issueTracker().currentUser()))
.anyMatch(comment -> comment.body().contains(hashUrl));
if (!alreadyPostedComment) {
issue.addComment(commitNotification);
}
if (issue.state() == Issue.State.OPEN) {
issue.setState(Issue.State.RESOLVED);
if (issue.assignees().isEmpty()) {
if (username.isPresent()) {
var assignee = issueProject.issueTracker().user(username.get());
if (assignee.isPresent()) {
issue.setAssignees(List.of(assignee.get()));
}
}
}
}
if (setFixVersion) {
if (requestedVersion != null) {
issue.setProperty("fixVersions", JSON.of(requestedVersion));
Backports.labelReleaseStreamDuplicates(issue, "hgupdate-sync");
}
}
}
}
}
@Override
public String name() {
return "issue";
}
}