Search in sources :

Example 1 with NewCodePeriodDto

use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.

the class ExportNewCodePeriodsStep method execute.

@Override
public void execute(Context context) {
    long count = 0L;
    try (StreamWriter<ProjectDump.NewCodePeriod> output = dumpWriter.newStreamWriter(DumpElement.NEW_CODE_PERIODS);
        DbSession dbSession = dbClient.openSession(false)) {
        final ProjectDump.NewCodePeriod.Builder builder = ProjectDump.NewCodePeriod.newBuilder();
        final List<NewCodePeriodDto> newCodePeriods = dbSession.getMapper(ProjectExportMapper.class).selectNewCodePeriodsForExport(projectHolder.projectDto().getUuid());
        for (NewCodePeriodDto newCodePeriod : newCodePeriods) {
            builder.clear().setUuid(newCodePeriod.getUuid()).setProjectUuid(newCodePeriod.getProjectUuid()).setType(newCodePeriod.getType().name());
            if (newCodePeriod.getBranchUuid() != null) {
                builder.setBranchUuid(newCodePeriod.getBranchUuid());
            }
            if (newCodePeriod.getValue() != null) {
                builder.setValue(newCodePeriod.getValue());
            }
            output.write(builder.build());
            ++count;
        }
        Loggers.get(getClass()).debug("{} new code periods exported", count);
    } catch (Exception e) {
        throw new IllegalStateException(format("New Code Periods Export failed after processing %d new code periods successfully", count), e);
    }
}
Also used : DbSession(org.sonar.db.DbSession) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) ProjectExportMapper(org.sonar.db.project.ProjectExportMapper)

Example 2 with NewCodePeriodDto

use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.

the class ExportNewCodePeriodsStepTest method test_exported_fields.

@Test
public void test_exported_fields() {
    NewCodePeriodDto dto = newDto("uuid1", PROJECT.uuid(), "branch-uuid-1", SPECIFIC_ANALYSIS, "analysis-uuid");
    insertNewCodePeriods(dto);
    underTest.execute(new TestComputationStepContext());
    ProjectDump.NewCodePeriod exportedNewCodePeriod = dumpWriter.getWrittenMessagesOf(DumpElement.NEW_CODE_PERIODS).get(0);
    assertThat(exportedNewCodePeriod.getUuid()).isEqualTo(dto.getUuid());
    assertThat(exportedNewCodePeriod.getProjectUuid()).isEqualTo(dto.getProjectUuid());
    assertThat(exportedNewCodePeriod.getBranchUuid()).isEqualTo(dto.getBranchUuid());
    assertThat(exportedNewCodePeriod.getType()).isEqualTo(dto.getType().name());
    assertThat(exportedNewCodePeriod.getValue()).isEqualTo(dto.getValue());
}
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 3 with NewCodePeriodDto

use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.

the class SetAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String projectKey = request.getParam(PARAM_PROJECT).emptyAsNull().or(() -> null);
    String branchKey = request.getParam(PARAM_BRANCH).emptyAsNull().or(() -> null);
    if (projectKey == null && branchKey != null) {
        throw new IllegalArgumentException("If branch key is specified, project key needs to be specified too");
    }
    try (DbSession dbSession = dbClient.openSession(false)) {
        String typeStr = request.mandatoryParam(PARAM_TYPE);
        String valueStr = request.getParam(PARAM_VALUE).emptyAsNull().or(() -> null);
        boolean isCommunityEdition = editionProvider.get().filter(t -> t == EditionProvider.Edition.COMMUNITY).isPresent();
        NewCodePeriodType type = validateType(typeStr, projectKey == null, branchKey != null || isCommunityEdition);
        NewCodePeriodDto dto = new NewCodePeriodDto();
        dto.setType(type);
        ProjectDto project = null;
        BranchDto branch = null;
        if (projectKey != null) {
            project = getProject(dbSession, projectKey);
            userSession.checkProjectPermission(UserRole.ADMIN, project);
            if (branchKey != null) {
                branch = getBranch(dbSession, project, branchKey);
                dto.setBranchUuid(branch.getUuid());
            } else if (isCommunityEdition) {
                // in CE set main branch value instead of project value
                branch = getMainBranch(dbSession, project);
                dto.setBranchUuid(branch.getUuid());
            }
            dto.setProjectUuid(project.getUuid());
        } else {
            userSession.checkIsSystemAdministrator();
        }
        setValue(dbSession, dto, type, project, branch, valueStr);
        newCodePeriodDao.upsert(dbSession, dto);
        dbSession.commit();
    }
}
Also used : BranchDto(org.sonar.db.component.BranchDto) ComponentFinder(org.sonar.server.component.ComponentFinder) NewCodePeriodDao(org.sonar.db.newcodeperiod.NewCodePeriodDao) NewCodePeriodParser(org.sonar.db.newcodeperiod.NewCodePeriodParser) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) WebService(org.sonar.api.server.ws.WebService) Locale(java.util.Locale) Response(org.sonar.api.server.ws.Response) NUMBER_OF_DAYS(org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS) NewCodePeriodType(org.sonar.db.newcodeperiod.NewCodePeriodType) EditionProvider(org.sonar.core.platform.EditionProvider) EnumSet(java.util.EnumSet) Nullable(javax.annotation.Nullable) PREVIOUS_VERSION(org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION) REFERENCE_BRANCH(org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH) PlatformEditionProvider(org.sonar.core.platform.PlatformEditionProvider) Set(java.util.Set) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) UserRole(org.sonar.api.web.UserRole) SPECIFIC_ANALYSIS(org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS) ProjectDto(org.sonar.db.project.ProjectDto) Preconditions(com.google.common.base.Preconditions) UserSession(org.sonar.server.user.UserSession) SnapshotDto(org.sonar.db.component.SnapshotDto) ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession) NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) BranchDto(org.sonar.db.component.BranchDto) NewCodePeriodType(org.sonar.db.newcodeperiod.NewCodePeriodType)

