Merge branch 'stable-2.15'

* stable-2.15:
  Implement change-deleted hook

Change-Id: Ia806e8c90e3b1189a2e8277ddd792f79e6c0cdc8
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/CommitReceived.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/CommitReceived.java
index b03a77b..74a0817 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/CommitReceived.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/CommitReceived.java
@@ -51,7 +51,7 @@
     args.add("--project", projectName);
     args.add("--refname", refname);
     args.add("--uploader", receiveEvent.user.getNameEmail());
-    args.add("--uploader-username", receiveEvent.user.getUserName());
+    args.add("--uploader-username", receiveEvent.user.getUserName().orElse(null));
     args.add("--oldrev", old.name());
     args.add("--newrev", receiveEvent.commit.name());
     args.add("--cmdref", commandRef);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookExecutor.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookExecutor.java
index 42d4e75..c54009a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookExecutor.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookExecutor.java
@@ -1,8 +1,10 @@
 package com.googlesource.gerrit.plugins.hooks;
 
+import com.google.common.flogger.FluentLogger;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.server.config.GerritServerConfig;
+import com.google.gerrit.server.logging.LoggingContextAwareExecutorService;
 import com.google.inject.Inject;
 import java.lang.Thread.UncaughtExceptionHandler;
 import java.nio.file.Files;
