Merge branch 'stable-3.8'

* stable-3.8:
  Download global-refdb from CI rather than from Maven
  Introduce function to refactor artifact download
  Download events-broker from CI rather than from Maven
  Cascade to pull-replication master branch if stable branch not found
  Cascade to healthcheck-master-branch if healthcheck-branch not found
  Use index on ChangeNotes which supports local and imported changes
  Consume events-broker directly from source

Change-Id: I79b81345448509dfebf45c719bb185262c757361
diff --git a/BUILD b/BUILD
index 34cc807..f9cfcee 100644
--- a/BUILD
+++ b/BUILD
@@ -18,9 +18,9 @@
     ],
     resources = glob(["src/main/resources/**/*"]),
     deps = [
+        ":events-broker-neverlink",
         ":pull-replication-neverlink",
         ":replication-neverlink",
-        "@events-broker//jar:neverlink",
         "@global-refdb//jar:neverlink",
     ],
 )
@@ -37,6 +37,12 @@
     exports = ["//plugins/pull-replication"],
 )
 
+java_library(
+    name = "events-broker-neverlink",
+    neverlink = 1,
+    exports = ["//plugins/events-broker"],
+)
+
 junit_tests(
     name = "multi_site_tests",
     srcs = glob(["src/test/java/**/*.java"]),
@@ -57,7 +63,7 @@
     exports = PLUGIN_DEPS + PLUGIN_TEST_DEPS + [
         ":multi-site__plugin",
         "@global-refdb//jar",
-        "@events-broker//jar",
+        "//plugins/events-broker",
         "//plugins/pull-replication",
         "//plugins/replication",
     ],
diff --git a/Jenkinsfile b/Jenkinsfile
index 1d09d53..5b02057 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,3 +1,4 @@
 pluginPipeline(formatCheckId: 'gerritforge:multi-site-format-47168e90078b0b3f11401610930e82830e76bff7',
                buildCheckId: 'gerritforge:multi-site-47168e90078b0b3f11401610930e82830e76bff7',
-               extraPlugins: [ 'pull-replication' ])
+               extraPlugins: [ 'pull-replication' ],
+               extraModules: [ 'events-broker' ])
diff --git a/e2e-tests/test.sh b/e2e-tests/test.sh
index 3e0f6c1..b103720 100755
--- a/e2e-tests/test.sh
+++ b/e2e-tests/test.sh
@@ -210,7 +210,6 @@
 done
 
 # Defaults
-EVENTS_BROKER_VER=`grep 'com.gerritforge:events-broker' ${LOCATION}/../external_plugin_deps.bzl | cut -d '"' -f 2 | cut -d ':' -f 3`
 GLOBAL_REFDB_VER=`grep 'com.gerritforge:global-refdb' ${LOCATION}/../external_plugin_deps.bzl | cut -d '"' -f 2 | cut -d ':' -f 3`
 DEPLOYMENT_LOCATION=$(mktemp -d || $(echo >&2 "Could not create temp dir" && exit 1))
 MULTISITE_LIB_LOCATION=${MULTISITE_LIB_LOCATION:-${DEF_MULTISITE_LOCATION}}
@@ -267,8 +266,7 @@
   -O $COMMON_LIBS/global-refdb.jar || { echo >&2 "Cannot download global-refdb library: Check internet connection. Aborting"; exit 1; }
 
 echo "Downloading events-broker library $GERRIT_BRANCH"
-wget https://repo1.maven.org/maven2/com/gerritforge/events-broker/$EVENTS_BROKER_VER/events-broker-$EVENTS_BROKER_VER.jar \
-  -O $COMMON_LIBS/events-broker.jar || { echo >&2 "Cannot download events-broker library: Check internet connection. Aborting"; exit 1; }
+cp bazel-bin/plugins/events-broker/events-broker.jar $COMMON_LIBS/events-broker.jar
 
 echo "Setting up directories"
 mkdir -p ${GERRIT_1_ETC} ${GERRIT_1_PLUGINS} ${GERRIT_1_LIBS} ${GERRIT_2_ETC} ${GERRIT_2_PLUGINS} ${GERRIT_2_LIBS}
diff --git a/external_plugin_deps.bzl b/external_plugin_deps.bzl
index b4fc691..7d855cf 100644
--- a/external_plugin_deps.bzl
+++ b/external_plugin_deps.bzl
@@ -6,10 +6,3 @@
         artifact = "com.gerritforge:global-refdb:3.8.0-rc5",
         sha1 = "3f54f99e4ddf454c119e8ac3183bba7e9d7c7897",
     )
