import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static java.util.Optional.ofNullable;
+import static java.util.function.Function.identity;
import static org.sonar.api.resources.Qualifiers.APP;
import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.api.resources.Qualifiers.VIEW;
ComponentQuery query = buildDbQuery(request);
Paging paging = buildPaging(dbSession, request, organization, query);
List<ComponentDto> components = dbClient.componentDao().selectByQuery(dbSession, organization.getUuid(), query, paging.offset(), paging.pageSize());
- Map<String, Long> analysisDateByComponentUuid = dbClient.snapshotDao()
- .selectLastAnalysesByRootComponentUuids(dbSession, components.stream().map(ComponentDto::uuid).collect(MoreCollectors.toList())).stream()
- .collect(MoreCollectors.uniqueIndex(SnapshotDto::getComponentUuid, SnapshotDto::getCreatedAt));
- return buildResponse(components, organization, analysisDateByComponentUuid, paging);
+ Set<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(MoreCollectors.toHashSet(components.size()));
+ Map<String, SnapshotDto> snapshotsByComponentUuid = dbClient.snapshotDao()
+ .selectLastAnalysesByRootComponentUuids(dbSession, componentUuids).stream()
+ .collect(MoreCollectors.uniqueIndex(SnapshotDto::getComponentUuid, identity()));
+ return buildResponse(components, organization, snapshotsByComponentUuid, paging);
}
}
.andTotal(total);
}
- private static SearchWsResponse buildResponse(List<ComponentDto> components, OrganizationDto organization, Map<String, Long> analysisDateByComponentUuid, Paging paging) {
+ private static SearchWsResponse buildResponse(List<ComponentDto> components, OrganizationDto organization, Map<String, SnapshotDto> snapshotsByComponentUuid, Paging paging) {
SearchWsResponse.Builder responseBuilder = newBuilder();
responseBuilder.getPagingBuilder()
.setPageIndex(paging.pageIndex())
.build();
components.stream()
- .map(dto -> dtoToProject(organization, dto, analysisDateByComponentUuid.get(dto.uuid())))
+ .map(dto -> dtoToProject(organization, dto, snapshotsByComponentUuid.get(dto.uuid())))
.forEach(responseBuilder::addComponents);
return responseBuilder.build();
}
- private static Component dtoToProject(OrganizationDto organization, ComponentDto dto, @Nullable Long analysisDate) {
+ private static Component dtoToProject(OrganizationDto organization, ComponentDto dto, @Nullable SnapshotDto snapshot) {
checkArgument(
organization.getUuid().equals(dto.getOrganizationUuid()),
"No Organization found for uuid '%s'",
.setName(dto.name())
.setQualifier(dto.qualifier())
.setVisibility(dto.isPrivate() ? PRIVATE.getLabel() : PUBLIC.getLabel());
- ofNullable(analysisDate).ifPresent(d -> builder.setLastAnalysisDate(formatDateTime(d)));
+ if (snapshot != null) {
+ // FIXME created_at should not be nullable
+ ofNullable(snapshot.getCreatedAt()).ifPresent(d -> builder.setLastAnalysisDate(formatDateTime(d)));
+ ofNullable(snapshot.getRevision()).ifPresent(builder::setRevision);
+ }
return builder.build();
}
*/
package org.sonar.server.project.ws;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import java.util.List;
import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;
+import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.api.utils.Paging.offset;
import static org.sonar.server.project.ws.SearchMyProjectsData.builder;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
.setId(dto.uuid())
.setKey(dto.getDbKey())
.setName(dto.name());
- data.lastAnalysisDateFor(dto.uuid()).ifPresent(project::setLastAnalysisDate);
+ data.lastSnapshot(dto.uuid()).ifPresent(s -> {
+ project.setLastAnalysisDate(formatDateTime(s.getCreatedAt()));
+ ofNullable(s.getRevision()).ifPresent(project::setRevision);
+ });
data.qualityGateStatusFor(dto.uuid()).ifPresent(project::setQualityGate);
ofNullable(emptyToNull(dto.description())).ifPresent(project::setDescription);
}
}
- SearchMyProjectsData load(DbSession dbSession, SearchMyProjectsRequest request) {
+ private SearchMyProjectsData load(DbSession dbSession, SearchMyProjectsRequest request) {
SearchMyProjectsData.Builder data = builder();
ProjectsResult searchResult = searchProjects(dbSession, request);
List<ComponentDto> projects = searchResult.projects;
return data.build();
}
- @VisibleForTesting
- ProjectsResult searchProjects(DbSession dbSession, SearchMyProjectsRequest request) {
+ private ProjectsResult searchProjects(DbSession dbSession, SearchMyProjectsRequest request) {
int userId = requireNonNull(userSession.getUserId(), "Current user must be authenticated");
List<Long> componentIds = dbClient.roleDao().selectComponentIdsByPermissionAndUserId(dbSession, UserRole.ADMIN, userId);
import static com.google.common.collect.ImmutableList.copyOf;
import static java.util.Objects.requireNonNull;
-import static org.sonar.api.utils.DateUtils.formatDateTime;
+import static java.util.function.Function.identity;
+import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
class SearchMyProjectsData {
private final List<ComponentDto> projects;
private final ListMultimap<String, ProjectLinkDto> projectLinksByProjectUuid;
- private final Map<String, String> lastAnalysisDates;
+ private final Map<String, SnapshotDto> snapshotsByComponentUuid;
private final Map<String, String> qualityGateStatuses;
private final int totalNbOfProject;
private SearchMyProjectsData(Builder builder) {
this.projects = copyOf(builder.projects);
this.projectLinksByProjectUuid = buildProjectLinks(builder.projectLinks);
- this.lastAnalysisDates = buildAnalysisDates(builder.snapshots);
+ this.snapshotsByComponentUuid =builder.snapshots.stream().collect(uniqueIndex(SnapshotDto::getComponentUuid, identity()));
this.qualityGateStatuses = buildQualityGateStatuses(builder.qualityGates);
this.totalNbOfProject = builder.totalNbOfProjects;
}
return projectLinksByProjectUuid.get(projectUuid);
}
- Optional<String> lastAnalysisDateFor(String componentUuid) {
- return Optional.ofNullable(lastAnalysisDates.get(componentUuid));
+ Optional<SnapshotDto> lastSnapshot(String componentUuid) {
+ return Optional.ofNullable(snapshotsByComponentUuid.get(componentUuid));
}
Optional<String> qualityGateStatusFor(String componentUuid) {
return projectLinks.build();
}
- private static Map<String, String> buildAnalysisDates(List<SnapshotDto> snapshots) {
- return ImmutableMap.copyOf(snapshots.stream().collect(Collectors.toMap(
- SnapshotDto::getComponentUuid,
- snapshot -> formatDateTime(snapshot.getCreatedAt()))));
- }
-
private static Map<String, String> buildQualityGateStatuses(List<LiveMeasureDto> measures) {
return ImmutableMap.copyOf(measures.stream()
.collect(Collectors.toMap(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getDataAsString)));
"name": "Project Name 1",
"qualifier": "TRK",
"visibility": "public",
- "lastAnalysisDate": "2017-03-01T11:39:03+0300"
+ "lastAnalysisDate": "2017-03-01T11:39:03+0300",
+ "revision": "cfb82f55c6ef32e61828c4cb3db2da12795fd767"
},
{
"organization": "my-org-1",
"name": "Project Name 1",
"qualifier": "TRK",
"visibility": "private",
- "lastAnalysisDate": "2017-03-02T15:21:47+0300"
+ "lastAnalysisDate": "2017-03-02T15:21:47+0300",
+ "revision": "7be96a94ac0c95a61ee6ee0ef9c6f808d386a355"
}
]
}
db.components().insertComponents(
publicProject,
privateProject);
- db.getDbClient().snapshotDao().insert(db.getSession(), newAnalysis(publicProject).setCreatedAt(parseDateTime("2017-03-01T11:39:03+0300").getTime()));
- db.getDbClient().snapshotDao().insert(db.getSession(), newAnalysis(privateProject).setCreatedAt(parseDateTime("2017-03-02T15:21:47+0300").getTime()));
+ db.getDbClient().snapshotDao().insert(db.getSession(), newAnalysis(publicProject)
+ .setCreatedAt(parseDateTime("2017-03-01T11:39:03+0300").getTime())
+ .setRevision("cfb82f55c6ef32e61828c4cb3db2da12795fd767"));
+ db.getDbClient().snapshotDao().insert(db.getSession(), newAnalysis(privateProject)
+ .setCreatedAt(parseDateTime("2017-03-02T15:21:47+0300").getTime())
+ .setRevision("7be96a94ac0c95a61ee6ee0ef9c6f808d386a355"));
db.commit();
String response = ws.newRequest()
optional string lastAnalysisDate = 6;
optional string qualityGate = 7;
repeated Link links = 8;
+ optional string revision = 9;
}
message Link {
optional string qualifier = 5;
optional string visibility = 6;
optional string lastAnalysisDate = 7;
+ optional string revision = 8;
}
}