'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>
This commit is contained in:
Edwin Kempin
2012-10-22 16:21:33 +02:00
parent 2dc9eda865
commit d04d31cf20

View File

@@ -28,6 +28,7 @@ import com.google.gerrit.reviewdb.client.PatchSetApproval;
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.Provider;
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 @@ public class EventFactory {
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;
}
/**
@@ -389,6 +394,20 @@ public class EventFactory {
return who;
}
/**
* 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 class EventFactory {
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;
}