Merge branch 'stable-2.16'

* stable-2.16: (47 commits)
  pom: Fix plugin name to be all lowercase
  Fix getRefsByPrefix usage
  Adapt to CurrentUser#getUserName changes
  Upgrade bazlets to latest stable-2.16 to build with 2.16.19 API
  Upgrade bazlets to latest stable-2.16 to build with 2.16.18 API
  Bump Bazel version to 3.1.0
  Bump Bazel version to 3.0.0
  Upgrade bazlets to latest stable-2.16 to build with 2.16.17 API
  Upgrade bazlets to latest stable-2.16
  Bump Bazel version to 2.2.0
  Bump required Bazel version to 2.1.0
  Upgrade bazlets to latest stable-2.16 to build with 2.16.16 API
  Add an empty tools/BUILD file explicitly for Bazel
  Upgrade bazlets to latest stable-2.16
  Upgrade bazlets to latest stable-2.16
  Upgrade bazlets to latest stable-2.15
  Bump Bazel version to 2.0.0
  Upgrade bazlets to latest stable-2.16 to build with 2.16.15 API
  Upgrade bazlets to latest stable-2.16 to build with 2.16.14 API
  Upgrade bazlets to latest stable-2.16 to build with 2.16.13 API
  ...

Change-Id: I64b8474ee652fee4463374770aa988901a48dfc6
diff --git a/.bazelrc b/.bazelrc
index 4ed16cf..3ae03ff 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,2 +1,2 @@
-build --workspace_status_command=./tools/workspace-status.sh
+build --workspace_status_command="python ./tools/workspace_status.py"
 test --build_tests_only
diff --git a/.bazelversion b/.bazelversion
new file mode 100644
index 0000000..fd2a018
--- /dev/null
+++ b/.bazelversion
@@ -0,0 +1 @@
+3.1.0
diff --git a/WORKSPACE b/WORKSPACE
index a9508bd..4890441 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -3,7 +3,7 @@
 load("//:bazlets.bzl", "load_bazlets")
 
 load_bazlets(
-    commit = "a5525ae2c43e61cf113dacc03691cb2bcf44fb12",
+    commit = "22b242638bd36017790d94ffcc18a57f223bda2f",
     #local_path = "/home/<user>/projects/bazlets",
 )
 
diff --git a/pom.xml b/pom.xml
index d10f765..ae82977 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,7 +22,7 @@
   <groupId>com.googlesource.gerrit.plugins.batch</groupId>
   <artifactId>batch</artifactId>
   <packaging>jar</packaging>
-  <version>2.16</version>
+  <version>2.16.11.1</version>
   <name>batch</name>
 
   <properties>
@@ -39,7 +39,7 @@
         <configuration>
           <archive>
             <manifestEntries>
