]> source.dussan.org Git - sonarqube.git/commitdiff
Apply feedback 54/head
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 23 Jan 2015 10:22:51 +0000 (11:22 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 23 Jan 2015 10:22:51 +0000 (11:22 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/issue/ScmAccountCacheLoader.java
server/sonar-server/src/main/java/org/sonar/server/user/index/UserIndex.java
server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexTest.java
server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexTest/user2-with-same-email-as-user1.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexTest/user4-with-same-email-as-user1.json [new file with mode: 0644]

index f4327afa2932098c542914a88806c91f508dcf9b..9f2b5ed5f412e9e6e40467b8411db37e0e9b2201 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.issue;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.collect.Collections2;
@@ -44,7 +45,8 @@ public class ScmAccountCacheLoader implements CacheLoader<String, String> {
     this(index, LoggerFactory.getLogger(ScmAccountCacheLoader.class));
   }
 
-  public ScmAccountCacheLoader(UserIndex index, Logger log) {
+  @VisibleForTesting
+  ScmAccountCacheLoader(UserIndex index, Logger log) {
     this.log = log;
     this.index = index;
   }
@@ -52,19 +54,20 @@ public class ScmAccountCacheLoader implements CacheLoader<String, String> {
   @Override
   public String load(String scmAccount) {
     List<UserDoc> users = index.getAtMostThreeUsersForScmAccount(scmAccount);
-    if (users.isEmpty()) {
-      return null;
-    }
     if (users.size() == 1) {
       return users.get(0).login();
     }
-    Collection<String> logins = Collections2.transform(users, new Function<UserDoc, String>() {
-      @Override
-      public String apply(UserDoc input) {
-        return input.login();
-      }
-    });
-    log.warn(String.format("Multiple users share the SCM account '%s': %s", scmAccount, Joiner.on(", ").join(logins)));
+    if (!users.isEmpty()) {
+      // multiple users are associated to the same SCM account, for example
+      // the same email
+      Collection<String> logins = Collections2.transform(users, new Function<UserDoc, String>() {
+        @Override
+        public String apply(UserDoc input) {
+          return input.login();
+        }
+      });
+      log.warn(String.format("Multiple users share the SCM account '%s': %s", scmAccount, Joiner.on(", ").join(logins)));
+    }
     return null;
   }
 
index 2044f29ce8d0701cc7a6363ce64b0caf690c1812..2fb347f93a49484decd0a3047222e5afdca11e9a 100644 (file)
@@ -56,6 +56,10 @@ public class UserIndex implements ServerComponent {
     return null;
   }
 
+  /**
+   * Returns the user associated with the given SCM account. If multiple users have the same
+   * SCM account, then result is null.
+   */
   @CheckForNull
   public UserDoc getNullableByScmAccount(String scmAccount) {
     if (!StringUtils.isEmpty(scmAccount)) {
@@ -85,7 +89,7 @@ public class UserIndex implements ServerComponent {
 
   /**
    * Returns the users (at most 3) who are associated to the given SCM account. This method can be used
-   * to detect user conflicts. It never returns null.
+   * to detect user conflicts.
    */
   public List<UserDoc> getAtMostThreeUsersForScmAccount(String scmAccount) {
     List<UserDoc> result = new ArrayList<>();
index 52c46612f161e463aedfbbeeb000cafa9755ce69..20030fbf9793932cd85d7ab1725aa488d0b60929 100644 (file)
@@ -125,4 +125,13 @@ public class UserIndexTest {
     assertThat(index.getAtMostThreeUsersForScmAccount("")).isEmpty();
     assertThat(index.getAtMostThreeUsersForScmAccount("unknown")).isEmpty();
   }
+
+  @Test
+  public void get_maximum_three_users_for_scm_account() throws Exception {
+    esTester.putDocuments(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER, this.getClass(), "user1.json",
+      "user2-with-same-email-as-user1.json", "user3-with-same-email-as-user1.json", "user4-with-same-email-as-user1.json");
+
+    // restrict results to 3 users
+    assertThat(index.getAtMostThreeUsersForScmAccount("user1@mail.com")).hasSize(3);
+  }
 }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexTest/user2-with-same-email-as-user1.json b/server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexTest/user2-with-same-email-as-user1.json
new file mode 100644 (file)
index 0000000..8b17f50
--- /dev/null
@@ -0,0 +1,10 @@
+{
+  "login": "user2",
+  "name": "User2",
+  "email": "user1@mail.com",
+  "active": true,
+  "scmAccounts": ["u2"],
+  "createdAt": 1500000000000,
+  "updatedAt": 1500000000000
+}
+
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexTest/user4-with-same-email-as-user1.json b/server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexTest/user4-with-same-email-as-user1.json
new file mode 100644 (file)
index 0000000..6cefc04
--- /dev/null
@@ -0,0 +1,10 @@
+{
+  "login": "user4",
+  "name": "User4",
+  "email": "user1@mail.com",
+  "active": true,
+  "scmAccounts": ["u4"],
+  "createdAt": 1500000000000,
+  "updatedAt": 1500000000000
+}
+