From 630dcde3a0b6499fe3c3c512e491c60d95494bcc Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Thu, 15 Aug 2019 12:52:29 +0900 Subject: [PATCH] InternalAccountQuery: Add back the oneByExternalId method Change I45ccec5ed removed several methods from InternalAccountQuery that are no longer used in core Gerrit. One of them, however, is used in CollabNet's integration. Add back the method that is used, and annotate it with @UsedAt. Make adaptations compared to the original version that was removed: - Use the FluentLogger rather than slf4j's Logger - Use the Stream API rather than Guava's Lists.transform This partially reverts commit 69f297f568aa3648df1096aba08eac387ef7e28d. Change-Id: I9170932646960754ef2ec4b9287d7c71844403a2 --- java/com/google/gerrit/server/UsedAt.java | 1 + .../query/account/InternalAccountQuery.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/java/com/google/gerrit/server/UsedAt.java b/java/com/google/gerrit/server/UsedAt.java index b564157bf1..a5459d1c16 100644 --- a/java/com/google/gerrit/server/UsedAt.java +++ b/java/com/google/gerrit/server/UsedAt.java @@ -35,6 +35,7 @@ public @interface UsedAt { /** Enumeration of projects that call a method that would otherwise be private. */ enum Project { GOOGLE, + COLLABNET, PLUGIN_DELETE_PROJECT, PLUGIN_SERVICEUSER, PLUGINS_ALL, // Use this project if a method/type is generally made available to all plugins. diff --git a/java/com/google/gerrit/server/query/account/InternalAccountQuery.java b/java/com/google/gerrit/server/query/account/InternalAccountQuery.java index e8e72a83b0..3a7d3e5104 100644 --- a/java/com/google/gerrit/server/query/account/InternalAccountQuery.java +++ b/java/com/google/gerrit/server/query/account/InternalAccountQuery.java @@ -17,15 +17,18 @@ package com.google.gerrit.server.query.account; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toSet; +import com.google.common.base.Joiner; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.Multimap; +import com.google.common.flogger.FluentLogger; import com.google.gerrit.index.FieldDef; import com.google.gerrit.index.IndexConfig; import com.google.gerrit.index.Schema; import com.google.gerrit.index.query.InternalQuery; import com.google.gerrit.reviewdb.client.Project; +import com.google.gerrit.server.UsedAt; import com.google.gerrit.server.account.AccountState; import com.google.gerrit.server.account.externalids.ExternalId; import com.google.gerrit.server.index.account.AccountField; @@ -43,6 +46,8 @@ import java.util.Set; * holding on to a single instance. */ public class InternalAccountQuery extends InternalQuery { + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); + @Inject InternalAccountQuery( AccountQueryProcessor queryProcessor, @@ -88,6 +93,22 @@ public class InternalAccountQuery extends InternalQuery { return query(AccountPredicates.externalIdIncludingSecondaryEmails(externalId.toString())); } + @UsedAt(UsedAt.Project.COLLABNET) + public AccountState oneByExternalId(ExternalId.Key externalId) throws OrmException { + List accountStates = byExternalId(externalId); + if (accountStates.size() == 1) { + return accountStates.get(0); + } else if (accountStates.size() > 0) { + StringBuilder msg = new StringBuilder(); + msg.append("Ambiguous external ID ").append(externalId).append(" for accounts: "); + Joiner.on(", ") + .appendTo( + msg, accountStates.stream().map(AccountState.ACCOUNT_ID_FUNCTION).collect(toList())); + logger.atWarning().log(msg.toString()); + } + return null; + } + public List byFullName(String fullName) throws OrmException { return query(AccountPredicates.fullName(fullName)); }