Return formatted patch in stateful `getPatchSet`
The getPatchSet method in the GerritClientPatchSetStateful class now
delivers the formatted patch of the current change.
This approach, utilized in stateful mode instead of the patch-set diff
in stateless mode, aims to reduce payload size by leveraging the
codebase context information stored in the assistant.
Additionally, partially omitting the formatted patch heading further
decreases the payload size.
Change-Id: I5d1653466df1b068f0974cb2a40e5ca7224065fe
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/patch/diff/FileDiffProcessed.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/patch/diff/FileDiffProcessed.java
index 75206de..1ea641f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/patch/diff/FileDiffProcessed.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/patch/diff/FileDiffProcessed.java
@@ -4,13 +4,13 @@
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritPatchSetFileDiff;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.patch.code.CodeFinderDiff;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.patch.diff.DiffContent;
+import com.googlesource.gerrit.plugins.chatgpt.settings.Settings;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;
@@ -19,14 +19,6 @@
@Slf4j
public class FileDiffProcessed {
private static final int MIN_RANDOM_PLACEHOLDER_VARIABLE_LENGTH = 1;
- private static final String[] COMMIT_MESSAGE_FILTER_OUT_PREFIXES = {
- "Parent:",
- "Author:",
- "AuthorDate:",
- "Commit:",
- "CommitDate:",
- "Change-Id:"
- };
private final Configuration config;
private final boolean isCommitMessage;
@@ -85,7 +77,7 @@
private void filterCommitMessageContent(List<String> fieldValue) {
fieldValue.removeIf(s ->
- s.isEmpty() || Arrays.stream(COMMIT_MESSAGE_FILTER_OUT_PREFIXES).anyMatch(s::startsWith));
+ s.isEmpty() || Settings.COMMIT_MESSAGE_FILTER_OUT_PREFIXES.values().stream().anyMatch(s::startsWith));
}
private void updateCodeEntities(Field diffField, List<String> diffLines) throws IllegalAccessException {
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 a987954..74f0cd3 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
@@ -2,6 +2,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.gerrit.server.account.AccountCache;
+import com.google.gerrit.server.util.ManualRequestContext;
import com.google.inject.Inject;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import com.googlesource.gerrit.plugins.chatgpt.data.PluginDataHandler;
@@ -13,11 +14,17 @@
import com.googlesource.gerrit.plugins.chatgpt.mode.stateful.client.api.git.GitRepoFiles;
import lombok.extern.slf4j.Slf4j;
+import java.util.regex.Pattern;
+
+import static com.googlesource.gerrit.plugins.chatgpt.settings.Settings.COMMIT_MESSAGE_FILTER_OUT_PREFIXES;
+
@Slf4j
public class GerritClientPatchSetStateful extends GerritClientPatchSet implements IGerritClientPatchSet {
private final GitRepoFiles gitRepoFiles;
private final PluginDataHandler pluginDataHandler;
+ private GerritChange change;
+
@VisibleForTesting
@Inject
public GerritClientPatchSetStateful(
@@ -30,11 +37,39 @@
this.pluginDataHandler = pluginDataHandler;
}
- public String getPatchSet(ChangeSetData changeSetData, GerritChange change) {
+ public String getPatchSet(ChangeSetData changeSetData, GerritChange change) throws Exception {
+ this.change = change;
ChatGptAssistant chatGptAssistant = new ChatGptAssistant(config, change, gitRepoFiles, pluginDataHandler);
chatGptAssistant.setupAssistant();
- return "";
+ return getPatchFromGerrit();
+ }
+
+ private String getPatchFromGerrit() throws Exception {
+ try (ManualRequestContext requestContext = config.openRequestContext()) {
+ String gerritPatch = config
+ .getGerritApi()
+ .changes()
+ .id(
+ change.getProjectName(),
+ change.getBranchNameKey().shortName(),
+ change.getChangeKey().get())
+ .current()
+ .patch()
+ .asString();
+ log.debug("Gerrit Patch retrieved: {}", gerritPatch);
+
+ return filterPatch(gerritPatch);
+ }
+ }
+
+ private String filterPatch(String gerritPatch) {
+ // 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(),
+ Pattern.DOTALL
+ );
+ return CONFIG_ID_HEADING_PATTERN.matcher(gerritPatch).replaceAll("");
}
}
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 24a6521..b9ecb3a 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
@@ -1,9 +1,20 @@
package com.googlesource.gerrit.plugins.chatgpt.settings;
+import java.util.Map;
+
public class Settings {
public static final String GERRIT_PATCH_SET_FILENAME = "/PATCHSET_LEVEL";
public static final String GERRIT_DEFAULT_MESSAGE_DONE = "Done";
public static final String GERRIT_AUTOGENERATED_PREFIX = "autogenerated:";
+ public static final Map<String , String> COMMIT_MESSAGE_FILTER_OUT_PREFIXES = Map.of(
+ "PARENT","Parent:",
+ "AUTHOR","Author:",
+ "AUTHOR_DATE", "AuthorDate:",
+ "COMMIT","Commit:",
+ "COMMIT_DATE", "CommitDate:",
+ "CHANGE_ID", "Change-Id:"
+ );
+
public static final String EMPTY_REVIEW_MESSAGE = "No update to show for this Change Set";
public enum MODES {