-
-    maven_jar(
-        name = "events-broker",
-        artifact = "com.gerritforge:events-broker:3.8.0-rc5",
-        sha1 = "d62a2e5e49a6f77fff1221d05835bc84481bcb0a",
-    )
-
diff --git a/setup_local_env/setup.sh b/setup_local_env/setup.sh
index a55feb7..32880a2 100755
--- a/setup_local_env/setup.sh
+++ b/setup_local_env/setup.sh
@@ -19,8 +19,6 @@
 GERRIT_BRANCH=stable-3.8
 GERRIT_CI=https://gerrit-ci.gerritforge.com/view/Plugins-$GERRIT_BRANCH/job
 LAST_BUILD=lastSuccessfulBuild/artifact/bazel-bin/plugins
-EVENTS_BROKER_VER=`grep 'com.gerritforge:events-broker' $(dirname $0)/../external_plugin_deps.bzl | cut -d '"' -f 2 | cut -d ':' -f 3`
-GLOBAL_REFDB_VER=`grep 'com.gerritforge:global-refdb' $(dirname $0)/../external_plugin_deps.bzl | cut -d '"' -f 2 | cut -d ':' -f 3`
 
 function check_application_requirements {
   type haproxy >/dev/null 2>&1 || { echo >&2 "Require haproxy but it's not installed. Aborting."; exit 1; }
@@ -226,6 +224,16 @@
   fi
 }
 
