HookArgs: Use IdentifiedUser.getNameEmail to format account name

In most of the hooks, the account is being formatted as either:

  Name Surname (email@domain.com)

when the account has an email address, or:

  Name Surname

when the account does not have an email address. If the account name
is empty the anonymous coward name is used.

Instead of implementing this logic in the hook, defer it to the
existing implementation in IdentifiedUser which properly formats
the name when there is an email address:

  Name Surname <email@domain.com>

and also handles the cases where the account does not have an email
address but instead has a username, or falls back to the account Id.

The names used in the ref-update and commit-received hooks were already
formatted like this, so this change makes the rest of the hooks
consistent.

Change-Id: I99eeab1759dcedfdcc8c689ee6ac4599e3b58325
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookArgs.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookArgs.java
index fa3baec..5379239 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookArgs.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookArgs.java
@@ -21,6 +21,8 @@
 import com.google.gerrit.extensions.common.AccountInfo;
 import com.google.gerrit.extensions.common.ApprovalInfo;
 import com.google.gerrit.extensions.common.ChangeInfo;
+import com.google.gerrit.reviewdb.client.Account;
+import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.config.SitePaths;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.inject.Provider;
@@ -29,7 +31,7 @@
 import java.util.Map;
 
 class HookArgs {
-  final String anonymousCowardName;
+  final IdentifiedUser.GenericFactory identifiedUserFactory;
   final Provider<String> urlProvider;
   final HookMetrics metrics;
   final GitRepositoryManager gitManager;
@@ -38,12 +40,12 @@
   private final List<String> args;
 
   HookArgs(
-      String anonymousCowardName,
+      IdentifiedUser.GenericFactory identifiedUserFactory,
       Provider<String> urlProvider,
       HookMetrics metrics,
       GitRepositoryManager gitManager,
       SitePaths sitePaths) {
-    this.anonymousCowardName = anonymousCowardName;
+    this.identifiedUserFactory = identifiedUserFactory;
     this.urlProvider = urlProvider;
     this.metrics = metrics;
     this.gitManager = gitManager;
@@ -111,10 +113,6 @@
   }
 
   private String format(AccountInfo account) {
-    StringBuilder who = new StringBuilder(firstNonNull(account.name, anonymousCowardName));
-    if (account.email != null) {
-      who.append(" (").append(account.email).append(")");
-    }
-    return who.toString();
+    return identifiedUserFactory.create(new Account.Id(account._accountId)).getNameEmail();
   }
 }
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 be08977..2969358 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookFactory.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookFactory.java
@@ -17,7 +17,7 @@
 import static com.google.common.base.MoreObjects.firstNonNull;
 
 import com.google.gerrit.common.Nullable;
-import com.google.gerrit.server.config.AnonymousCowardName;
+import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.config.CanonicalWebUrl;
 import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.config.SitePaths;
@@ -34,7 +34,7 @@
   private final HookQueue queue;
   private final HookExecutor syncHookExecutor;
   private final Config config;
-  private final String anonymousCowardName;
+  private final IdentifiedUser.GenericFactory identifiedUserFactory;
   private final HookMetrics metrics;
   private final Provider<String> urlProvider;
   private final Path hooksPath;
@@ -46,7 +46,7 @@
       HookQueue queue,
       HookExecutor syncHookExecutor,
       @GerritServerConfig Config config,
-      @AnonymousCowardName String anonymousCowardName,
+      IdentifiedUser.GenericFactory identifiedUserFactory,
       @CanonicalWebUrl @Nullable Provider<String> urlProvider,
       HookMetrics metrics,
       SitePaths sitePaths,
@@ -54,7 +54,7 @@
     this.queue = queue;
     this.syncHookExecutor = syncHookExecutor;
     this.config = config;
-    this.anonymousCowardName = anonymousCowardName;
+    this.identifiedUserFactory = identifiedUserFactory;
     this.metrics = metrics;
     this.urlProvider = urlProvider;
     this.gitManager = gitManager;
@@ -82,6 +82,6 @@
   }
 
   public HookArgs createArgs() {
-    return new HookArgs(anonymousCowardName, urlProvider, metrics, gitManager, sitePaths);
+    return new HookArgs(identifiedUserFactory, urlProvider, metrics, gitManager, sitePaths);
   }
 }