Refactor: GerritClientDetail in GerritClientFacade
Transitioned management of the GerritClientDetail class from being a
component of PatchSetReviewer to being part of the GerritClientFacade
Singleton. This allows PatchSet details to be loaded a single time and
utilized in various code locations.
Consequently, the instantiation and disposal of singletons are now
consolidated within the EventListenerHandler class.
Jira-Id: IT-103
Change-Id: I9118f0d64ca1cfd417b47019dc09645adb1e4cf5
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/DynamicSettings.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/DynamicSettings.java
index f0352b0..97c9c6f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/DynamicSettings.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/DynamicSettings.java
@@ -1,8 +1,5 @@
package com.googlesource.gerrit.plugins.chatgpt;
-import com.googlesource.gerrit.plugins.chatgpt.client.gerrit.GerritChange;
-import com.googlesource.gerrit.plugins.chatgpt.utils.SingletonManager;
-
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@@ -22,10 +19,4 @@
private Integer votingMaxScore;
private Boolean forcedReview = false;
private Boolean forcedReviewChangeSet = false;
-
- public static void destroy(GerritChange change) {
- log.debug("Destroying DynamicSettings instance for change: {}", change.getFullChangeId());
- SingletonManager.removeInstance(DynamicSettings.class, change.getFullChangeId());
- }
-
}
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 b8b72ed..f9f08c8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/PatchSetReviewer.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/PatchSetReviewer.java
@@ -6,13 +6,12 @@
import com.googlesource.gerrit.plugins.chatgpt.client.chatgpt.ChatGptClient;
import com.googlesource.gerrit.plugins.chatgpt.client.gerrit.GerritChange;
import com.googlesource.gerrit.plugins.chatgpt.client.gerrit.GerritClient;
-import com.googlesource.gerrit.plugins.chatgpt.client.gerrit.GerritClientDetail;
import com.googlesource.gerrit.plugins.chatgpt.client.model.chatGpt.ChatGptReplyItem;
import com.googlesource.gerrit.plugins.chatgpt.client.model.chatGpt.ChatGptResponseContent;
-import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritPatchSetDetail;
import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritCodeRange;
import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritComment;
import com.googlesource.gerrit.plugins.chatgpt.client.model.ReviewBatch;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritPermittedVotingRange;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import com.googlesource.gerrit.plugins.chatgpt.utils.SingletonManager;
import lombok.extern.slf4j.Slf4j;
@@ -62,9 +61,7 @@
dynamicSettings.setCommentPropertiesSize(commentProperties.size());
dynamicSettings.setGptRequestUserPrompt(gerritClient.getUserRequests(change));
if (config.isVotingEnabled() && !change.getIsCommentEvent()) {
- GerritClientDetail gerritClientDetail = new GerritClientDetail(config, dynamicSettings.getGptAccountId());
- GerritPatchSetDetail.PermittedVotingRange permittedVotingRange = gerritClientDetail.getPermittedVotingRange(
- change.getFullChangeId());
+ GerritPermittedVotingRange permittedVotingRange = gerritClient.getPermittedVotingRange(change);
if (permittedVotingRange != null) {
if (permittedVotingRange.getMin() > config.getVotingMinScore()) {
log.debug("Minimum ChatGPT voting score set to {}", permittedVotingRange.getMin());
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClient.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClient.java
index fc346f0..84c79ed 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClient.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClient.java
@@ -4,6 +4,7 @@
import com.googlesource.gerrit.plugins.chatgpt.client.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritComment;
import com.googlesource.gerrit.plugins.chatgpt.client.model.ReviewBatch;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritPermittedVotingRange;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import com.googlesource.gerrit.plugins.chatgpt.utils.SingletonManager;
import lombok.extern.slf4j.Slf4j;
@@ -26,6 +27,15 @@
gerritClientFacade = SingletonManager.getInstance(GerritClientFacade.class, change, config);
}
+ public void loadClientDetail(GerritChange change, Integer gptAccountId) {
+ gerritClientFacade.loadClientDetail(change, gptAccountId);
+ }
+
+ public GerritPermittedVotingRange getPermittedVotingRange(GerritChange change) {
+ updateGerritClientFacade(change);
+ return gerritClientFacade.getPermittedVotingRange();
+ }
+
public String getPatchSet(String fullChangeId) throws Exception {
return getPatchSet(new GerritChange(fullChangeId));
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClientDetail.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClientDetail.java
index cdc5378..254d175 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClientDetail.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClientDetail.java
@@ -2,6 +2,7 @@
import com.googlesource.gerrit.plugins.chatgpt.client.UriResourceLocator;
import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritPatchSetDetail;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritPermittedVotingRange;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.extern.slf4j.Slf4j;
@@ -10,22 +11,24 @@
@Slf4j
public class GerritClientDetail extends GerritClientBase {
- private final Integer gptAccountId;
+ private Integer gptAccountId;
+ private GerritPatchSetDetail gerritPatchSetDetail;
- public GerritClientDetail(Configuration config, Integer gptAccountId) {
+ public GerritClientDetail(Configuration config) {
super(config);
- this.gptAccountId = gptAccountId;
}
- public GerritPatchSetDetail.PermittedVotingRange getPermittedVotingRange(String fullChangeId) {
- GerritPatchSetDetail gerritPatchSetDetail;
+ public void loadClientDetail(GerritChange change, Integer gptAccountId) {
+ this.gptAccountId = gptAccountId;
try {
- gerritPatchSetDetail = getReviewDetail(fullChangeId);
+ gerritPatchSetDetail = getReviewDetail(change.getFullChangeId());
}
catch (Exception e) {
log.debug("Error retrieving PatchSet details", e);
- return null;
}
+ }
+
+ public GerritPermittedVotingRange getPermittedVotingRange() {
List<GerritPatchSetDetail.Permission> permissions = gerritPatchSetDetail.getLabels().getCodeReview().getAll();
if (permissions == null) {
log.debug("No limitations on the ChatGPT voting range were detected");
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClientFacade.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClientFacade.java
index fbd1f70..db6bd0c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClientFacade.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/gerrit/GerritClientFacade.java
@@ -3,6 +3,7 @@
import com.googlesource.gerrit.plugins.chatgpt.client.FileDiffProcessed;
import com.googlesource.gerrit.plugins.chatgpt.client.model.ReviewBatch;
import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritComment;
+import com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit.GerritPermittedVotingRange;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.extern.slf4j.Slf4j;
@@ -11,16 +12,26 @@
@Slf4j
public class GerritClientFacade {
+ private final GerritClientDetail gerritClientDetail;
private final GerritClientPatchSet gerritClientPatchSet;
private final GerritClientComments gerritClientComments;
private final GerritClientReview gerritClientReview;
public GerritClientFacade(Configuration config) {
+ gerritClientDetail = new GerritClientDetail(config);
gerritClientPatchSet = new GerritClientPatchSet(config);
gerritClientComments = new GerritClientComments(config);
gerritClientReview = new GerritClientReview(config);
}
+ public void loadClientDetail(GerritChange change, Integer gptAccountId) {
+ gerritClientDetail.loadClientDetail(change, gptAccountId);
+ }
+
+ public GerritPermittedVotingRange getPermittedVotingRange() {
+ return gerritClientDetail.getPermittedVotingRange();
+ }
+
public String getPatchSet(GerritChange change) throws Exception {
return gerritClientPatchSet.getPatchSet(change);
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/gerrit/GerritPatchSetDetail.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/gerrit/GerritPatchSetDetail.java
index 2570f4c..5605592 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/gerrit/GerritPatchSetDetail.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/gerrit/GerritPatchSetDetail.java
@@ -25,15 +25,9 @@
private Integer value;
private String date;
@SerializedName("permitted_voting_range")
- private PermittedVotingRange permittedVotingRange;
+ private GerritPermittedVotingRange permittedVotingRange;
@SerializedName("_account_id")
private int accountId;
}
- @Data
- public static class PermittedVotingRange {
- private int min;
- private int max;
- }
-
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/gerrit/GerritPermittedVotingRange.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/gerrit/GerritPermittedVotingRange.java
new file mode 100644
index 0000000..89bc5cd
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/client/model/gerrit/GerritPermittedVotingRange.java
@@ -0,0 +1,9 @@
+package com.googlesource.gerrit.plugins.chatgpt.client.model.gerrit;
+
+import lombok.Data;
+
+@Data
+public class GerritPermittedVotingRange {
+ private int min;
+ private int max;
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventListenerHandler.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventListenerHandler.java
index e0a6c14..7230371 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventListenerHandler.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventListenerHandler.java
@@ -52,14 +52,20 @@
addShutdownHoot();
}
+ public void initializeSingletons(GerritChange change) {
+ gerritClient.initialize(config, change);
+ Integer gptAccountId = gerritClient.getNotNullAccountId(change, config.getGerritUserName());
+ dynamicSettings = createDynamicSettings(change, gptAccountId);
+ gerritClient.loadClientDetail(change, gptAccountId);
+ }
+
public void handleEvent(Configuration config, Event event) {
this.config = config;
GerritChange change = new GerritChange(event);
- gerritClient.initialize(config, change);
- dynamicSettings = createDynamicSettings(change);
+ initializeSingletons(change);
if (!preProcessEvent(change)) {
- postProcessEvent(change);
+ destroySingletons(change);
return;
}
@@ -75,7 +81,7 @@
Thread.currentThread().interrupt();
}
} finally {
- postProcessEvent(change);
+ destroySingletons(change);
}
}, executorService);
}
@@ -94,9 +100,9 @@
}));
}
- private DynamicSettings createDynamicSettings(GerritChange change) {
+ private DynamicSettings createDynamicSettings(GerritChange change, Integer gptAccountId) {
return SingletonManager.getNewInstance(DynamicSettings.class, change,
- gerritClient.getNotNullAccountId(change, config.getGerritUserName()),
+ gptAccountId,
config.getVotingMinScore(),
config.getVotingMaxScore());
}
@@ -189,9 +195,9 @@
return true;
}
- private void postProcessEvent(GerritChange change) {
+ private void destroySingletons(GerritChange change) {
gerritClient.destroy(change);
- DynamicSettings.destroy(change);
+ SingletonManager.removeInstance(DynamicSettings.class, change.getFullChangeId());
}
}