/
CheckRun.java
842 lines (754 loc) · 38 KB
/
CheckRun.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*
* 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.pr;
import org.openjdk.skara.email.EmailAddress;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.HostUser;
import org.openjdk.skara.issuetracker.Comment;
import org.openjdk.skara.issuetracker.IssueProject;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.vcs.openjdk.Issue;
import java.io.*;
import java.nio.file.Path;
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.*;
class CheckRun {
private final CheckWorkItem workItem;
private final PullRequest pr;
private final Repository localRepo;
private final List<Comment> comments;
private final List<Review> allReviews;
private final List<Review> activeReviews;
private final Set<String> labels;
private final CensusInstance censusInstance;
private final boolean ignoreStaleReviews;
private final Hash baseHash;
private final CheckablePullRequest checkablePullRequest;
private static final Logger log = Logger.getLogger("org.openjdk.skara.bots.pr");
private static final String progressMarker = "<!-- Anything below this marker will be automatically updated, please do not edit manually! -->";
private static final String mergeReadyMarker = "<!-- PullRequestBot merge is ready comment -->";
private static final String outdatedHelpMarker = "<!-- PullRequestBot outdated help comment -->";
private static final String sourceBranchWarningMarker = "<!-- PullRequestBot source branch warning comment -->";
private static final String mergeCommitWarningMarker = "<!-- PullRequestBot merge commit warning comment -->";
private static final String emptyPrBodyMarker = "<!--\nReplace this text with a description of your pull request (also remove the surrounding HTML comment markers).\n" +
"If in doubt, feel free to delete everything in this edit box first, the bot will restore the progress section as needed.\n-->";
private final Set<String> newLabels;
static final Pattern ISSUE_ID_PATTERN = Pattern.compile("^(?:[A-Za-z][A-Za-z0-9]+-)?([0-9]+)$");
private CheckRun(CheckWorkItem workItem, PullRequest pr, Repository localRepo, List<Comment> comments,
List<Review> allReviews, List<Review> activeReviews, Set<String> labels,
CensusInstance censusInstance, boolean ignoreStaleReviews) throws IOException {
this.workItem = workItem;
this.pr = pr;
this.localRepo = localRepo;
this.comments = comments;
this.allReviews = allReviews;
this.activeReviews = activeReviews;
this.labels = new HashSet<>(labels);
this.newLabels = new HashSet<>(labels);
this.censusInstance = censusInstance;
this.ignoreStaleReviews = ignoreStaleReviews;
baseHash = PullRequestUtils.baseHash(pr, localRepo);
checkablePullRequest = new CheckablePullRequest(pr, localRepo, ignoreStaleReviews,
workItem.bot.confOverrideRepository().orElse(null),
workItem.bot.confOverrideName(),
workItem.bot.confOverrideRef());
}
static void execute(CheckWorkItem workItem, PullRequest pr, Repository localRepo, List<Comment> comments,
List<Review> allReviews, List<Review> activeReviews, Set<String> labels, CensusInstance censusInstance,
boolean ignoreStaleReviews) throws IOException {
var run = new CheckRun(workItem, pr, localRepo, comments, allReviews, activeReviews, labels, censusInstance, ignoreStaleReviews);
run.checkStatus();
}
private boolean isTargetBranchAllowed() {
var matcher = workItem.bot.allowedTargetBranches().matcher(pr.targetRef());
return matcher.matches();
}
private Set<String> allowedIssueTypes() {
return workItem.bot.allowedIssueTypes();
}
private List<Issue> issues() {
var issue = Issue.fromStringRelaxed(pr.title());
if (issue.isPresent()) {
var issues = new ArrayList<Issue>();
issues.add(issue.get());
issues.addAll(SolvesTracker.currentSolved(pr.repository().forge().currentUser(), comments));
return issues;
}
return List.of();
}
private IssueProject issueProject() {
return workItem.bot.issueProject();
}
private List<org.openjdk.skara.issuetracker.Issue> issuesOfDisallowedType() {
var issueProject = issueProject();
var allowed = allowedIssueTypes();
if (issueProject != null && allowed != null) {
return issues().stream()
.filter(i -> i.project().equals(Optional.of(issueProject.name())))
.map(i -> issueProject.issue(i.shortId()))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(i -> i.properties().containsKey("issuetype"))
.filter(i -> !allowed.contains(i.properties().get("issuetype").asString()))
.collect(Collectors.toList());
}
return List.of();
}
private List<String> allowedTargetBranches() {
return pr.repository()
.branches()
.stream()
.map(HostedBranch::name)
.map(name -> workItem.bot.allowedTargetBranches().matcher(name))
.filter(Matcher::matches)
.map(Matcher::group)
.collect(Collectors.toList());
}
// For unknown contributors, check that all commits have the same name and email
private boolean checkCommitAuthor(List<CommitMetadata> commits) throws IOException {
var author = censusInstance.namespace().get(pr.author().id());
if (author != null) {
return true;
}
var names = new HashSet<String>();
var emails = new HashSet<String>();
for (var commit : commits) {
names.add(commit.author().name());
emails.add(commit.author().email());
}
return ((names.size() == 1) && emails.size() == 1);
}
// Additional bot-specific checks that are not handled by JCheck
private List<String> botSpecificChecks(Hash finalHash) throws IOException {
var ret = new ArrayList<String>();
if (bodyWithoutStatus().isBlank()) {
var error = "The pull request body must not be empty.";
ret.add(error);
}
if (!isTargetBranchAllowed()) {
var error = "The branch `" + pr.targetRef() + "` is not allowed as target branch. The allowed target branches are:\n" +
allowedTargetBranches().stream()
.map(name -> " - " + name)
.collect(Collectors.joining("\n"));
ret.add(error);
}
var disallowedIssues = issuesOfDisallowedType();
if (!disallowedIssues.isEmpty()) {
var s = disallowedIssues.size() > 1 ? "s " : " ";
var are = disallowedIssues.size() > 1 ? "are" : "is";
var links = disallowedIssues.stream()
.map(i -> "[" + i.id() + "](" + i.webUrl() + ")")
.collect(Collectors.toList());
var error = "The issue" + s + String.join(",", links) + " " + are + " not of the expected type. The allowed issue types are:\n" +
allowedIssueTypes().stream()
.map(name -> " - " + name)
.collect(Collectors.joining("\n"));
ret.add(error);
}
var headHash = pr.headHash();
var originalCommits = localRepo.commitMetadata(baseHash, headHash);
if (!checkCommitAuthor(originalCommits)) {
var error = "For contributors who are not existing OpenJDK Authors, commit attribution will be taken from " +
"the commits in the PR. However, the commits in this PR have inconsistent user names and/or " +
"email addresses. Please amend the commits.";
ret.add(error);
}
for (var blocker : workItem.bot.blockingCheckLabels().entrySet()) {
if (labels.contains(blocker.getKey())) {
ret.add(blocker.getValue());
}
}
return ret;
}
private void updateCheckBuilder(CheckBuilder checkBuilder, PullRequestCheckIssueVisitor visitor, List<String> additionalErrors) {
if (visitor.isReadyForReview() && additionalErrors.isEmpty()) {
checkBuilder.complete(true);
} else {
checkBuilder.title("Required");
var summary = Stream.concat(visitor.messages().stream(), additionalErrors.stream())
.sorted()
.map(m -> "- " + m)
.collect(Collectors.joining("\n"));
checkBuilder.summary(summary);
for (var annotation : visitor.getAnnotations()) {
checkBuilder.annotation(annotation);
}
checkBuilder.complete(false);
}
}
private void updateReadyForReview(PullRequestCheckIssueVisitor visitor, List<String> additionalErrors) {
// Additional errors are not allowed
if (!additionalErrors.isEmpty()) {
newLabels.remove("rfr");
return;
}
// Draft requests are not for review
if (pr.isDraft()) {
newLabels.remove("rfr");
return;
}
// Check if the visitor found any issues that should be resolved before reviewing
if (visitor.isReadyForReview()) {
newLabels.add("rfr");
} else {
newLabels.remove("rfr");
}
}
private String getRole(String username) {
var project = censusInstance.project();
var version = censusInstance.census().version().format();
if (project.isReviewer(username, version)) {
return "**Reviewer**";
} else if (project.isCommitter(username, version)) {
return "Committer";
} else if (project.isAuthor(username, version)) {
return "Author";
} else {
return "no project role";
}
}
private String formatReviewer(HostUser reviewer) {
var namespace = censusInstance.namespace();
var contributor = namespace.get(reviewer.id());
if (contributor == null) {
return reviewer.userName() + " (no known " + namespace.name() + " user name / role)";
} else {
var userNameLink = "[" + contributor.username() + "](@" + reviewer.userName() + ")";
return contributor.fullName().orElse(contributor.username()) + " (" + userNameLink + " - " +
getRole(contributor.username()) + ")";
}
}
private String getChecksList(PullRequestCheckIssueVisitor visitor) {
return visitor.getChecks().entrySet().stream()
.map(entry -> "- [" + (entry.getValue() ? "x" : " ") + "] " + entry.getKey())
.collect(Collectors.joining("\n"));
}
private String getAdditionalErrorsList(List<String> additionalErrors) {
return additionalErrors.stream()
.sorted()
.map(err -> " ⚠️ " + err)
.collect(Collectors.joining("\n"));
}
private Optional<String> getReviewersList(List<Review> reviews) {
var reviewers = reviews.stream()
.filter(review -> review.verdict() == Review.Verdict.APPROVED)
.map(review -> {
var entry = " * " + formatReviewer(review.reviewer());
if (!review.hash().equals(pr.headHash())) {
if (ignoreStaleReviews) {
entry += " 🔄 Re-review required (review applies to " + review.hash() + ")";
} else {
entry += " ⚠️ Review applies to " + review.hash();
}
}
return entry;
})
.collect(Collectors.joining("\n"));
// Check for manually added reviewers
if (!ignoreStaleReviews) {
var namespace = censusInstance.namespace();
var allReviewers = PullRequestUtils.reviewerNames(reviews, namespace);
var additionalEntries = new ArrayList<String>();
for (var additional : Reviewers.reviewers(pr.repository().forge().currentUser(), comments)) {
if (!allReviewers.contains(additional)) {
additionalEntries.add(" * " + additional + " - " + getRole(additional) + " ⚠️ Added manually");
}
}
if (!reviewers.isBlank()) {
reviewers += "\n";
}
reviewers += String.join("\n", additionalEntries);
}
if (reviewers.length() > 0) {
return Optional.of(reviewers);
} else {
return Optional.empty();
}
}
private String formatContributor(EmailAddress contributor) {
var name = contributor.fullName().orElseThrow();
return name + " `<" + contributor.address() + ">`";
}
private Optional<String> getContributorsList(List<Comment> comments) {
var contributors = Contributors.contributors(pr.repository().forge().currentUser(), comments)
.stream()
.map(c -> " * " + formatContributor(c))
.collect(Collectors.joining("\n"));
if (contributors.length() > 0) {
return Optional.of(contributors);
} else {
return Optional.empty();
}
}
private boolean relaxedEquals(String s1, String s2) {
return s1.trim()
.replaceAll("\\s+", " ")
.equalsIgnoreCase(s2.trim()
.replaceAll("\\s+", " "));
}
private String getStatusMessage(List<Comment> comments, List<Review> reviews, PullRequestCheckIssueVisitor visitor, List<String> additionalErrors) {
var progressBody = new StringBuilder();
progressBody.append("---------\n");
progressBody.append("### Progress\n");
progressBody.append(getChecksList(visitor));
var allAdditionalErrors = Stream.concat(visitor.hiddenMessages().stream(), additionalErrors.stream())
.sorted()
.collect(Collectors.toList());
if (!allAdditionalErrors.isEmpty()) {
progressBody.append("\n\n### Error");
if (allAdditionalErrors.size() > 1) {
progressBody.append("s");
}
progressBody.append("\n");
progressBody.append(getAdditionalErrorsList(allAdditionalErrors));
}
var issues = issues();
var issueProject = issueProject();
if (issueProject != null && !issues.isEmpty()) {
progressBody.append("\n\n### Issue");
if (issues.size() > 1) {
progressBody.append("s");
}
progressBody.append("\n");
for (var currentIssue : issues) {
progressBody.append(" * ");
if (currentIssue.project().isPresent() && !currentIssue.project().get().equals(issueProject.name())) {
progressBody.append("⚠️ Issue `");
progressBody.append(currentIssue.id());
progressBody.append("` does not belong to the `");
progressBody.append(issueProject.name());
progressBody.append("` project.");
} else {
try {
var iss = issueProject.issue(currentIssue.shortId());
if (iss.isPresent()) {
progressBody.append("[");
progressBody.append(iss.get().id());
progressBody.append("](");
progressBody.append(iss.get().webUrl());
progressBody.append("): ");
progressBody.append(iss.get().title());
if (!relaxedEquals(iss.get().title(), currentIssue.description())) {
progressBody.append(" ⚠️ Title mismatch between PR and JBS.");
}
progressBody.append("\n");
} else {
progressBody.append("⚠️ Failed to retrieve information on issue `");
progressBody.append(currentIssue.id());
progressBody.append("`.\n");
}
} catch (RuntimeException e) {
progressBody.append("⚠️ Temporary failure when trying to retrieve information on issue `");
progressBody.append(currentIssue.id());
progressBody.append("`.\n");
}
}
}
}
getReviewersList(reviews).ifPresent(reviewers -> {
progressBody.append("\n\n### Reviewers\n");
progressBody.append(reviewers);
});
getContributorsList(comments).ifPresent(contributors -> {
progressBody.append("\n\n### Contributors\n");
progressBody.append(contributors);
});
progressBody.append("\n\n### Download\n");
progressBody.append(checkoutCommands());
return progressBody.toString();
}
private String checkoutCommands() {
var repoUrl = pr.repository().webUrl();
return
"`$ git fetch " + repoUrl + " " + pr.fetchRef() + ":pull/" + pr.id() + "`\n" +
"`$ git checkout pull/" + pr.id() + "`\n";
}
private String bodyWithoutStatus() {
var description = pr.body();
var markerIndex = description.lastIndexOf(progressMarker);
return (markerIndex < 0 ?
description :
description.substring(0, markerIndex)).trim();
}
private String updateStatusMessage(String message) {
var description = pr.body();
var markerIndex = description.lastIndexOf(progressMarker);
if (markerIndex >= 0 && description.substring(markerIndex).equals(message)) {
log.info("Progress already up to date");
return description;
}
var originalBody = bodyWithoutStatus();
if (originalBody.isBlank()) {
originalBody = emptyPrBodyMarker;
}
var newBody = originalBody + "\n" + progressMarker + "\n" + message;
// TODO? Retrieve the body again here to lower the chance of concurrent updates
pr.setBody(newBody);
return newBody;
}
private String updateTitle() {
var title = pr.title();
var m = ISSUE_ID_PATTERN.matcher(title);
var project = issueProject();
var newTitle = title;
if (m.matches() && project != null) {
var id = m.group(1);
var issue = project.issue(id);
if (issue.isPresent()) {
newTitle = id + ": " + issue.get().title();
}
}
if (!title.equals(newTitle)) {
pr.setTitle(newTitle);
}
return newTitle;
}
private String verdictToString(Review.Verdict verdict) {
switch (verdict) {
case APPROVED:
return "changes are approved";
case DISAPPROVED:
return "more changes needed";
case NONE:
return "comment added";
default:
throw new RuntimeException("Unknown verdict: " + verdict);
}
}
private void updateReviewedMessages(List<Comment> comments, List<Review> reviews) {
var reviewTracker = new ReviewTracker(comments, reviews);
for (var added : reviewTracker.newReviews().entrySet()) {
var body = added.getValue() + "\n" +
"This PR has been reviewed by " +
formatReviewer(added.getKey().reviewer()) + " - " +
verdictToString(added.getKey().verdict()) + ".";
pr.addComment(body);
}
}
private Optional<Comment> findComment(List<Comment> comments, String marker) {
var self = pr.repository().forge().currentUser();
return comments.stream()
.filter(comment -> comment.author().equals(self))
.filter(comment -> comment.body().contains(marker))
.findAny();
}
private String getMergeReadyComment(String commitMessage, List<Review> reviews) {
var message = new StringBuilder();
message.append("@");
message.append(pr.author().userName());
message.append(" This change now passes all automated pre-integration checks");
try {
var hasContributingFile =
!localRepo.files(pr.targetHash(), Path.of("CONTRIBUTING.md")).isEmpty();
if (hasContributingFile) {
message.append(". When the change also fulfills all ");
message.append("[project specific requirements](https://github.com/");
message.append(pr.repository().name());
message.append("/blob/");
message.append(pr.targetRef());
message.append("/CONTRIBUTING.md)");
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
message.append(", type `/integrate` in a new comment to proceed. After integration, ");
message.append("the commit message will be:\n");
message.append("```\n");
message.append(commitMessage);
message.append("\n```\n");
message.append("- If you would like to add a summary, use the `/summary` command.\n");
message.append("- To credit additional contributors, use the `/contributor` command.\n");
message.append("- To add additional solved issues, use the `/issue` command.\n");
var divergingCommits = checkablePullRequest.divergingCommits();
if (divergingCommits.size() > 0) {
message.append("\n");
message.append("Since the source branch of this PR was last updated there ");
if (divergingCommits.size() == 1) {
message.append("has been 1 commit ");
} else {
message.append("have been ");
message.append(divergingCommits.size());
message.append(" commits ");
}
message.append("pushed to the `");
message.append(pr.targetRef());
message.append("` branch:\n\n");
divergingCommits.stream()
.limit(10)
.forEach(c -> message.append(" * ").append(c.hash().hex()).append(": ").append(c.message().get(0)).append("\n"));
if (divergingCommits.size() > 10) {
message.append(" * ... and ").append(divergingCommits.size() - 10).append(" more: ")
.append(pr.repository().webUrl(baseHash.hex(), pr.targetRef())).append("\n");
}
message.append("\n");
message.append("As there are no conflicts, your changes will automatically be rebased on top of ");
message.append("these commits when integrating. If you prefer to avoid automatic rebasing, please merge `");
message.append(pr.targetRef());
message.append("` into your branch, and then specify the current head hash when integrating, like this: ");
message.append("`/integrate ");
message.append(pr.targetHash());
message.append("`.\n");
} else {
message.append("\n");
message.append("There are currently no new commits on the `");
message.append(pr.targetRef());
message.append("` branch since the last update of the source branch of this PR. If another commit should be pushed before ");
message.append("you perform the `/integrate` command, your PR will be automatically rebased. If you would like to avoid ");
message.append("potential automatic rebasing, specify the current head hash when integrating, like this: ");
message.append("`/integrate ");
message.append(pr.targetHash());
message.append("`.\n");
}
if (!censusInstance.isCommitter(pr.author())) {
message.append("\n");
var contributor = censusInstance.namespace().get(pr.author().id());
if (contributor == null) {
message.append("As you are not a known OpenJDK [Author](https://openjdk.java.net/bylaws#author), ");
} else {
message.append("As you do not have Committer status in this project, ");
}
message.append("an existing [Committer](https://openjdk.java.net/bylaws#committer) must agree to ");
message.append("[sponsor](https://openjdk.java.net/sponsor/) your change. ");
var candidates = reviews.stream()
.filter(review -> censusInstance.isCommitter(review.reviewer()))
.map(review -> "@" + review.reviewer().userName())
.collect(Collectors.joining(", "));
if (candidates.length() > 0) {
message.append("Possible candidates are the reviewers of this PR (");
message.append(candidates);
message.append(") but any other Committer may sponsor as well. ");
}
message.append("\n\n");
message.append("➡️ To flag this PR as ready for integration with the above commit message, type ");
message.append("`/integrate` in a new comment. (Afterwards, your sponsor types ");
message.append("`/sponsor` in a new comment to perform the integration).\n");
} else {
message.append("\n");
message.append("➡️ To integrate this PR with the above commit message to the `" + pr.targetRef() + "` branch, type ");
message.append("`/integrate` in a new comment.\n");
}
message.append(mergeReadyMarker);
return message.toString();
}
private String getMergeNoLongerReadyComment() {
var message = new StringBuilder();
message.append("@");
message.append(pr.author().userName());
message.append(" This change is no longer ready for integration - check the PR body for details.\n");
message.append(mergeReadyMarker);
return message.toString();
}
private void updateMergeReadyComment(boolean isReady, String commitMessage, List<Comment> comments, List<Review> reviews, boolean rebasePossible) {
var existing = findComment(comments, mergeReadyMarker);
if (isReady && rebasePossible) {
var message = getMergeReadyComment(commitMessage, reviews);
if (existing.isEmpty()) {
pr.addComment(message);
} else {
pr.updateComment(existing.get().id(), message);
}
} else {
existing.ifPresent(comment -> pr.updateComment(comment.id(), getMergeNoLongerReadyComment()));
}
}
private void addSourceBranchWarningComment(List<Comment> comments) throws IOException {
var existing = findComment(comments, sourceBranchWarningMarker);
if (existing.isPresent()) {
// Only add the comment once per PR
return;
}
var branch = pr.sourceRef();
var message = ":warning: @" + pr.author().userName() + " " +
"a branch with the same name as the source branch for this pull request (`" + branch + "`) " +
"is present in the [target repository](" + pr.repository().nonTransformedWebUrl() + "). " +
"If you eventually integrate this pull request then the branch `" + branch + "` " +
"in your [personal fork](" + pr.sourceRepository().get().nonTransformedWebUrl() + ") will diverge once you sync " +
"your personal fork with the upstream repository.\n" +
"\n" +
"To avoid this situation, create a new branch for your changes and reset the `" + branch + "` branch. " +
"You can do this by running the following commands in a local repository for your personal fork. " +
"_Note_: you do *not* have to name the new branch `NEW-BRANCH-NAME`." +
"\n" +
"```" +
"$ git checkout " + branch + "\n" +
"$ git checkout -b NEW-BRANCH-NAME\n" +
"$ git branch -f " + branch + " " + baseHash.hex() + "\n" +
"$ git push -f origin " + branch + "\n" +
"```\n" +
"\n" +
"Then proceed to create a new pull request with `NEW-BRANCH-NAME` as the source branch and " +
"close this one.\n" +
sourceBranchWarningMarker;
pr.addComment(message);
}
private void addOutdatedComment(List<Comment> comments) {
var existing = findComment(comments, outdatedHelpMarker);
if (existing.isPresent()) {
// Only add the comment once per PR
return;
}
var message = "@" + pr.author().userName() + " this pull request can not be integrated into " +
"`" + pr.targetRef() + "` due to one or more merge conflicts. To resolve these merge conflicts " +
"and update this pull request you can run the following commands in the local repository for your personal fork:\n" +
"```bash\n" +
"git checkout " + pr.sourceRef() + "\n" +
"git fetch " + pr.repository().webUrl() + " " + pr.targetRef() + "\n" +
"git merge FETCH_HEAD\n" +
"# resolve conflicts and follow the instructions given by git merge\n" +
"git commit -m \"Merge " + pr.targetRef() + "\"\n" +
"git push\n" +
"```\n" +
outdatedHelpMarker;
pr.addComment(message);
}
private void addMergeCommitWarningComment(List<Comment> comments) {
var existing = findComment(comments, mergeCommitWarningMarker);
if (existing.isPresent()) {
// Only add the comment once per PR
return;
}
var message = "⚠️ @" + pr.author().userName() +
" This pull request contains merges that bring in commits not present in the target repository." +
" Since this is not a \"merge style\" pull request, these changes will be squashed when this pull request in integrated." +
" If this is your intention, then please ignore this message. If you want to preserve the commit structure, you must change" +
" the title of this pull request to `Merge <project>:<branch>` where `<project>` is the name of another project in the" +
" [OpenJDK organization](https://github.com/openjdk) (for example `Merge jdk:master`).\n" +
mergeCommitWarningMarker;
pr.addComment(message);
}
private void checkStatus() {
var checkBuilder = CheckBuilder.create("jcheck", pr.headHash());
var censusDomain = censusInstance.configuration().census().domain();
Exception checkException = null;
try {
// Post check in-progress
log.info("Starting to run jcheck on PR head");
pr.createCheck(checkBuilder.build());
var ignored = new PrintWriter(new StringWriter());
var rebasePossible = true;
var commitHash = pr.headHash();
var mergedHash = checkablePullRequest.mergeTarget(ignored);
if (mergedHash.isPresent()) {
commitHash = mergedHash.get();
} else {
rebasePossible = false;
}
List<String> additionalErrors = List.of();
Hash localHash;
try {
localHash = checkablePullRequest.commit(commitHash, censusInstance.namespace(), censusDomain, null);
} catch (CommitFailure e) {
additionalErrors = List.of(e.getMessage());
localHash = baseHash;
}
PullRequestCheckIssueVisitor visitor = checkablePullRequest.createVisitor(localHash, censusInstance);
if (!localHash.equals(baseHash)) {
// Determine current status
var additionalConfiguration = AdditionalConfiguration.get(localRepo, localHash, pr.repository().forge().currentUser(), comments);
checkablePullRequest.executeChecks(localHash, censusInstance, visitor, additionalConfiguration);
additionalErrors = botSpecificChecks(localHash);
} else {
if (additionalErrors.isEmpty()) {
additionalErrors = List.of("This PR contains no changes");
}
}
updateCheckBuilder(checkBuilder, visitor, additionalErrors);
updateReadyForReview(visitor, additionalErrors);
// Calculate and update the status message if needed
var statusMessage = getStatusMessage(comments, activeReviews, visitor, additionalErrors);
var updatedBody = updateStatusMessage(statusMessage);
var title = updateTitle();
// Post / update approval messages (only needed if the review itself can't contain a body)
if (!pr.repository().forge().supportsReviewBody()) {
updateReviewedMessages(comments, allReviews);
}
var commit = localRepo.lookup(localHash).orElseThrow();
var commitMessage = String.join("\n", commit.message());
var readyForIntegration = visitor.messages().isEmpty() && additionalErrors.isEmpty();
updateMergeReadyComment(readyForIntegration, commitMessage, comments, activeReviews, rebasePossible);
if (readyForIntegration && rebasePossible) {
newLabels.add("ready");
} else {
newLabels.remove("ready");
}
if (!rebasePossible) {
if (!labels.contains("failed-auto-merge")) {
addOutdatedComment(comments);
}
newLabels.add("merge-conflict");
} else {
newLabels.remove("merge-conflict");
}
if (pr.sourceRepository().isPresent()) {
var branchNames = pr.repository().branches().stream().map(HostedBranch::name).collect(Collectors.toSet());
if (!pr.repository().url().equals(pr.sourceRepository().get().url()) && branchNames.contains(pr.sourceRef())) {
addSourceBranchWarningComment(comments);
}
}
if (!PullRequestUtils.isMerge(pr) && PullRequestUtils.containsForeignMerge(pr, localRepo)) {
addMergeCommitWarningComment(comments);
}
// Ensure that the ready for sponsor label is up to date
newLabels.remove("sponsor");
var readyHash = ReadyForSponsorTracker.latestReadyForSponsor(pr.repository().forge().currentUser(), comments);
if (readyHash.isPresent() && readyForIntegration) {
var acceptedHash = readyHash.get();
if (pr.headHash().equals(acceptedHash)) {
newLabels.add("sponsor");
}
}
// Calculate current metadata to avoid unnecessary future checks
var metadata = workItem.getMetadata(title, updatedBody, pr.comments(), activeReviews, newLabels,
censusInstance, pr.targetHash(), pr.isDraft());
checkBuilder.metadata(metadata);
} catch (Exception e) {
log.throwing("CommitChecker", "checkStatus", e);
newLabels.remove("ready");
checkBuilder.metadata("invalid");
checkBuilder.title("Exception occurred during jcheck - the operation will be retried");
checkBuilder.summary(e.getMessage());
checkBuilder.complete(false);
checkException = e;
}
var check = checkBuilder.build();
pr.updateCheck(check);
// Synchronize the wanted set of labels
for (var newLabel : newLabels) {
if (!labels.contains(newLabel)) {
pr.addLabel(newLabel);
}
}
for (var oldLabel : labels) {
if (!newLabels.contains(oldLabel)) {
pr.removeLabel(oldLabel);
}
}
// After updating the PR, rethrow any exception to automatically retry on transient errors
if (checkException != null) {
throw new RuntimeException("Exception during jcheck", checkException);
}
}
}