+function download_artifact_from_ci {
+  local artifact_name=$1
+  local prefix=${2:-plugin}
+  wget $GERRIT_CI/$prefix-$artifact_name-bazel-$GERRIT_BRANCH/$LAST_BUILD/$artifact_name/$artifact_name.jar \
+  -O $DEPLOYMENT_LOCATION/$artifact_name.jar || \
+  wget $GERRIT_CI/$prefix-$artifact_name-bazel-master-$GERRIT_BRANCH/$LAST_BUILD/$artifact_name/$artifact_name.jar \
+  -O $DEPLOYMENT_LOCATION/$artifact_name.jar || \
+  { echo >&2 "Cannot download $artifact_name $prefix: Check internet connection. Aborting"; exit 1; }
+}
+
 while [ $# -ne 0 ]
 do
 case "$1" in
@@ -411,80 +419,43 @@
 fi
 if [ $DOWNLOAD_WEBSESSION_PLUGIN = "true" ];then
   echo "Downloading websession-broker plugin $GERRIT_BRANCH"
-  wget $GERRIT_CI/plugin-websession-broker-bazel-$GERRIT_BRANCH/$LAST_BUILD/websession-broker/websession-broker.jar \
-  -O $DEPLOYMENT_LOCATION/websession-broker.jar || \
-  wget $GERRIT_CI/plugin-websession-broker-bazel-master-$GERRIT_BRANCH/$LAST_BUILD/websession-broker/websession-broker.jar \
-  -O $DEPLOYMENT_LOCATION/websession-broker.jar || \
-  { echo >&2 "Cannot download websession-broker plugin: Check internet connection. Abort\
-ing"; exit 1; }
-  wget $GERRIT_CI/plugin-healthcheck-bazel-$GERRIT_BRANCH/$LAST_BUILD/healthcheck/healthcheck.jar \
-  -O $DEPLOYMENT_LOCATION/healthcheck.jar || { echo >&2 "Cannot download healthcheck plugin: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci websession-broker
+  download_artifact_from_ci healthcheck
+
 else
   echo "Without the websession-broker; user login via haproxy will fail."
 fi
 
 echo "Downloading zookeeper plugin $GERRIT_BRANCH"
-  wget $GERRIT_CI/plugin-zookeeper-refdb-bazel-$GERRIT_BRANCH/$LAST_BUILD/zookeeper-refdb/zookeeper-refdb.jar \
-  -O $DEPLOYMENT_LOCATION/zookeeper-refdb.jar || \
-  wget $GERRIT_CI/plugin-zookeeper-refdb-bazel-master-$GERRIT_BRANCH/$LAST_BUILD/zookeeper-refdb/zookeeper-refdb.jar \
-  -O $DEPLOYMENT_LOCATION/zookeeper-refdb.jar || \
-  { echo >&2 "Cannot download zookeeper plugin: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci zookeeper-refdb
 
 echo "Downloading global-refdb library $GERRIT_BRANCH"
-  wget https://repo1.maven.org/maven2/com/gerritforge/global-refdb/$GLOBAL_REFDB_VER/global-refdb-$GLOBAL_REFDB_VER.jar \
-  -O $DEPLOYMENT_LOCATION/global-refdb.jar || { echo >&2 "Cannot download global-refdb library: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci global-refdb "module"
 
 echo "Downloading events-broker library $GERRIT_BRANCH"
-  wget https://repo1.maven.org/maven2/com/gerritforge/events-broker/$EVENTS_BROKER_VER/events-broker-$EVENTS_BROKER_VER.jar \
-  -O $DEPLOYMENT_LOCATION/events-broker.jar || { echo >&2 "Cannot download events-broker library: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci events-broker "module"
 
 if [ "$BROKER_TYPE" = "kafka" ]; then
 echo "Downloading events-kafka plugin $GERRIT_BRANCH"
-  wget $GERRIT_CI/plugin-events-kafka-bazel-$GERRIT_BRANCH/$LAST_BUILD/events-kafka/events-kafka.jar \
-  -O $DEPLOYMENT_LOCATION/events-kafka.jar || \
-  wget $GERRIT_CI/plugin-events-kafka-bazel-master-$GERRIT_BRANCH/$LAST_BUILD/events-kafka/events-kafka.jar \
-  -O $DEPLOYMENT_LOCATION/events-kafka.jar || \
-  { echo >&2 "Cannot download events-kafka plugin: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci events-kafka
 fi
 
 if [ "$BROKER_TYPE" = "kinesis" ]; then
 echo "Downloading events-aws-kinesis plugin $GERRIT_BRANCH"
-  wget $GERRIT_CI/plugin-events-aws-kinesis-bazel-$GERRIT_BRANCH/$LAST_BUILD/events-aws-kinesis/events-aws-kinesis.jar \
-  -O $DEPLOYMENT_LOCATION/events-aws-kinesis.jar || \
-  wget $GERRIT_CI/plugin-events-aws-kinesis-bazel-master-$GERRIT_BRANCH/$LAST_BUILD/events-aws-kinesis/events-aws-kinesis.jar \
-  -O $DEPLOYMENT_LOCATION/events-aws-kinesis.jar || \
-  { echo >&2 "Cannot download events-aws-kinesis plugin: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci events-aws-kinesis
 fi
 
 
 if [ "$BROKER_TYPE" = "gcloud-pubsub" ]; then
 echo "Downloading events-gcloud-pubsub plugin $GERRIT_BRANCH"
-  wget $GERRIT_CI/plugin-events-gcloud-pubsub-bazel-$GERRIT_BRANCH/$LAST_BUILD/events-gcloud-pubsub/events-gcloud-pubsub.jar \
-  -O $DEPLOYMENT_LOCATION/events-gcloud-pubsub.jar || \
-  wget $GERRIT_CI/plugin-events-gcloud-pubsub-bazel-master-$GERRIT_BRANCH/$LAST_BUILD/events-gcloud-pubsub/events-gcloud-pubsub.jar \
-  -O $DEPLOYMENT_LOCATION/events-gcloud-pubsub.jar || \
-  { echo >&2 "Cannot download events-gcloud-pubsub plugin: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci events-gcloud-pubsub
 fi
 
 echo "Downloading metrics-reporter-prometheus plugin $GERRIT_BRANCH"
-  wget $GERRIT_CI/plugin-metrics-reporter-prometheus-bazel-$GERRIT_BRANCH/$LAST_BUILD/metrics-reporter-prometheus/metrics-reporter-prometheus.jar \
-  -O $DEPLOYMENT_LOCATION/metrics-reporter-prometheus.jar || \
-  wget $GERRIT_CI/plugin-metrics-reporter-prometheus-bazel-master-$GERRIT_BRANCH/$LAST_BUILD/metrics-reporter-prometheus/metrics-reporter-prometheus.jar \
-  -O $DEPLOYMENT_LOCATION/metrics-reporter-prometheus.jar || \
-  { echo >&2 "Cannot download metrics-reporter-prometheus plugin: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci metrics-reporter-prometheus
 
 echo "Downloading pull-replication plugin $GERRIT_BRANCH"
-  wget $GERRIT_CI/plugin-pull-replication-bazel-$GERRIT_BRANCH/$LAST_BUILD/pull-replication/pull-replication.jar \
-  -O $DEPLOYMENT_LOCATION/pull-replication.jar || { echo >&2 "Cannot download pull-replication plugin: Check internet connection. Abort\
-ing"; exit 1; }
+  download_artifact_from_ci pull-replication
 
 if [ "$HTTPS_ENABLED" = "true" ];then
   export HTTP_PROTOCOL="https"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandler.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandler.java
index 522a78f..b3fb27c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandler.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandler.java
@@ -95,7 +95,7 @@
       Optional<ChangeNotes> changeNotes = checker.getChangeNotes();
       ChangeNotes notes = changeNotes.get();
       notes.reload();
-      indexer.index(notes.getChange());
+      indexer.index(notes);
     }
   }
 
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandlerTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandlerTest.java
index 2c4b157..7133a0d 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandlerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/forwarder/ForwardedIndexChangeHandlerTest.java
@@ -88,7 +88,6 @@
     when(ctxMock.open()).thenReturn(manualRequestContextMock);
     id = Change.id(TEST_CHANGE_NUMBER);
     change = new Change(null, id, null, null, TimeUtil.now());
-    when(changeNotes.getChange()).thenReturn(change);
     when(changeCheckerFactoryMock.create(any())).thenReturn(changeCheckerAbsentMock);
     when(configurationMock.index()).thenReturn(index);
     when(index.numStripedLocks()).thenReturn(10);
@@ -102,7 +101,7 @@
   public void changeIsIndexedWhenUpToDate() throws Exception {
     setupChangeAccessRelatedMocks(CHANGE_EXISTS, CHANGE_UP_TO_DATE, CHANGE_CONSISTENT);
     handler.index(TEST_CHANGE_ID, Operation.INDEX, Optional.empty());
-    verify(indexerMock, times(1)).index(any(Change.class));
+    verify(indexerMock, times(1)).index(any(ChangeNotes.class));
   }
 
   @Test
@@ -112,7 +111,7 @@
         TEST_CHANGE_ID,
         Operation.INDEX,
         Optional.of(new ChangeIndexEvent("foo", 1, false, "instance-id")));
-    verify(indexerMock, times(1)).index(any(Change.class));
+    verify(indexerMock, times(1)).index(any(ChangeNotes.class));
   }
 
   @Test
@@ -127,14 +126,14 @@
         TEST_CHANGE_ID,
         Operation.INDEX,
         Optional.of(new ChangeIndexEvent("foo", 1, false, "instance-id")));
-    verify(indexerMock, never()).index(any(Change.class));
+    verify(indexerMock, never()).index(any(ChangeNotes.class));
     verify(indexExecutorMock, times(1)).schedule(any(Runnable.class), anyLong(), any());
 
     handler.index(
         TEST_CHANGE_ID,
         Operation.INDEX,
         Optional.of(new ChangeIndexEvent("foo", 1, false, "instance-id")));
-    verify(indexerMock, times(1)).index(any(Change.class));
+    verify(indexerMock, times(1)).index(any(ChangeNotes.class));
   }
 
   @Test
@@ -172,13 +171,13 @@
                   return null;
                 })
         .when(indexerMock)
-        .index(any(Change.class));
+        .index(any(ChangeNotes.class));
 
     assertThat(Context.isForwardedEvent()).isFalse();
     handler.index(TEST_CHANGE_ID, Operation.INDEX, Optional.empty());
     assertThat(Context.isForwardedEvent()).isFalse();
 
-    verify(indexerMock, times(1)).index(any(Change.class));
+    verify(indexerMock, times(1)).index(any(ChangeNotes.class));
   }
 
   @Test
@@ -191,7 +190,7 @@
                   throw new IOException("someMessage");
                 })
         .when(indexerMock)
-        .index(any(Change.class));
+        .index(any(ChangeNotes.class));
 
     assertThat(Context.isForwardedEvent()).isFalse();
     IOException thrown =
@@ -201,7 +200,7 @@
     assertThat(thrown).hasMessageThat().isEqualTo("someMessage");
     assertThat(Context.isForwardedEvent()).isFalse();
 
-    verify(indexerMock, times(1)).index(any(Change.class));
+    verify(indexerMock, times(1)).index(any(ChangeNotes.class));
   }
 
   private void setupChangeAccessRelatedMocks(boolean changeExist, boolean changeUpToDate)
@@ -226,7 +225,7 @@
       when(changeCheckerFactoryMock.create(TEST_CHANGE_ID)).thenReturn(changeCheckerPresentMock);
       when(changeCheckerPresentMock.getChangeNotes()).thenReturn(Optional.of(changeNotes));
       if (storageException) {
-        doThrow(new StorageException("io-error")).when(indexerMock).index(any(Change.class));
+        doThrow(new StorageException("io-error")).when(indexerMock).index(any(ChangeNotes.class));
       }
     }