Skip to content

Commit 90735c4

Browse files
committed
#469 Delete Editorial bot comments when violations are fixed
1 parent 3c4873e commit 90735c4

8 files changed

+364
-61
lines changed

src/main/java/io/quarkus/bot/CheckIssueEditorialRules.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package io.quarkus.bot;
22

3-
import java.io.IOException;
4-
5-
import jakarta.inject.Inject;
6-
7-
import org.jboss.logging.Logger;
8-
import org.kohsuke.github.GHEventPayload;
9-
import org.kohsuke.github.GHIssue;
10-
113
import io.quarkiverse.githubapp.ConfigFile;
124
import io.quarkiverse.githubapp.event.Issue;
135
import io.quarkus.bot.config.Feature;
146
import io.quarkus.bot.config.QuarkusGitHubBotConfig;
157
import io.quarkus.bot.config.QuarkusGitHubBotConfigFile;
168
import io.quarkus.bot.util.Strings;
9+
import jakarta.inject.Inject;
10+
import org.jboss.logging.Logger;
11+
import org.kohsuke.github.GHEventPayload;
12+
import org.kohsuke.github.GHIssue;
13+
14+
import java.io.IOException;
1715

1816
public class CheckIssueEditorialRules {
1917
private static final Logger LOG = Logger.getLogger(CheckIssueEditorialRules.class);
2018

2119
private static final String ZULIP_URL = "https://quarkusio.zulipchat.com/";
22-
public static final String ZULIP_WARNING = Strings.commentByBot(
20+
public static final String ZULIP_WARNING = Strings.editorialCommentByBot(
2321
"You added a link to a Zulip discussion, please make sure the description of the issue is comprehensive and doesn't require accessing Zulip");
2422

2523
@Inject

src/main/java/io/quarkus/bot/CheckPullRequestEditorialRules.java

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package io.quarkus.bot;
22

3-
import java.io.IOException;
4-
import java.util.ArrayList;
5-
import java.util.Arrays;
6-
import java.util.Collections;
7-
import java.util.List;
8-
import java.util.Locale;
9-
import java.util.regex.Pattern;
10-
11-
import jakarta.inject.Inject;
12-
13-
import org.jboss.logging.Logger;
14-
import org.kohsuke.github.GHEventPayload;
15-
import org.kohsuke.github.GHPullRequest;
16-
173
import io.quarkiverse.githubapp.ConfigFile;
184
import io.quarkiverse.githubapp.event.PullRequest;
195
import io.quarkus.bot.config.Feature;
@@ -22,6 +8,22 @@
228
import io.quarkus.bot.util.GHPullRequests;
239
import io.quarkus.bot.util.PullRequestFilesMatcher;
2410
import io.quarkus.bot.util.Strings;
11+
import jakarta.inject.Inject;
12+
import org.jboss.logging.Logger;
13+
import org.kohsuke.github.GHEventPayload;
14+
import org.kohsuke.github.GHIssueComment;
15+
import org.kohsuke.github.GHPullRequest;
16+
17+
import java.io.IOException;
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.Collections;
21+
import java.util.List;
22+
import java.util.Locale;
23+
import java.util.Optional;
24+
import java.util.regex.Pattern;
25+
26+
import static io.quarkus.bot.util.Strings.EDITORIAL_RULES_COMMENT_MARKER;
2527

2628
class CheckPullRequestEditorialRules {
2729

@@ -80,12 +82,30 @@ void checkPullRequestEditorialRules(@PullRequest.Opened GHEventPayload.PullReque
8082
}
8183

8284
if (!quarkusBotConfig.isDryRun()) {
83-
pullRequest.comment(Strings.commentByBot(comment.toString()));
85+
pullRequest.comment(Strings.editorialCommentByBot(comment.toString()));
8486
} else {
8587
LOG.info("Pull request #" + pullRequest.getNumber() + " - Add comment " + comment.toString());
8688
}
8789
}
8890

91+
void checkPullRequestEditorialRulesOnEdit(@PullRequest.Edited GHEventPayload.PullRequest pullRequestPayload,
92+
@ConfigFile("quarkus-github-bot.yml") QuarkusGitHubBotConfigFile quarkusBotConfigFile) throws IOException {
93+
if (!Feature.CHECK_EDITORIAL_RULES.isEnabled(quarkusBotConfigFile)) {
94+
return;
95+
}
96+
var pullRequest = pullRequestPayload.getPullRequest();
97+
98+
var titleErrorMessages = getTitleErrorMessages(pullRequest.getTitle());
99+
var bodyErrorMessages = getBodyErrorMessages(pullRequest.getBody(), pullRequest);
100+
101+
if (titleErrorMessages.isEmpty() && bodyErrorMessages.isEmpty()) {
102+
removeBotComment(pullRequest);
103+
} else {
104+
LOG.debug("Pull request #" + pullRequest.getNumber() + " - Violations still exist. No action taken");
105+
}
106+
// Note: We could potentially update the comment if some rules are fixed but others remain..
107+
}
108+
89109
private static List<String> getTitleErrorMessages(String title) {
90110
List<String> errorMessages = new ArrayList<>();
91111

@@ -166,4 +186,34 @@ private static boolean isMeaningfulPullRequest(GHPullRequest pullRequest) throws
166186

167187
return true;
168188
}
189+
190+
private Optional<GHIssueComment> findBotComment(GHPullRequest pullRequest) throws IOException {
191+
List<GHIssueComment> comments = pullRequest.getComments();
192+
if (comments == null || comments.isEmpty()) {
193+
return Optional.empty();
194+
}
195+
196+
return comments.stream()
197+
.filter(comment -> comment.getBody() != null)
198+
.filter(comment -> comment.getBody().contains(EDITORIAL_RULES_COMMENT_MARKER))
199+
.findFirst();
200+
}
201+
202+
private void removeBotComment(GHPullRequest pullRequest) throws IOException {
203+
Optional<GHIssueComment> commentToDeleteOpt = findBotComment(pullRequest);
204+
if (commentToDeleteOpt.isPresent()) {
205+
GHIssueComment commentToDelete = commentToDeleteOpt.get();
206+
if (!quarkusBotConfig.isDryRun()) {
207+
try {
208+
commentToDelete.delete();
209+
} catch (IOException e) {
210+
LOG.error(String.format("Pull Request # %s - Failed to delete comment %d", pullRequest.getNumber(),
211+
commentToDelete.getId()), e);
212+
}
213+
} else {
214+
LOG.info(String.format("Pull Request # %s - Delete comment %d", pullRequest.getNumber(),
215+
commentToDelete.getId()));
216+
}
217+
}
218+
}
169219
}

src/main/java/io/quarkus/bot/util/Strings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkus.bot.util;
22

33
public class Strings {
4+
public static final String EDITORIAL_RULES_COMMENT_MARKER = "\n<!-- Quarkus-Bot-Editor-Rules -->";
45

56
public static boolean isNotBlank(String string) {
67
return string != null && !string.trim().isEmpty();
@@ -19,6 +20,10 @@ public static String commentByBot(String originalComment) {
1920
return sb.toString();
2021
}
2122

23+
public static String editorialCommentByBot(String originalComment) {
24+
return commentByBot(originalComment) + EDITORIAL_RULES_COMMENT_MARKER;
25+
}
26+
2227
private Strings() {
2328
}
2429
}

0 commit comments

Comments
 (0)