import com.google.common.io.Resources;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static java.util.Optional.ofNullable;
import static org.sonar.api.web.UserRole.USER;
-import static org.sonar.core.util.stream.Collectors.toList;
import static org.sonar.core.util.stream.Collectors.uniqueIndex;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_INDEX;
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
- List<ComponentDto> projects = getAuthorizedProjects(dbSession, searchProjects(dbSession, request));
+ List<ComponentDto> projects = getAuthorizedComponents(dbSession, searchComponents(dbSession, request));
JsonWriter json = response.newJsonWriter();
json.beginArray();
for (ComponentDto project : projects) {
}
}
- private List<ComponentDto> searchProjects(DbSession dbSession, Request request) {
+ private List<ComponentDto> searchComponents(DbSession dbSession, Request request) {
String projectKey = request.param(PARAM_KEY);
List<ComponentDto> projects = new ArrayList<>();
if (projectKey != null) {
return projects;
}
- private List<ComponentDto> getAuthorizedProjects(DbSession dbSession, List<ComponentDto> projectDtos) {
- if (projectDtos.isEmpty()) {
+ private List<ComponentDto> getAuthorizedComponents(DbSession dbSession, List<ComponentDto> components) {
+ if (components.isEmpty()) {
return Collections.emptyList();
}
- Map<String, Long> projectIdsByUuids = projectDtos.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
- Set<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession,
- projectDtos.stream().map(ComponentDto::getId).collect(toList()),
- userSession.getUserId(), USER);
- return projectDtos.stream()
- .filter(c -> authorizedProjectIds.contains(projectIdsByUuids.get(c.projectUuid())))
+ Set<String> projectUuids = components.stream().map(ComponentDto::projectUuid).collect(Collectors.toSet());
+ List<ComponentDto> projects = dbClient.componentDao().selectByUuids(dbSession, projectUuids);
+ Map<String, Long> projectIdsByUuids = projects.stream().collect(uniqueIndex(ComponentDto::uuid, ComponentDto::getId));
+ Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIdsByUuids.values(), userSession.getUserId(), USER);
+ return components.stream()
+ .filter(component -> authorizedProjectIds.contains(projectIdsByUuids.get(component.projectUuid())))
.collect(Collectors.toList());
}
}
@Test
- public void search_project_by_name() throws Exception {
+ public void search_projects_by_name() throws Exception {
insertProjectsAuthorizedForUser(
newProjectDto(db.getDefaultOrganization()).setKey("org.jenkins-ci.plugins:sonar").setName("Jenkins Sonar Plugin"),
newProjectDto(db.getDefaultOrganization()).setKey("org.codehaus.sonar-plugins:sonar-ant-task").setName("Sonar Ant Task"),
String result = call(null, "Plu", null);
- verifyResult(result, "search_project_by_name.json");
+ verifyResult(result, "search_projects_by_name.json");
+ }
+
+ @Test
+ public void search_projects_with_modules_by_name() throws Exception {
+ ComponentDto project1 = newProjectDto(db.getDefaultOrganization()).setKey("org.jenkins-ci.plugins:sonar").setName("Jenkins Sonar Plugin");
+ ComponentDto project2 = newProjectDto(db.getDefaultOrganization()).setKey("org.codehaus.sonar-plugins:sonar-ant-task").setName("Sonar Ant Task");
+ insertProjectsAuthorizedForUser(project1, project2);
+ db.components().insertComponents(
+ newModuleDto(project1).setKey("org.jenkins-ci.plugins:sonar-common-db").setName("Jenkins Common DB"),
+ newModuleDto(project1).setKey("org.jenkins-ci.plugins:sonar-common-server").setName("Jenkins Common Server"),
+ newModuleDto(project2).setKey("org.codehaus.sonar-plugins:sonar-ant-db").setName("Ant DB"));
+
+ String result = call(null, "Com", true);
+
+ verifyResult(result, "search_projects_with_modules_by_name.json");
}
@Test