Refactor codebase for improved readability
- The event listeners GptMentionedCommentListener and
PatchSetCreatedListener have been consolidated into a single listener
named GerritListener.
- Data classes have been relocated to the "/model" subdirectory within
"/client".
Jira-Id: IT-103
Change-Id: Ica786f1e7516a6a6a85bcb5fe73f9ceefa5166a5
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/Module.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/Module.java
index cfc0a1f..c06abd1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/Module.java
@@ -3,15 +3,13 @@
import com.google.gerrit.server.events.EventListener;
import com.google.inject.AbstractModule;
import com.google.inject.multibindings.Multibinder;
-import com.googlesource.gerrit.plugins.chatgpt.listener.GptMentionedCommentListener;
-import com.googlesource.gerrit.plugins.chatgpt.listener.PatchSetCreatedListener;
+import com.googlesource.gerrit.plugins.chatgpt.listener.GerritListener;
public class Module extends AbstractModule {
@Override
protected void configure() {
Multibinder<EventListener> eventListenerBinder = Multibinder.newSetBinder(binder(), EventListener.class);
- eventListenerBinder.addBinding().to(PatchSetCreatedListener.class);
- eventListenerBinder.addBinding().to(GptMentionedCommentListener.class);
+ eventListenerBinder.addBinding().to(GerritListener.class);
}
}
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 f7c6755..3897279 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
@@ -2,6 +2,7 @@
import com.google.gson.JsonObject;
import com.google.gson.Gson;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.GerritCommentRange;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/OpenAiClient.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/OpenAiClient.java
index b75f361..1361f17 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/OpenAiClient.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/OpenAiClient.java
@@ -3,6 +3,9 @@
import com.google.common.net.HttpHeaders;
import com.google.gson.Gson;
import com.google.inject.Singleton;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.ChatCompletionRequest;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.ChatCompletionResponse;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.ChatCompletionResponseMessage;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionBase.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionBase.java
similarity index 89%
rename from src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionBase.java
rename to src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionBase.java
index 202454a..2c427cd 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionBase.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionBase.java
@@ -1,4 +1,4 @@
-package com.googlesource.gerrit.plugins.chatgpt.client;
+package com.googlesource.gerrit.plugins.chatgpt.client.model;
import com.google.gson.annotations.SerializedName;
import lombok.Data;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionRequest.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionRequest.java
similarity index 85%
rename from src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionRequest.java
rename to src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionRequest.java
index 04d5f91..9817f80 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionRequest.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionRequest.java
@@ -1,4 +1,4 @@
-package com.googlesource.gerrit.plugins.chatgpt.client;
+package com.googlesource.gerrit.plugins.chatgpt.client.model;
import lombok.Builder;
import lombok.Data;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionResponse.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionResponse.java
similarity index 71%
rename from src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionResponse.java
rename to src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionResponse.java
index ee0c1db..d447d10 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionResponse.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionResponse.java
@@ -1,4 +1,4 @@
-package com.googlesource.gerrit.plugins.chatgpt.client;
+package com.googlesource.gerrit.plugins.chatgpt.client.model;
import lombok.Data;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionResponseMessage.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionResponseMessage.java
similarity index 86%
rename from src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionResponseMessage.java
rename to src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionResponseMessage.java
index c9bd202..88ce0cc 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/ChatCompletionResponseMessage.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/ChatCompletionResponseMessage.java
@@ -1,4 +1,4 @@
-package com.googlesource.gerrit.plugins.chatgpt.client;
+package com.googlesource.gerrit.plugins.chatgpt.client.model;
import lombok.Data;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritCommentRange.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/GerritCommentRange.java
similarity index 74%
rename from src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritCommentRange.java
rename to src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/GerritCommentRange.java
index 30df278..610f575 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/GerritCommentRange.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/GerritCommentRange.java
@@ -1,4 +1,4 @@
-package com.googlesource.gerrit.plugins.chatgpt.client;
+package com.googlesource.gerrit.plugins.chatgpt.client.model;
import lombok.Data;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/PatchSetCreatedListener.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritListener.java
similarity index 82%
rename from src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/PatchSetCreatedListener.java
rename to src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritListener.java
index 225ac88..8ee403f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/PatchSetCreatedListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritListener.java
@@ -4,6 +4,7 @@
import com.google.gerrit.server.events.ChangeEvent;
import com.google.gerrit.server.events.Event;
import com.google.gerrit.server.events.EventListener;
+import com.google.gerrit.server.events.CommentAddedEvent;
import com.google.gerrit.server.events.PatchSetCreatedEvent;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.inject.Inject;
@@ -12,20 +13,20 @@
import lombok.extern.slf4j.Slf4j;
@Slf4j
-public class PatchSetCreatedListener implements EventListener {
+public class GerritListener implements EventListener {
private final ConfigCreator configCreator;
private final EventListenerHandler eventListenerHandler;
@Inject
- public PatchSetCreatedListener(ConfigCreator configCreator, EventListenerHandler eventListenerHandler) {
+ public GerritListener(ConfigCreator configCreator, EventListenerHandler eventListenerHandler) {
this.configCreator = configCreator;
this.eventListenerHandler = eventListenerHandler;
}
@Override
public void onEvent(Event event) {
- if (!(event instanceof PatchSetCreatedEvent)) {
+ if (!(event instanceof CommentAddedEvent || event instanceof PatchSetCreatedEvent)) {
log.debug("The event is not a PatchSetCreatedEvent, it is: {}", event);
return;
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GptMentionedCommentListener.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GptMentionedCommentListener.java
deleted file mode 100644
index 88aa1ea..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GptMentionedCommentListener.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package com.googlesource.gerrit.plugins.chatgpt.listener;
-
-import com.google.gerrit.entities.Project;
-import com.google.gerrit.server.events.ChangeEvent;
-import com.google.gerrit.server.events.CommentAddedEvent;
-import com.google.gerrit.server.events.Event;
-import com.google.gerrit.server.events.EventListener;
-import com.google.gerrit.server.project.NoSuchProjectException;
-import com.google.inject.Inject;
-import com.googlesource.gerrit.plugins.chatgpt.config.ConfigCreator;
-import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
-import lombok.extern.slf4j.Slf4j;
-
-@Slf4j
-public class GptMentionedCommentListener implements EventListener {
-
- private final ConfigCreator configCreator;
-
- private final EventListenerHandler eventListenerHandler;
-
- @Inject
- public GptMentionedCommentListener(ConfigCreator configCreator, EventListenerHandler eventListenerHandler) {
- this.configCreator = configCreator;
- this.eventListenerHandler = eventListenerHandler;
- }
-
- @Override
- public void onEvent(Event event) {
- if (!(event instanceof CommentAddedEvent)) {
- log.debug("The event is not a CommentAddedEvent, it is: {}", event);
- return;
- }
-
- log.info("Processing event: {}", event);
- ChangeEvent changeEvent = (ChangeEvent) event;
- Project.NameKey projectNameKey = changeEvent.getProjectNameKey();
-
- try {
- Configuration config = configCreator.createConfig(projectNameKey);
- eventListenerHandler.handleEvent(config, changeEvent);
- } catch (NoSuchProjectException e) {
- log.error("Project not found: {}", projectNameKey, e);
- }
-
- }
-}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java
index d928cd8..a7bac26 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTest.java
@@ -17,8 +17,7 @@
import com.googlesource.gerrit.plugins.chatgpt.config.ConfigCreator;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import com.googlesource.gerrit.plugins.chatgpt.listener.EventListenerHandler;
-import com.googlesource.gerrit.plugins.chatgpt.listener.GptMentionedCommentListener;
-import com.googlesource.gerrit.plugins.chatgpt.listener.PatchSetCreatedListener;
+import com.googlesource.gerrit.plugins.chatgpt.listener.GerritListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.junit.Assert;
@@ -136,8 +135,8 @@
when(event.getChangeKey()).thenReturn(CHANGE_ID);
EventListenerHandler eventListenerHandler = new EventListenerHandler(patchSetReviewer, gerritClient);
- PatchSetCreatedListener patchSetCreatedListener = new PatchSetCreatedListener(mockConfigCreator, eventListenerHandler);
- patchSetCreatedListener.onEvent(event);
+ GerritListener gerritListener = new GerritListener(mockConfigCreator, eventListenerHandler);
+ gerritListener.onEvent(event);
CompletableFuture<Void> future = eventListenerHandler.getLatestFuture();
future.get();
@@ -165,9 +164,9 @@
when(event.getChangeKey()).thenReturn(CHANGE_ID);
EventListenerHandler eventListenerHandler = new EventListenerHandler(patchSetReviewer, gerritClient);
- GptMentionedCommentListener gptMentionedCommentListener = new GptMentionedCommentListener(mockConfigCreator, eventListenerHandler);
+ GerritListener gerritListener = new GerritListener(mockConfigCreator, eventListenerHandler);
event.comment = "@gpt Hello!";
- gptMentionedCommentListener.onEvent(event);
+ gerritListener.onEvent(event);
CompletableFuture<Void> future = eventListenerHandler.getLatestFuture();
future.get();