Merge branch 'stable-3.4'

* stable-3.4:
  Add Zuul postflight publish job
  docker: make sure to cleanup containers before force exit
  Harden shell commands
  Enable compiler warnings as errors
  Add bazel target for junit tests
  Add Zuul configuration to run tests on the CI node

Change-Id: Ice2cade040460ca6c86af8dab64d55e9bdf2f6fd
diff --git a/.zuul.yaml b/.zuul.yaml
index 330d73f..448faaf 100644
--- a/.zuul.yaml
+++ b/.zuul.yaml
@@ -5,7 +5,15 @@
         tools/playbooks/install_docker.yaml
 
 - project:
+    description: |
+      Build the plugin in check, and also build and publish it after
+      every merged commit.
     check:
       jobs:
         - plugins-events-build
-
+    post:
+      jobs:
+        - plugins-events-build
+        - gerrit-plugin-publish:
+            dependencies:
+              - plugins-events-build
diff --git a/BUILD b/BUILD
index 98e5b3b..4843927 100644
--- a/BUILD
+++ b/BUILD
@@ -1,10 +1,10 @@
+load("//tools/bzl:junit.bzl", "junit_tests")
 load(
     "//tools/bzl:plugin.bzl",
     "PLUGIN_DEPS",
     "PLUGIN_TEST_DEPS",
     "gerrit_plugin",
 )
-
 load("@rules_java//java:defs.bzl", "java_library", "java_plugin")
 
 plugin_name = "events"
@@ -20,13 +20,27 @@
         "Gerrit-SshModule: com.googlesource.gerrit.plugins.events.SshModule",
     ],
     resources = glob(["src/main/resources/**/*"]),
+    javacopts = [ "-Werror", "-Xlint:all", "-Xlint:-classfile", "-Xlint:-processing"],
+)
+
+junit_tests(
+    name = "events_tests",
+    size = "small",
+    srcs = glob(["src/test/java/**/*Test.java"]),
+    tags = ["events"],
+    deps = PLUGIN_TEST_DEPS + PLUGIN_DEPS + [
+        plugin_name,
+    ],
 )
 
 sh_test(
     name = "docker-tests",
     size = "medium",
     srcs = ["test/docker/run.sh"],
-    args = ["--events-plugin-jar", "$(location :events)"],
+    args = [
+        "--events-plugin-jar",
+        "$(location :events)",
+    ],
     data = [plugin_name] + glob(["test/**"]),
     local = True,
 )
diff --git a/src/main/java/com/googlesource/gerrit/plugins/events/StreamEvents.java b/src/main/java/com/googlesource/gerrit/plugins/events/StreamEvents.java
index 7a8c198..c22cb4b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/events/StreamEvents.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/events/StreamEvents.java
@@ -67,7 +67,7 @@
       }
 
       try {
-        resume = new Long(ids[1]);
+        resume = Long.parseLong(ids[1]);
         return;
       } catch (NumberFormatException e) { // fall through
       }
diff --git a/test/docker/run.sh b/test/docker/run.sh
index d4ae3fa..5b1894d 100755
--- a/test/docker/run.sh
+++ b/test/docker/run.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 
-readlink --canonicalize / &> /dev/null || readlink() { greadlink "$@" ; } # for MacOS
+readlink -f / &> /dev/null || readlink() { greadlink "$@" ; } # for MacOS
 MYDIR=$(dirname -- "$(readlink -f -- "$0")")
 ARTIFACTS=$MYDIR/gerrit/artifacts
 
@@ -78,15 +78,14 @@
 COMPOSE_ARGS=(--project-name "$PROJECT_NAME" -f "$COMPOSE_YAML")
 check_prerequisite
 mkdir -p -- "$ARTIFACTS"
-[ -n "$EVENTS_PLUGIN_JAR" ] && cp -f "$EVENTS_PLUGIN_JAR" "$ARTIFACTS/events.jar"
+[ -n "$EVENTS_PLUGIN_JAR" ] && cp -f -- "$EVENTS_PLUGIN_JAR" "$ARTIFACTS/events.jar"
 if [ ! -e "$ARTIFACTS/events.jar" ] ; then
     MISSING="Missing $ARTIFACTS/events.jar"
     [ -n "$EVENTS_PLUGIN_JAR" ] && die "$MISSING, check for copy failure?"
     usage "$MISSING, did you forget --events-plugin-jar?"
 fi
