Use AccountCache#maybeGet instead of AccountCache#get
AccountCache#get returns an empty AccountState to represent a missing
account and the full name and the preferred email of the empty
AccountState are always null. Handle the missing account case explicitly
instead of using an empty AccountState.
Change-Id: I340d1b426304550ae9b368623e15291f8f1dbb92
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
index 33915e1..633cb5f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
@@ -26,6 +26,7 @@
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountCache;
+import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.config.AnonymousCowardName;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.git.LockFailureException;
@@ -272,12 +273,18 @@
} else {
LabelType type = labelTypes.byLabel(a.getLabelId());
if (type != null) {
- fmt.appendApproval(type, a.getValue(), accountCache.get(a.getAccountId()).getAccount());
+ fmt.appendApproval(
+ type,
+ a.getValue(),
+ a.getAccountId(),
+ accountCache.maybeGet(a.getAccountId()).map(AccountState::getAccount));
}
}
}
if (submit != null) {
- fmt.appendSubmittedBy(accountCache.get(submit.getAccountId()).getAccount());
+ fmt.appendSubmittedBy(
+ submit.getAccountId(),
+ accountCache.maybeGet(submit.getAccountId()).map(AccountState::getAccount));
fmt.appendSubmittedAt(submit.getGranted());
}
if (canonicalWebUrl != null) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/HeaderFormatter.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/HeaderFormatter.java
index 8633d5c..3f7d406 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/HeaderFormatter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/HeaderFormatter.java
@@ -14,6 +14,8 @@
package com.googlesource.gerrit.plugins.reviewnotes;
+import static com.google.common.base.Preconditions.checkState;
+
import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.common.data.LabelValue;
import com.google.gerrit.reviewdb.client.Account;
@@ -23,6 +25,7 @@
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
+import java.util.Optional;
import java.util.TimeZone;
/**
@@ -48,34 +51,57 @@
sb.append("Change-Id: ").append(changeKey.get()).append("\n");
}
- void appendApproval(LabelType label, short value, Account user) {
+ /**
+ * Appends a header for an approval.
+ *
+ * @param label the label on which the approval was done
+ * @param value the voting value
+ * @param accountId the account ID of the approver
+ * @param account the account of the approver, can be {@link Optional#empty} if the account is
+ * missing
+ */
+ void appendApproval(
+ LabelType label, short value, Account.Id accountId, Optional<Account> account) {
sb.append(label.getName());
sb.append(LabelValue.formatValue(value));
sb.append(": ");
- appendUserData(user);
+ appendUserData(accountId, account);
sb.append("\n");
}
- private void appendUserData(Account user) {
+ /**
+ * Appends user data.
+ *
+ * @param accountId the ID of the account
+ * @param account the account, can be {link Optional#empty} if the account is missing
+ */
+ private void appendUserData(Account.Id accountId, Optional<Account> account) {
+ checkState(
+ !account.isPresent() || accountId.equals(account.get().getId()), "mismatching account IDs");
+
boolean needSpace = false;
boolean wroteData = false;
- if (user.getFullName() != null && !user.getFullName().isEmpty()) {
- sb.append(user.getFullName());
- needSpace = true;
- wroteData = true;
- }
-
- if (user.getPreferredEmail() != null && !user.getPreferredEmail().isEmpty()) {
- if (needSpace) {
- sb.append(" ");
+ if (account.isPresent()) {
+ String fullName = account.get().getFullName();
+ if (fullName != null && !fullName.isEmpty()) {
+ sb.append(fullName);
+ needSpace = true;
+ wroteData = true;
}
- sb.append("<").append(user.getPreferredEmail()).append(">");
- wroteData = true;
+
+ String preferredEmail = account.get().getPreferredEmail();
+ if (preferredEmail != null && !preferredEmail.isEmpty()) {
+ if (needSpace) {
+ sb.append(" ");
+ }
+ sb.append("<").append(preferredEmail).append(">");
+ wroteData = true;
+ }
}
if (!wroteData) {
- sb.append(anonymousCowardName).append(" #").append(user.getId());
+ sb.append(anonymousCowardName).append(" #").append(accountId);
}
}
@@ -87,9 +113,16 @@
sb.append("Branch: ").append(branch).append("\n");
}
- void appendSubmittedBy(Account user) {
+ /**
+ * Appends a header with the submitter information.
+ *
+ * @param accountId the account ID of the submitter
+ * @param account the account of the submitter, can be {@link Optional#empty()} if the account is
+ * missing
+ */
+ void appendSubmittedBy(Account.Id accountId, Optional<Account> account) {
sb.append("Submitted-by: ");
- appendUserData(user);
+ appendUserData(accountId, account);
sb.append("\n");
}