Search in sources :

Example 86 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class ReportSubmitter method submit.

public CeTask submit(String projectKey, @Nullable String projectName, Map<String, String> characteristics, InputStream reportInput) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        boolean projectCreated = false;
        // Note: when the main branch is analyzed, the characteristics may or may not have the branch name, so componentKey#isMainBranch is not
        // reliable!
        BranchSupport.ComponentKey componentKey = branchSupport.createComponentKey(projectKey, characteristics);
        Optional<ComponentDto> mainBranchComponentOpt = dbClient.componentDao().selectByKey(dbSession, componentKey.getKey());
        ComponentDto mainBranchComponent;
        if (mainBranchComponentOpt.isPresent()) {
            mainBranchComponent = mainBranchComponentOpt.get();
            validateProject(dbSession, mainBranchComponent, projectKey);
        } else {
            mainBranchComponent = createProject(dbSession, componentKey.getMainBranchComponentKey(), projectName);
            projectCreated = true;
        }
        BranchDto mainBranch = dbClient.branchDao().selectByUuid(dbSession, mainBranchComponent.projectUuid()).orElseThrow(() -> new IllegalStateException("Couldn't find the main branch of the project"));
        ComponentDto branchComponent;
        if (isMainBranch(componentKey, mainBranch)) {
            branchComponent = mainBranchComponent;
        } else {
            branchComponent = dbClient.componentDao().selectByKey(dbSession, componentKey.getDbKey()).orElseGet(() -> branchSupport.createBranchComponent(dbSession, componentKey, mainBranchComponent, mainBranch));
        }
        if (projectCreated) {
            componentUpdater.commitAndIndex(dbSession, mainBranchComponent);
        } else {
            dbSession.commit();
        }
        checkScanPermission(branchComponent);
        return submitReport(dbSession, reportInput, branchComponent, characteristics);
    }
}
Also used : DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) ComponentDto(org.sonar.db.component.ComponentDto)

Example 87 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class ComponentUpdater method createMainBranch.

private void createMainBranch(DbSession session, String componentUuid, @Nullable String mainBranch) {
    BranchDto branch = new BranchDto().setBranchType(BranchType.BRANCH).setUuid(componentUuid).setKey(Optional.ofNullable(mainBranch).orElse(BranchDto.DEFAULT_MAIN_BRANCH_NAME)).setMergeBranchUuid(null).setExcludeFromPurge(true).setProjectUuid(componentUuid);
    dbClient.branchDao().upsert(session, branch);
}
Also used : BranchDto(org.sonar.db.component.BranchDto)

Example 88 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class QualityGateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    response.setHeader("Cache-Control", "no-cache");
    response.stream().setMediaType(SVG);
    try (DbSession dbSession = dbClient.openSession(false)) {
        support.validateToken(request);
        BranchDto branch = support.getBranch(dbSession, request);
        Level qualityGateStatus = getQualityGate(dbSession, branch);
        String result = svgGenerator.generateQualityGate(qualityGateStatus);
        String eTag = getETag(result);
        Optional<String> requestedETag = request.header("If-None-Match");
        if (requestedETag.filter(eTag::equals).isPresent()) {
            response.stream().setStatus(304);
            return;
        }
        response.setHeader("ETag", eTag);
        write(result, response.stream().output(), UTF_8);
    } catch (ProjectBadgesException | ForbiddenException | NotFoundException e) {
        // There is an issue, so do not return any ETag but make this response expire now
        SimpleDateFormat sdf = new SimpleDateFormat(RFC1123_DATE, Locale.US);
        response.setHeader("Expires", sdf.format(new Date()));
        write(svgGenerator.generateError(e.getMessage()), response.stream().output(), UTF_8);
    }
}
Also used : DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) NotFoundException(org.sonar.server.exceptions.NotFoundException) Level(org.sonar.api.measures.Metric.Level) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 89 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class ReferenceBranchComponentUuids method lazyInit.

private void lazyInit() {
    if (referenceBranchComponentsUuidsByKey == null) {
        String referenceBranchUuid = analysisMetadataHolder.getBranch().getReferenceBranchUuid();
        referenceBranchComponentsUuidsByKey = new HashMap<>();
        try (DbSession dbSession = dbClient.openSession(false)) {
            Optional<BranchDto> opt = dbClient.branchDao().selectByUuid(dbSession, referenceBranchUuid);
            checkState(opt.isPresent(), "Reference branch '%s' does not exist", referenceBranchUuid);
            referenceBranchName = opt.get().getKey();
            init(referenceBranchUuid, dbSession);
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto)

Example 90 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class BranchPersisterImplTest method branch_is_not_excluded_from_purge_when_it_does_not_match_setting.

@Test
public void branch_is_not_excluded_from_purge_when_it_does_not_match_setting() {
    analysisMetadataHolder.setProject(PROJECT);
    analysisMetadataHolder.setBranch(createBranch(BRANCH, false, "BRANCH_KEY"));
    treeRootHolder.setRoot(BRANCH1);
    ComponentDto mainComponent = dbTester.components().insertPublicProject(p -> p.setDbKey(MAIN.getDbKey()).setUuid(MAIN.getUuid()));
    ComponentDto component = ComponentTesting.newBranchComponent(mainComponent, new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(BRANCH));
    dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component);
    settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "abc.*");
    dbTester.commit();
    underTest.persist(dbTester.getSession());
    Optional<BranchDto> branchDto = dbTester.getDbClient().branchDao().selectByUuid(dbTester.getSession(), BRANCH1.getUuid());
    assertThat(branchDto).isPresent();
    assertThat(branchDto.get().isExcludeFromPurge()).isFalse();
}
Also used : BranchDto(org.sonar.db.component.BranchDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Aggregations

BranchDto (org.sonar.db.component.BranchDto)111 Test (org.junit.Test)62 ComponentDto (org.sonar.db.component.ComponentDto)52 ProjectDto (org.sonar.db.project.ProjectDto)42 DbSession (org.sonar.db.DbSession)31 SnapshotDto (org.sonar.db.component.SnapshotDto)22 List (java.util.List)15 ComponentTesting.newBranchDto (org.sonar.db.component.ComponentTesting.newBranchDto)13 DbClient (org.sonar.db.DbClient)12 MetricDto (org.sonar.db.metric.MetricDto)12 Map (java.util.Map)9 Optional (java.util.Optional)9 Nullable (javax.annotation.Nullable)9 WebService (org.sonar.api.server.ws.WebService)9 System2 (org.sonar.api.utils.System2)9 NotFoundException (org.sonar.server.exceptions.NotFoundException)9 Collection (java.util.Collection)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Rule (org.junit.Rule)8 Request (org.sonar.api.server.ws.Request)8