Merge changes I9369c39f,I03d7eeb4

* changes:
  ChangeNoteUtil: Support PersonIdent creation by account ID
  FromAddressGeneratorProvider: Use AccountCache#maybeGet instead of AccountCache#get
This commit is contained in:
Edwin Kempin
2018-02-02 15:10:48 +00:00
committed by Gerrit Code Review
6 changed files with 20 additions and 27 deletions

View File

@@ -20,6 +20,7 @@ import com.google.gerrit.common.data.ParameterizedString;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.GerritPersonIdent;
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.GerritServerConfig;
import com.google.gerrit.server.mail.Address;
@@ -29,6 +30,7 @@ import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
import org.eclipse.jgit.lib.Config;
@@ -121,9 +123,9 @@ public class FromAddressGeneratorProvider implements Provider<FromAddressGenerat
public Address from(Account.Id fromId) {
String senderName;
if (fromId != null) {
Account a = accountCache.get(fromId).getAccount();
String fullName = a.getFullName();
String userEmail = a.getPreferredEmail();
Optional<Account> a = accountCache.maybeGet(fromId).map(AccountState::getAccount);
String fullName = a.map(Account::getFullName).orElse(null);
String userEmail = a.map(Account::getPreferredEmail).orElse(null);
if (canRelay(userEmail)) {
return new Address(fullName, userEmail);
}
@@ -206,8 +208,8 @@ public class FromAddressGeneratorProvider implements Provider<FromAddressGenerat
final String senderName;
if (fromId != null) {
final Account account = accountCache.get(fromId).getAccount();
String fullName = account.getFullName();
String fullName =
accountCache.maybeGet(fromId).map(a -> a.getAccount().getFullName()).orElse(null);
if (fullName == null || "".equals(fullName)) {
fullName = anonymousCowardName;
}

View File

@@ -174,8 +174,8 @@ public abstract class AbstractChangeUpdate {
return accountId;
}
protected PersonIdent newIdent(Account author, Date when) {
return noteUtil.newIdent(author, when, serverIdent);
protected PersonIdent newIdent(Account.Id authorId, Date when) {
return noteUtil.newIdent(authorId, when, serverIdent);
}
/** Whether no updates have been done. */

View File

@@ -129,6 +129,12 @@ public class ChangeNoteUtil {
this.writeJson = config.getBoolean("notedb", "writeJson", true);
}
public PersonIdent newIdent(Account.Id authorId, Date when, PersonIdent serverIdent) {
Account author = accountCache.get(authorId).getAccount();
return new PersonIdent(
author.getName(), author.getId().get() + "@" + serverId, when, serverIdent.getTimeZone());
}
@VisibleForTesting
public PersonIdent newIdent(Account author, Date when, PersonIdent serverIdent) {
return new PersonIdent(
@@ -609,7 +615,7 @@ public class ChangeNoteUtil {
}
private void appendIdent(PrintWriter writer, String header, Account.Id id, Timestamp ts) {
PersonIdent ident = newIdent(accountCache.get(id).getAccount(), ts, serverIdent);
PersonIdent ident = newIdent(id, ts, serverIdent);
StringBuilder name = new StringBuilder();
PersonIdent.appendSanitized(name, ident.getName());
name.append(" <");

View File

@@ -61,7 +61,6 @@ import com.google.gerrit.reviewdb.client.RevId;
import com.google.gerrit.reviewdb.client.RobotComment;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.mail.Address;
import com.google.gerrit.server.project.ProjectCache;
@@ -124,7 +123,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
ChangeNotes notes, CurrentUser user, Date when, Comparator<String> labelNameComparator);
}
private final AccountCache accountCache;
private final NoteDbUpdateManager.Factory updateManagerFactory;
private final ChangeDraftUpdate.Factory draftUpdateFactory;
private final RobotCommentUpdate.Factory robotCommentUpdateFactory;
@@ -168,7 +166,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
@GerritServerConfig Config cfg,
@GerritPersonIdent PersonIdent serverIdent,
NotesMigration migration,
AccountCache accountCache,
NoteDbUpdateManager.Factory updateManagerFactory,
ChangeDraftUpdate.Factory draftUpdateFactory,
RobotCommentUpdate.Factory robotCommentUpdateFactory,
@@ -181,7 +178,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
cfg,
serverIdent,
migration,
accountCache,
updateManagerFactory,
draftUpdateFactory,
robotCommentUpdateFactory,
@@ -198,7 +194,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
@GerritServerConfig Config cfg,
@GerritPersonIdent PersonIdent serverIdent,
NotesMigration migration,
AccountCache accountCache,
NoteDbUpdateManager.Factory updateManagerFactory,
ChangeDraftUpdate.Factory draftUpdateFactory,
RobotCommentUpdate.Factory robotCommentUpdateFactory,
@@ -212,7 +207,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
cfg,
serverIdent,
migration,
accountCache,
updateManagerFactory,
draftUpdateFactory,
robotCommentUpdateFactory,
@@ -234,7 +228,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
@GerritServerConfig Config cfg,
@GerritPersonIdent PersonIdent serverIdent,
NotesMigration migration,
AccountCache accountCache,
NoteDbUpdateManager.Factory updateManagerFactory,
ChangeDraftUpdate.Factory draftUpdateFactory,
RobotCommentUpdate.Factory robotCommentUpdateFactory,
@@ -245,7 +238,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
@Assisted Comparator<String> labelNameComparator,
ChangeNoteUtil noteUtil) {
super(cfg, migration, notes, user, serverIdent, noteUtil, when);
this.accountCache = accountCache;
this.updateManagerFactory = updateManagerFactory;
this.draftUpdateFactory = draftUpdateFactory;
this.robotCommentUpdateFactory = robotCommentUpdateFactory;
@@ -258,7 +250,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
@GerritServerConfig Config cfg,
@GerritPersonIdent PersonIdent serverIdent,
NotesMigration migration,
AccountCache accountCache,
NoteDbUpdateManager.Factory updateManagerFactory,
ChangeDraftUpdate.Factory draftUpdateFactory,
RobotCommentUpdate.Factory robotCommentUpdateFactory,
@@ -281,7 +272,6 @@ public class ChangeUpdate extends AbstractChangeUpdate {
realAccountId,
authorIdent,
when);
this.accountCache = accountCache;
this.draftUpdateFactory = draftUpdateFactory;
this.robotCommentUpdateFactory = robotCommentUpdateFactory;
this.updateManagerFactory = updateManagerFactory;
@@ -861,8 +851,7 @@ public class ChangeUpdate extends AbstractChangeUpdate {
}
private StringBuilder addIdent(StringBuilder sb, Account.Id accountId) {
Account account = accountCache.get(accountId).getAccount();
PersonIdent ident = newIdent(account, when);
PersonIdent ident = newIdent(accountId, when);
PersonIdent.appendSanitized(sb, ident.getName());
sb.append(" <");

View File

@@ -49,7 +49,6 @@ import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.reviewdb.server.ReviewDbUtil;
import com.google.gerrit.server.CommentsUtil;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.GerritServerId;
import com.google.gerrit.server.notedb.ChangeBundle;
@@ -113,7 +112,6 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
*/
static final long MAX_DELTA_MS = SECONDS.toMillis(1);
private final AccountCache accountCache;
private final ChangeBundleReader bundleReader;
private final ChangeDraftUpdate.Factory draftUpdateFactory;
private final ChangeNoteUtil changeNoteUtil;
@@ -132,7 +130,6 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
ChangeRebuilderImpl(
@GerritServerConfig Config cfg,
SchemaFactory<ReviewDb> schemaFactory,
AccountCache accountCache,
ChangeBundleReader bundleReader,
ChangeDraftUpdate.Factory draftUpdateFactory,
ChangeNoteUtil changeNoteUtil,
@@ -146,7 +143,6 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
@Nullable ProjectCache projectCache,
@GerritServerId String serverId) {
super(schemaFactory);
this.accountCache = accountCache;
this.bundleReader = bundleReader;
this.draftUpdateFactory = draftUpdateFactory;
this.changeNoteUtil = changeNoteUtil;
@@ -551,8 +547,7 @@ public class ChangeRebuilderImpl extends ChangeRebuilder {
if (id == null) {
return new PersonIdent(serverIdent, events.getWhen());
}
return changeNoteUtil.newIdent(
accountCache.get(id).getAccount(), events.getWhen(), serverIdent);
return changeNoteUtil.newIdent(id, events.getWhen(), serverIdent);
}
private List<HashtagsEvent> getHashtagsEvents(Change change, NoteDbUpdateManager manager)

View File

@@ -30,6 +30,7 @@ import com.google.gerrit.server.config.AllUsersNameProvider;
import com.google.gerrit.server.mail.Address;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent;
import org.junit.Before;
@@ -368,7 +369,7 @@ public class FromAddressGeneratorProviderTest {
private Account.Id user(String name, String email) {
final AccountState s = makeUser(name, email);
expect(accountCache.get(eq(s.getAccount().getId()))).andReturn(s);
expect(accountCache.maybeGet(eq(s.getAccount().getId()))).andReturn(Optional.of(s));
return s.getAccount().getId();
}