Refactor: separate mode-specific URI locators
Mode-specific URI resource locator methods have been separated from
common resource locator methods to simplify maintenance of mode-specific
code. Additionally, the classes have been moved from the `client` to the
`client.api` package to enhance overall project readability.
Change-Id: I8c0f081d175e4839175cee7ec44f7b0d0bff3537
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/UriResourceLocator.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/UriResourceLocator.java
similarity index 70%
rename from src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/UriResourceLocator.java
rename to src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/UriResourceLocator.java
index c05a6ec..084657f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/UriResourceLocator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/UriResourceLocator.java
@@ -1,4 +1,4 @@
-package com.googlesource.gerrit.plugins.chatgpt.mode.common.client;
+package com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@@ -7,10 +7,6 @@
public class UriResourceLocator {
private static final String AUTH_PREFIX_URI = "/a";
- private UriResourceLocator() {
- throw new IllegalStateException("Utility class");
- }
-
public static String gerritAccountsUri() {
return AUTH_PREFIX_URI + "/accounts";
}
@@ -27,18 +23,10 @@
return revisionBase > 0 ? "/?base=" + revisionBase : "";
}
- public static String gerritDiffPostfixUri(String filename) {
- return "/" + URLEncoder.encode(filename, StandardCharsets.UTF_8) + "/diff";
- }
-
public static String gerritPatchSetRevisionsUri(String fullChangeId) {
return gerritSetChangesUri(fullChangeId, "/?o=ALL_REVISIONS");
}
- public static String gerritPatchSetFilesUri(String fullChangeId) {
- return gerritSetChangesUri(fullChangeId, "/revisions/current/files");
- }
-
public static String gerritGetAllPatchSetCommentsUri(String fullChangeId) {
return gerritSetChangesUri(fullChangeId, "/comments");
}
@@ -51,11 +39,7 @@
return gerritSetChangesUri(fullChangeId, "/detail");
}
- public static String chatCompletionsUri() {
- return "/v1/chat/completions";
- }
-
- private static String gerritSetChangesUri(String fullChangeId, String uriPostfix) {
+ protected static String gerritSetChangesUri(String fullChangeId, String uriPostfix) {
return AUTH_PREFIX_URI + "/changes/" + fullChangeId + uriPostfix;
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientAccount.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientAccount.java
index b940871..5baa465 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientAccount.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientAccount.java
@@ -2,7 +2,7 @@
import com.google.gson.JsonArray;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator;
+import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.UriResourceLocator;
import lombok.extern.slf4j.Slf4j;
import java.net.URI;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientComments.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientComments.java
index cd383fe..cecebbb 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientComments.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientComments.java
@@ -3,7 +3,7 @@
import com.google.gerrit.server.events.CommentAddedEvent;
import com.google.gson.reflect.TypeToken;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator;
+import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.UriResourceLocator;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.messages.ClientMessage;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritComment;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.data.CommentData;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientDetail.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientDetail.java
index 1049c59..03bc163 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientDetail.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientDetail.java
@@ -2,7 +2,7 @@
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import com.googlesource.gerrit.plugins.chatgpt.data.ChangeSetDataHandler;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator;
+import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.UriResourceLocator;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritComment;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritPatchSetDetail;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritPermittedVotingRange;
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 3856c73..a30bc85 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
@@ -3,7 +3,7 @@
import com.google.gson.JsonObject;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import com.googlesource.gerrit.plugins.chatgpt.data.ChangeSetDataHandler;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator;
+import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.UriResourceLocator;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientReview.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientReview.java
index edc77ee..10451a0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientReview.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/api/gerrit/GerritClientReview.java
@@ -2,7 +2,7 @@
import com.google.common.net.HttpHeaders;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator;
+import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.UriResourceLocator;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritComment;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritReview;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.review.ReviewBatch;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/UriResourceLocatorStateless.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/UriResourceLocatorStateless.java
new file mode 100644
index 0000000..299da78
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/UriResourceLocatorStateless.java
@@ -0,0 +1,21 @@
+package com.googlesource.gerrit.plugins.chatgpt.mode.stateless.client.api;
+
+import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.UriResourceLocator;
+
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+
+public class UriResourceLocatorStateless extends UriResourceLocator {
+ public static String gerritDiffPostfixUri(String filename) {
+ return "/" + URLEncoder.encode(filename, StandardCharsets.UTF_8) + "/diff";
+ }
+
+ public static String gerritPatchSetFilesUri(String fullChangeId) {
+ return gerritSetChangesUri(fullChangeId, "/revisions/current/files");
+ }
+
+ public static String chatCompletionsUri() {
+ return "/v1/chat/completions";
+ }
+
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/chatgpt/ChatGptClientStateless.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/chatgpt/ChatGptClientStateless.java
index f8aa4df..a22252d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/chatgpt/ChatGptClientStateless.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/chatgpt/ChatGptClientStateless.java
@@ -4,11 +4,11 @@
import com.google.inject.Singleton;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.HttpClientWithRetry;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.chatgpt.ChatGptTools;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritChange;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.chatgpt.*;
import com.googlesource.gerrit.plugins.chatgpt.mode.interfaces.client.api.chatgpt.IChatGptClient;
+import com.googlesource.gerrit.plugins.chatgpt.mode.stateless.client.api.UriResourceLocatorStateless;
import com.googlesource.gerrit.plugins.chatgpt.mode.stateless.client.prompt.ChatGptPromptStateless;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@@ -101,7 +101,7 @@
}
private HttpRequest createRequest(Configuration config, String changeId, String patchSet) {
- URI uri = URI.create(URI.create(config.getGptDomain()) + UriResourceLocator.chatCompletionsUri());
+ URI uri = URI.create(config.getGptDomain() + UriResourceLocatorStateless.chatCompletionsUri());
log.debug("ChatGPT request URI: {}", uri);
requestBody = createRequestBody(config, changeId, patchSet);
log.debug("ChatGPT request body: {}", requestBody);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/gerrit/GerritClientPatchSetStateless.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/gerrit/GerritClientPatchSetStateless.java
index 5a1f160..b22de58 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/gerrit/GerritClientPatchSetStateless.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/api/gerrit/GerritClientPatchSetStateless.java
@@ -3,13 +3,13 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritChange;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritClientPatchSet;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.patch.diff.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritPatchSetFileDiff;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.gerrit.GerritReviewFileDiff;
import com.googlesource.gerrit.plugins.chatgpt.mode.interfaces.client.api.gerrit.IGerritClientPatchSet;
+import com.googlesource.gerrit.plugins.chatgpt.mode.stateless.client.api.UriResourceLocatorStateless;
import lombok.extern.slf4j.Slf4j;
import java.net.URI;
@@ -45,8 +45,8 @@
private List<String> getAffectedFiles(String fullChangeId, int revisionBase) throws Exception {
URI uri = URI.create(config.getGerritAuthBaseUrl()
- + UriResourceLocator.gerritPatchSetFilesUri(fullChangeId)
- + UriResourceLocator.gerritRevisionBasePostfixUri(revisionBase));
+ + UriResourceLocatorStateless.gerritPatchSetFilesUri(fullChangeId)
+ + UriResourceLocatorStateless.gerritRevisionBasePostfixUri(revisionBase));
log.debug("Affected Files URI: '{}'", uri);
JsonObject affectedFileMap = forwardGetRequestReturnJsonObject(uri);
List<String> files = new ArrayList<>();
@@ -87,9 +87,9 @@
continue;
}
URI uri = URI.create(config.getGerritAuthBaseUrl()
- + UriResourceLocator.gerritPatchSetFilesUri(fullChangeId)
- + UriResourceLocator.gerritDiffPostfixUri(filename)
- + UriResourceLocator.gerritRevisionBasePostfixUri(revisionBase));
+ + UriResourceLocatorStateless.gerritPatchSetFilesUri(fullChangeId)
+ + UriResourceLocatorStateless.gerritDiffPostfixUri(filename)
+ + UriResourceLocatorStateless.gerritRevisionBasePostfixUri(revisionBase));
log.debug("getFileDiffsJson URI: '{}'", uri);
String fileDiffJson = forwardGetRequest(uri).replaceAll("^[')\\]}]+", "");
processFileDiff(filename, fileDiffJson);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatelessTest.java
similarity index 95%
rename from src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java
rename to src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatelessTest.java
index 17a16ab..c4debeb 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewStatelessTest.java
@@ -21,9 +21,9 @@
import com.googlesource.gerrit.plugins.chatgpt.data.PluginDataHandler;
import com.googlesource.gerrit.plugins.chatgpt.listener.EventListenerHandler;
import com.googlesource.gerrit.plugins.chatgpt.listener.GerritListener;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritChange;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritClient;
+import com.googlesource.gerrit.plugins.chatgpt.mode.stateless.client.api.UriResourceLocatorStateless;
import com.googlesource.gerrit.plugins.chatgpt.mode.stateless.client.prompt.ChatGptPromptStateless;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
@@ -49,7 +49,7 @@
import java.util.concurrent.ExecutionException;
import static com.google.gerrit.extensions.client.ChangeKind.REWORK;
-import static com.googlesource.gerrit.plugins.chatgpt.mode.common.client.UriResourceLocator.*;
+import static com.googlesource.gerrit.plugins.chatgpt.mode.stateless.client.api.UriResourceLocatorStateless.*;
import static com.googlesource.gerrit.plugins.chatgpt.utils.GsonUtils.getGson;
import static com.googlesource.gerrit.plugins.chatgpt.utils.TextUtils.joinWithNewLine;
import static java.net.HttpURLConnection.HTTP_OK;
@@ -58,7 +58,7 @@
@Slf4j
@RunWith(MockitoJUnitRunner.class)
-public class ChatGptReviewTest {
+public class ChatGptReviewStatelessTest {
private static final Path basePath = Paths.get("src/test/resources");
private static final String GERRIT_AUTH_BASE_URL = "http://localhost:9527";
private static final int GERRIT_GPT_ACCOUNT_ID = 1000000;
@@ -164,7 +164,7 @@
.withBody("[{\"_account_id\": " + GERRIT_USER_ACCOUNT_ID + "}]")));
// Mock the behavior of the gerritAccountGroups request
- WireMock.stubFor(WireMock.get(UriResourceLocator.gerritAccountsUri() +
+ WireMock.stubFor(WireMock.get(UriResourceLocatorStateless.gerritAccountsUri() +
gerritGroupPostfixUri(GERRIT_USER_ACCOUNT_ID))
.willReturn(WireMock.aResponse()
.withStatus(HTTP_OK)
@@ -186,21 +186,21 @@
.withBodyFile("gerritPatchSetDetail.json")));
// Mock the behavior of the gerritPatchSetFiles request
- WireMock.stubFor(WireMock.get(gerritPatchSetFilesUri(fullChangeId))
+ WireMock.stubFor(WireMock.get(UriResourceLocatorStateless.gerritPatchSetFilesUri(fullChangeId))
.willReturn(WireMock.aResponse()
.withStatus(HTTP_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
.withBodyFile("gerritPatchSetFiles.json")));
// Mock the behavior of the gerritPatchSet diff requests
- WireMock.stubFor(WireMock.get(gerritPatchSetFilesUri(fullChangeId) +
- gerritDiffPostfixUri("/COMMIT_MSG"))
+ WireMock.stubFor(WireMock.get(UriResourceLocatorStateless.gerritPatchSetFilesUri(fullChangeId) +
+ UriResourceLocatorStateless.gerritDiffPostfixUri("/COMMIT_MSG"))
.willReturn(WireMock.aResponse()
.withStatus(HTTP_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
.withBodyFile("gerritPatchSetDiffCommitMsg.json")));
- WireMock.stubFor(WireMock.get(gerritPatchSetFilesUri(fullChangeId) +
- gerritDiffPostfixUri("test_file.py"))
+ WireMock.stubFor(WireMock.get(UriResourceLocatorStateless.gerritPatchSetFilesUri(fullChangeId) +
+ UriResourceLocatorStateless.gerritDiffPostfixUri("test_file.py"))
.willReturn(WireMock.aResponse()
.withStatus(HTTP_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
@@ -215,7 +215,7 @@
// Mock the behavior of the askGpt request
WireMock.stubFor(WireMock.post(WireMock.urlEqualTo(URI.create(config.getGptDomain()
- + UriResourceLocator.chatCompletionsUri()).getPath()))
+ + UriResourceLocatorStateless.chatCompletionsUri()).getPath()))
.willReturn(WireMock.aResponse()
.withStatus(HTTP_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
@@ -322,7 +322,7 @@
chatGptPromptStateless.setCommentEvent(false);
WireMock.stubFor(WireMock.post(WireMock.urlEqualTo(URI.create(config.getGptDomain()
- + UriResourceLocator.chatCompletionsUri()).getPath()))
+ + UriResourceLocatorStateless.chatCompletionsUri()).getPath()))
.willReturn(WireMock.aResponse()
.withStatus(HTTP_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
@@ -388,7 +388,7 @@
when(mockConfigCreator.createConfig(ArgumentMatchers.any())).thenReturn(config);
chatGptPromptStateless.setCommentEvent(true);
WireMock.stubFor(WireMock.post(WireMock.urlEqualTo(URI.create(config.getGptDomain()
- + UriResourceLocator.chatCompletionsUri()).getPath()))
+ + UriResourceLocatorStateless.chatCompletionsUri()).getPath()))
.willReturn(WireMock.aResponse()
.withStatus(HTTP_OK)
.withHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())