Search in sources :

Example 91 with Component

use of org.sonar.ce.task.projectanalysis.component.Component in project sonarqube by SonarSource.

the class EnableAnalysisStep method execute.

@Override
public void execute(ComputationStep.Context context) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        Component project = treeRootHolder.getRoot();
        dbClient.snapshotDao().switchIsLastFlagAndSetProcessedStatus(dbSession, project.getUuid(), analysisMetadataHolder.getUuid());
        dbClient.componentDao().applyBChangesForRootComponentUuid(dbSession, project.getUuid());
        dbSession.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) Component(org.sonar.ce.task.projectanalysis.component.Component)

Example 92 with Component

use of org.sonar.ce.task.projectanalysis.component.Component in project sonarqube by SonarSource.

the class UpdateQualityProfilesLastUsedDateStep method execute.

@Override
public void execute(ComputationStep.Context context) {
    Component root = treeRootHolder.getRoot();
    Metric metric = metricRepository.getByKey(QUALITY_PROFILES_KEY);
    Set<QualityProfile> qualityProfiles = parseQualityProfiles(measureRepository.getRawMeasure(root, metric));
    try (DbSession dbSession = dbClient.openSession(true)) {
        for (QualityProfile qualityProfile : qualityProfiles) {
            updateDate(dbSession, qualityProfile.getQpKey(), analysisMetadataHolder.getAnalysisDate());
        }
        dbSession.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) Metric(org.sonar.ce.task.projectanalysis.metric.Metric) QualityProfile(org.sonar.server.qualityprofile.QualityProfile) Component(org.sonar.ce.task.projectanalysis.component.Component)

Example 93 with Component

use of org.sonar.ce.task.projectanalysis.component.Component 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 94 with Component

use of org.sonar.ce.task.projectanalysis.component.Component in project sonarqube by SonarSource.

the class FileMoveDetectionStep method execute.

@Override
public void execute(ComputationStep.Context context) {
    // do nothing if no files in db (first analysis)
    if (analysisMetadataHolder.isFirstAnalysis()) {
        LOG.debug("First analysis. Do nothing.");
        return;
    }
    Profiler p = Profiler.createIfTrace(LOG);
    p.start();
    Map<String, Component> reportFilesByUuid = getReportFilesByUuid(this.rootHolder.getRoot());
    context.getStatistics().add("reportFiles", reportFilesByUuid.size());
    if (reportFilesByUuid.isEmpty()) {
        LOG.debug("No files in report. No file move detection.");
        return;
    }
    Map<String, DbComponent> dbFilesByUuid = getDbFilesByUuid();
    context.getStatistics().add("dbFiles", dbFilesByUuid.size());
    Set<String> addedFileUuids = difference(reportFilesByUuid.keySet(), dbFilesByUuid.keySet());
    context.getStatistics().add("addedFiles", addedFileUuids.size());
    if (dbFilesByUuid.isEmpty()) {
        registerAddedFiles(addedFileUuids, reportFilesByUuid, null);
        LOG.debug("Previous snapshot has no file. No file move detection.");
        return;
    }
    Set<String> removedFileUuids = difference(dbFilesByUuid.keySet(), reportFilesByUuid.keySet());
    // can't find matches if at least one of the added or removed files groups is empty => abort
    if (addedFileUuids.isEmpty() || removedFileUuids.isEmpty()) {
        registerAddedFiles(addedFileUuids, reportFilesByUuid, null);
        LOG.debug("Either no files added or no files removed. Do nothing.");
        return;
    }
    // retrieve file data from report
    Map<String, File> addedFileHashesByUuid = getReportFileHashesByUuid(reportFilesByUuid, addedFileUuids);
    p.stopTrace("loaded");
    // compute score matrix
    p.start();
    ScoreMatrix scoreMatrix = computeScoreMatrix(dbFilesByUuid, removedFileUuids, addedFileHashesByUuid);
    p.stopTrace("Score matrix computed");
    scoreMatrixDumper.dumpAsCsv(scoreMatrix);
    // not a single match with score higher than MIN_REQUIRED_SCORE => abort
    if (scoreMatrix.getMaxScore() < MIN_REQUIRED_SCORE) {
        context.getStatistics().add("movedFiles", 0);
        registerAddedFiles(addedFileUuids, reportFilesByUuid, null);
        LOG.debug("max score in matrix is less than min required score ({}). Do nothing.", MIN_REQUIRED_SCORE);
        return;
    }
    p.start();
    MatchesByScore matchesByScore = MatchesByScore.create(scoreMatrix);
    ElectedMatches electedMatches = electMatches(removedFileUuids, addedFileHashesByUuid, matchesByScore);
    p.stopTrace("Matches elected");
    context.getStatistics().add("movedFiles", electedMatches.size());
    registerMatches(dbFilesByUuid, reportFilesByUuid, electedMatches);
    registerAddedFiles(addedFileUuids, reportFilesByUuid, electedMatches);
}
Also used : Profiler(org.sonar.core.util.logs.Profiler) Component(org.sonar.ce.task.projectanalysis.component.Component) File(org.sonar.ce.task.projectanalysis.filemove.FileSimilarity.File)

Example 95 with Component

use of org.sonar.ce.task.projectanalysis.component.Component in project sonarqube by SonarSource.

the class FileMoveDetectionStep method getReportFilesByUuid.

private static Map<String, Component> getReportFilesByUuid(Component root) {
    final ImmutableMap.Builder<String, Component> builder = ImmutableMap.builder();
    new DepthTraversalTypeAwareCrawler(new TypeAwareVisitorAdapter(CrawlerDepthLimit.FILE, POST_ORDER) {

        @Override
        public void visitFile(Component file) {
            builder.put(file.getUuid(), file);
        }
    }).visit(root);
    return builder.build();
}
Also used : DepthTraversalTypeAwareCrawler(org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler) TypeAwareVisitorAdapter(org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter) Component(org.sonar.ce.task.projectanalysis.component.Component) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Component (org.sonar.ce.task.projectanalysis.component.Component)118 Test (org.junit.Test)82 ReportComponent (org.sonar.ce.task.projectanalysis.component.ReportComponent)47 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)31 ComponentDto (org.sonar.db.component.ComponentDto)25 ViewsComponent (org.sonar.ce.task.projectanalysis.component.ViewsComponent)14 DefaultIssue (org.sonar.core.issue.DefaultIssue)14 DbSession (org.sonar.db.DbSession)11 Period (org.sonar.ce.task.projectanalysis.period.Period)8 SnapshotDto (org.sonar.db.component.SnapshotDto)8 FileAttributes (org.sonar.ce.task.projectanalysis.component.FileAttributes)6 DbClient (org.sonar.db.DbClient)6 List (java.util.List)5 ComputationStep (org.sonar.ce.task.step.ComputationStep)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Optional (java.util.Optional)4 DepthTraversalTypeAwareCrawler (org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler)4 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)3 Map (java.util.Map)3