From 11667a12328610224d2934ae050860a4c10e63e3 Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Mon, 30 Sep 2019 09:53:47 +0200 Subject: OutgoingEmail: Handle null accountId in getNameEmailFor(Account.Id) method If an email should be sent and a current user is not available (e.g. because the operation is executed in the background) we cannot set an account ID as the sender of the email. In this case we skip calling OutgoingEmail#setFrom(Account.Id) and the fromId stays null (e.g. see AbandonOp which can be executed in the background). If the fromId is null and we setup the soy context (ChangeEmail#setupSoyContext()) we get a NullPointerException when trying to set the fromEmail field. This is because getNameEmailFor(Account.Id) doesn't expect null as value. In contrast to this method, the getNameFor(Account.Id) method which is called before to set the fromName field does handle null as value by falling back to the Gerrit person ident. Fix the NullPointerException in getNameEmailFor(Account.Id) by doing the same, and make it also fall back to the Gerrit person ident if the given account ID is null. Signed-off-by: Edwin Kempin Change-Id: I63d50f4407b4f95eaaf73e41c5a1288ed4a16eb4 (cherry picked from commit 32cf19ad46ff5e3984e794544a9ae524fa4ab3e9) --- java/com/google/gerrit/server/mail/send/OutgoingEmail.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/java/com/google/gerrit/server/mail/send/OutgoingEmail.java b/java/com/google/gerrit/server/mail/send/OutgoingEmail.java index 664c1bf8a5..5b131692fb 100644 --- a/java/com/google/gerrit/server/mail/send/OutgoingEmail.java +++ b/java/com/google/gerrit/server/mail/send/OutgoingEmail.java @@ -22,6 +22,7 @@ import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ListMultimap; import com.google.common.collect.Sets; import com.google.common.flogger.FluentLogger; +import com.google.gerrit.common.Nullable; import com.google.gerrit.common.errors.EmailException; import com.google.gerrit.extensions.api.changes.NotifyHandling; import com.google.gerrit.extensions.api.changes.RecipientType; @@ -340,7 +341,7 @@ public abstract class OutgoingEmail { } /** Lookup a human readable name for an account, usually the "full name". */ - protected String getNameFor(Account.Id accountId) { + protected String getNameFor(@Nullable Account.Id accountId) { if (accountId == null) { return args.gerritPersonIdent.getName(); } @@ -366,7 +367,14 @@ public abstract class OutgoingEmail { * @param accountId user to fetch. * @return name/email of account, or Anonymous Coward if unset. */ - protected String getNameEmailFor(Account.Id accountId) { + protected String getNameEmailFor(@Nullable Account.Id accountId) { + if (accountId == null) { + return args.gerritPersonIdent.getName() + + " <" + + args.gerritPersonIdent.getEmailAddress() + + ">"; + } + Optional account = args.accountCache.get(accountId).map(AccountState::getAccount); if (account.isPresent()) { String name = account.get().getFullName(); -- cgit v1.2.3