Search in sources :

Example 26 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class BuildComponentTreeStepTest method generate_keys_when_using_existing_branch.

@Test
public void generate_keys_when_using_existing_branch() {
    ComponentDto projectDto = dbTester.components().insertPublicProject();
    ComponentDto branchDto = dbTester.components().insertProjectBranch(projectDto);
    Branch branch = mock(Branch.class);
    when(branch.getName()).thenReturn(branchDto.getBranch());
    when(branch.isMain()).thenReturn(false);
    when(branch.generateKey(any(), any())).thenReturn(branchDto.getDbKey());
    analysisMetadataHolder.setRootComponentRef(ROOT_REF).setAnalysisDate(ANALYSIS_DATE).setProject(Project.from(projectDto)).setBranch(branch);
    BuildComponentTreeStep underTest = new BuildComponentTreeStep(dbClient, reportReader, treeRootHolder, analysisMetadataHolder, reportModulesPath);
    reportReader.putComponent(component(ROOT_REF, PROJECT, branchDto.getKey()));
    underTest.execute(new TestComputationStepContext());
    verifyComponentByRef(ROOT_REF, branchDto.getDbKey(), branchDto.getKey(), analysisMetadataHolder.getProject().getName(), branchDto.uuid());
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) ComponentDto(org.sonar.db.component.ComponentDto) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Test(org.junit.Test)

Example 27 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class BuildComponentTreeStep method execute.

@Override
public void execute(ComputationStep.Context context) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        ScannerReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
        ComponentKeyGenerator keyGenerator = loadKeyGenerator();
        ComponentKeyGenerator publicKeyGenerator = loadPublicKeyGenerator();
        ScannerReport.Metadata metadata = reportReader.readMetadata();
        // root key of branch, not necessarily of project
        String rootKey = keyGenerator.generateKey(reportProject.getKey(), null);
        Function<String, String> pathToKey = path -> keyGenerator.generateKey(reportProject.getKey(), path);
        // loads the UUIDs from database. If they don't exist, then generate new ones
        ComponentUuidFactoryWithMigration componentUuidFactoryWithMigration = new ComponentUuidFactoryWithMigration(dbClient, dbSession, rootKey, pathToKey, reportModulesPath.get());
        String rootUuid = componentUuidFactoryWithMigration.getOrCreateForKey(rootKey);
        Optional<SnapshotDto> baseAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, rootUuid);
        ComponentTreeBuilder builder = new ComponentTreeBuilder(keyGenerator, publicKeyGenerator, componentUuidFactoryWithMigration::getOrCreateForKey, reportReader::readComponent, analysisMetadataHolder.getProject(), analysisMetadataHolder.getBranch(), createProjectAttributes(metadata, baseAnalysis.orElse(null)));
        String relativePathFromScmRoot = metadata.getRelativePathFromScmRoot();
        Component reportTreeRoot = builder.buildProject(reportProject, relativePathFromScmRoot);
        if (analysisMetadataHolder.isPullRequest()) {
            Component changedComponentTreeRoot = builder.buildChangedComponentTreeRoot(reportTreeRoot);
            treeRootHolder.setRoots(changedComponentTreeRoot, reportTreeRoot);
        } else {
            treeRootHolder.setRoots(reportTreeRoot, reportTreeRoot);
        }
        analysisMetadataHolder.setBaseAnalysis(baseAnalysis.map(BuildComponentTreeStep::toAnalysis).orElse(null));
        context.getStatistics().add("components", treeRootHolder.getSize());
    }
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) Component(org.sonar.ce.task.projectanalysis.component.Component) Analysis(org.sonar.ce.task.projectanalysis.analysis.Analysis) ComponentKeyGenerator(org.sonar.ce.task.projectanalysis.component.ComponentKeyGenerator) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) StringUtils.trimToNull(org.apache.commons.lang.StringUtils.trimToNull) ProjectAttributes(org.sonar.ce.task.projectanalysis.component.ProjectAttributes) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) DbClient(org.sonar.db.DbClient) MutableAnalysisMetadataHolder(org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolder) ComputationStep(org.sonar.ce.task.step.ComputationStep) BatchReportReader(org.sonar.ce.task.projectanalysis.batch.BatchReportReader) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) ComponentTreeBuilder(org.sonar.ce.task.projectanalysis.component.ComponentTreeBuilder) DefaultBranchImpl(org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl) SnapshotDto(org.sonar.db.component.SnapshotDto) MutableTreeRootHolder(org.sonar.ce.task.projectanalysis.component.MutableTreeRootHolder) Nullable(javax.annotation.Nullable) ComponentUuidFactoryWithMigration(org.sonar.ce.task.projectanalysis.component.ComponentUuidFactoryWithMigration) ReportModulesPath(org.sonar.ce.task.projectanalysis.component.ReportModulesPath) SnapshotDto(org.sonar.db.component.SnapshotDto) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) ComponentTreeBuilder(org.sonar.ce.task.projectanalysis.component.ComponentTreeBuilder) DbSession(org.sonar.db.DbSession) ComponentUuidFactoryWithMigration(org.sonar.ce.task.projectanalysis.component.ComponentUuidFactoryWithMigration) ComponentKeyGenerator(org.sonar.ce.task.projectanalysis.component.ComponentKeyGenerator) Component(org.sonar.ce.task.projectanalysis.component.Component)

