Merge branch 'origin/stable-3.10'

* stable-3.10:
  Trigger collection on ref-replicated event

Change-Id: Iaf6227c3f8212d07472edda0d6bd8011e74147ba
diff --git a/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/GitRepoUpdateListener.java b/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/GitRepoUpdateListener.java
index aaf8c25..678961a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/GitRepoUpdateListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/gitrepometrics/GitRepoUpdateListener.java
@@ -27,6 +27,7 @@
 
 class GitRepoUpdateListener implements EventListener {
   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
+  protected static final String REF_REPLICATED_EVENT_SUFFIX = "ref-replicated";
   private final ExecutorService executor;
   private final UpdateGitMetricsTask.Factory updateGitMetricsTaskFactory;
   private final GitRepoMetricsCache gitRepoMetricsCache;
@@ -46,7 +47,7 @@
 
   @Override
   public void onEvent(Event event) {
-    if (event instanceof RefUpdatedEvent || isReplicationDoneEvent(event)) {
+    if (isMyEvent(event) && (isRefUpdatedEvent(event) || isRefReplicatedEvent(event))) {
       String projectName = ((ProjectEvent) event).getProjectNameKey().get();
       logger.atFine().log(
           "Got %s event from %s. Might need to collect metrics for project %s",
@@ -59,12 +60,19 @@
     }
   }
 
-  private boolean isReplicationDoneEvent(Event event) {
+  private boolean isRefReplicatedEvent(Event event) {
     // Check the name of the event instead of checking the class type
     // to avoid importing pull and push replication plugin dependencies
     // only for this check.
-    return event.type != null
-        && !Objects.equals(event.instanceId, instanceId)
-        && event.type.endsWith("-replication-done");
+
+    return event.type.endsWith(REF_REPLICATED_EVENT_SUFFIX);
+  }
+
+  private boolean isRefUpdatedEvent(Event event) {
+    return event.type.equals(RefUpdatedEvent.TYPE);
+  }
+
+  private boolean isMyEvent(Event event) {
+    return event.type != null && Objects.equals(event.instanceId, instanceId);
   }
 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitUpdateListenerTest.java b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitUpdateListenerTest.java
index b046758..72a24a7 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitUpdateListenerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitUpdateListenerTest.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.gitrepometrics;
 
 import static com.google.common.truth.Truth.assertThat;
+import static com.googlesource.gerrit.plugins.gitrepometrics.GitRepoUpdateListener.REF_REPLICATED_EVENT_SUFFIX;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
@@ -57,8 +58,6 @@
   private final String disabledProject = "disabledProject";
   private final Project.NameKey disabledProjectNameKey = Project.nameKey(disabledProject);
   private final String producerInstanceId = "producerInstanceId";
-  private final String consumerInstanceId = "consumerInstanceId";
-  private final String refReplicationDoneType = "ref-replication-done";
 
   @Inject private UpdateGitMetricsTask.Factory updateGitMetricsTaskFactory;
 
@@ -114,35 +113,54 @@
   }
 
   @Test
-  public void shouldUpdateMetricsIfProjectIsEnabledOnRefReplicationDone() {
+  public void shouldNotUpdateMetricsOnRefReplicatedFromOtherNode() {
     gitRepoUpdateListener.onEvent(
-        getRefReplicationEvent(refReplicationDoneType, enabledProject, consumerInstanceId));
+        getRefReplicationEvent(
+            REF_REPLICATED_EVENT_SUFFIX, enabledProject, "another-node-instance-id"));
+    assertMetricsAreNotUpdated();
+  }
+
+  @Test
+  public void shouldNotUpdateMetricsOnRefUpdatedFromOtherNode() {
+    gitRepoUpdateListener.onEvent(getRefUpdatedEvent(enabledProject, "another-node-instance-id"));
+    assertMetricsAreNotUpdated();
+  }
+
+  @Test
+  public void shouldUpdateMetricsIfProjectIsEnabledOnRefReplicated() {
+    gitRepoUpdateListener.onEvent(
+        getRefReplicationEvent(REF_REPLICATED_EVENT_SUFFIX, enabledProject, producerInstanceId));
     assertMetricsAreUpdated();
   }
 
   @Test
-  public void shouldNotUpdateMetricsIfProjectIsDisabledOnReplicationDone() {
+  public void shouldNotUpdateMetricsIfProjectIsDisabledOnOnRefReplicated() {
     gitRepoUpdateListener.onEvent(
-        getRefReplicationEvent(refReplicationDoneType, disabledProject, consumerInstanceId));
+        getRefReplicationEvent(REF_REPLICATED_EVENT_SUFFIX, disabledProject, producerInstanceId));
     assertMetricsAreNotUpdated();
   }
 
   @Test
   public void shouldNotUpdateMetricsOnUnknownEvent() {
     gitRepoUpdateListener.onEvent(
-        getRefReplicationEvent("any-event", enabledProject, consumerInstanceId));
+        getRefReplicationEvent("any-event", enabledProject, producerInstanceId));
     assertMetricsAreNotUpdated();
   }
 
   @Test
-  public void shouldNotUpdateMetricsOnRefReplicationDoneFromSameNode() {
+  public void shouldUpdateMetricsOnRefReplicatedFromSameNode() {
     gitRepoUpdateListener.onEvent(
-        getRefReplicationEvent(refReplicationDoneType, enabledProject, producerInstanceId));
-    assertMetricsAreNotUpdated();
+        getRefReplicationEvent(REF_REPLICATED_EVENT_SUFFIX, enabledProject, producerInstanceId));
+    assertMetricsAreUpdated();
   }
 
   private RefUpdatedEvent getRefUpdatedEvent(String projectName) {
+    return getRefUpdatedEvent(projectName, producerInstanceId);
+  }
+
+  private RefUpdatedEvent getRefUpdatedEvent(String projectName, String instanceId) {
     RefUpdatedEvent refUpdatedEvent = new RefUpdatedEvent();
+    refUpdatedEvent.instanceId = instanceId;
     refUpdatedEvent.refUpdate =
         () -> {
           RefUpdateAttribute attributes = new RefUpdateAttribute();