-              <Gerrit-PluginName>Batch</Gerrit-PluginName>
+              <Gerrit-PluginName>batch</Gerrit-PluginName>
               <Gerrit-Module>com.googlesource.gerrit.plugins.batch.Module</Gerrit-Module>
               <Gerrit-SshModule>com.googlesource.gerrit.plugins.batch.ssh.SshModule</Gerrit-SshModule>
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchCloser.java b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchCloser.java
index 5f68d7c..a528d2f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchCloser.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchCloser.java
@@ -56,9 +56,9 @@
 
   protected String getBatchRef(Batch batch, Batch.Destination dest) {
     // AccountId is always present, UserName is optional but the preferred identifier
-    if (user.getUserName() != null) {
+    if (user.getUserName().isPresent()) {
       return String.format(
-          "refs/batch/%s/%s/%s/%s", "users", user.getUserName(), batch.id, dest.ref);
+          "refs/batch/%s/%s/%s/%s", "users", user.getUserName().get(), batch.id, dest.ref);
     }
     return String.format(
         "refs/batch/%s/%s/%s/%s",
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
index 9c0db7b..d582af4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
@@ -62,8 +62,9 @@
     List<Batch> batches = new ArrayList<>();
     try (Repository repo = repoManager.openRepository(project)) {
       for (Ref ref : repo.getRefDatabase().getRefsByPrefix(BATCHES_REF)) {
+        String id = ref.getName().substring(BATCHES_REF.length());
         try {
-          batches.add((includeBatchInfo == true) ? read(ref.getName()) : new Batch(ref.getName()));
+          batches.add((includeBatchInfo == true) ? read(id) : new Batch(id));
         } catch (NoSuchBatchException e) {
           continue;
         }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java
index 0c9adb4..3893d3c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java
@@ -43,7 +43,6 @@
 import com.google.inject.Inject;
 import com.googlesource.gerrit.plugins.batch.exception.NoSuchBatchException;
 import java.io.IOException;
-import java.util.Collection;
 import org.eclipse.jgit.errors.RepositoryNotFoundException;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ObjectInserter;
@@ -126,9 +125,7 @@
           RestApiException, UpdateException, PermissionBackendException {
     for (Batch.Destination dest : batch.listDestinations()) {
       updateRef(dest);
-      if (dest.changes != null) {
-        closeChanges(dest.changes);
-      }
+      closeChanges(dest);
     }
   }
 
@@ -139,15 +136,17 @@
     refUpdater.forceUpdate(branch, ObjectId.fromString(dest.sha1));
   }
 
-  private void closeChanges(Collection<Batch.Change> changes)
+  private void closeChanges(Batch.Destination dest)
       throws IOException, OrmException, RepositoryNotFoundException, RestApiException,
           UpdateException, PermissionBackendException {
-    for (Batch.Change change : changes) {
-      closeChange(change.toPatchSetId());
+    if (dest.changes != null) {
+      for (Batch.Change change : dest.changes) {
+        closeChange(change.toPatchSetId(), dest.sha1);
+      }
     }
   }
 
-  private void closeChange(PatchSet.Id psId)
+  private void closeChange(PatchSet.Id psId, String sha1)
       throws IOException, OrmException, RepositoryNotFoundException, RestApiException,
           UpdateException, PermissionBackendException {
     ChangeNotes changeNotes = notesFactory.createChecked(psId.getParentKey());
@@ -178,7 +177,7 @@
       bu.setRefLogMessage("merged (batch submit)");
       bu.addOp(
           psId.getParentKey(),
-          mergedByPushOpFactory.create(requestScopePropagator, psId, destination.get()));
+          mergedByPushOpFactory.create(requestScopePropagator, psId, destination.get(), sha1));
       bu.execute();
     }
   }
diff --git a/tools/BUILD b/tools/BUILD
new file mode 100644
index 0000000..1fa2160
--- /dev/null
+++ b/tools/BUILD
@@ -0,0 +1 @@
+# Empty file - bazel treat directories with BUILD file as a package
\ No newline at end of file
diff --git a/tools/bzl/classpath.bzl b/tools/bzl/classpath.bzl
index d5764f7..c921d01 100644
--- a/tools/bzl/classpath.bzl
+++ b/tools/bzl/classpath.bzl
@@ -1,4 +1,6 @@
 load(
     "@com_googlesource_gerrit_bazlets//tools:classpath.bzl",
-    "classpath_collector",
+    _classpath_collector = "classpath_collector",
 )
+
+classpath_collector = _classpath_collector
diff --git a/tools/bzl/plugin.bzl b/tools/bzl/plugin.bzl
index 0b25d23..4d2dbdd 100644
--- a/tools/bzl/plugin.bzl
+++ b/tools/bzl/plugin.bzl
@@ -1,6 +1,10 @@
 load(
     "@com_googlesource_gerrit_bazlets//:gerrit_plugin.bzl",
-    "PLUGIN_DEPS",
-    "PLUGIN_TEST_DEPS",
-    "gerrit_plugin",
+    _gerrit_plugin = "gerrit_plugin",
+    _plugin_deps = "PLUGIN_DEPS",
+    _plugin_test_deps = "PLUGIN_TEST_DEPS",
 )
+
+gerrit_plugin = _gerrit_plugin
+PLUGIN_DEPS = _plugin_deps
+PLUGIN_TEST_DEPS = _plugin_test_deps
diff --git a/tools/workspace-status.sh b/tools/workspace-status.sh
deleted file mode 100755
index 71b2eeb..0000000
--- a/tools/workspace-status.sh
+++ /dev/null
@@ -1,16 +0,0 @@
-#!/bin/bash
-
-# This script will be run by bazel when the build process starts to
-# generate key-value information that represents the status of the
-# workspace. The output should be like
-#
-# KEY1 VALUE1
-# KEY2 VALUE2
-#
-# If the script exits with non-zero code, it's considered as a failure
-# and the output will be discarded.
-
-function rev() {
-  git describe --always --match "v[0-9].*" --dirty
-}
-echo STABLE_BUILD_BATCH_LABEL $(rev)
diff --git a/tools/workspace_status.py b/tools/workspace_status.py
new file mode 100644
index 0000000..829f438
--- /dev/null
+++ b/tools/workspace_status.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+
+# This script will be run by bazel when the build process starts to
+# generate key-value information that represents the status of the
+# workspace. The output should be like
+#
+# KEY1 VALUE1
+# KEY2 VALUE2
+#
+# If the script exits with non-zero code, it's considered as a failure
+# and the output will be discarded.
+
+from __future__ import print_function
+import subprocess
+import sys
+
+CMD = ['git', 'describe', '--always', '--match', 'v[0-9].*', '--dirty']
+
+
+def revision():
+    try:
+        return subprocess.check_output(CMD).strip().decode("utf-8")
+    except OSError as err:
+        print('could not invoke git: %s' % err, file=sys.stderr)
+        sys.exit(1)
+    except subprocess.CalledProcessError as err:
+        print('error using git: %s' % err, file=sys.stderr)
+        sys.exit(1)
+
+
+print("STABLE_BUILD_BATCH_LABEL %s" % revision())