Refine class responsibilities
Transformed FileDiffProcessed from a basic data class into a utility
class by integrating specific data processing methods, which were
previously part of the GerritClientPatchSet class.
Jira-Id: IT-103
Change-Id: I89555a57115a7eca437a582ff83e20a9b2fcb776
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/PatchSetReviewer.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/PatchSetReviewer.java
index a92ccf7..414b6f1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/PatchSetReviewer.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/PatchSetReviewer.java
@@ -9,7 +9,7 @@
import com.googlesource.gerrit.plugins.chatgpt.client.InlineCode;
import com.googlesource.gerrit.plugins.chatgpt.client.OpenAiClient;
import com.googlesource.gerrit.plugins.chatgpt.client.model.ChatGptSuggestionPoint;
-import com.googlesource.gerrit.plugins.chatgpt.client.model.FileDiffProcessed;
+import com.googlesource.gerrit.plugins.chatgpt.client.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.client.model.GerritCommentRange;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.Setter;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/FileDiffProcessed.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/FileDiffProcessed.java
new file mode 100644
index 0000000..b4d5d1d
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/FileDiffProcessed.java
@@ -0,0 +1,88 @@
+package com.googlesource.gerrit.plugins.chatgpt.client;
+
+import com.googlesource.gerrit.plugins.chatgpt.client.model.InputFileDiff;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.InputFileDiff.Content;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.OutputFileDiff;
+import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+@Slf4j
+public class FileDiffProcessed {
+ private static final String[] COMMIT_MESSAGE_FILTER_OUT_PREFIXES = {
+ "Parent:",
+ "Author:",
+ "AuthorDate:",
+ "Commit:",
+ "CommitDate:",
+ "Change-Id:"
+ };
+
+ private final Configuration config;
+ @Getter
+ private List<Content> diff;
+ @Getter
+ private List<String> newContent;
+ @Getter
+ private List<OutputFileDiff.Content> outputDiffContent;
+ private final boolean isCommitMessage;
+
+ public FileDiffProcessed(Configuration config, boolean isCommitMessage, InputFileDiff inputFileDiff) {
+ this.config = config;
+ this.isCommitMessage = isCommitMessage;
+ newContent = new ArrayList<>() {{
+ add("DUMMY LINE #0");
+ }};
+ diff = inputFileDiff.getContent();
+ outputDiffContent = new ArrayList<>();
+ List<Content> inputDiffContent = inputFileDiff.getContent();
+ // Iterate over the items of the diff content
+ for (Content inputContentItem : inputDiffContent) {
+ OutputFileDiff.Content outputContentItem = new OutputFileDiff.Content();
+ // Iterate over the fields `a`, `b` and `ab` of each diff content
+ for (Field inputDiffField : Content.class.getDeclaredFields()) {
+ processFileDiffItem(inputDiffField, inputContentItem, outputContentItem);
+ }
+ outputDiffContent.add(outputContentItem);
+ }
+ }
+
+ private void filterCommitMessageContent(List<String> fieldValue) {
+ fieldValue.removeIf(s ->
+ s.isEmpty() || Arrays.stream(COMMIT_MESSAGE_FILTER_OUT_PREFIXES).anyMatch(s::startsWith));
+ }
+
+ private void processFileDiffItem(Field inputDiffField, Content contentItem,
+ OutputFileDiff.Content outputContentItem) {
+ String diffType = inputDiffField.getName();
+ try {
+ // Get the `a`, `b` or `ab` field's value from the input diff content
+ @SuppressWarnings("unchecked")
+ List<String> diffLines = (List<String>) inputDiffField.get(contentItem);
+ if (diffLines == null) {
+ return;
+ }
+ if (isCommitMessage) {
+ filterCommitMessageContent(diffLines);
+ }
+ if (config.getGptFullFileReview() || !diffType.equals("ab")) {
+ // Get the corresponding `a`, `b` or `ab` field from the output diff class
+ Field outputDiffField = OutputFileDiff.Content.class.getDeclaredField(diffType);
+ // Store the new field's value in the output diff content `outputContentItem`
+ outputDiffField.set(outputContentItem, String.join("\n", diffLines));
+ }
+ // If the lines modified in the PatchSet are not deleted, they are utilized to populate newContent
+ if (diffType.contains("b")) {
+ newContent.addAll(diffLines);
+ }
+ } catch (IllegalAccessException | NoSuchFieldException e) {
+ log.error("Error while processing file difference (diff type: {})", diffType, e);
+ }
+ }
+
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClient.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClient.java
index c2e8f73..44e7854 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClient.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClient.java
@@ -3,7 +3,6 @@
import com.google.gerrit.server.events.Event;
import com.google.gson.JsonObject;
import com.google.inject.Singleton;
-import com.googlesource.gerrit.plugins.chatgpt.client.model.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.extern.slf4j.Slf4j;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientBase.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientBase.java
index a1534eb..fd6b148 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientBase.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientBase.java
@@ -4,7 +4,6 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
-import com.googlesource.gerrit.plugins.chatgpt.client.model.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
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 cb3ad65..aae091b 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
@@ -8,7 +8,6 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.googlesource.gerrit.plugins.chatgpt.client.model.ChatGptRequestPoint;
-import com.googlesource.gerrit.plugins.chatgpt.client.model.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientPatchSet.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientPatchSet.java
index 8a2a691..4818273 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientPatchSet.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritClientPatchSet.java
@@ -4,7 +4,6 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
-import com.googlesource.gerrit.plugins.chatgpt.client.model.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.client.model.InputFileDiff;
import com.googlesource.gerrit.plugins.chatgpt.client.model.OutputFileDiff;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
@@ -12,26 +11,15 @@
import java.net.URI;
import java.util.*;
-import java.lang.reflect.Field;
@Slf4j
public class GerritClientPatchSet extends GerritClientAccount {
- private static final String[] COMMIT_MESSAGE_FILTER_OUT_PREFIXES = {
- "Parent:",
- "Author:",
- "AuthorDate:",
- "Commit:",
- "CommitDate:",
- "Change-Id:"
- };
-
private final Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.create();
private boolean isCommitMessage;
private final List<String> diffs;
- private List<String> newFileContent;
public GerritClientPatchSet(Configuration config) {
super(config);
@@ -76,60 +64,14 @@
return files;
}
- private void filterCommitMessageContent(List<String> fieldValue) {
- fieldValue.removeIf(s ->
- s.isEmpty() || Arrays.stream(COMMIT_MESSAGE_FILTER_OUT_PREFIXES).anyMatch(s::startsWith));
- }
-
- private void processFileDiffItem(Field inputDiffField, InputFileDiff.Content contentItem,
- OutputFileDiff.Content outputContentItem) {
- String diffType = inputDiffField.getName();
- try {
- // Get the `a`, `b` or `ab` field's value from the input diff content
- @SuppressWarnings("unchecked")
- List<String> diffLines = (List<String>) inputDiffField.get(contentItem);
- if (diffLines == null) {
- return;
- }
- if (isCommitMessage) {
- filterCommitMessageContent(diffLines);
- }
- if (config.getGptFullFileReview() || !diffType.equals("ab")) {
- // Get the corresponding `a`, `b` or `ab` field from the output diff class
- Field outputDiffField = OutputFileDiff.Content.class.getDeclaredField(diffType);
- // Store the new field's value in the output diff content `outputContentItem`
- outputDiffField.set(outputContentItem, String.join("\n", diffLines));
- }
- // If the lines modified in the PatchSet are not deleted, they are utilized to populate newFileContent
- if (diffType.contains("b")) {
- newFileContent.addAll(diffLines);
- }
- } catch (IllegalAccessException | NoSuchFieldException e) {
- log.error("Error while processing file difference (diff type: {})", diffType, e);
- }
- }
-
private void processFileDiff(String filename, String fileDiffJson) {
log.debug("FileDiff Json processed: {}", fileDiffJson);
- newFileContent = new ArrayList<>() {{
- add("DUMMY LINE #0");
- }};
InputFileDiff inputFileDiff = gson.fromJson(fileDiffJson, InputFileDiff.class);
// Initialize the reduced output file diff with fields `meta_a` and `meta_b`
OutputFileDiff outputFileDiff = new OutputFileDiff(inputFileDiff.getMeta_a(), inputFileDiff.getMeta_b());
- List<OutputFileDiff.Content> outputDiffContent = new ArrayList<>();
- List<InputFileDiff.Content> inputDiffContent = inputFileDiff.getContent();
- // Iterate over the items of the diff content
- for (InputFileDiff.Content inputContentItem : inputDiffContent) {
- OutputFileDiff.Content outputContentItem = new OutputFileDiff.Content();
- // Iterate over the fields `a`, `b` and `ab` of each diff content
- for (Field inputDiffField : InputFileDiff.Content.class.getDeclaredFields()) {
- processFileDiffItem(inputDiffField, inputContentItem, outputContentItem);
- }
- outputDiffContent.add(outputContentItem);
- }
- fileDiffsProcessed.put(filename, new FileDiffProcessed(inputDiffContent, newFileContent));
- outputFileDiff.setContent(outputDiffContent);
+ FileDiffProcessed fileDiffProcessed = new FileDiffProcessed(config, isCommitMessage, inputFileDiff);
+ fileDiffsProcessed.put(filename, fileDiffProcessed);
+ outputFileDiff.setContent(fileDiffProcessed.getOutputDiffContent());
diffs.add(gson.toJson(outputFileDiff));
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/InlineCode.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/InlineCode.java
index 4063fb9..edc64d1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/InlineCode.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/InlineCode.java
@@ -3,7 +3,6 @@
import com.google.gson.JsonObject;
import com.google.gson.Gson;
import com.googlesource.gerrit.plugins.chatgpt.client.model.ChatGptSuggestionPoint;
-import com.googlesource.gerrit.plugins.chatgpt.client.model.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.client.model.GerritCommentRange;
import lombok.extern.slf4j.Slf4j;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/FileDiffProcessed.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/FileDiffProcessed.java
deleted file mode 100644
index 3dc2015..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/FileDiffProcessed.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.googlesource.gerrit.plugins.chatgpt.client.model;
-
-import lombok.AllArgsConstructor;
-import lombok.Data;
-
-import java.util.List;
-
-@Data
-@AllArgsConstructor
-public class FileDiffProcessed {
- private List<InputFileDiff.Content> diff;
- private List<String> newContent;
-}