Search in sources :

Example 1 with PrStatistics

use of org.sonar.server.issue.index.PrStatistics in project sonarqube by SonarSource.

the class ListAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectKey = request.mandatoryParam(PARAM_PROJECT);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
        checkPermission(project);
        List<BranchDto> pullRequests = dbClient.branchDao().selectByProject(dbSession, project).stream().filter(b -> b.getBranchType() == PULL_REQUEST).collect(toList());
        List<String> pullRequestUuids = pullRequests.stream().map(BranchDto::getUuid).collect(toList());
        Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao().selectByUuids(dbSession, pullRequests.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())).stream().collect(uniqueIndex(BranchDto::getUuid));
        Map<String, PrStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.getUuid(), pullRequestUuids).stream().collect(uniqueIndex(PrStatistics::getBranchUuid, Function.identity()));
        Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, singletonList(ALERT_STATUS_KEY)).stream().collect(uniqueIndex(LiveMeasureDto::getComponentUuid));
        Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream().collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt())));
        ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder();
        pullRequests.forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid())));
        writeProtobuf(protobufResponse.build(), request, response);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) AbstractUserSession.insufficientPrivilegesException(org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException) ComponentFinder(org.sonar.server.component.ComponentFinder) StringUtils(org.apache.commons.lang.StringUtils) ProjectPullRequests(org.sonarqube.ws.ProjectPullRequests) GlobalPermission(org.sonar.db.permission.GlobalPermission) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) PULL_REQUEST(org.sonar.db.component.BranchType.PULL_REQUEST) DbProjectBranches(org.sonar.db.protobuf.DbProjectBranches) Collections.singletonList(java.util.Collections.singletonList) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) IssueIndex(org.sonar.server.issue.index.IssueIndex) PullRequestsWs.addProjectParam(org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) PrStatistics(org.sonar.server.issue.index.PrStatistics) Nullable(javax.annotation.Nullable) MoreCollectors.toList(org.sonar.core.util.stream.MoreCollectors.toList) PARAM_PROJECT(org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT) USER(org.sonar.api.web.UserRole.USER) Optional.ofNullable(java.util.Optional.ofNullable) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) UserRole(org.sonar.api.web.UserRole) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) Change(org.sonar.api.server.ws.Change) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) BranchDto(org.sonar.db.component.BranchDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) PrStatistics(org.sonar.server.issue.index.PrStatistics) DbSession(org.sonar.db.DbSession) Objects(java.util.Objects)

Aggregations

Strings.emptyToNull (com.google.common.base.Strings.emptyToNull)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Function (java.util.function.Function)1 Nullable (javax.annotation.Nullable)1 StringUtils (org.apache.commons.lang.StringUtils)1 ALERT_STATUS_KEY (org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY)1 Change (org.sonar.api.server.ws.Change)1 Request (org.sonar.api.server.ws.Request)1 Response (org.sonar.api.server.ws.Response)1 WebService (org.sonar.api.server.ws.WebService)1 DateUtils.formatDateTime (org.sonar.api.utils.DateUtils.formatDateTime)1 UserRole (org.sonar.api.web.UserRole)1 USER (org.sonar.api.web.UserRole.USER)1 MoreCollectors.toList (org.sonar.core.util.stream.MoreCollectors.toList)1