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 <ekempin@google.com> Change-Id: I63d50f4407b4f95eaaf73e41c5a1288ed4a16eb4
This commit is contained in:
		| @@ -19,6 +19,7 @@ import static com.google.gerrit.extensions.client.GeneralPreferencesInfo.EmailSt | ||||
| import static java.util.Objects.requireNonNull; | ||||
|  | ||||
| import com.google.common.flogger.FluentLogger; | ||||
| import com.google.gerrit.common.Nullable; | ||||
| import com.google.gerrit.exceptions.EmailException; | ||||
| import com.google.gerrit.extensions.api.changes.RecipientType; | ||||
| import com.google.gerrit.extensions.client.GeneralPreferencesInfo; | ||||
| @@ -319,7 +320,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(); | ||||
|     } | ||||
| @@ -345,7 +346,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> account = args.accountCache.get(accountId).map(AccountState::account); | ||||
|     if (account.isPresent()) { | ||||
|       String name = account.get().fullName(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Edwin Kempin
					Edwin Kempin