Search in sources :

Example 16 with NewCodePeriodDto

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

Example 17 with NewCodePeriodDto

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");
}
Also used : ProjectDump(com.sonarsource.governance.projectdump.protobuf.ProjectDump) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Test(org.junit.Test)

Example 18 with NewCodePeriodDto

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);
    }
}
Also used : DbSession(org.sonar.db.DbSession) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) Period(org.sonar.ce.task.projectanalysis.period.Period)

Example 19 with NewCodePeriodDto

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));
    }
}
Also used : DbSession(org.sonar.db.DbSession) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto)

Example 20 with NewCodePeriodDto

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");
}
Also used : NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Aggregations

NewCodePeriodDto (org.sonar.db.newcodeperiod.NewCodePeriodDto)27 Test (org.junit.Test)20 ComponentDto (org.sonar.db.component.ComponentDto)16 SnapshotDto (org.sonar.db.component.SnapshotDto)12 ShowWSResponse (org.sonarqube.ws.NewCodePeriods.ShowWSResponse)10 DbSession (org.sonar.db.DbSession)6 BranchDto (org.sonar.db.component.BranchDto)6 ListWSResponse (org.sonarqube.ws.NewCodePeriods.ListWSResponse)6 WebService (org.sonar.api.server.ws.WebService)4 UserRole (org.sonar.api.web.UserRole)4 DbClient (org.sonar.db.DbClient)4 NewCodePeriodDao (org.sonar.db.newcodeperiod.NewCodePeriodDao)4 NewCodePeriodType (org.sonar.db.newcodeperiod.NewCodePeriodType)4 ComponentFinder (org.sonar.server.component.ComponentFinder)4 DateUtils (org.sonar.api.utils.DateUtils)3 BranchType (org.sonar.db.component.BranchType)3 NotFoundException (org.sonar.server.exceptions.NotFoundException)3 NewCodePeriods (org.sonarqube.ws.NewCodePeriods)3 ProjectDump (com.sonarsource.governance.projectdump.protobuf.ProjectDump)2 Instant (java.time.Instant)2