import static com.google.common.base.Strings.isNullOrEmpty;
import static java.lang.String.format;
import static org.sonar.core.util.Protobuf.setNullable;
-import static org.sonar.server.ws.WsUtils.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class SearchMyProjectsAction implements ProjectsWsAction {
private SearchMyProjectsWsResponse doHandle(SearchMyProjectsRequest request) {
checkAuthenticated();
- DbSession dbSession = dbClient.openSession(false);
- try {
- SearchMyProjectsData data = dataLoader.load(request);
+
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ SearchMyProjectsData data = dataLoader.load(dbSession, request);
return buildResponse(request, data);
- } finally {
- dbClient.closeSession(dbSession);
}
}
}
private static SearchMyProjectsRequest toRequest(Request request) {
- SearchMyProjectsRequest searchMyProjectsRequest = SearchMyProjectsRequest.builder()
- .setQuery(request.param(Param.TEXT_QUERY))
+ return SearchMyProjectsRequest.builder()
.setPage(request.mandatoryParamAsInt(Param.PAGE))
.setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
.build();
-
- String searchQuery = searchMyProjectsRequest.getQuery();
- checkRequest(searchQuery == null || searchQuery.length() >= QUERY_MINIMUM_LENGTH,
- "The '%s' parameter must have at least %d characters", Param.TEXT_QUERY, QUERY_MINIMUM_LENGTH);
- return searchMyProjectsRequest;
}
private static class ProjectDtoToWs implements Function<ComponentDto, Project> {
this.dbClient = dbClient;
}
- SearchMyProjectsData load(SearchMyProjectsRequest request) {
- DbSession dbSession = dbClient.openSession(false);
- try {
- SearchMyProjectsData.Builder data = builder();
- ProjectsResult searchResult = searchProjects(dbSession, request);
- List<ComponentDto> projects = searchResult.projects;
- List<String> projectUuids = Lists.transform(projects, ComponentDto::projectUuid);
- List<ComponentLinkDto> projectLinks = dbClient.componentLinkDao().selectByComponentUuids(dbSession, projectUuids);
- List<SnapshotDto> snapshots = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, projectUuids);
- MetricDto gateStatusMetric = dbClient.metricDao().selectOrFailByKey(dbSession, CoreMetrics.ALERT_STATUS_KEY);
- MeasureQuery measureQuery = MeasureQuery.builder()
- .setProjectUuids(projectUuids)
- .setMetricId(gateStatusMetric.getId())
- .build();
- List<MeasureDto> qualityGates = dbClient.measureDao().selectByQuery(dbSession, measureQuery);
+ SearchMyProjectsData load(DbSession dbSession, SearchMyProjectsRequest request) {
+ SearchMyProjectsData.Builder data = builder();
+ ProjectsResult searchResult = searchProjects(dbSession, request);
+ List<ComponentDto> projects = searchResult.projects;
+ List<String> projectUuids = Lists.transform(projects, ComponentDto::projectUuid);
+ List<ComponentLinkDto> projectLinks = dbClient.componentLinkDao().selectByComponentUuids(dbSession, projectUuids);
+ List<SnapshotDto> snapshots = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, projectUuids);
+ MetricDto gateStatusMetric = dbClient.metricDao().selectOrFailByKey(dbSession, CoreMetrics.ALERT_STATUS_KEY);
+ MeasureQuery measureQuery = MeasureQuery.builder()
+ .setProjectUuids(projectUuids)
+ .setMetricId(gateStatusMetric.getId())
+ .build();
+ List<MeasureDto> qualityGates = dbClient.measureDao().selectByQuery(dbSession, measureQuery);
- data.setProjects(projects)
- .setProjectLinks(projectLinks)
- .setSnapshots(snapshots)
- .setQualityGates(qualityGates)
- .setTotalNbOfProjects(searchResult.total);
+ data.setProjects(projects)
+ .setProjectLinks(projectLinks)
+ .setSnapshots(snapshots)
+ .setQualityGates(qualityGates)
+ .setTotalNbOfProjects(searchResult.total);
- return data.build();
- } finally {
- dbClient.closeSession(dbSession);
- }
+ return data.build();
}
@VisibleForTesting
List<Long> componentIds = dbClient.roleDao().selectComponentIdsByPermissionAndUserId(dbSession, UserRole.ADMIN, userId);
ComponentQuery dbQuery = ComponentQuery.builder()
.setQualifiers(Qualifiers.PROJECT)
- .setNameOrKeyQuery(request.getQuery())
.setComponentIds(ImmutableSet.copyOf(componentIds))
.build();
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
-import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
import static org.sonar.db.component.ComponentTesting.newDeveloper;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
import static org.sonar.db.component.ComponentTesting.newView;
assertThat(result.getProjectsList()).extracting(Project::getId).containsOnly(jdk7.uuid(), cLang.uuid(), sonarqube.uuid());
}
- @Test
- public void search_my_projects_by_name() {
- OrganizationDto organizationDto = db.organizations().insert();
- ComponentDto sonarqube = db.components().insertComponent(newProjectDto(organizationDto).setName("ONE_PROJECT_NAME"));
- ComponentDto jdk8 = db.components().insertComponent(newProjectDto(organizationDto).setName("TWO_PROJECT_NAME"));
- ComponentDto ruby = db.components().insertComponent(newProjectDto(organizationDto).setName("ANOTHER_42"));
- dbClient.snapshotDao().insert(dbSession, newAnalysis(sonarqube), newAnalysis(jdk8), newAnalysis(ruby));
- db.components().indexAllComponents();
- db.commit();
-
- db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, sonarqube);
- db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk8);
- db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, ruby);
-
- SearchMyProjectsWsResponse result = call_ws(ws.newRequest().setParam(TEXT_QUERY, "_project_"));
-
- assertThat(result.getProjectsCount()).isEqualTo(2);
- assertThat(result.getProjectsList()).extracting(Project::getId)
- .containsOnlyOnce(sonarqube.uuid(), jdk8.uuid())
- .doesNotContain(ruby.uuid());
- }
-
- @Test
- public void search_my_projects_by_exact_match_on_key() {
- OrganizationDto organizationDto = db.organizations().insert();
- ComponentDto sonarqube = db.components().insertComponent(newProjectDto(organizationDto).setKey("MY_PROJECT_KEY"));
- ComponentDto ruby = db.components().insertComponent(newProjectDto(organizationDto).setKey("MY_PROJECT_KEY_OR_ELSE"));
- dbClient.snapshotDao().insert(dbSession, newAnalysis(sonarqube), newAnalysis(ruby));
- db.components().indexAllComponents();
- db.commit();
-
- db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, sonarqube);
- db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, ruby);
-
- SearchMyProjectsWsResponse result = call_ws(ws.newRequest().setParam(TEXT_QUERY, "MY_PROJECT_KEY"));
-
- assertThat(result.getProjectsCount()).isEqualTo(1);
- assertThat(result.getProjectsList()).extracting(Project::getId)
- .containsOnlyOnce(sonarqube.uuid())
- .doesNotContain(ruby.uuid());
- }
-
@Test
public void empty_response() {
String result = ws.newRequest().execute().getInput();
call_ws();
}
- @Test
- public void fail_if_query_length_is_less_than_3_characters() {
- expectedException.expect(BadRequestException.class);
- expectedException.expectMessage("The 'q' parameter must have at least 3 characters");
-
- call_ws(ws.newRequest().setParam(TEXT_QUERY, "ab"));
- }
-
private ComponentDto insertClang(OrganizationDto organizationDto) {
return db.components().insertComponent(newProjectDto(organizationDto, Uuids.UUID_EXAMPLE_01)
.setName("Clang")
@Immutable
public class SearchMyProjectsRequest {
- private final String query;
private final Integer page;
private final Integer pageSize;
private SearchMyProjectsRequest(Builder builder) {
- this.query = builder.query;
this.page = builder.page;
this.pageSize = builder.pageSize;
}
- @CheckForNull
- public String getQuery() {
- return query;
- }
-
@CheckForNull
public Integer getPage() {
return page;
}
public static class Builder {
- private String query;
private Integer page;
private Integer pageSize;
// enforce method constructor
}
- public Builder setQuery(@Nullable String query) {
- this.query = query;
- return this;
- }
-
public Builder setPage(@Nullable Integer page) {
this.page = page;
return this;