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;
}
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) {
import java.util.Map;
+@FunctionalInterface
public interface SettingsLoader {
Map<String, String> load(String componentKey);
}
*/
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;
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();
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;
}
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) {
import org.junit.Before;
import com.google.common.collect.ImmutableList;
+import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
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");
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;
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"));
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);