Apply file extension filter to git repo files To prevent unnecessary increases in the size of git repo files uploaded to ChatGPT, the `enabledFileExtensions` filter used for patch review files is now also applied to the repo files. Change-Id: I0120d14f34192da6097b15c65f519f228996a7f8 Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientPatchSet.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientPatchSet.java index ff54e0e..9ff6046 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientPatchSet.java +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientPatchSet.java
@@ -18,6 +18,7 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import static com.googlesource.gerrit.plugins.chatgpt.utils.FileUtils.matchesExtensionList; import static com.googlesource.gerrit.plugins.chatgpt.utils.GsonUtils.getNoEscapedGson; import static java.util.stream.Collectors.toList; @@ -67,8 +68,7 @@ try (ManualRequestContext requestContext = config.openRequestContext()) { for (String filename : files) { isCommitMessage = filename.equals("/COMMIT_MSG"); - if (!isCommitMessage && (filename.lastIndexOf(".") < 1 || - !enabledFileExtensions.contains(filename.substring(filename.lastIndexOf("."))))) { + if (!isCommitMessage && !matchesExtensionList(filename, enabledFileExtensions)) { continue; } DiffInfo diff =
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptAssistantBase.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptAssistantBase.java index 435efc9..1fa9c9e 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptAssistantBase.java +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptAssistantBase.java
@@ -65,7 +65,7 @@ } private String uploadRepoFiles() { - String repoFiles = gitRepoFiles.getGitRepoFiles(change); + String repoFiles = gitRepoFiles.getGitRepoFiles(config, change); Path repoPath = createTempFileWithContent(change.getProjectName(), ".json", repoFiles); ChatGptFiles chatGptFiles = new ChatGptFiles(config); ChatGptFilesResponse chatGptFilesResponse = chatGptFiles.uploadFiles(repoPath);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/git/GitRepoFiles.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/git/GitRepoFiles.java index ce794e1..d518049 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/git/GitRepoFiles.java +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/git/GitRepoFiles.java
@@ -1,5 +1,6 @@ package com.googlesource.gerrit.plugins.chatgpt.mode.stateful.client.api.git; +import com.googlesource.gerrit.plugins.chatgpt.config.Configuration; import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritChange; import lombok.extern.slf4j.Slf4j; import org.eclipse.jgit.api.errors.GitAPIException; @@ -14,18 +15,25 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.HashMap; +import java.util.List; import java.util.Map; +import static com.googlesource.gerrit.plugins.chatgpt.utils.FileUtils.matchesExtensionList; import static com.googlesource.gerrit.plugins.chatgpt.utils.GsonUtils.getGson; @Slf4j public class GitRepoFiles { public static final String REPO_PATTERN = "git/%s.git"; - public String getGitRepoFiles(GerritChange change) { + private List<String> enabledFileExtensions; + + public String getGitRepoFiles(Configuration config, GerritChange change) { + enabledFileExtensions = config.getEnabledFileExtensions(); + log.debug("Open Repo from {}", change.getProjectNameKey()); String repoPath = String.format(REPO_PATTERN, change.getProjectNameKey().toString()); try { Repository repository = openRepository(repoPath); + log.debug("Open Repo path {}", repoPath); Map<String, String> filesWithContent = listFilesWithContent(repository); return getGson().toJson(filesWithContent); @@ -49,9 +57,11 @@ while (treeWalk.next()) { String path = treeWalk.getPathString(); + if (!matchesExtensionList(path, enabledFileExtensions)) continue; ObjectId objectId = treeWalk.getObjectId(0); byte[] bytes = reader.open(objectId).getBytes(); String content = new String(bytes, StandardCharsets.UTF_8); // Assumes text files with UTF-8 encoding + log.debug("Repo File loaded: {}", path); filesWithContent.put(path, content); } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java index 07763cd..fe8f03c 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java
@@ -5,6 +5,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.Objects; public class FileUtils { @@ -25,4 +26,9 @@ return tempFile; } + + public static boolean matchesExtensionList(String filename, List<String> extensions) { + int extIndex = filename.lastIndexOf('.'); + return extIndex >= 1 && extensions.contains(filename.substring(extIndex)); + } }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatefulTest.java b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatefulTest.java index 0dbaafe..9dccaae 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatefulTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatefulTest.java
@@ -74,7 +74,7 @@ // Mock the behavior of the Git Repository Manager String repoJson = readTestFile("__files/stateful/gitProjectFiles.json"); - when(gitRepoFiles.getGitRepoFiles(any())).thenReturn(repoJson); + when(gitRepoFiles.getGitRepoFiles(any(), any())).thenReturn(repoJson); // Mock the behavior of the ChatGPT create-file request WireMock.stubFor(WireMock.post(WireMock.urlEqualTo(URI.create(config.getGptDomain()