use of org.sonar.db.newcodeperiod.NewCodePeriodDto 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);
}
}
use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.
the class ExportNewCodePeriodsStepTest method export_only_project_new_code_periods_on_branches_excluded_from_purge.
@Test
public void export_only_project_new_code_periods_on_branches_excluded_from_purge() {
NewCodePeriodDto newCodePeriod1 = newDto("uuid1", PROJECT.uuid(), null, SPECIFIC_ANALYSIS, "analysis-uuid");
NewCodePeriodDto newCodePeriod2 = newDto("uuid2", PROJECT.uuid(), "branch-uuid-1", SPECIFIC_ANALYSIS, "analysis-uuid");
// the following new code periods are not exported
NewCodePeriodDto newCodePeriod3 = newDto("uuid3", PROJECT.uuid(), "branch-uuid-2", SPECIFIC_ANALYSIS, "analysis-uuid");
NewCodePeriodDto anotherProjectNewCodePeriods = newDto("uuid4", ANOTHER_PROJECT.uuid(), "branch-uuid-3", SPECIFIC_ANALYSIS, "analysis-uuid");
NewCodePeriodDto globalNewCodePeriod = newDto("uuid5", null, null, PREVIOUS_VERSION, null);
insertNewCodePeriods(newCodePeriod1, newCodePeriod2, newCodePeriod3, anotherProjectNewCodePeriods, globalNewCodePeriod);
underTest.execute(new TestComputationStepContext());
List<ProjectDump.NewCodePeriod> exportedProps = dumpWriter.getWrittenMessagesOf(DumpElement.NEW_CODE_PERIODS);
assertThat(exportedProps).hasSize(2);
assertThat(exportedProps).extracting(ProjectDump.NewCodePeriod::getUuid).containsOnly("uuid1", "uuid2");
assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("2 new code periods exported");
}
use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.
the class LoadPeriodsStep method execute.
@Override
public void execute(ComputationStep.Context context) {
if (!analysisMetadataHolder.isBranch()) {
periodsHolder.setPeriod(null);
return;
}
String projectUuid = getProjectBranchUuid();
String branchUuid = treeRootHolder.getRoot().getUuid();
String projectVersion = treeRootHolder.getRoot().getProjectAttributes().getProjectVersion();
try (DbSession dbSession = dbClient.openSession(false)) {
Optional<NewCodePeriodDto> dto = firstPresent(Arrays.asList(() -> getBranchSetting(dbSession, projectUuid, branchUuid), () -> getProjectSetting(dbSession, projectUuid), () -> getGlobalSetting(dbSession)));
NewCodePeriodDto newCodePeriod = dto.orElse(NewCodePeriodDto.defaultInstance());
if (analysisMetadataHolder.isFirstAnalysis() && newCodePeriod.getType() != NewCodePeriodType.REFERENCE_BRANCH) {
periodsHolder.setPeriod(null);
return;
}
Period period = resolver.resolve(dbSession, branchUuid, newCodePeriod, projectVersion);
periodsHolder.setPeriod(period);
}
}
use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.
the class SettingsSection method addDefaultNewCodeDefinition.
private void addDefaultNewCodeDefinition(Builder protobuf) {
try (DbSession dbSession = dbClient.openSession(false)) {
Optional<NewCodePeriodDto> period = dbClient.newCodePeriodDao().selectGlobal(dbSession);
setAttribute(protobuf, "Default New Code Definition", parseDefaultNewCodeDefinition(period));
}
}
use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.
the class DeleteActionTest method fail_when_analysis_is_new_code_period_baseline.
@Test
public void fail_when_analysis_is_new_code_period_baseline() {
String analysisUuid = RandomStringUtils.randomAlphabetic(12);
ComponentDto project = db.components().insertPrivateProject();
SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(project).setUuid(analysisUuid).setLast(false));
db.newCodePeriods().insert(new NewCodePeriodDto().setProjectUuid(project.uuid()).setBranchUuid(project.uuid()).setType(NewCodePeriodType.SPECIFIC_ANALYSIS).setValue(analysis.getUuid()));
db.commit();
logInAsProjectAdministrator(project);
assertThatThrownBy(() -> call(analysisUuid)).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("The analysis '" + analysisUuid + "' can not be deleted because it is set as a new code period baseline");
}
Aggregations