Consider name and e-mail matching edge cases
When checking for full matching of full name or e-mail
there are a couple of extra edge cases to consider:
- NULL fullname
- EMail included only in the email field of external ids
while keeping support for username matching as well.
Change-Id: I065f60631ee949a3eeb881fc8f99ae62c627a22e
diff --git a/owners-common/src/main/java/com/vmware/gerrit/owners/common/AccountsImpl.java b/owners-common/src/main/java/com/vmware/gerrit/owners/common/AccountsImpl.java
index ca64560..a1fc7b0 100644
--- a/owners-common/src/main/java/com/vmware/gerrit/owners/common/AccountsImpl.java
+++ b/owners-common/src/main/java/com/vmware/gerrit/owners/common/AccountsImpl.java
@@ -14,6 +14,8 @@
package com.vmware.gerrit.owners.common;
+import static com.google.gerrit.server.account.ExternalId.*;
+
import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Account.Id;
@@ -23,6 +25,7 @@
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.account.AccountState;
+import com.google.gerrit.server.account.ExternalId;
import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.account.GroupMembers;
import com.google.gerrit.server.project.NoSuchProjectException;
@@ -129,18 +132,37 @@
private boolean isFullMatch(Account.Id id, String nameOrEmail) {
AccountState account = byId.get(id);
- return account.getAccount().getFullName().trim().equalsIgnoreCase(nameOrEmail)
+ return isFullNameMatch(account, nameOrEmail)
|| account
.getExternalIds()
.stream()
- .anyMatch(
- extId ->
- getSchemeRest(extId.key().scheme(), extId.key().get())
- .trim()
- .equalsIgnoreCase(nameOrEmail));
+ .anyMatch(eid -> isEMailMatch(eid, nameOrEmail) || isUsernameMatch(eid, nameOrEmail));
}
- private String getSchemeRest(String scheme, String key) {
- return null != scheme ? key.substring(scheme.length() + 1) : key;
+ private boolean isFullNameMatch(AccountState account, String fullName) {
+ return Optional.ofNullable(account.getAccount().getFullName())
+ .filter(n -> n.trim().equalsIgnoreCase(fullName))
+ .isPresent();
+ }
+
+ private boolean isUsernameMatch(ExternalId externalId, String username) {
+ return keySchemeRest(SCHEME_GERRIT, externalId.key())
+ .filter(name -> name.equals(username))
+ .isPresent();
+ }
+
+ private boolean isEMailMatch(ExternalId externalId, String email) {
+ ExternalId.Key externalKey = externalId.key();
+ return OptionalUtils.combine(
+ Optional.ofNullable(externalId.email()).filter(mail -> mail.equalsIgnoreCase(email)),
+ keySchemeRest(SCHEME_MAILTO, externalKey).filter(mail -> mail.equalsIgnoreCase(email)))
+ .isPresent();
+ }
+
+ private Optional<String> keySchemeRest(String scheme, ExternalId.Key key) {
+ if (scheme != null && key.isScheme(scheme)) {
+ return Optional.of(key.get().substring(scheme.length() + 1));
+ }
+ return Optional.empty();
}
}
diff --git a/owners-common/src/main/java/com/vmware/gerrit/owners/common/OptionalUtils.java b/owners-common/src/main/java/com/vmware/gerrit/owners/common/OptionalUtils.java
new file mode 100644
index 0000000..7a6f83c
--- /dev/null
+++ b/owners-common/src/main/java/com/vmware/gerrit/owners/common/OptionalUtils.java
@@ -0,0 +1,24 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.vmware.gerrit.owners.common;
+
+import java.util.Optional;
+
+public class OptionalUtils {
+
+ public static <T> Optional<T> combine(Optional<T> firstChoice, Optional<T> secondChoice) {
+ return Optional.ofNullable(firstChoice.orElse(secondChoice.orElse(null)));
+ }
+}