Search in sources :

Example 1 with DebtRatingGrid

use of org.sonar.server.measure.DebtRatingGrid in project sonarqube by SonarSource.

the class MaintainabilityMeasuresVisitorTest method setUp.

@Before
public void setUp() {
    // assumes rating configuration is consistent
    when(ratingSettings.getDebtRatingGrid()).thenReturn(new DebtRatingGrid(RATING_GRID));
    when(ratingSettings.getDevCost(LANGUAGE_KEY_1)).thenReturn(DEV_COST_LANGUAGE_1);
    when(ratingSettings.getDevCost(LANGUAGE_KEY_2)).thenReturn(DEV_COST_LANGUAGE_2);
    underTest = new VisitorsCrawler(singletonList(new MaintainabilityMeasuresVisitor(metricRepository, measureRepository, ratingSettings)));
}
Also used : VisitorsCrawler(org.sonar.ce.task.projectanalysis.component.VisitorsCrawler) DebtRatingGrid(org.sonar.server.measure.DebtRatingGrid) Before(org.junit.Before)

Example 2 with DebtRatingGrid

use of org.sonar.server.measure.DebtRatingGrid in project sonarqube by SonarSource.

the class LiveMeasureComputerImpl method refreshComponentsOnSameProject.

private Optional<QGChangeEvent> refreshComponentsOnSameProject(DbSession dbSession, List<ComponentDto> touchedComponents) {
    // load all the components to be refreshed, including their ancestors
    List<ComponentDto> components = loadTreeOfComponents(dbSession, touchedComponents);
    ComponentDto branchComponent = findBranchComponent(components);
    BranchDto branch = loadBranch(dbSession, branchComponent);
    ProjectDto project = loadProject(dbSession, branch.getProjectUuid());
    Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, branchComponent.uuid());
    if (!lastAnalysis.isPresent()) {
        return Optional.empty();
    }
    QualityGate qualityGate = qGateComputer.loadQualityGate(dbSession, project, branch);
    Collection<String> metricKeys = getKeysOfAllInvolvedMetrics(qualityGate);
    List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
    Map<String, MetricDto> metricsPerId = metrics.stream().collect(uniqueIndex(MetricDto::getUuid));
    List<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(toArrayList(components.size()));
    List<LiveMeasureDto> dbMeasures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricUuids(dbSession, componentUuids, metricsPerId.keySet());
    // previous status must be load now as MeasureMatrix mutate the LiveMeasureDto which are passed to it
    Metric.Level previousStatus = loadPreviousStatus(metrics, dbMeasures);
    Configuration config = projectConfigurationLoader.loadProjectConfiguration(dbSession, branchComponent);
    DebtRatingGrid debtRatingGrid = new DebtRatingGrid(config);
    MeasureMatrix matrix = new MeasureMatrix(components, metricsPerId.values(), dbMeasures);
    FormulaContextImpl context = new FormulaContextImpl(matrix, debtRatingGrid);
    long beginningOfLeak = getBeginningOfLeakPeriod(lastAnalysis, branch);
    components.forEach(c -> {
        IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByBaseComponent(dbSession, c, beginningOfLeak));
        for (IssueMetricFormula formula : formulaFactory.getFormulas()) {
            // use formulas when the leak period is defined, it's a PR, or the formula is not about the leak period
            if (shouldUseLeakFormulas(lastAnalysis.get(), branch) || !formula.isOnLeak()) {
                context.change(c, formula);
                try {
                    formula.compute(context, issueCounter);
                } catch (RuntimeException e) {
                    throw new IllegalStateException("Fail to compute " + formula.getMetric().getKey() + " on " + context.getComponent().getDbKey(), e);
                }
            }
        }
    });
    EvaluatedQualityGate evaluatedQualityGate = qGateComputer.refreshGateStatus(branchComponent, qualityGate, matrix, config);
    // persist the measures that have been created or updated
    matrix.getChanged().sorted(LiveMeasureComparator.INSTANCE).forEach(m -> dbClient.liveMeasureDao().insertOrUpdate(dbSession, m));
    projectIndexer.commitAndIndexComponents(dbSession, singleton(branchComponent), ProjectIndexer.Cause.MEASURE_CHANGE);
    return Optional.of(new QGChangeEvent(project, branch, lastAnalysis.get(), config, previousStatus, () -> Optional.of(evaluatedQualityGate)));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) Configuration(org.sonar.api.config.Configuration) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) QGChangeEvent(org.sonar.server.qualitygate.changeevent.QGChangeEvent) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) MetricDto(org.sonar.db.metric.MetricDto) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Metric(org.sonar.api.measures.Metric) DebtRatingGrid(org.sonar.server.measure.DebtRatingGrid)

Example 3 with DebtRatingGrid

use of org.sonar.server.measure.DebtRatingGrid in project sonarqube by SonarSource.

the class NewMaintainabilityMeasuresVisitorTest method setUp.

@Before
public void setUp() {
    when(ratingSettings.getDebtRatingGrid()).thenReturn(new DebtRatingGrid(RATING_GRID));
    underTest = new VisitorsCrawler(Arrays.asList(new NewMaintainabilityMeasuresVisitor(metricRepository, measureRepository, newLinesRepository, ratingSettings)));
}
Also used : VisitorsCrawler(org.sonar.ce.task.projectanalysis.component.VisitorsCrawler) DebtRatingGrid(org.sonar.server.measure.DebtRatingGrid) Before(org.junit.Before)

Aggregations

DebtRatingGrid (org.sonar.server.measure.DebtRatingGrid)3 Before (org.junit.Before)2 VisitorsCrawler (org.sonar.ce.task.projectanalysis.component.VisitorsCrawler)2 Configuration (org.sonar.api.config.Configuration)1 Metric (org.sonar.api.measures.Metric)1 BranchDto (org.sonar.db.component.BranchDto)1 ComponentDto (org.sonar.db.component.ComponentDto)1 SnapshotDto (org.sonar.db.component.SnapshotDto)1 LiveMeasureDto (org.sonar.db.measure.LiveMeasureDto)1 MetricDto (org.sonar.db.metric.MetricDto)1 ProjectDto (org.sonar.db.project.ProjectDto)1 EvaluatedQualityGate (org.sonar.server.qualitygate.EvaluatedQualityGate)1 QualityGate (org.sonar.server.qualitygate.QualityGate)1 QGChangeEvent (org.sonar.server.qualitygate.changeevent.QGChangeEvent)1