Search in sources :

Example 1 with ComponentTreeBuilder

use of org.sonar.ce.task.projectanalysis.component.ComponentTreeBuilder 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)

Aggregations

MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Nullable (javax.annotation.Nullable)1 StringUtils.trimToNull (org.apache.commons.lang.StringUtils.trimToNull)1 Analysis (org.sonar.ce.task.projectanalysis.analysis.Analysis)1 Branch (org.sonar.ce.task.projectanalysis.analysis.Branch)1 MutableAnalysisMetadataHolder (org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolder)1 BatchReportReader (org.sonar.ce.task.projectanalysis.batch.BatchReportReader)1 Component (org.sonar.ce.task.projectanalysis.component.Component)1 ComponentKeyGenerator (org.sonar.ce.task.projectanalysis.component.ComponentKeyGenerator)1 ComponentTreeBuilder (org.sonar.ce.task.projectanalysis.component.ComponentTreeBuilder)1 ComponentUuidFactoryWithMigration (org.sonar.ce.task.projectanalysis.component.ComponentUuidFactoryWithMigration)1 DefaultBranchImpl (org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl)1 MutableTreeRootHolder (org.sonar.ce.task.projectanalysis.component.MutableTreeRootHolder)1 ProjectAttributes (org.sonar.ce.task.projectanalysis.component.ProjectAttributes)1 ReportModulesPath (org.sonar.ce.task.projectanalysis.component.ReportModulesPath)1 ComputationStep (org.sonar.ce.task.step.ComputationStep)1 DbClient (org.sonar.db.DbClient)1 DbSession (org.sonar.db.DbSession)1