@Override public void handle(Request request, Response response) throws Exception { String projectKey = request.mandatoryParam(PARAM_PROJECT); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getByKey(dbSession, projectKey); checkPermission(project); checkArgument(ALLOWED_QUALIFIERS.contains(project.qualifier()), "Invalid project"); Collection<BranchDto> branches = dbClient.branchDao().selectByComponent(dbSession, project).stream() .filter(b -> b.getBranchType() == SHORT || b.getBranchType() == LONG) .collect(toList()); List<String> branchUuids = branches.stream().map(BranchDto::getUuid).collect(toList()); Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao() .selectByUuids(dbSession, branches.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())) .stream().collect(uniqueIndex(BranchDto::getUuid)); Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao() .selectByComponentUuidsAndMetricKeys(dbSession, branchUuids, singletonList(ALERT_STATUS_KEY)).stream() .collect(uniqueIndex(LiveMeasureDto::getComponentUuid)); Map<String, BranchStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.uuid(), branches.stream() .filter(b -> b.getBranchType().equals(SHORT)) .map(BranchDto::getUuid).collect(toList())).stream() .collect(uniqueIndex(BranchStatistics::getBranchUuid, Function.identity())); Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao() .selectLastAnalysesByRootComponentUuids(dbSession, branchUuids).stream() .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt()))); ProjectBranches.ListWsResponse.Builder protobufResponse = ProjectBranches.ListWsResponse.newBuilder(); branches.forEach(b -> addBranch(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid()))); WsUtils.writeProtobuf(protobufResponse.build(), request, response); } }
@Override public void handle(Request request, Response response) throws Exception { String projectKey = request.mandatoryParam(PARAM_PROJECT); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getByKey(dbSession, projectKey); checkPermission(project); checkArgument(project.isEnabled() && PROJECT.equals(project.qualifier()), "Invalid project key"); List<BranchDto> pullRequests = dbClient.branchDao().selectByComponent(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, BranchStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.uuid(), pullRequestUuids).stream() .collect(uniqueIndex(BranchStatistics::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); } }
@Test public void selectByUuids() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch1 = db.components().insertProjectBranch(project); ComponentDto branch2 = db.components().insertProjectBranch(project); ComponentDto branch3 = db.components().insertProjectBranch(project); assertThat(underTest.selectByUuids(db.getSession(), asList(branch1.uuid(), branch2.uuid(), branch3.uuid()))) .extracting(BranchDto::getUuid) .containsExactlyInAnyOrder(branch1.uuid(), branch2.uuid(), branch3.uuid()); assertThat(underTest.selectByUuids(db.getSession(), singletonList(branch1.uuid()))) .extracting(BranchDto::getUuid) .containsExactlyInAnyOrder(branch1.uuid()); assertThat(underTest.selectByUuids(db.getSession(), singletonList("unknown"))).isEmpty(); }
@Override public void handle(Request request, Response response) throws Exception { String projectKey = request.mandatoryParam(PARAM_PROJECT); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getByKey(dbSession, projectKey); checkPermission(project); checkArgument(project.isEnabled() && PROJECT.equals(project.qualifier()), "Invalid project key"); List<BranchDto> pullRequests = dbClient.branchDao().selectByComponent(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, BranchStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.uuid(), pullRequestUuids).stream() .collect(uniqueIndex(BranchStatistics::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); } }
@Override public void handle(Request request, Response response) throws Exception { String projectKey = request.mandatoryParam(PARAM_PROJECT); try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto project = componentFinder.getByKey(dbSession, projectKey); checkPermission(project); checkArgument(project.isEnabled() && PROJECT.equals(project.qualifier()), "Invalid project key"); Collection<BranchDto> branches = dbClient.branchDao().selectByComponent(dbSession, project).stream() .filter(b -> b.getBranchType() == SHORT || b.getBranchType() == LONG) .collect(MoreCollectors.toList()); List<String> branchUuids = branches.stream().map(BranchDto::getUuid).collect(toList()); Map<String, BranchDto> mergeBranchesByUuid = dbClient.branchDao() .selectByUuids(dbSession, branches.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())) .stream().collect(uniqueIndex(BranchDto::getUuid)); Map<String, LiveMeasureDto> qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao() .selectByComponentUuidsAndMetricKeys(dbSession, branchUuids, singletonList(ALERT_STATUS_KEY)).stream() .collect(uniqueIndex(LiveMeasureDto::getComponentUuid)); Map<String, BranchStatistics> branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.uuid(), branches.stream() .filter(b -> b.getBranchType().equals(SHORT)) .map(BranchDto::getUuid).collect(toList())).stream() .collect(uniqueIndex(BranchStatistics::getBranchUuid, Function.identity())); Map<String, String> analysisDateByBranchUuid = dbClient.snapshotDao() .selectLastAnalysesByRootComponentUuids(dbSession, branchUuids).stream() .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt()))); ProjectBranches.ListWsResponse.Builder protobufResponse = ProjectBranches.ListWsResponse.newBuilder(); branches.forEach(b -> addBranch(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), analysisDateByBranchUuid.get(b.getUuid()))); WsUtils.writeProtobuf(protobufResponse.build(), request, response); } }