Fix errors with slashes in project names
Issues has been fixed where the creation of storage and temporary files
for project-scoped persistent data would fail if the project name
included slashes.
Change-Id: Ic5b1e553ef97d768ba82af7ef9ebad73ea363599
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/data/PluginDataHandlerProvider.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/data/PluginDataHandlerProvider.java
index 54ba288..b178743 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/data/PluginDataHandlerProvider.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/data/PluginDataHandlerProvider.java
@@ -7,6 +7,8 @@
import java.nio.file.Path;
+import static com.googlesource.gerrit.plugins.chatgpt.utils.FileUtils.sanitizeFilename;
+
@Singleton
public class PluginDataHandlerProvider extends PluginDataHandlerBaseProvider implements Provider<PluginDataHandler> {
private final String projectName;
@@ -18,7 +20,7 @@
GerritChange change
) {
super(defaultPluginDataPath);
- projectName = change.getProjectName();
+ projectName = sanitizeFilename(change.getProjectName());
changeKey = change.getChangeKey().toString();
}
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 1fa9c9e..9b26bac 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
@@ -22,6 +22,7 @@
import static com.googlesource.gerrit.plugins.chatgpt.mode.stateful.client.api.chatgpt.ChatGptVectorStore.KEY_VECTOR_STORE_ID;
import static com.googlesource.gerrit.plugins.chatgpt.utils.FileUtils.createTempFileWithContent;
+import static com.googlesource.gerrit.plugins.chatgpt.utils.FileUtils.sanitizeFilename;
import static com.googlesource.gerrit.plugins.chatgpt.utils.GsonUtils.getGson;
@Slf4j
@@ -66,7 +67,7 @@
private String uploadRepoFiles() {
String repoFiles = gitRepoFiles.getGitRepoFiles(config, change);
- Path repoPath = createTempFileWithContent(change.getProjectName(), ".json", repoFiles);
+ Path repoPath = createTempFileWithContent(sanitizeFilename(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/utils/FileUtils.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/FileUtils.java
index fe8f03c..e37c4bd 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
@@ -31,4 +31,9 @@
int extIndex = filename.lastIndexOf('.');
return extIndex >= 1 && extensions.contains(filename.substring(extIndex));
}
+
+ public static String sanitizeFilename (String filename) {
+ // Replace any characters that are invalid in filenames (especially slashes) with a "+"
+ return filename.replaceAll("[^-_a-zA-Z0-9]", "+");
+ }
}