@@ -13,16 +15,15 @@
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import org.eclipse.jgit.lib.Config;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class HookExecutor implements LifecycleListener {
-  private static final Logger log = LoggerFactory.getLogger(HookExecutor.class);
+  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
+
   private static final UncaughtExceptionHandler LOG_UNCAUGHT_EXCEPTION =
       new UncaughtExceptionHandler() {
         @Override
         public void uncaughtException(Thread t, Throwable e) {
-          log.error("HookExecutor thread {} threw exception", t.getName(), e);
+          logger.atSevere().withCause(e).log("HookExecutor thread %s threw exception", t.getName());
         }
       };
 
@@ -33,11 +34,12 @@
   HookExecutor(@GerritServerConfig Config config) {
     this.timeout = config.getInt("hooks", "syncHookTimeout", 30);
     this.threadPool =
-        Executors.newCachedThreadPool(
-            new ThreadFactoryBuilder()
-                .setNameFormat("SyncHook-%d")
-                .setUncaughtExceptionHandler(LOG_UNCAUGHT_EXCEPTION)
-                .build());
+        new LoggingContextAwareExecutorService(
+            Executors.newCachedThreadPool(
+                new ThreadFactoryBuilder()
+                    .setNameFormat("SyncHook-%d")
+                    .setUncaughtExceptionHandler(LOG_UNCAUGHT_EXCEPTION)
+                    .build()));
   }
 
   HookResult submit(Path hook, HookArgs args) {
@@ -46,7 +48,7 @@
 
   HookResult submit(String projectName, Path hook, HookArgs args) {
     if (!Files.exists(hook)) {
-      log.debug("Hook file not found: {}", hook);
+      logger.atFine().log("Hook file not found: %s", hook);
       return null;
     }
     HookTask.Sync hookTask = new HookTask.Sync(projectName, hook, args);
@@ -58,10 +60,10 @@
       return task.get(timeout, TimeUnit.SECONDS);
     } catch (TimeoutException e) {
       message = "Synchronous hook timed out " + hook;
-      log.error(message);
+      logger.atSevere().log(message);
     } catch (Exception e) {
       message = "Error running hook " + hook;
-      log.error(message, e);
+      logger.atSevere().withCause(e).log(message);
     }
     task.cancel(true);
     hookTask.cancel();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookFactory.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookFactory.java
index f3d8293..afd189d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookFactory.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookFactory.java
@@ -16,6 +16,7 @@
 
 import static com.google.common.base.MoreObjects.firstNonNull;
 
+import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.config.SitePaths;
 import com.google.inject.Inject;
@@ -23,12 +24,10 @@
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import org.eclipse.jgit.lib.Config;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 @Singleton
 public class HookFactory {
-  private static final Logger log = LoggerFactory.getLogger(HookFactory.class);
+  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
 
   private final HookQueue queue;
   private final HookExecutor syncHookExecutor;
@@ -54,13 +53,13 @@
     } else {
       this.hooksPath = sitePaths.hooks_dir;
     }
-    log.info("hooks.path: {}", this.hooksPath);
+    logger.atInfo().log("hooks.path: %s", this.hooksPath);
   }
 
   private Path getHookPath(String configName, String defaultName) {
     String v = config.getString("hooks", null, configName);
     Path hookPath = hooksPath.resolve(firstNonNull(v, defaultName));
-    log.info("hooks.{} resolved to {}", configName, hookPath);
+    logger.atInfo().log("hooks.%s resolved to %s", configName, hookPath);
     return hookPath;
   }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookQueue.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookQueue.java
index 1b2a307..88ae311 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookQueue.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookQueue.java
@@ -14,17 +14,16 @@
 
 package com.googlesource.gerrit.plugins.hooks;
 
+import com.google.common.flogger.FluentLogger;
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.server.git.WorkQueue;
 import com.google.inject.Inject;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.concurrent.ScheduledExecutorService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 class HookQueue implements LifecycleListener {
-  private static final Logger log = LoggerFactory.getLogger(HookQueue.class);
+  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
 
   private final WorkQueue workQueue;
 
@@ -41,7 +40,7 @@
 
   void submit(String projectName, Path hook, HookArgs args) {
     if (!Files.exists(hook)) {
-      log.debug("Hook file not found: {}", hook.toAbsolutePath());
+      logger.atFine().log("Hook file not found: %s", hook.toAbsolutePath());
       return;
     }
     queue.submit(new HookTask.Async(projectName, hook, args));
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookTask.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookTask.java
index b6654da..8b06a24 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookTask.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookTask.java
@@ -16,6 +16,7 @@
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 
+import com.google.common.flogger.FluentLogger;
 import com.google.common.io.ByteStreams;
 import com.google.gerrit.metrics.Timer1;
 import com.google.gerrit.reviewdb.client.Project;
@@ -29,11 +30,9 @@
 import java.util.Map;
 import java.util.concurrent.Callable;
 import org.eclipse.jgit.lib.Repository;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 class HookTask {
-  private static final Logger log = LoggerFactory.getLogger(HookTask.class);
+  private static final FluentLogger logger = FluentLogger.forEnclosingClass();
 
   private final Path sitePath;
   private final String projectName;
@@ -113,25 +112,25 @@
     } catch (InterruptedException iex) {
       // InterruptedException - timeout or cancel
       args.metrics.timeout(name);
-      log.error("hook[{}] timed out: {}", name, iex.getMessage());
+      logger.atSevere().log("hook[%s] timed out: %s", name, iex.getMessage());
     } catch (Throwable err) {
       args.metrics.error(name);
-      log.error("Error running hook {}", hook.toAbsolutePath(), err);
+      logger.atSevere().withCause(err).log("Error running hook %s", hook.toAbsolutePath());
     }
 
     if (result != null) {
       int exitValue = result.getExitValue();
       if (exitValue != 0) {
-        log.error("hook[{}] exited with error status: {}", name, exitValue);
+        logger.atSevere().log("hook[%s] exited with error status: %d", name, exitValue);
       }
 
-      if (log.isDebugEnabled()) {
+      if (logger.atFine().isEnabled()) {
         try (BufferedReader br = new BufferedReader(new StringReader(result.getOutput()))) {
           br.lines()
               .filter(s -> !s.isEmpty())
-              .forEach(line -> log.debug("hook[{}] output: {}", name, line));
+              .forEach(line -> logger.atFine().log("hook[%s] output: %s", name, line));
         } catch (IOException iox) {
-          log.error("Error writing hook [{}] output", name, iox);
+          logger.atSevere().withCause(iox).log("Error writing hook [%s] output", name);
         }
       }
     }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/RefUpdate.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/RefUpdate.java
index 7eedfa5..90a1b93 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/RefUpdate.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/RefUpdate.java
@@ -49,7 +49,7 @@
     HookArgs args = hookFactory.createArgs();
     args.add("--project", projectName);
     args.add("--uploader", refEvent.user.getNameEmail());
-    args.add("--uploader-username", refEvent.user.getUserName());
+    args.add("--uploader-username", refEvent.user.getUserName().orElse(null));
     args.add("--oldrev", getObjectId(refEvent.command.getOldId()).getName());
     args.add("--newrev", getObjectId(refEvent.command.getNewId()).getName());
     args.add("--refname", refEvent.command.getRefName());
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/Submit.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/Submit.java
index 8e2f1e0..7474553 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/Submit.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/Submit.java
@@ -51,7 +51,7 @@
     args.add("--project", projectName);
     args.add("--branch", destBranch.get());
     args.add("--submitter", caller.getNameEmail());
-    args.add("--submitter-username", caller.getUserName());
+    args.add("--submitter-username", caller.getUserName().get());
     args.add("--patchset", patchSetId.get());
     args.add("--commit", commit.getId().name());