Initial add of GerritEventContextModule
Slowly migrate to Guice injection by introducing a
`GerritEventContectModule` and creating a child injector with bindings
for shared instances.
Change-Id: Iac7209cd3fea208fb956e23f338829b57943a61a
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventHandlerExecutor.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventHandlerExecutor.java
index 955b59a..9f6ce1a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventHandlerExecutor.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventHandlerExecutor.java
@@ -6,6 +6,7 @@
import com.google.gerrit.server.git.WorkQueue;
import com.google.inject.Inject;
import com.google.inject.Singleton;
+import com.google.inject.Injector;
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.extern.slf4j.Slf4j;
@@ -14,23 +15,26 @@
@Slf4j
@Singleton
public class EventHandlerExecutor {
+ private final Injector injector;
private final ScheduledExecutorService executor;
- private final EventHandlerTask.Factory taskHandlerFactory;
@Inject
EventHandlerExecutor(
+ Injector injector,
WorkQueue workQueue,
- EventHandlerTask.Factory taskHandlerFactory,
@PluginName String pluginName,
PluginConfigFactory pluginConfigFactory
) {
- this.taskHandlerFactory = taskHandlerFactory;
+ this.injector = injector;
int maximumPoolSize = pluginConfigFactory.getFromGerritConfig(pluginName)
.getInt("maximumPoolSize", 2);
this.executor = workQueue.createQueue(maximumPoolSize, "ChatGPT request executor");
}
public void execute(Configuration config, Event event) {
- executor.execute(taskHandlerFactory.create(config, event));
+ GerritEventContextModule contextModule = new GerritEventContextModule(config);
+ EventHandlerTask.Factory taskHandlerFactory = injector.createChildInjector(contextModule)
+ .getInstance(EventHandlerTask.Factory.class);
+ executor.execute(taskHandlerFactory.create(event));
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventHandlerTask.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventHandlerTask.java
index 69ddfcf..ff05b16 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventHandlerTask.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/EventHandlerTask.java
@@ -38,7 +38,7 @@
};
public interface Factory {
- EventHandlerTask create(Configuration config, Event event);
+ EventHandlerTask create(Event event);
}
@VisibleForTesting
@@ -60,11 +60,11 @@
@Inject
EventHandlerTask(
+ Configuration config,
PatchSetReviewer reviewer,
GerritClient gerritClient,
GitRepoFiles gitRepoFiles,
PluginDataHandler pluginDataHandler,
- @Assisted Configuration config,
@Assisted Event event
) {
this.reviewer = reviewer;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritEventContextModule.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritEventContextModule.java
new file mode 100644
index 0000000..d71d69a
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritEventContextModule.java
@@ -0,0 +1,19 @@
+package com.googlesource.gerrit.plugins.chatgpt.listener;
+
+import com.google.gerrit.extensions.config.FactoryModule;
+import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
+
+public class GerritEventContextModule extends FactoryModule {
+ private final Configuration config;
+
+ public GerritEventContextModule(Configuration config) {
+ this.config = config;
+ }
+
+ @Override
+ protected void configure() {
+ install(EventHandlerTask.MODULE);
+
+ bind(Configuration.class).toInstance(config);
+ }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTestBase.java b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTestBase.java
index 91c8984..ddba64a 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTestBase.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/chatgpt/ChatGptReviewTestBase.java
@@ -24,6 +24,7 @@
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import com.googlesource.gerrit.plugins.chatgpt.data.PluginDataHandler;
import com.googlesource.gerrit.plugins.chatgpt.listener.EventHandlerTask;
+import com.googlesource.gerrit.plugins.chatgpt.listener.GerritEventContextModule;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.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;
@@ -207,6 +208,8 @@
EventHandlerTask.Factory factory = Guice.createInjector(EventHandlerTask.MODULE, new AbstractModule() {
@Override
protected void configure() {
+ install(new GerritEventContextModule(config));
+
bind(GerritClient.class).toInstance(gerritClient);
bind(GitRepoFiles.class).toInstance(gitRepoFiles);
bind(ConfigCreator.class).toInstance(mockConfigCreator);
@@ -214,7 +217,7 @@
bind(PluginDataHandler.class).toInstance(pluginDataHandler);
}
}).getInstance(EventHandlerTask.Factory.class);
- return factory.create(config, event).execute();
+ return factory.create(event).execute();
}
protected void testRequestSent() {