Search in sources :

Example 91 with BranchDto

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

the class BranchPersisterImplTest method persist_creates_row_in_PROJECTS_BRANCHES_for_pull_request.

@Test
public void persist_creates_row_in_PROJECTS_BRANCHES_for_pull_request() {
    String pullRequestId = "pr-123";
    // add project and branch in table PROJECTS
    ComponentDto mainComponent = ComponentTesting.newPrivateProjectDto(MAIN.getUuid()).setDbKey(MAIN.getKey());
    ComponentDto component = ComponentTesting.newBranchComponent(mainComponent, new BranchDto().setUuid(BRANCH1.getUuid()).setKey(BRANCH1.getKey()).setBranchType(PULL_REQUEST));
    dbTester.components().insertComponents(mainComponent, component);
    // set project in metadata
    treeRootHolder.setRoot(BRANCH1);
    analysisMetadataHolder.setBranch(createBranch(PULL_REQUEST, false, pullRequestId, "mergeBanchUuid"));
    analysisMetadataHolder.setProject(Project.from(mainComponent));
    analysisMetadataHolder.setPullRequestKey(pullRequestId);
    underTest.persist(dbTester.getSession());
    dbTester.getSession().commit();
    assertThat(dbTester.countRowsOfTable("components")).isEqualTo(2);
    Optional<BranchDto> branchDto = dbTester.getDbClient().branchDao().selectByUuid(dbTester.getSession(), BRANCH1.getUuid());
    assertThat(branchDto).isPresent();
    assertThat(branchDto.get().getBranchType()).isEqualTo(PULL_REQUEST);
    assertThat(branchDto.get().getKey()).isEqualTo(pullRequestId);
    assertThat(branchDto.get().getMergeBranchUuid()).isEqualTo("mergeBanchUuid");
    assertThat(branchDto.get().getProjectUuid()).isEqualTo(MAIN.getUuid());
    assertThat(branchDto.get().getPullRequestData()).isEqualTo(DbProjectBranches.PullRequestData.newBuilder().setBranch(pullRequestId).setTarget("mergeBanchUuid").setTitle(pullRequestId).build());
}
Also used : BranchDto(org.sonar.db.component.BranchDto) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 92 with BranchDto

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

the class BranchPersisterImplTest method pull_request_is_never_excluded_from_branch_purge_even_if_its_source_branch_name_matches_sonar_dbcleaner_keepFromPurge_property.

