55 return true;
56 }
57 return false;
58 }
59
60 // Prune durations are on the order of days and weeks
61 private String formatDuration(Duration duration) {
62 var count = duration.toDays();
63 var unit = "day";
64
65 if (count > 14) {
66 count /= 7;
67 unit = "week";
68 }
69 if (count != 1) {
70 unit += "s";
71 }
72 return count + " " + unit;
73 }
74
75 @Override
76 public void run(Path scratchPath) {
77 var message = "@" + pr.author().userName() + " This pull request has been inactive for more than " +
78 formatDuration(maxAge) + " and will be automatically closed. If you think this is incorrect, " +
79 "feel free to reopen it!";
80
81 log.fine("Posting prune message");
82 pr.addComment(message);
83
84 pr.setState(PullRequest.State.CLOSED);
85 }
86 }
87
88 public class PullRequestPrunerBot implements Bot {
89 private final HostedRepository repository;
90 private final Duration maxAge;
91
92 PullRequestPrunerBot(HostedRepository repository, Duration maxAge) {
93 this.repository = repository;
94 this.maxAge = maxAge;
95 }
96
97 @Override
98 public List<WorkItem> getPeriodicItems() {
99 List<WorkItem> ret = new LinkedList<>();
100 var oldestAllowed = ZonedDateTime.now().minus(maxAge);
101
102 for (var pr : repository.pullRequests()) {
103 if (pr.updatedAt().isBefore(oldestAllowed)) {
104 var item = new PullRequestPrunerBotWorkItem(repository, pr, maxAge);
|
55 return true;
56 }
57 return false;
58 }
59
60 // Prune durations are on the order of days and weeks
61 private String formatDuration(Duration duration) {
62 var count = duration.toDays();
63 var unit = "day";
64
65 if (count > 14) {
66 count /= 7;
67 unit = "week";
68 }
69 if (count != 1) {
70 unit += "s";
71 }
72 return count + " " + unit;
73 }
74
75 private final String noticeMarker = "<!-- PullrequestCloserBot auto close notification -->";
76
77 @Override
78 public void run(Path scratchPath) {
79 var comments = pr.comments();
80 if (comments.size() > 0) {
81 var lastComment = comments.get(comments.size() - 1);
82 if (lastComment.author().equals(repository.forge().currentUser()) && lastComment.body().contains(noticeMarker)) {
83 var message = "@" + pr.author().userName() + " This pull request has been inactive for more than " +
84 formatDuration(maxAge.multipliedBy(2)) + " and will now be automatically closed. If you would " +
85 "like to continue working on this pull request in the future, feel free to reopen it!";
86 log.fine("Posting prune message");
87 pr.addComment(message);
88 pr.setState(PullRequest.State.CLOSED);
89 return;
90 }
91 }
92
93 var message = "@" + pr.author().userName() + " This pull request has been inactive for more than " +
94 formatDuration(maxAge) + " and will be automatically closed if another " + formatDuration(maxAge) +
95 " passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free " +
96 "to ask for assistance if you need help with progressing this pull request towards integration!";
97
98 log.fine("Posting prune notification message");
99 pr.addComment(noticeMarker + "\n\n" + message);
100 }
101 }
102
103 public class PullRequestPrunerBot implements Bot {
104 private final HostedRepository repository;
105 private final Duration maxAge;
106
107 PullRequestPrunerBot(HostedRepository repository, Duration maxAge) {
108 this.repository = repository;
109 this.maxAge = maxAge;
110 }
111
112 @Override
113 public List<WorkItem> getPeriodicItems() {
114 List<WorkItem> ret = new LinkedList<>();
115 var oldestAllowed = ZonedDateTime.now().minus(maxAge);
116
117 for (var pr : repository.pullRequests()) {
118 if (pr.updatedAt().isBefore(oldestAllowed)) {
119 var item = new PullRequestPrunerBotWorkItem(repository, pr, maxAge);
|