-[ -n "$GERRIT_WAR" ] && cp -f "$GERRIT_WAR" "$ARTIFACTS/gerrit.war"
-progress "Building docker images" build_images
-run_events_plugin_tests ; RESULT=$?
-cleanup
-
-exit "$RESULT"
+[ -n "$GERRIT_WAR" ] && cp -f -- "$GERRIT_WAR" "$ARTIFACTS/gerrit.war"
+( trap cleanup EXIT SIGTERM
+    progress "Building docker images" build_images
+    run_events_plugin_tests
+)
diff --git a/test/test_events_plugin.sh b/test/test_events_plugin.sh
index f44d51e..6d88db9 100755
--- a/test/test_events_plugin.sh
+++ b/test/test_events_plugin.sh
@@ -118,8 +118,8 @@
     [ -n "$count" ] || count=1
 
     # Re-create the fifo to ensure that is is empty
-    rm -f "$EVENT_FIFO"
-    mkfifo "$EVENT_FIFO"
+    rm -f -- "$EVENT_FIFO"
+    mkfifo -- "$EVENT_FIFO"
 
     head -n $count < "$EVENT_FIFO" > "$EVENTS" &
     CAPTURE_PID_HEAD=$!
@@ -195,17 +195,18 @@
     [ -n "$REF_BRANCH" ] || usage "ref branch not set"
 }
 
-MYPROG=$(basename "$0")
-MYDIR=$(dirname "$0")
+readlink -f / &> /dev/null || readlink() { greadlink "$@" ; } # for MacOS
+MYDIR=$(dirname -- "$(readlink -f -- "$0")")
+MYPROG=$(basename -- "$0")
 
 source "$MYDIR/lib_result.sh"
 
 parseArgs "$@"
 
 TEST_DIR=$MYDIR/../target/test
-rm -rf "$TEST_DIR"
-mkdir -p "$TEST_DIR"
-TEST_DIR=$(readlink -f "$TEST_DIR")
+rm -rf -- "$TEST_DIR"
+mkdir -p -- "$TEST_DIR"
+TEST_DIR=$(readlink -f -- "$TEST_DIR")
 
 REST_API_CHANGES_URL="http://$SERVER:8080/a/changes"
 GITURL=ssh://$SERVER:29418/$PROJECT
@@ -214,7 +215,7 @@
 git ls-remote "$GITURL" | grep -q "$DEST_REF" || usage "invalid project/server/ref"
 
 REPO_DIR=$TEST_DIR/repo
-q git init "$REPO_DIR"
+q git init -- "$REPO_DIR"
 GIT_DIR="$REPO_DIR/.git"
 FILE_A="$REPO_DIR/fileA"
 
@@ -287,7 +288,7 @@
 
 # ------------------------- Compare them all to Core -------------------------
 
-out=$(diff "$EVENTS_CORE" "$EVENTS_PLUGIN")
+out=$(diff -- "$EVENTS_CORE" "$EVENTS_PLUGIN")
 result "core/plugin diff" "$out"
 
 exit $RESULT
diff --git a/tools/bzl/junit.bzl b/tools/bzl/junit.bzl
new file mode 100644
index 0000000..97307bd
--- /dev/null
+++ b/tools/bzl/junit.bzl
@@ -0,0 +1,6 @@
+load(
+    "@com_googlesource_gerrit_bazlets//tools:junit.bzl",
+    _junit_tests = "junit_tests",
+)
+
+junit_tests = _junit_tests
diff --git a/tools/playbooks/install_docker.yaml b/tools/playbooks/install_docker.yaml
index eabfe44..89cf315 100644
--- a/tools/playbooks/install_docker.yaml
+++ b/tools/playbooks/install_docker.yaml
@@ -6,4 +6,3 @@
       shell: |
         sudo curl -L "https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
         sudo chmod +x /usr/local/bin/docker-compose
-