Search in sources :

Example 11 with QualityGate

use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.

the class LiveQualityGateComputerImpl method refreshGateStatus.

@Override
public EvaluatedQualityGate refreshGateStatus(ComponentDto project, QualityGate gate, MeasureMatrix measureMatrix, Configuration configuration) {
    QualityGateEvaluator.Measures measures = metricKey -> {
        Optional<LiveMeasureDto> liveMeasureDto = measureMatrix.getMeasure(project, metricKey);
        if (!liveMeasureDto.isPresent()) {
            return Optional.empty();
        }
        MetricDto metric = measureMatrix.getMetricByUuid(liveMeasureDto.get().getMetricUuid());
        return Optional.of(new LiveMeasure(liveMeasureDto.get(), metric));
    };
    EvaluatedQualityGate evaluatedGate = evaluator.evaluate(gate, measures, configuration);
    measureMatrix.setValue(project, CoreMetrics.ALERT_STATUS_KEY, evaluatedGate.getStatus().name());
    measureMatrix.setValue(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY, QualityGateConverter.toJson(evaluatedGate));
    return evaluatedGate;
}
Also used : BranchDto(org.sonar.db.component.BranchDto) QualityGateConverter(org.sonar.server.qualitygate.QualityGateConverter) QualityGateEvaluator(org.sonar.server.qualitygate.QualityGateEvaluator) OptionalDouble(java.util.OptionalDouble) DbSession(org.sonar.db.DbSession) QualityGateFinder(org.sonar.server.qualitygate.QualityGateFinder) Condition(org.sonar.server.qualitygate.Condition) HashSet(java.util.HashSet) QualityGateConditionDto(org.sonar.db.qualitygate.QualityGateConditionDto) Map(java.util.Map) Configuration(org.sonar.api.config.Configuration) QualityGate(org.sonar.server.qualitygate.QualityGate) BranchType(org.sonar.db.component.BranchType) Collection(java.util.Collection) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Set(java.util.Set) QualityGateDto(org.sonar.db.qualitygate.QualityGateDto) MoreCollectors.toHashSet(org.sonar.core.util.stream.MoreCollectors.toHashSet) CoreMetrics(org.sonar.api.measures.CoreMetrics) DbClient(org.sonar.db.DbClient) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) MetricDto(org.sonar.db.metric.MetricDto) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) ProjectDto(org.sonar.db.project.ProjectDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) Optional(java.util.Optional) Metric(org.sonar.api.measures.Metric) MetricDto(org.sonar.db.metric.MetricDto) Optional(java.util.Optional) QualityGateEvaluator(org.sonar.server.qualitygate.QualityGateEvaluator) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate)

Example 12 with QualityGate

use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.

the class LiveQualityGateComputerImplTest method loadQualityGate_on_branch_returns_default_gate.

@Test
public void loadQualityGate_on_branch_returns_default_gate() {
    ProjectDto project = db.components().insertPublicProjectDto();
    BranchDto branch = db.components().insertProjectBranch(project).setBranchType(BranchType.BRANCH);
    MetricDto metric = db.measures().insertMetric();
    QualityGateDto gate = db.qualityGates().insertQualityGate();
    db.qualityGates().setDefaultQualityGate(gate);
    QualityGateConditionDto condition = db.qualityGates().addCondition(gate, metric);
    QualityGate result = underTest.loadQualityGate(db.getSession(), project, branch);
    assertThat(result.getId()).isEqualTo("" + gate.getUuid());
    assertThat(result.getConditions()).extracting(Condition::getMetricKey, Condition::getOperator, Condition::getErrorThreshold).containsExactlyInAnyOrder(tuple(metric.getKey(), Condition.Operator.fromDbValue(condition.getOperator()), condition.getErrorThreshold()));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) MetricTesting.newMetricDto(org.sonar.db.metric.MetricTesting.newMetricDto) MetricDto(org.sonar.db.metric.MetricDto) QualityGateConditionDto(org.sonar.db.qualitygate.QualityGateConditionDto) QualityGateDto(org.sonar.db.qualitygate.QualityGateDto) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Test(org.junit.Test)

Example 13 with QualityGate

use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.

the class LiveQualityGateComputerImplTest method loadQualityGate_returns_hardcoded_gate_for_pull_requests.

@Test
public void loadQualityGate_returns_hardcoded_gate_for_pull_requests() {
    ProjectDto project = db.components().insertPublicProjectDto();
    BranchDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST));
    MetricDto metric1 = db.measures().insertMetric(m -> m.setKey("new_metric"));
    MetricDto metric2 = db.measures().insertMetric(m -> m.setKey("metric"));
    QualityGateDto gate = db.qualityGates().insertQualityGate();
    db.qualityGates().setDefaultQualityGate(gate);
    db.qualityGates().addCondition(gate, metric1);
    db.qualityGates().addCondition(gate, metric2);
    QualityGate result = underTest.loadQualityGate(db.getSession(), project, branch);
    assertThat(result.getConditions()).extracting(Condition::getMetricKey).containsExactly("new_metric");
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) MetricTesting.newMetricDto(org.sonar.db.metric.MetricTesting.newMetricDto) MetricDto(org.sonar.db.metric.MetricDto) QualityGateDto(org.sonar.db.qualitygate.QualityGateDto) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) Test(org.junit.Test)

