Refactor patch processing functions Moved functions for processing formatted patches from GerritClientPatchSetStateful to a newly created GerritClientPatchSetHelper class. This change improves code segregation and facilitates future reuse of these functions. Change-Id: I91fc56b11ad9a4879002cdd3b6ebcadcda026f21 Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/gerrit/GerritClientPatchSetHelper.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/gerrit/GerritClientPatchSetHelper.java new file mode 100644 index 0000000..93e6e6c --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/gerrit/GerritClientPatchSetHelper.java
@@ -0,0 +1,46 @@ +package com.googlesource.gerrit.plugins.chatgpt.mode.stateful.client.api.gerrit; + +import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritChange; +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static com.googlesource.gerrit.plugins.chatgpt.settings.Settings.COMMIT_MESSAGE_FILTER_OUT_PREFIXES; +import static com.googlesource.gerrit.plugins.chatgpt.settings.Settings.GERRIT_COMMIT_MESSAGE_PREFIX; + +@Slf4j +public class GerritClientPatchSetHelper { + private static final Pattern EXTRACT_B_FILENAMES_FROM_PATCH_SET = Pattern.compile("^diff --git .*? b/(.*)$", + Pattern.MULTILINE); + + public static 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); + } + + public static String filterPatchWithoutCommitMessage(GerritChange change, 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(), + Pattern.DOTALL + ); + return CONFIG_ID_HEADING_PATTERN.matcher(formattedPatch).replaceAll(""); + } + + public static List<String> extractFilesFromPatch(String formattedPatch) { + Matcher extractFilenameMatcher = EXTRACT_B_FILENAMES_FROM_PATCH_SET.matcher(formattedPatch); + List<String> files = new ArrayList<>(); + while (extractFilenameMatcher.find()) { + files.add(extractFilenameMatcher.group(1)); + } + return files; + } +}
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 4e8ee74..f233dfd 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
@@ -11,19 +11,12 @@ import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritClientPatchSet; import lombok.extern.slf4j.Slf4j; -import java.util.ArrayList; import java.util.List; -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; +import static com.googlesource.gerrit.plugins.chatgpt.mode.stateful.client.api.gerrit.GerritClientPatchSetHelper.*; @Slf4j public class GerritClientPatchSetStateful extends GerritClientPatchSet implements IGerritClientPatchSet { - private static final Pattern EXTRACT_B_FILENAMES_FROM_PATCH_SET = Pattern.compile("^diff --git .*? b/(.*)$", - Pattern.MULTILINE); - private GerritChange change; @VisibleForTesting @@ -66,35 +59,7 @@ return filterPatchWithCommitMessage(formattedPatch); } else { - return filterPatchWithoutCommitMessage(formattedPatch); + return filterPatchWithoutCommitMessage(change, 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(), - Pattern.DOTALL - ); - return CONFIG_ID_HEADING_PATTERN.matcher(formattedPatch).replaceAll(""); - } - - private List<String> extractFilesFromPatch(String formattedPatch) { - Matcher extractFilenameMatcher = EXTRACT_B_FILENAMES_FROM_PATCH_SET.matcher(formattedPatch); - List<String> files = new ArrayList<>(); - while (extractFilenameMatcher.find()) { - files.add(extractFilenameMatcher.group(1)); - } - return files; - } }