@Test
public void pull_request_is_never_excluded_from_branch_purge_even_if_its_source_branch_name_matches_sonar_dbcleaner_keepFromPurge_property() {
    settings.setProperty(BRANCHES_TO_KEEP_WHEN_INACTIVE, "develop");
    analysisMetadataHolder.setBranch(createPullRequest(PR1.getKey(), MAIN.getUuid()));
    analysisMetadataHolder.setPullRequestKey(PR1.getKey());
    treeRootHolder.setRoot(PR1);
    ComponentDto mainComponent = dbTester.components().insertPublicProject(p -> p.setDbKey(MAIN.getDbKey()).setUuid(MAIN.getUuid()));
    ComponentDto component = ComponentTesting.newBranchComponent(mainComponent, new BranchDto().setUuid(PR1.getUuid()).setKey(PR1.getKey()).setProjectUuid(MAIN.getUuid()).setBranchType(PULL_REQUEST).setMergeBranchUuid(MAIN.getUuid()));
    dbTester.getDbClient().componentDao().insert(dbTester.getSession(), component);
    dbTester.commit();
    underTest.persist(dbTester.getSession());
    Optional<BranchDto> branchDto = dbTester.getDbClient().branchDao().selectByUuid(dbTester.getSession(), PR1.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)

Example 93 with BranchDto

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

the class PurgeDaoTest method deleteAnalyses_deletes_rows_in_events_and_event_component_changes.

@Test
public void deleteAnalyses_deletes_rows_in_events_and_event_component_changes() {
    ComponentDto project = ComponentTesting.newPrivateProjectDto();
    dbClient.componentDao().insert(dbSession, project);
    SnapshotDto projectAnalysis1 = db.components().insertSnapshot(project);
    SnapshotDto projectAnalysis2 = db.components().insertSnapshot(project);
    SnapshotDto projectAnalysis3 = db.components().insertSnapshot(project);
    SnapshotDto projectAnalysis4 = db.components().insertSnapshot(project);
    EventDto projectEvent1 = db.events().insertEvent(projectAnalysis1);
    EventDto projectEvent2 = db.events().insertEvent(projectAnalysis2);
    EventDto projectEvent3 = db.events().insertEvent(projectAnalysis3);
    // note: projectAnalysis4 has no event
    ComponentDto referencedProjectA = db.components().insertPublicProject();
    ComponentDto referencedProjectB = db.components().insertPublicProject();
    db.events().insertEventComponentChanges(projectEvent1, projectAnalysis1, randomChangeCategory(), referencedProjectA, null);
    db.events().insertEventComponentChanges(projectEvent1, projectAnalysis1, randomChangeCategory(), referencedProjectB, null);
    BranchDto branchProjectA = newBranchDto(referencedProjectA);
    ComponentDto cptBranchProjectA = ComponentTesting.newBranchComponent(referencedProjectA, branchProjectA);
    db.events().insertEventComponentChanges(projectEvent2, projectAnalysis2, randomChangeCategory(), cptBranchProjectA, branchProjectA);
    // note: projectEvent3 has no component change
    // delete non existing analysis has no effect
    underTest.deleteAnalyses(dbSession, new PurgeProfiler(), singletonList("foo"));
    assertThat(uuidsIn("event_component_changes", "event_analysis_uuid")).containsOnly(projectAnalysis1.getUuid(), projectAnalysis2.getUuid());
    assertThat(db.countRowsOfTable("event_component_changes")).isEqualTo(3);
    assertThat(uuidsIn("events")).containsOnly(projectEvent1.getUuid(), projectEvent2.getUuid(), projectEvent3.getUuid());
    underTest.deleteAnalyses(dbSession, new PurgeProfiler(), singletonList(projectAnalysis1.getUuid()));
    assertThat(uuidsIn("event_component_changes", "event_analysis_uuid")).containsOnly(projectAnalysis2.getUuid());
    assertThat(db.countRowsOfTable("event_component_changes")).isOne();
    assertThat(uuidsIn("events")).containsOnly(projectEvent2.getUuid(), projectEvent3.getUuid());
    underTest.deleteAnalyses(dbSession, new PurgeProfiler(), singletonList(projectAnalysis4.getUuid()));
    assertThat(uuidsIn("event_component_changes", "event_analysis_uuid")).containsOnly(projectAnalysis2.getUuid());
    assertThat(db.countRowsOfTable("event_component_changes")).isOne();
    assertThat(uuidsIn("events")).containsOnly(projectEvent2.getUuid(), projectEvent3.getUuid());
    underTest.deleteAnalyses(dbSession, new PurgeProfiler(), singletonList(projectAnalysis3.getUuid()));
    assertThat(uuidsIn("event_component_changes", "event_analysis_uuid")).containsOnly(projectAnalysis2.getUuid());
    assertThat(db.countRowsOfTable("event_component_changes")).isOne();
    assertThat(uuidsIn("events")).containsOnly(projectEvent2.getUuid());
    underTest.deleteAnalyses(dbSession, new PurgeProfiler(), singletonList(projectAnalysis2.getUuid()));
    assertThat(db.countRowsOfTable("event_component_changes")).isZero();
    assertThat(db.countRowsOfTable("events")).isZero();
}
Also used : BranchDto(org.sonar.db.component.BranchDto) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) EventDto(org.sonar.db.event.EventDto) Test(org.junit.Test)

Example 94 with BranchDto

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

the class PurgeDaoTest method deleteProject_deletes_app_projects.

@Test
public void deleteProject_deletes_app_projects() {
    ProjectDto app = db.components().insertPrivateApplicationDto();
    BranchDto appBranch = db.components().insertProjectBranch(app);
    ProjectDto project = db.components().insertPublicProjectDto();
    BranchDto projectBranch = db.components().insertProjectBranch(project);
    ProjectDto otherProject = db.components().insertPublicProjectDto();
    db.components().addApplicationProject(app, project, otherProject);
    db.components().addProjectBranchToApplicationBranch(appBranch, projectBranch);
    assertThat(db.countRowsOfTable("app_branch_project_branch")).isOne();
    underTest.deleteProject(dbSession, project.getUuid(), project.getQualifier(), project.getName(), project.getKey());
    assertThat(dbClient.applicationProjectsDao().selectProjects(dbSession, app.getUuid())).extracting(ProjectDto::getUuid).containsExactlyInAnyOrder(otherProject.getUuid());
    assertThat(db.countRowsOfTable("app_branch_project_branch")).isZero();
}
Also used : PortfolioProjectDto(org.sonar.db.portfolio.PortfolioProjectDto) ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) ComponentTesting.newBranchDto(org.sonar.db.component.ComponentTesting.newBranchDto) Test(org.junit.Test)

Example 95 with BranchDto

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

the class NewCodeReferenceBranchComponentUuids method lazyInit.

private void lazyInit() {
    if (referenceBranchComponentsUuidsByKey == null) {
        Preconditions.checkState(periodHolder.hasPeriod() && periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name()));
        referenceBranchComponentsUuidsByKey = new HashMap<>();
        try (DbSession dbSession = dbClient.openSession(false)) {
            String referenceKey = periodHolder.getPeriod().getModeParameter() != null ? periodHolder.getPeriod().getModeParameter() : "";
            Optional<BranchDto> opt = dbClient.branchDao().selectByBranchKey(dbSession, analysisMetadataHolder.getProject().getUuid(), referenceKey);
            if (opt.isPresent()) {
                init(opt.get().getUuid(), dbSession);
            }
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto)

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