Enable commit message reviews in stateful requests
Commit message reviews are now enabled for stateful review requests,
bringing them in line with the functionality available for stateless
requests.
Change-Id: I0128bd025e78fdf5d0e1ba763a9dd080fdda90ff
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/prompt/ChatGptPrompt.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/prompt/ChatGptPrompt.java
index 6af9e39..3b7971e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/prompt/ChatGptPrompt.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/prompt/ChatGptPrompt.java
@@ -45,6 +45,7 @@
public static String DEFAULT_GPT_REQUEST_PROMPT_REQUESTS;
public static String DEFAULT_GPT_REVIEW_PROMPT_COMMIT_MESSAGES;
public static String DEFAULT_GPT_RELEVANCE_RULES;
+ public static String DEFAULT_GPT_HOW_TO_FIND_COMMIT_MESSAGE;
public static Map<String, String> DEFAULT_GPT_REPLIES_ATTRIBUTES;
protected final Configuration config;
@@ -71,6 +72,10 @@
String.format(DEFAULT_GPT_REPLIES_PROMPT_ENFORCE_RESPONSE_CHECK, commentPropertiesSize);
}
+ public static String getReviewPromptCommitMessages() {
+ return String.format(DEFAULT_GPT_REVIEW_PROMPT_COMMIT_MESSAGES, DEFAULT_GPT_HOW_TO_FIND_COMMIT_MESSAGE);
+ }
+
protected void loadPrompts(String promptFilename) {
String promptFile = String.format("Config/%s.json", promptFilename);
Class<? extends ChatGptPrompt> me = this.getClass();
@@ -126,7 +131,6 @@
DEFAULT_GPT_REPLIES_PROMPT_INLINE;
}
-
private void updateScoreDescription() {
String scoreDescription = DEFAULT_GPT_REPLIES_ATTRIBUTES.get(ATTRIBUTE_SCORE);
if (scoreDescription.contains("%d")) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/gerrit/GerritClientPatchSetStateful.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/gerrit/GerritClientPatchSetStateful.java
index e7c92c9..63e6c66 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/gerrit/GerritClientPatchSetStateful.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/gerrit/GerritClientPatchSetStateful.java
@@ -19,6 +19,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import static com.googlesource.gerrit.plugins.chatgpt.settings.Settings.GERRIT_COMMIT_MESSAGE_PREFIX;
import static com.googlesource.gerrit.plugins.chatgpt.settings.Settings.COMMIT_MESSAGE_FILTER_OUT_PREFIXES;
@Slf4j
@@ -74,6 +75,25 @@
}
private String filterPatch(String formattedPatch) {
+ if (config.getGptReviewCommitMessages()) {
+ return filterPatchWithCommitMessage(formattedPatch);
+ }
+ else {
+ return filterPatchWithoutCommitMessage(formattedPatch);
+ }
+ }
+
+ private String filterPatchWithCommitMessage(String formattedPatch) {
+ // Remove Patch heading up to the Date annotation, so that the commit message is included. Additionally, remove
+ // the change type between brackets
+ Pattern CONFIG_ID_HEADING_PATTERN = Pattern.compile(
+ "^.*?" + GERRIT_COMMIT_MESSAGE_PREFIX + "(?:\\[[^\\]]+\\] )?",
+ Pattern.DOTALL
+ );
+ return CONFIG_ID_HEADING_PATTERN.matcher(formattedPatch).replaceAll(GERRIT_COMMIT_MESSAGE_PREFIX);
+ }
+
+ private String filterPatchWithoutCommitMessage(String formattedPatch) {
// Remove Patch heading up to the Change-Id annotation
Pattern CONFIG_ID_HEADING_PATTERN = Pattern.compile(
"^.*?" + COMMIT_MESSAGE_FILTER_OUT_PREFIXES.get("CHANGE_ID") + " " + change.getChangeKey().get(),
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/prompt/ChatGptPromptStateful.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/prompt/ChatGptPromptStateful.java
index 8299cb1..2a57413 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/prompt/ChatGptPromptStateful.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/prompt/ChatGptPromptStateful.java
@@ -30,9 +30,13 @@
}
public String getDefaultGptAssistantInstructions() {
- return DEFAULT_GPT_SYSTEM_PROMPT + DOT +
+ String instructions = DEFAULT_GPT_SYSTEM_PROMPT + DOT +
String.format(DEFAULT_GPT_ASSISTANT_INSTRUCTIONS, change.getProjectName()) + SPACE +
getPatchSetReviewUserPrompt();
+ if (config.getGptReviewCommitMessages()) {
+ instructions += SPACE + getReviewPromptCommitMessages();
+ }
+ return instructions;
}
public String getDefaultGptThreadReviewMessage(String patchSet) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/prompt/ChatGptPromptStateless.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/prompt/ChatGptPromptStateless.java
index 484ba0d..e4bb4ec 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/prompt/ChatGptPromptStateless.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/prompt/ChatGptPromptStateless.java
@@ -1,7 +1,6 @@
package com.googlesource.gerrit.plugins.chatgpt.mode.stateless.client.prompt;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
-import com.googlesource.gerrit.plugins.chatgpt.data.ChangeSetDataHandler;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.prompt.ChatGptPrompt;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.data.ChangeSetData;
import lombok.extern.slf4j.Slf4j;
@@ -83,7 +82,7 @@
private void loadStatelessPrompts() {
// Avoid repeated loading of prompt constants
- if (DEFAULT_GPT_REVIEW_PROMPT == null) {
+ if (DEFAULT_GPT_SYSTEM_PROMPT_INPUT_DESCRIPTION == null) {
loadPrompts("promptsStateless");
}
}
@@ -92,7 +91,7 @@
List<String> steps = new ArrayList<>(){};
steps.add(ChatGptPromptStateless.DEFAULT_GPT_REVIEW_PROMPT_REVIEW + SPACE + getPatchSetReviewUserPrompt());
if (config.getGptReviewCommitMessages()) {
- steps.add(DEFAULT_GPT_REVIEW_PROMPT_COMMIT_MESSAGES);
+ steps.add(getReviewPromptCommitMessages());
}
return steps;
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/settings/Settings.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/settings/Settings.java
index b9ecb3a..ca32c0d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/settings/Settings.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/settings/Settings.java
@@ -14,6 +14,7 @@
"COMMIT_DATE", "CommitDate:",
"CHANGE_ID", "Change-Id:"
);
+ public static final String GERRIT_COMMIT_MESSAGE_PREFIX = "Subject: ";
public static final String EMPTY_REVIEW_MESSAGE = "No update to show for this Change Set";
diff --git a/src/main/resources/Config/prompts.json b/src/main/resources/Config/prompts.json
index e128262..b358bb9 100644
--- a/src/main/resources/Config/prompts.json
+++ b/src/main/resources/Config/prompts.json
@@ -1,7 +1,7 @@
{
"DEFAULT_GPT_SYSTEM_PROMPT": "Act as a PatchSet Reviewer",
"DEFAULT_GPT_REVIEW_PROMPT_DIRECTIVES": "Here are the Directives:",
- "DEFAULT_GPT_REVIEW_PROMPT_COMMIT_MESSAGES": "Review the commit message of the PatchSet and provide your feedback in an additional reply. The commit message is provided in the \"content\" field of \"/COMMIT_MSG\" in the same way as the file changes. Ensure that the commit message accurately and succinctly describes the changes made, and verify if it matches the nature and scope of the changes in the PatchSet. If your feedback on the commit message is negative, you are required to supply an example of commit message that meets these criteria. For instance, if your comment is \"The commit message lacks detail\", you should follow up with \"A clearer commit message would be '...'\".",
+ "DEFAULT_GPT_REVIEW_PROMPT_COMMIT_MESSAGES": "You MUST review the commit message of the PatchSet and provide your feedback in an additional reply. The commit message is provided in %s. Ensure that the commit message accurately and succinctly describes the changes made, and verify if it matches the nature and scope of the changes in the PatchSet. If your feedback on the commit message is negative, you are required to supply an example of commit message that meets these criteria. For instance, if your comment is \"The commit message lacks detail\", you should follow up with \"A clearer commit message would be '...'\".",
"DEFAULT_GPT_REQUEST_PROMPT_DIFF": "I have some requests about the following PatchSet Diff:",
"DEFAULT_GPT_REQUEST_PROMPT_REQUESTS": "My requests are given in a JSON-formatted array, where each element includes the compulsory field `request`, the field `history` with any prior exchanged messages, and, for inline code comments, the fields `filename`, `lineNumber`, and `codeSnippet`:",
"DEFAULT_GPT_REPLIES_PROMPT": "Each reply must be formatted as an individual object within an array in the key `replies`, as defined in the `format_replies` tools function. The object includes the string attributes %s, with the following specifications: %s.",
diff --git a/src/main/resources/Config/promptsStateful.json b/src/main/resources/Config/promptsStateful.json
index 26ed239..f7f4108 100644
--- a/src/main/resources/Config/promptsStateful.json
+++ b/src/main/resources/Config/promptsStateful.json
@@ -2,5 +2,6 @@
"DEFAULT_GPT_ASSISTANT_NAME": "PatchSet Reviewer",
"DEFAULT_GPT_ASSISTANT_DESCRIPTION": "PatchSet Reviewer for project %s.",
"DEFAULT_GPT_ASSISTANT_INSTRUCTIONS": "The JSON project file uploaded includes the source files for the `%s` project. The structure uses the file paths from the project's root as keys, and arrays of lines as their values. This arrangement ensures that the line number for any given line corresponds to its index in the array plus one. You will receive a patch in the standard git format-patch format. Your tasks include: 1. applying this patch to the corresponding existing files, and 2. conducting a review of the patch. Here are the guidelines for reviewing the patch: A. Identify any potential problems and offer suggestions for enhancements, presenting each point as a separate reply; B. Focus solely on identifying and suggesting solutions for issues; refrain from highlighting any positive aspects; C. Only evaluate the code that has been modified in the patch; refrain from reviewing any other parts of the project's code that were not changed.",
- "DEFAULT_GPT_MESSAGE_REVIEW": "Review the following Patch Set: ```%s```"
+ "DEFAULT_GPT_MESSAGE_REVIEW": "Review the following Patch Set: ```%s```",
+ "DEFAULT_GPT_HOW_TO_FIND_COMMIT_MESSAGE": "the \"Subject:\" entry of the Patch Set"
}
diff --git a/src/main/resources/Config/promptsStateless.json b/src/main/resources/Config/promptsStateless.json
index eff788d..06110a8 100644
--- a/src/main/resources/Config/promptsStateless.json
+++ b/src/main/resources/Config/promptsStateless.json
@@ -4,5 +4,6 @@
"DEFAULT_GPT_REVIEW_PROMPT": "To conduct your review, follow these steps in the given order:",
"DEFAULT_GPT_REVIEW_PROMPT_REVIEW": "Begin with examining the PatchSet Diff, focusing exclusively on the \"a\" and \"b\" items, and using the \"ab\" items solely as context to understand the changes better. Provide insights on any potential issues you foresee and suggestions for improvements if necessary, with each insight articulated as a separate reply. Concentrate exclusively on spotting and rectifying issues; avoid mentioning any positive elements. For instance, instead of saying \"this is good, but that needs improvement\", simply state \"that needs improvement\".",
"DEFAULT_GPT_REVIEW_PROMPT_DIFF": "Here are the PatchSet Diffs:",
- "DEFAULT_GPT_REVIEW_PROMPT_MESSAGE_HISTORY": "Here are the message histories:"
+ "DEFAULT_GPT_REVIEW_PROMPT_MESSAGE_HISTORY": "Here are the message histories:",
+ "DEFAULT_GPT_HOW_TO_FIND_COMMIT_MESSAGE": "the \"content\" field of \"/COMMIT_MSG\" in the same way as the file changes"
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatelessTest.java b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatelessTest.java
index ced9b39..219fa0d 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatelessTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatelessTest.java
@@ -111,7 +111,7 @@
ChatGptPromptStateless.DEFAULT_GPT_REVIEW_PROMPT,
ChatGptPromptStateless.DEFAULT_GPT_REVIEW_PROMPT_REVIEW + " " +
chatGptPromptStateless.getPatchSetReviewUserPrompt(),
- ChatGptPromptStateless.DEFAULT_GPT_REVIEW_PROMPT_COMMIT_MESSAGES,
+ ChatGptPromptStateless.getReviewPromptCommitMessages(),
ChatGptPromptStateless.DEFAULT_GPT_REVIEW_PROMPT_DIFF,
diffContent,
ChatGptPromptStateless.DEFAULT_GPT_REVIEW_PROMPT_MESSAGE_HISTORY,