Example 28 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class BranchPersisterImplTest method createBranch.

private static Branch createBranch(BranchType type, boolean isMain, String name, @Nullable String mergeBranchUuid) {
    Branch branch = mock(Branch.class);
    when(branch.getType()).thenReturn(type);
    when(branch.getName()).thenReturn(name);
    when(branch.isMain()).thenReturn(isMain);
    when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
    when(branch.getTargetBranchName()).thenReturn(mergeBranchUuid);
    return branch;
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch)

Example 29 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class SiblingComponentsWithOpenIssuesTest method setBranch.

private void setBranch(BranchType currentBranchType, @Nullable String mergeBranchUuid) {
    Branch branch = mock(Branch.class);
    when(branch.getType()).thenReturn(currentBranchType);
    when(branch.getReferenceBranchUuid()).thenReturn(mergeBranchUuid);
    metadataHolder.setBranch(branch);
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch)

Example 30 with Branch

use of org.sonar.ce.task.projectanalysis.analysis.Branch in project sonarqube by SonarSource.

the class ReferenceBranchComponentUuidsTest method setUp.

@Before
public void setUp() {
    underTest = new ReferenceBranchComponentUuids(analysisMetadataHolder, db.getDbClient());
    project = mock(Project.class);
    analysisMetadataHolder.setProject(project);
    analysisMetadataHolder.setBranch(branch);
    ComponentDto projectDto = db.components().insertPublicProject();
    when(project.getUuid()).thenReturn(projectDto.uuid());
    branch1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch1"));
    branch2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch2"));
    pr1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr1").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
    pr2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr2").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
    branch1File = ComponentTesting.newFileDto(branch1, null, "file").setUuid("branch1File");
    branch2File = ComponentTesting.newFileDto(branch2, null, "file").setUuid("branch2File");
    pr1File = ComponentTesting.newFileDto(pr1, null, "file").setUuid("file1");
    pr2File = ComponentTesting.newFileDto(pr2, null, "file").setUuid("file2");
    db.components().insertComponents(branch1File, pr1File, pr2File, branch2File);
}
Also used : Branch(org.sonar.ce.task.projectanalysis.analysis.Branch) DbTester(org.sonar.db.DbTester) BranchType(org.sonar.db.component.BranchType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) SnapshotTesting.newAnalysis(org.sonar.db.component.SnapshotTesting.newAnalysis) AnalysisMetadataHolderRule(org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule) ComponentTesting(org.sonar.db.component.ComponentTesting) Before(org.junit.Before) Project(org.sonar.server.project.Project) Mockito.mock(org.mockito.Mockito.mock) Project(org.sonar.server.project.Project) ComponentDto(org.sonar.db.component.ComponentDto) Before(org.junit.Before)

Aggregations

Branch (org.sonar.ce.task.projectanalysis.analysis.Branch)40 Test (org.junit.Test)21 DefaultIssue (org.sonar.core.issue.DefaultIssue)7 ComponentDto (org.sonar.db.component.ComponentDto)7 Before (org.junit.Before)5 BranchType (org.sonar.db.component.BranchType)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Rule (org.junit.Rule)4 Mockito.mock (org.mockito.Mockito.mock)4 Mockito.when (org.mockito.Mockito.when)4 AnalysisMetadataHolderRule (org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule)4 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)4 DbTester (org.sonar.db.DbTester)4 Project (org.sonar.server.project.Project)4 Date (java.util.Date)3 FieldDiffs (org.sonar.core.issue.FieldDiffs)3 ComponentTesting (org.sonar.db.component.ComponentTesting)3 SnapshotTesting.newAnalysis (org.sonar.db.component.SnapshotTesting.newAnalysis)3 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)3 RuleKey (org.sonar.api.rule.RuleKey)2