'query --comments --format=JSON': fix NPE if change message author is null

Creating the JSON output for a change message fails with a
NullPointerException if the author of the change message is null. The
author of a change message is null if the change message was created by
Gerrit and not by a user (e.g. the message that is added to a change on
submit if the change cannot be merged due to conflicts).

If the user of a change message is null, use the Gerrit Server identity
to generate the author information.

Change-Id: Ia3fb2a63c760cc2341372ddb0358e6f10b5bf435
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java b/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java
index ab3ca56..f07ae0c 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/events/EventFactory.java
@@ -28,6 +28,7 @@
 import com.google.gerrit.reviewdb.client.RevId;
 import com.google.gerrit.reviewdb.client.TrackingId;
 import com.google.gerrit.reviewdb.server.ReviewDb;
+import com.google.gerrit.server.GerritPersonIdent;
 import com.google.gerrit.server.account.AccountCache;
 import com.google.gerrit.server.config.CanonicalWebUrl;
 import com.google.gerrit.server.patch.PatchList;
@@ -41,6 +42,7 @@
 import com.google.inject.Singleton;
 
 import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.PersonIdent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -59,17 +61,20 @@
   private final ApprovalTypes approvalTypes;
   private final PatchListCache patchListCache;
   private final SchemaFactory<ReviewDb> schema;
+  private final PersonIdent myIdent;
 
   @Inject
   EventFactory(AccountCache accountCache,
       @CanonicalWebUrl @Nullable Provider<String> urlProvider,
       ApprovalTypes approvalTypes,
-      PatchListCache patchListCache, SchemaFactory<ReviewDb> schema) {
+      PatchListCache patchListCache, SchemaFactory<ReviewDb> schema,
+      @GerritPersonIdent PersonIdent myIdent) {
     this.accountCache = accountCache;
     this.urlProvider = urlProvider;
     this.approvalTypes = approvalTypes;
     this.patchListCache = patchListCache;
     this.schema = schema;
+    this.myIdent = myIdent;
   }
 
   /**
@@ -390,6 +395,20 @@
   }
 
   /**
+   * Create an AuthorAttribute for the given person ident suitable for
+   * serialization to JSON.
+   *
+   * @param ident
+   * @return object suitable for serialization to JSON
+   */
+  public AccountAttribute asAccountAttribute(PersonIdent ident) {
+    AccountAttribute who = new AccountAttribute();
+    who.name = ident.getName();
+    who.email = ident.getEmailAddress();
+    return who;
+  }
+
+  /**
    * Create an ApprovalAttribute for the given approval suitable for
    * serialization to JSON.
    *
@@ -413,7 +432,9 @@
   public MessageAttribute asMessageAttribute(ChangeMessage message) {
     MessageAttribute a = new MessageAttribute();
     a.timestamp = message.getWrittenOn().getTime() / 1000L;
-    a.reviewer = asAccountAttribute(message.getAuthor());
+    a.reviewer =
+        message.getAuthor() != null ? asAccountAttribute(message.getAuthor())
+            : asAccountAttribute(myIdent);
     a.message = message.getMessage();
     return a;
   }