Example 14 with QualityGate

use of org.sonar.server.qualitygate.QualityGate in project sonarqube by SonarSource.

the class LiveMeasureComputerImplTest method event_contains_newQualityGate_computed_by_LiveQualityGateComputer.

@Test
public void event_contains_newQualityGate_computed_by_LiveQualityGateComputer() {
    markProjectAsAnalyzed(project);
    db.measures().insertLiveMeasure(project, alertStatusMetric, m -> m.setData(Metric.Level.ERROR.name()));
    db.measures().insertLiveMeasure(project, intMetric, m -> m.setVariation(42.0).setValue(null));
    BranchDto branch = db.getDbClient().branchDao().selectByBranchKey(db.getSession(), project.projectUuid(), "master").orElseThrow(() -> new IllegalStateException("Can't find master branch"));
    List<QGChangeEvent> result = run(file1, newQualifierBasedIntLeakFormula());
    assertThat(result).extracting(QGChangeEvent::getQualityGateSupplier).extracting(Supplier::get).containsExactly(Optional.of(newQualityGate));
    verify(qGateComputer).loadQualityGate(any(DbSession.class), argThat(p -> p.getUuid().equals(projectDto.getUuid())), eq(branch));
    verify(qGateComputer).getMetricsRelatedTo(qualityGate);
    verify(qGateComputer).refreshGateStatus(eq(project), same(qualityGate), any(MeasureMatrix.class), any(Configuration.class));
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) CorePropertyDefinitions(org.sonar.core.config.CorePropertyDefinitions) MapSettings(org.sonar.api.config.internal.MapSettings) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) Collections.singleton(java.util.Collections.singleton) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Configuration(org.sonar.api.config.Configuration) ComponentTesting(org.sonar.db.component.ComponentTesting) PropertyDefinitions(org.sonar.api.config.PropertyDefinitions) QualityGate(org.sonar.server.qualitygate.QualityGate) TestProjectConfigurationLoader(org.sonar.server.setting.TestProjectConfigurationLoader) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) CoreMetrics(org.sonar.api.measures.CoreMetrics) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) ProjectDto(org.sonar.db.project.ProjectDto) Optional(java.util.Optional) QGChangeEvent(org.sonar.server.qualitygate.changeevent.QGChangeEvent) Mockito.mock(org.mockito.Mockito.mock) BranchDto(org.sonar.db.component.BranchDto) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RunWith(org.junit.runner.RunWith) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Supplier(java.util.function.Supplier) TestProjectIndexers(org.sonar.server.es.TestProjectIndexers) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ProjectIndexer(org.sonar.server.es.ProjectIndexer) ORDERED_BOTTOM_UP(org.sonar.api.resources.Qualifiers.ORDERED_BOTTOM_UP) Nullable(javax.annotation.Nullable) Before(org.junit.Before) BranchType(org.sonar.db.component.BranchType) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) ProjectConfigurationLoader(org.sonar.server.setting.ProjectConfigurationLoader) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) MetricDto(org.sonar.db.metric.MetricDto) Qualifiers(org.sonar.api.resources.Qualifiers) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Metric(org.sonar.api.measures.Metric) Rating(org.sonar.server.measure.Rating) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) Configuration(org.sonar.api.config.Configuration) QGChangeEvent(org.sonar.server.qualitygate.changeevent.QGChangeEvent) Test(org.junit.Test)

Aggregations

EvaluatedQualityGate (org.sonar.server.qualitygate.EvaluatedQualityGate)14 QualityGate (org.sonar.server.qualitygate.QualityGate)14 Test (org.junit.Test)11 MetricDto (org.sonar.db.metric.MetricDto)8 Condition (org.sonar.server.qualitygate.Condition)6 BranchDto (org.sonar.db.component.BranchDto)5 ComponentDto (org.sonar.db.component.ComponentDto)5 ProjectDto (org.sonar.db.project.ProjectDto)5 LiveMeasureDto (org.sonar.db.measure.LiveMeasureDto)4 MetricTesting.newMetricDto (org.sonar.db.metric.MetricTesting.newMetricDto)4 QualityGateDto (org.sonar.db.qualitygate.QualityGateDto)4 EvaluatedCondition (org.sonar.server.qualitygate.EvaluatedCondition)4 Configuration (org.sonar.api.config.Configuration)3 Metric (org.sonar.api.measures.Metric)3 QualityGateConditionDto (org.sonar.db.qualitygate.QualityGateConditionDto)3 Collection (java.util.Collection)2 Optional (java.util.Optional)2 CoreMetrics (org.sonar.api.measures.CoreMetrics)2 DbSession (org.sonar.db.DbSession)2 BranchType (org.sonar.db.component.BranchType)2