]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8386 Fetch all usernames in a single request in preview mode 1586/head
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 30 Jan 2017 09:41:40 +0000 (10:41 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 30 Jan 2017 13:02:31 +0000 (14:02 +0100)
sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultIssueCallback.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/settings/SettingsLoader.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/user/UserRepositoryLoader.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/report/JSONReport.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultIssueCallbackTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/user/UserRepositoryLoaderTest.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java

index 20f628c4216305daa1b0de08feae806203fb3f23..d6afff29173893827d1d1ef339c56ace379590e1 100644 (file)
@@ -28,6 +28,8 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
+
 import org.sonar.scanner.issue.tracking.TrackedIssue;
 import org.sonar.scanner.protocol.input.ScannerInput.User;
 import org.sonar.scanner.repository.user.UserRepositoryLoader;
@@ -104,12 +106,10 @@ public class DefaultIssueCallback implements IssueCallback {
   }
 
   private void getUsers() {
-    for (String loginName : userLoginNames) {
-      User user = userRepository.load(loginName);
-      if (user != null) {
-        userMap.put(user.getLogin(), user.getName());
-      }
-    }
+    Map<String, User> map = userRepository.map(userLoginNames);
+
+    userMap = map.entrySet().stream()
+      .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getName()));
   }
 
   private String getRuleName(RuleKey ruleKey) {
index eeebeb6b52b97ac8c9bcdd03e41b849f719da6f1..e4e1fda927fde99eef8d6b846401f396bd319099 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.scanner.repository.settings;
 
 import java.util.Map;
 
+@FunctionalInterface
 public interface SettingsLoader {
   Map<String, String> load(String componentKey);
 }
index 54dabf622ff0bde504aae4be3ef27b28f5bd634d..ab65d6ac35d73f7974d3093686321f46a221ee6b 100644 (file)
  */
 package org.sonar.scanner.repository.user;
 
-import org.apache.commons.io.IOUtils;
-import com.google.common.collect.Lists;
-import com.google.common.base.Joiner;
-import org.sonar.scanner.bootstrap.ScannerWsClient;
-import org.sonar.scanner.protocol.input.ScannerInput;
-import org.sonar.scanner.util.ScannerUtils;
-import org.sonarqube.ws.client.GetRequest;
-import com.google.common.base.Function;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import org.apache.commons.io.IOUtils;
+import org.sonar.scanner.bootstrap.ScannerWsClient;
+import org.sonar.scanner.protocol.input.ScannerInput;
+import org.sonar.scanner.util.ScannerUtils;
+import org.sonarqube.ws.client.GetRequest;
 
 public class UserRepositoryLoader {
   private final ScannerWsClient wsClient;
@@ -46,15 +48,29 @@ public class UserRepositoryLoader {
     return parseUser(is);
   }
 
-  public Collection<ScannerInput.User> load(List<String> userLogins) {
+  public Collection<ScannerInput.User> load(Collection<String> userLogins) {
     if (userLogins.isEmpty()) {
       return Collections.emptyList();
     }
-    InputStream is = loadQuery(Joiner.on(',').join(Lists.transform(userLogins, new UserEncodingFunction())));
+    UserEncodingFunction userEncodingFunction = new UserEncodingFunction();
+    String encodedUserLogins = userLogins.stream()
+      .map(userEncodingFunction::apply)
+      .collect(Collectors.joining(","));
+    InputStream is = loadQuery(encodedUserLogins);
 
     return parseUsers(is);
   }
 
+  public Map<String, ScannerInput.User> map(Collection<String> userLogins) {
+    Collection<ScannerInput.User> users = load(userLogins);
+    Map<String, ScannerInput.User> map = new HashMap<>();
+
+    for (ScannerInput.User user : users) {
+      map.put(user.getLogin(), user);
+    }
+    return map;
+  }
+
   private InputStream loadQuery(String loginsQuery) {
     GetRequest getRequest = new GetRequest("/batch/users?logins=" + loginsQuery);
     return wsClient.call(getRequest).contentStream();
index 6551ddb2dc77ed620b7e23a5bd45bd884290488e..68c2c20f9b2cd7f8a3d00f59ad5f686fa8a4604e 100644 (file)
@@ -29,8 +29,6 @@ import java.io.Writer;
 import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Set;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
@@ -214,13 +212,7 @@ public class JSONReport implements Reporter {
   }
 
   private void writeUsers(JsonWriter json, Collection<String> userLogins) throws IOException {
-    List<ScannerInput.User> users = new LinkedList<>();
-    for (String userLogin : userLogins) {
-      User user = userRepository.load(userLogin);
-      if (user != null) {
-        users.add(user);
-      }
-    }
+    Collection<User> users = userRepository.load(userLogins);
 
     json.name("users").beginArray();
     for (ScannerInput.User user : users) {
index 6bde07dd80aa2458e802e6dbad9d2b1b297935e1..326e534af3ad07db31b166685cb3d094fc843b91 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.scanner.repository.user.UserRepositoryLoader;
 import org.junit.Before;
 import com.google.common.collect.ImmutableList;
 
+import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -69,7 +70,8 @@ public class DefaultIssueCallbackTest {
     ScannerInput.User.Builder userBuilder = ScannerInput.User.newBuilder();
     userBuilder.setLogin("user");
     userBuilder.setName("name");
-    when(userRepository.load("user")).thenReturn(userBuilder.build());
+    when(userRepository.map(Collections.singleton("user")))
+      .thenReturn(Collections.singletonMap("user", userBuilder.build()));
 
     Rule r = mock(Rule.class);
     when(r.name()).thenReturn("rule name");
index 40c944ee8d86b8d152b80af5d8f458cdcc214bcd..11db8fc231a99e99ed4bd6e7c5812ef3587a83d2 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.scanner.repository.user;
 import org.assertj.core.util.Lists;
 import org.sonar.scanner.bootstrap.ScannerWsClient;
 import org.sonar.scanner.protocol.input.ScannerInput;
+import org.sonar.scanner.protocol.input.ScannerInput.User;
 import org.sonar.scanner.repository.user.UserRepositoryLoader;
 import org.sonar.scanner.WsTestUtil;
 import org.junit.Before;
@@ -67,6 +68,26 @@ public class UserRepositoryLoaderTest {
     assertThat(userRepo.load(Arrays.asList("fmallet", "sbrandhof"))).extracting("login", "name").containsOnly(tuple("fmallet", "Freddy Mallet"), tuple("sbrandhof", "Simon"));
   }
 
+  @Test
+  public void testLoadListWithSingleUser() throws IOException {
+    Map<String, String> userMap = ImmutableMap.of("fmallet", "Freddy Mallet");
+    InputStream is = createUsersMock(userMap);
+    WsTestUtil.mockStream(wsClient, "/batch/users?logins=fmallet", is);
+    assertThat(userRepo.load(Arrays.asList("fmallet"))).extracting("login", "name").containsOnly(tuple("fmallet", "Freddy Mallet"));
+  }
+
+  @Test
+  public void testMapUsers() throws IOException {
+    Map<String, String> userMap = ImmutableMap.of("fmallet", "Freddy Mallet");
+    InputStream is = createUsersMock(userMap);
+    WsTestUtil.mockStream(wsClient, "/batch/users?logins=fmallet,sbrandhof", is);
+    Map<String, User> map = userRepo.map(Arrays.asList("fmallet", "sbrandhof"));
+
+    // one user doesn't exist
+    assertThat(map).hasSize(1);
+    assertThat(map.values().iterator().next().getLogin()).isEqualTo("fmallet");
+  }
+
   @Test
   public void testLoadSingleUser() throws IOException {
     InputStream is = createUsersMock(ImmutableMap.of("fmallet", "Freddy Mallet"));
index e464770bb66a834580e88b7910ccee4faa5c804c..c86b5ada7e7cb7d0e2e666fe8d98aab9c083cf32 100644 (file)
@@ -123,7 +123,7 @@ public class JSONReportTest {
     issue.setNew(false);
     when(issueCache.all()).thenReturn(Collections.singleton(issue));
     ScannerInput.User user = ScannerInput.User.newBuilder().setLogin("simon").setName("Simon").build();
-    when(userRepository.load("simon")).thenReturn(user);
+    when(userRepository.load(Collections.singleton("simon"))).thenReturn(Collections.singleton(user));
 
     StringWriter writer = new StringWriter();
     jsonReport.writeJson(writer);