Example 4 with NewCodePeriodDto

use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.

the class PurgeDaoTest method selectPurgeableAnalyses_does_not_return_the_baseline.

@Test
public void selectPurgeableAnalyses_does_not_return_the_baseline() {
    ComponentDto project1 = db.components().insertPublicProject("master");
    SnapshotDto analysis1 = db.components().insertSnapshot(newSnapshot().setComponentUuid(project1.uuid()).setStatus(STATUS_PROCESSED).setLast(false));
    dbClient.newCodePeriodDao().insert(dbSession, new NewCodePeriodDto().setProjectUuid(project1.uuid()).setBranchUuid(project1.uuid()).setType(NewCodePeriodType.SPECIFIC_ANALYSIS).setValue(analysis1.getUuid()));
    ComponentDto project2 = db.components().insertPrivateProject();
    SnapshotDto analysis2 = db.components().insertSnapshot(newSnapshot().setComponentUuid(project2.uuid()).setStatus(STATUS_PROCESSED).setLast(false));
    db.components().insertProjectBranch(project2);
    assertThat(underTest.selectPurgeableAnalyses(project1.uuid(), dbSession)).isEmpty();
    assertThat(underTest.selectPurgeableAnalyses(project2.uuid(), dbSession)).extracting(PurgeableAnalysisDto::getAnalysisUuid).containsOnly(analysis2.getUuid());
}
Also used : NewCodePeriodDto(org.sonar.db.newcodeperiod.NewCodePeriodDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 5 with NewCodePeriodDto

use of org.sonar.db.newcodeperiod.NewCodePeriodDto in project sonarqube by SonarSource.

the class PurgeDaoTest method selectPurgeableAnalyses_does_not_return_the_baseline_of_specific_branch.

@Test
public void selectPurgeableAnalyses_does_not_return_the_baseline_of_specific_branch() {
    ComponentDto project = db.components().insertPublicProject("master");
    SnapshotDto analysisProject = db.components().insertSnapshot(newSnapshot().setComponentUuid(project.uuid()).setStatus(STATUS_PROCESSED).setLast(false));
    dbClient.newCodePeriodDao().insert(dbSession, new NewCodePeriodDto().setProjectUuid(project.uuid()).setBranchUuid(project.uuid()).setType(NewCodePeriodType.SPECIFIC_ANALYSIS).setValue(analysisProject.getUuid()));
    ComponentDto branch1 = db.components().insertProjectBranch(project);
    SnapshotDto analysisBranch1 = db.components().insertSnapshot(newSnapshot().setComponentUuid(branch1.uuid()).setStatus(STATUS_PROCESSED).setLast(false));
    ComponentDto branch2 = db.components().insertProjectBranch(project);
    SnapshotDto analysisBranch2 = db.components().insertSnapshot(newSnapshot().setComponentUuid(branch2.uuid()).setStatus(STATUS_PROCESSED).setLast(false));
    dbClient.newCodePeriodDao().insert(dbSession, new NewCodePeriodDto().setProjectUuid(project.uuid()).setBranchUuid(branch2.uuid()).setType(NewCodePeriodType.SPECIFIC_ANALYSIS).setValue(analysisBranch2.getUuid()));
    dbSession.commit();
    assertThat(underTest.selectPurgeableAnalyses(project.uuid(), dbSession)).isEmpty();
    assertThat(underTest.selectPurgeableAnalyses(branch1.uuid(), dbSession)).extracting(PurgeableAnalysisDto::getAnalysisUuid).containsOnly(analysisBranch1.getUuid());
    assertThat(underTest.selectPurgeableAnalyses(branch2.uuid(), dbSession)).isEmpty();
}
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