Search in sources :

Example 1 with NewCodePeriods

use of org.sonarqube.ws.NewCodePeriods 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.getProjectByKey(dbSession, projectKey);
        userSession.checkProjectPermission(UserRole.ADMIN, project);
        Collection<BranchDto> branches = dbClient.branchDao().selectByProject(dbSession, project).stream().filter(b -> b.getBranchType() == BranchType.BRANCH).sorted(Comparator.comparing(BranchDto::getKey)).collect(toList());
        List<NewCodePeriodDto> newCodePeriods = newCodePeriodDao.selectAllByProject(dbSession, project.getUuid());
        Map<String, InheritedNewCodePeriod> newCodePeriodByBranchUuid = newCodePeriods.stream().collect(Collectors.toMap(NewCodePeriodDto::getBranchUuid, dto -> new InheritedNewCodePeriod(dto, dto.getBranchUuid() == null)));
        InheritedNewCodePeriod projectDefault = newCodePeriodByBranchUuid.getOrDefault(null, getGlobalOrDefault(dbSession));
        Map<String, String> analysis = newCodePeriods.stream().filter(newCodePeriodDto -> newCodePeriodDto.getType().equals(NewCodePeriodType.SPECIFIC_ANALYSIS)).collect(Collectors.toMap(NewCodePeriodDto::getUuid, NewCodePeriodDto::getValue));
        Map<String, Long> analysisUuidDateMap = dbClient.snapshotDao().selectByUuids(dbSession, new HashSet<>(analysis.values())).stream().collect(Collectors.toMap(SnapshotDto::getUuid, SnapshotDto::getCreatedAt));
        ListWSResponse.Builder builder = ListWSResponse.newBuilder();
        for (BranchDto branch : branches) {
            InheritedNewCodePeriod inherited = newCodePeriodByBranchUuid.getOrDefault(branch.getUuid(), projectDefault);
            String effectiveValue = null;
            // handles specific analysis only
            Long analysisDate = analysisUuidDateMap.get(analysis.get(inherited.getUuid()));
            if (analysisDate != null) {
                effectiveValue = DateUtils.formatDateTime(analysisDate);
            }
            builder.addNewCodePeriods(build(projectKey, branch.getKey(), inherited.getType(), inherited.getValue(), inherited.inherited, effectiveValue));
        }
        writeProtobuf(builder.build(), request, response);
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) ComponentFinder(org.sonar.server.component.ComponentFinder) NewCodePeriodDao(org.sonar.db.newcodeperiod.NewCodePeriodDao) DbSession(org.sonar.db.DbSession) HashSet(java.util.HashSet) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) DateUtils(org.sonar.api.utils.DateUtils) Map(java.util.Map) Response(org.sonar.api.server.ws.Response) NewCodePeriodType(org.sonar.db.newcodeperiod.NewCodePeriodType) Nullable(javax.annotation.Nullable) ListWSResponse(org.sonarqube.ws.NewCodePeriods.ListWSResponse) MoreCollectors.toList(org.sonar.core.util.stream.MoreCollectors.toList) BranchType(org.sonar.db.component.BranchType) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) NewCodePeriods(org.sonarqube.ws.NewCodePeriods) DbClient(org.sonar.db.DbClient) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) List(java.util.List) UserRole(org.sonar.api.web.UserRole) ProjectDto(org.sonar.db.project.ProjectDto) UserSession(org.sonar.server.user.UserSession) Comparator(java.util.Comparator) SnapshotDto(org.sonar.db.component.SnapshotDto) ShowWSResponse.newBuilder(org.sonarqube.ws.NewCodePeriods.ShowWSResponse.newBuilder) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) ListWSResponse(org.sonarqube.ws.NewCodePeriods.ListWSResponse) BranchDto(org.sonar.db.component.BranchDto) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) DbSession(org.sonar.db.DbSession)

Aggregations

Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)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 (org.sonar.api.utils.DateUtils)1 UserRole (org.sonar.api.web.UserRole)1 MoreCollectors.toList (org.sonar.core.util.stream.MoreCollectors.toList)1 DbClient (org.sonar.db.DbClient)1 DbSession (org.sonar.db.DbSession)1 BranchDto (org.sonar.db.component.BranchDto)1 BranchType (org.sonar.db.component.BranchType)1 SnapshotDto (org.sonar.db.component.SnapshotDto)1 NewCodePeriodDao (org.sonar.db.newcodeperiod.NewCodePeriodDao)1 NewCodePeriodDto (org.sonar.db.newcodeperiod.NewCodePeriodDto)1