Ignore events from other instances
In a multisite setup, events are propagated across Gerrit nodes. This
could result in multiple comments being added by ChatGPT for a single
request. To prevent that, we only process events comming from the
current node.
Bug: Issue 336648780
Change-Id: I07f6b6f39cd00636c7952f069e7de88984ba6f26
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritListener.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritListener.java
index 3464306..589e538 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/listener/GerritListener.java
@@ -1,6 +1,8 @@
package com.googlesource.gerrit.plugins.chatgpt.listener;
+import com.google.gerrit.common.Nullable;
import com.google.gerrit.entities.Project;
+import com.google.gerrit.server.config.GerritInstanceId;
import com.google.gerrit.server.events.PatchSetEvent;
import com.google.gerrit.server.events.Event;
import com.google.gerrit.server.events.EventListener;
@@ -12,19 +14,31 @@
import com.googlesource.gerrit.plugins.chatgpt.config.Configuration;
import lombok.extern.slf4j.Slf4j;
+import java.util.Objects;
+
@Slf4j
public class GerritListener implements EventListener {
+ private final String myInstanceId;
private final ConfigCreator configCreator;
private final EventHandlerExecutor evenHandlerExecutor;
@Inject
- public GerritListener(ConfigCreator configCreator, EventHandlerExecutor evenHandlerExecutor) {
+ public GerritListener(
+ ConfigCreator configCreator,
+ EventHandlerExecutor evenHandlerExecutor,
+ @GerritInstanceId @Nullable String myInstanceId
+ ) {
this.configCreator = configCreator;
this.evenHandlerExecutor = evenHandlerExecutor;
+ this.myInstanceId = myInstanceId;
}
@Override
public void onEvent(Event event) {
+ if (!Objects.equals(event.instanceId, myInstanceId)) {
+ log.debug("Ignore event from another instance");
+ return;
+ }
if (!(event instanceof CommentAddedEvent || event instanceof PatchSetCreatedEvent)) {
log.debug("The event is not a PatchSetCreatedEvent, it is: {}", event);
return;