Only publish stream events when sendStreamEvents is set.

The publishing of stream events is only enabled when the
"sendStreamEvents' configuration is set as follows:

[plugin "events-gcloud-pubsub"]
   sendStreamEvents=true

Note that this constitutes a breaking change compared to the previous
stable version (stable-3.4), in which stream events were published by
default.

This is because multi-site now already publishes stream events
[see Iafe5a8155] by binding directly the StreamEventsPublisher from the
events-broker library. Thus direct publishing from events-gcloud-pubsub
would be redundant and cause twice as many messages to be published.

Bug: Issue 14910
Change-Id: I8569d093405457654cda21a8acb8c0dff36646f2
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
index 50fad61..24663b5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
@@ -30,17 +30,25 @@
 
   private PubSubApiModule pubSubApiModule;
   private EnvironmentChecker environmentChecker;
+  private final PubSubConfiguration configuration;
 
   @Inject
-  public Module(PubSubApiModule pubSubApiModule, EnvironmentChecker environmentChecker) {
+  public Module(
+      PubSubApiModule pubSubApiModule,
+      EnvironmentChecker environmentChecker,
+      PubSubConfiguration configuration) {
     this.pubSubApiModule = pubSubApiModule;
     this.environmentChecker = environmentChecker;
+    this.configuration = configuration;
   }
 
   @Override
   protected void configure() {
     DynamicSet.bind(binder(), LifecycleListener.class).to(Manager.class);
-    DynamicSet.bind(binder(), EventListener.class).to(PubSubEventListener.class);
+
+    if (configuration.isSendStreamEvents()) {
+      DynamicSet.bind(binder(), EventListener.class).to(PubSubEventListener.class);
+    }
     factory(PubSubPublisher.Factory.class);
     factory(PubSubEventSubscriber.Factory.class);
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
index 614d0aa..484013f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
@@ -29,6 +29,7 @@
   private static final String DEFAULT_SUBSCTIPRION_TIMEOUT = "10";
   private static final String DEFAULT_SHUTDOWN_TIMEOUT = "10";
   private static final String DEFAULT_STREAM_EVENTS_TOPIC = "gerrit";
+  private static final boolean DEFAULT_SEND_STREAM_EVENTS = false;
 
   private final String gcloudProject;
   private final String subscriptionId;
@@ -39,6 +40,7 @@
   private final Long shutdownTimeoutInSeconds;
   private final String streamEventsTopic;
   private final PluginConfig fromGerritConfig;
+  private final boolean sendStreamEvents;
 
   @Inject
   public PubSubConfiguration(
@@ -51,6 +53,8 @@
     this.privateKeyLocation = getMandatoryString("privateKeyLocation");
     this.streamEventsTopic =
         fromGerritConfig.getString("streamEventsTopic", DEFAULT_STREAM_EVENTS_TOPIC);
+    this.sendStreamEvents =
+        fromGerritConfig.getBoolean("sendStreamEvents", DEFAULT_SEND_STREAM_EVENTS);
     this.numberOfSubscribers =
         Integer.parseInt(
             fromGerritConfig.getString("numberOfSubscribers", DEFAULT_NUMBER_OF_SUBSCRIBERS));
@@ -110,4 +114,8 @@
     }
     return value;
   }
+
+  public boolean isSendStreamEvents() {
+    return sendStreamEvents;
+  }
 }
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index f3263d3..a15a8a4 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -47,3 +47,7 @@
 :   Optional. Name of the GCloud PubSub topic for stream events. events-gcloud-pubsub plugin exposes
     all stream events under this topic name.
     Default: gerrit
+
+`plugin.events-gcloud-pubsub.sendStreamEvents`
+:   Whether to send stream events to the `streamEventsTopic` topic.
+    Default: false
\ No newline at end of file
diff --git a/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java b/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
index d202f25..e27e946 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
@@ -135,6 +135,7 @@
   @GerritConfig(
       name = "plugin.events-gcloud-pubsub.privateKeyLocation",
       value = PRIVATE_KEY_LOCATION)
+  @GerritConfig(name = "plugin.events-gcloud-pubsub.sendStreamEvents", value = "true")
   public void shouldProduceStreamEvents() throws Exception {
     String subscriptionId = "gerrit-subscription-id";
     String topicId = "gerrit";
@@ -165,6 +166,26 @@
   @GerritConfig(
       name = "plugin.events-gcloud-pubsub.privateKeyLocation",
       value = PRIVATE_KEY_LOCATION)
+  @GerritConfig(name = "plugin.events-gcloud-pubsub.sendStreamEvents", value = "false")
+  public void shouldNotProduceStreamEventsWhenDisabled() throws Exception {
+    String subscriptionId = "gerrit-subscription-id";
+    String topicId = "gerrit";
+    createSubscription(subscriptionId, topicId, channelProvider, credentialsProvider);
+
+    createChange();
+
+    readMessageAndValidate(
+        (pullResponse) -> assertThat(pullResponse.getReceivedMessagesList()).isEmpty(),
+        PROJECT_ID,
+        subscriptionId);
+  }
+
+  @Test
+  @GerritConfig(name = "plugin.events-gcloud-pubsub.gcloudProject", value = PROJECT_ID)
+  @GerritConfig(name = "plugin.events-gcloud-pubsub.subscriptionId", value = SUBSCRIPTION_ID)
+  @GerritConfig(
+      name = "plugin.events-gcloud-pubsub.privateKeyLocation",
+      value = PRIVATE_KEY_LOCATION)
   public void shouldConsumeEvent() throws InterruptedException {
     Event event = new ProjectCreatedEvent();
     event.instanceId = DEFAULT_INSTANCE_ID;