Enhance ChatGPT user mention detection
Refined the process for identifying mentions of ChatGPT in comments by
ensuring a whole-word match for either the ChatGPT Gerrit username or
its Gerrit user email, provided the email follows the format
'gerritUserName@<any_email_domain>'.
Jira-Id: IT-103
Change-Id: I2a46c863c7beccaedda937e0982b3436a6d509cf
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/README.md b/README.md
index b066914..b285b83 100644
--- a/README.md
+++ b/README.md
@@ -6,8 +6,9 @@
1. This plugin allows you to use ChatGPT for code review in Gerrit conveniently. After submitting a patchSet, OpenAI
will provide review feedback in the form of comments.
-2. You can continue to ask ChatGPT by @{gerritUserName} in the comments to further guide it in generating more
- targeted review comments.
+2. You can continue to ask ChatGPT by @{gerritUserName} or @{gerritEmailAddress} (provided that `gerritEmailAddress` is
+ in the form "gerritUserName@<any_email_domain>") in the comments to further guide it in generating more targeted
+ review comments.
## Getting Started
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientComments.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientComments.java
index ece7e54..b9751e7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientComments.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientComments.java
@@ -23,6 +23,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import static java.net.HttpURLConnection.HTTP_OK;
@@ -90,11 +92,18 @@
return filterLastComments(forwardGetRequest(uri));
}
+ private Pattern getBotMentionPattern() {
+ String escapedUserName = Pattern.quote(config.getGerritUserName());
+ String emailRegex = "@" + escapedUserName + "(?:@[A-Za-z0-9.-]+\\.[A-Za-z]{2,})?\\b";
+ return Pattern.compile(emailRegex);
+ }
+
private boolean isBotAddressed(String comment) {
log.info("Processing comment: {}", comment);
- if (comment == null || !comment.contains("@" + config.getGerritUserName())) {
- log.debug("Skipping action since the comment does not mention the ChatGpt bot." +
+ Matcher userMatcher = getBotMentionPattern().matcher(comment);
+ if (!userMatcher.find()) {
+ log.debug("Skipping action since the comment does not mention the ChatGPT bot." +
" Expected bot name in comment: {}, Actual comment text: {}",
config.getGerritUserName(), comment);
return false;
@@ -102,9 +111,8 @@
return true;
}
- private String removeTagsFromComment(String comment) {
- return comment.substring(comment.indexOf("@" + config.getGerritUserName())
- + config.getGerritUserName().length() + 1);
+ private String removeMentionsFromComment(String comment) {
+ return comment.replaceAll(getBotMentionPattern().pattern(), "");
}
private void addAllComments(String fullChangeId) {
@@ -167,7 +175,7 @@
));
}
String commentMessage = commentProperty.get("message").getAsString();
- commentString.append(removeTagsFromComment(commentMessage).trim());
+ commentString.append(removeMentionsFromComment(commentMessage).trim());
return commentString.toString();
}