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);
}
}
Aggregations