Merge branch 'stable-2.15'

* stable-2.15:
  pom: Fix plugin name to be all lowercase

Change-Id: I54c52b9371959bf98c06a2eef430067ef34f232c
diff --git a/.bazelversion b/.bazelversion
index 9084fa2..fd2a018 100644
--- a/.bazelversion
+++ b/.bazelversion
@@ -1 +1 @@
-1.1.0
+3.1.0
diff --git a/.gitignore b/.gitignore
index 8c58b98..ec6ea01 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,4 @@
 /buck-cache
 /buck-out
 *.iml
+/.apt_generated/
diff --git a/BUILD b/BUILD
index 82c5dfb..9acc76b 100644
--- a/BUILD
+++ b/BUILD
@@ -5,7 +5,7 @@
     srcs = glob(["src/main/java/**/*.java"]),
     manifest_entries = [
         "Gerrit-PluginName: batch",
-        "Gerrit-ApiVersion: 2.15",
+        "Gerrit-ApiVersion: 2.16",
         "Implementation-Title: Batch Plugin",
         "Implementation-URL: https://gerrit-review.googlesource.com/#/admin/projects/plugins/batch",
         "Gerrit-Module: com.googlesource.gerrit.plugins.batch.Module",
diff --git a/WORKSPACE b/WORKSPACE
index 4f05d1e..4890441 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -3,7 +3,7 @@
 load("//:bazlets.bzl", "load_bazlets")
 
 load_bazlets(
-    commit = "f53f51fb660552d0581aa0ba52c3836ed63d56a3",
+    commit = "22b242638bd36017790d94ffcc18a57f223bda2f",
     #local_path = "/home/<user>/projects/bazlets",
 )
 
diff --git a/pom.xml b/pom.xml
index dfd207a..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.15.7</version>
+  <version>2.16.11.1</version>
   <name>batch</name>
 
   <properties>
diff --git a/src/main/java/com/google/gerrit/server/git/meta/GitFile.java b/src/main/java/com/google/gerrit/server/git/meta/GitFile.java
index beca5b3..ce9505e 100644
--- a/src/main/java/com/google/gerrit/server/git/meta/GitFile.java
+++ b/src/main/java/com/google/gerrit/server/git/meta/GitFile.java
@@ -18,8 +18,6 @@
 import com.google.gerrit.reviewdb.client.File;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.git.GitRepositoryManager;
-import com.google.gerrit.server.git.MetaDataUpdate;
-import com.google.gerrit.server.git.VersionedMetaData;
 import com.google.gerrit.server.project.NoSuchProjectException;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
@@ -58,7 +56,7 @@
   public String read() throws ConfigInvalidException, IOException, NoSuchProjectException {
     Project.NameKey project = branch.getParentKey();
     try (Repository repo = repos.openRepository(project)) {
-      load(repo);
+      load(project, repo);
       return text;
     } catch (RepositoryNotFoundException e) {
       throw new NoSuchProjectException(project);
diff --git a/src/main/java/com/google/gerrit/server/util/RefUpdater.java b/src/main/java/com/google/gerrit/server/util/RefUpdater.java
index c2c700d..818d857 100644
--- a/src/main/java/com/google/gerrit/server/util/RefUpdater.java
+++ b/src/main/java/com/google/gerrit/server/util/RefUpdater.java
@@ -13,6 +13,7 @@
 // limitations under the License.
 package com.google.gerrit.server.util;
 
+import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.reviewdb.client.Branch;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.CurrentUser;
@@ -32,11 +33,9 @@
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.RefUpdate;
 import org.eclipse.jgit.lib.Repository;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class RefUpdater {
-  private static final Logger log = LoggerFactory.getLogger(RefUpdater.class);
+  private static final FluentLogger log = FluentLogger.forEnclosingClass();
 
   public class Args {
     public final Branch.NameKey branch;
@@ -142,7 +141,8 @@
           initUpdate();
           handleResult(runUpdate());
         } catch (IOException err) {
-          log.error("RefUpdate failed: branch not updated: " + branch.get(), err);
+          log.atSevere().withCause(err).log(
+              "RefUpdate failed: branch not updated: %s", branch.get());
           throw err;
         } finally {
           repo.close();
@@ -202,8 +202,8 @@
             project, update.getName(), update.getOldObjectId(), args.newObjectId);
       }
       if (userProvider.get().isIdentifiedUser()) {
-        AccountState accountState = accountCache.get(userProvider.get().getAccountId());
-        gitRefUpdated.fire(project, update, accountState.getAccount());
+        AccountState accountState = accountCache.get(userProvider.get().getAccountId()).get();
+        gitRefUpdated.fire(project, update, accountState);
       }
     }
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchCleaner.java b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchCleaner.java
index b099fa6..e0fb174 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchCleaner.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchCleaner.java
@@ -17,6 +17,7 @@
 import static java.util.concurrent.TimeUnit.DAYS;
 import static java.util.concurrent.TimeUnit.MINUTES;
 
+import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.server.CurrentUser;
@@ -30,12 +31,10 @@
 import java.util.ArrayList;
 import java.util.concurrent.ScheduledFuture;
 import org.eclipse.jgit.lib.Config;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /** Clean up expired batches daily. */
 public class BatchCleaner implements CancelableRunnable {
-  private static final Logger log = LoggerFactory.getLogger(BatchCleaner.class);
+  private static final FluentLogger log = FluentLogger.forEnclosingClass();
 
   public static class Lifecycle implements LifecycleListener {
     public static final long DEFAULT_START_MINUTES = MINUTES.convert(1, MINUTES);
@@ -119,7 +118,7 @@
     try {
       batches = list.getBatches();
     } catch (Exception e) {
-      log.error("getting list of batches to clean.", e);
+      log.atSevere().withCause(e).log("getting list of batches to clean.");
       // Ignore errors and hope someone notices the log file and fixes before the next run
       return;
     }
@@ -131,7 +130,7 @@
       try {
         remover.remove(batch.id);
       } catch (Exception e) {
-        log.error("cleaning batch: " + batch.id, e);
+        log.atSevere().withCause(e).log("cleaning batch: %s", batch.id);
         // Ignore errors and hope someone notices the log file and fixes before the next run
       }
     }
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 6d693df..d582af4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchStore.java
@@ -29,7 +29,6 @@
 import java.util.ConcurrentModificationException;
 import java.util.Date;
 import java.util.List;
-import java.util.Map;
 import javax.inject.Singleton;
 import org.eclipse.jgit.errors.ConfigInvalidException;
 import org.eclipse.jgit.lib.Ref;
@@ -62,10 +61,10 @@
   public List<Batch> find(boolean includeBatchInfo) throws IOException {
     List<Batch> batches = new ArrayList<>();
     try (Repository repo = repoManager.openRepository(project)) {
-      for (Map.Entry<String, Ref> entry : repo.getRefDatabase().getRefs(BATCHES_REF).entrySet()) {
+      for (Ref ref : repo.getRefDatabase().getRefsByPrefix(BATCHES_REF)) {
+        String id = ref.getName().substring(BATCHES_REF.length());
         try {
-          batches.add(
-              (includeBatchInfo == true) ? read(entry.getKey()) : new Batch(entry.getKey()));
+          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 7697dab..3893d3c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/BatchSubmitter.java
@@ -14,7 +14,7 @@
 
 package com.googlesource.gerrit.plugins.batch;
 
-import com.google.gerrit.common.TimeUtil;
+import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.extensions.restapi.AuthException;
 import com.google.gerrit.extensions.restapi.RestApiException;
 import com.google.gerrit.reviewdb.client.Branch;
@@ -26,6 +26,8 @@
 import com.google.gerrit.server.PatchSetUtil;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.gerrit.server.git.MergedByPushOp;
+import com.google.gerrit.server.logging.RequestId;
+import com.google.gerrit.server.logging.TraceContext;
 import com.google.gerrit.server.notedb.ChangeNotes;
 import com.google.gerrit.server.permissions.ChangePermission;
 import com.google.gerrit.server.permissions.PermissionBackend;
@@ -35,24 +37,21 @@
 import com.google.gerrit.server.update.BatchUpdate;
 import com.google.gerrit.server.update.UpdateException;
 import com.google.gerrit.server.util.RefUpdater;
-import com.google.gerrit.server.util.RequestId;
 import com.google.gerrit.server.util.RequestScopePropagator;
+import com.google.gerrit.server.util.time.TimeUtil;
 import com.google.gwtorm.server.OrmException;
 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;
 import org.eclipse.jgit.lib.ObjectReader;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevWalk;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class BatchSubmitter {
-  private static final Logger log = LoggerFactory.getLogger(BatchSubmitter.class);
+  private static final FluentLogger log = FluentLogger.forEnclosingClass();
 
   protected final ReviewDb db;
   protected final GitRepositoryManager repoManager;
@@ -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());
@@ -155,7 +154,7 @@
     Change change = changeNotes.getChange();
     PatchSet ps = psUtil.get(db, changeNotes, psId);
     if (change == null || ps == null) {
-      log.error("" + psId + " is missing");
+      log.atSevere().log("%s is missing", psId);
       return;
     }
 
@@ -166,18 +165,19 @@
     Branch.NameKey destination = change.getDest();
     Project.NameKey project = destination.getParentKey();
 
-    try (Repository repo = repoManager.openRepository(project);
+    try (TraceContext traceContext =
+            TraceContext.open()
+                .addTag(RequestId.Type.SUBMISSION_ID, new RequestId(change.getId().toString()));
+        Repository repo = repoManager.openRepository(project);
         BatchUpdate bu = batchUpdateFactory.create(db, project, user, TimeUtil.nowTs());
         ObjectInserter ins = repo.newObjectInserter();
         ObjectReader reader = ins.newReader();
         RevWalk walk = new RevWalk(reader)) {
       bu.setRepository(repo, walk, ins).updateChangesInParallel();
-      bu.setRequestId(RequestId.forChange(change));
       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/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/MergeChangeCommand.java b/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/MergeChangeCommand.java
index 444b148..37b4428 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/MergeChangeCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/ssh/MergeChangeCommand.java
@@ -18,7 +18,6 @@
 import com.google.gerrit.reviewdb.client.PatchSet;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.OutputFormat;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.gerrit.server.project.NoSuchRefException;
@@ -86,7 +85,6 @@
   @Inject protected MergeBranch.Factory mergeBranchFactory;
   @Inject protected BatchCloser batchCloser;
   @Inject protected ReviewDb db;
-  @Inject protected IdentifiedUser user;
   @Inject protected GitRepositoryManager repoManager;
   protected Map<PatchSet.Id, List<ObjectId>> parentsByPsarg = new HashMap<>();
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/util/MergeBranch.java b/src/main/java/com/googlesource/gerrit/plugins/batch/util/MergeBranch.java
index c766351..5d9323e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/util/MergeBranch.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/util/MergeBranch.java
@@ -14,16 +14,16 @@
 package com.googlesource.gerrit.plugins.batch.util;
 
 import com.google.gerrit.common.Nullable;
-import com.google.gerrit.extensions.client.InheritableBoolean;
 import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.reviewdb.client.BooleanProjectConfig;
 import com.google.gerrit.reviewdb.client.Branch;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.reviewdb.client.RefNames;
 import com.google.gerrit.server.git.GitRepositoryManager;
-import com.google.gerrit.server.git.IntegrationException;
 import com.google.gerrit.server.project.NoSuchRefException;
 import com.google.gerrit.server.project.ProjectCache;
 import com.google.gerrit.server.project.ProjectState;
+import com.google.gerrit.server.submit.IntegrationException;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 import com.googlesource.gerrit.plugins.batch.util.MergeBuilder.FastForwardMode;
@@ -112,8 +112,8 @@
 
   protected MergeStrategy defaultStrategy(MergeStrategy strategy) {
     if (strategy == null) {
-      Project project = projectFromName(projectName);
-      if (project != null && project.getUseContentMerge() == InheritableBoolean.TRUE) {
+      ProjectState project = projectFromName(projectName);
+      if (project != null && project.is(BooleanProjectConfig.USE_CONTENT_MERGE)) {
         return MergeStrategy.RESOLVE;
       }
       return MergeStrategy.SIMPLE_TWO_WAY_IN_CORE;
@@ -121,11 +121,8 @@
     return strategy;
   }
 
-  protected Project projectFromName(Project.NameKey name) {
+  protected ProjectState projectFromName(Project.NameKey name) {
     ProjectState ps = projectCache.get(name);
-    if (ps == null) {
-      return null;
-    }
-    return ps.getProject();
+    return ps != null ? ps : null;
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/batch/util/MergeBuilder.java b/src/main/java/com/googlesource/gerrit/plugins/batch/util/MergeBuilder.java
index 6e5c4a8..72bd980 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/batch/util/MergeBuilder.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/batch/util/MergeBuilder.java
@@ -18,7 +18,7 @@
 import com.google.gerrit.server.GerritPersonIdent;
 import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.git.GitRepositoryManager;
-import com.google.gerrit.server.git.IntegrationException;
+import com.google.gerrit.server.submit.IntegrationException;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 import java.io.IOException;
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