*/
package org.sonar.ce.task.projectanalysis.step;
-import java.util.Collection;
import javax.annotation.Nullable;
+import org.sonar.api.CoreProperties;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.ce.task.projectanalysis.component.Component;
+import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
import org.sonar.ce.task.projectanalysis.measure.Measure;
import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
import org.sonar.ce.task.projectanalysis.qualitygate.EvaluationResult;
import org.sonar.ce.task.projectanalysis.step.QualityGateMeasuresStep.MetricEvaluationResult;
-import static java.util.Arrays.asList;
+import static org.sonar.server.qualitygate.QualityGateEvaluatorImpl.MAXIMUM_NEW_LINES_FOR_SMALL_CHANGESETS;
+import static org.sonar.server.qualitygate.QualityGateEvaluatorImpl.METRICS_TO_IGNORE_ON_SMALL_CHANGESETS;
public class SmallChangesetQualityGateSpecialCase {
-
- /**
- * Some metrics will be ignored on very small change sets.
- */
- private static final Collection<String> METRICS_TO_IGNORE_ON_SMALL_CHANGESETS = asList(
- CoreMetrics.NEW_COVERAGE_KEY,
- CoreMetrics.NEW_LINE_COVERAGE_KEY,
- CoreMetrics.NEW_BRANCH_COVERAGE_KEY,
- CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY,
- CoreMetrics.NEW_DUPLICATED_LINES_KEY,
- CoreMetrics.NEW_BLOCKS_DUPLICATED_KEY);
- private static final int MAXIMUM_NEW_LINES_FOR_SMALL_CHANGESETS = 19;
-
private final MeasureRepository measureRepository;
private final MetricRepository metricRepository;
+ private final ConfigurationRepository config;
- public SmallChangesetQualityGateSpecialCase(MeasureRepository measureRepository, MetricRepository metricRepository) {
+ public SmallChangesetQualityGateSpecialCase(MeasureRepository measureRepository, MetricRepository metricRepository, ConfigurationRepository config) {
this.measureRepository = measureRepository;
this.metricRepository = metricRepository;
+ this.config = config;
}
public boolean appliesTo(Component project, @Nullable MetricEvaluationResult metricEvaluationResult) {
return metricEvaluationResult != null
&& metricEvaluationResult.evaluationResult.getLevel() != Measure.Level.OK
&& METRICS_TO_IGNORE_ON_SMALL_CHANGESETS.contains(metricEvaluationResult.condition.getMetric().getKey())
+ && config.getConfiguration().getBoolean(CoreProperties.QUALITY_GATE_IGNORE_SMALL_CHANGES).orElse(true)
&& isSmallChangeset(project);
}
private boolean isSmallChangeset(Component project) {
return measureRepository.getRawMeasure(project, metricRepository.getByKey(CoreMetrics.NEW_LINES_KEY))
- .map(newLines -> newLines.hasVariation() && newLines.getVariation() <= MAXIMUM_NEW_LINES_FOR_SMALL_CHANGESETS)
+ .map(newLines -> newLines.hasVariation() && newLines.getVariation() < MAXIMUM_NEW_LINES_FOR_SMALL_CHANGESETS)
.orElse(false);
}
}
import org.junit.rules.ExpectedException;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
+import org.sonar.api.config.internal.ConfigurationBridge;
+import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.ReportComponent;
+import org.sonar.ce.task.projectanalysis.component.TestSettingsRepository;
import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.ce.task.projectanalysis.measure.Measure;
import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
@Rule
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
-
private EvaluationResultTextConverter resultTextConverter = mock(EvaluationResultTextConverter.class);
+ private MapSettings mapSettings = new MapSettings();
+ private TestSettingsRepository settings = new TestSettingsRepository(new ConfigurationBridge(mapSettings));
private QualityGateMeasuresStep underTest = new QualityGateMeasuresStep(treeRootHolder, qualityGateHolder, qualityGateStatusHolder, measureRepository, metricRepository,
- resultTextConverter, new SmallChangesetQualityGateSpecialCase(measureRepository, metricRepository));
+ resultTextConverter, new SmallChangesetQualityGateSpecialCase(measureRepository, metricRepository, settings));
@Before
public void setUp() {
import org.junit.Rule;
import org.junit.Test;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.internal.ConfigurationBridge;
+import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.ce.task.projectanalysis.component.Component;
+import org.sonar.ce.task.projectanalysis.component.TestSettingsRepository;
import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.ce.task.projectanalysis.measure.Measure;
import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
.add(CoreMetrics.NEW_BUGS);
@Rule
public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
- private final SmallChangesetQualityGateSpecialCase underTest = new SmallChangesetQualityGateSpecialCase(measureRepository, metricRepository);
+
+ private final MapSettings mapSettings = new MapSettings();
+ private final TestSettingsRepository settings = new TestSettingsRepository(new ConfigurationBridge(mapSettings));
+ private final SmallChangesetQualityGateSpecialCase underTest = new SmallChangesetQualityGateSpecialCase(measureRepository, metricRepository, settings);
@Test
public void ignore_errors_about_new_coverage_for_small_changesets() {
+ mapSettings.setProperty(CoreProperties.QUALITY_GATE_IGNORE_SMALL_CHANGES, true);
QualityGateMeasuresStep.MetricEvaluationResult metricEvaluationResult = generateEvaluationResult(NEW_COVERAGE_KEY, ERROR);
Component project = generateNewRootProject();
measureRepository.addRawMeasure(PROJECT_REF, CoreMetrics.NEW_LINES_KEY, newMeasureBuilder().setVariation(19).create(1000));
assertThat(result).isTrue();
}
+ @Test
+ public void dont_ignore_errors_about_new_coverage_for_small_changesets_if_disabled() {
+ mapSettings.setProperty(CoreProperties.QUALITY_GATE_IGNORE_SMALL_CHANGES, false);
+ QualityGateMeasuresStep.MetricEvaluationResult metricEvaluationResult = generateEvaluationResult(NEW_COVERAGE_KEY, ERROR);
+ Component project = generateNewRootProject();
+ measureRepository.addRawMeasure(PROJECT_REF, CoreMetrics.NEW_LINES_KEY, newMeasureBuilder().setVariation(19).create(1000));
+
+ boolean result = underTest.appliesTo(project, metricEvaluationResult);
+
+ assertThat(result).isFalse();
+ }
+
@Test
public void should_not_change_for_bigger_changesets() {
QualityGateMeasuresStep.MetricEvaluationResult metricEvaluationResult = generateEvaluationResult(NEW_COVERAGE_KEY, ERROR);
import java.util.OptionalDouble;
import java.util.Set;
import org.sonar.api.ce.ComputeEngineSide;
+import org.sonar.api.config.Configuration;
import org.sonar.api.measures.Metric;
import org.sonar.api.server.ServerSide;
* @param measures must provide the measures related to the metrics
* defined by {@link #getMetricKeys(QualityGate)}
*/
- EvaluatedQualityGate evaluate(QualityGate gate, Measures measures);
+ EvaluatedQualityGate evaluate(QualityGate gate, Measures measures, Configuration configuration);
/**
* Keys of the metrics involved in the computation of gate status.
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Configuration;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric.Level;
import org.sonar.server.qualitygate.EvaluatedCondition.EvaluationStatus;
public class QualityGateEvaluatorImpl implements QualityGateEvaluator {
- private static final int MAXIMUM_NEW_LINES_FOR_SMALL_CHANGESETS = 20;
+ public static final int MAXIMUM_NEW_LINES_FOR_SMALL_CHANGESETS = 20;
/**
* Some metrics will be ignored on very small change sets.
*/
- private static final Set<String> METRICS_TO_IGNORE_ON_SMALL_CHANGESETS = ImmutableSet.of(
+ public static final Set<String> METRICS_TO_IGNORE_ON_SMALL_CHANGESETS = ImmutableSet.of(
CoreMetrics.NEW_COVERAGE_KEY,
CoreMetrics.NEW_LINE_COVERAGE_KEY,
CoreMetrics.NEW_BRANCH_COVERAGE_KEY,
CoreMetrics.NEW_BLOCKS_DUPLICATED_KEY);
@Override
- public EvaluatedQualityGate evaluate(QualityGate gate, Measures measures) {
+ public EvaluatedQualityGate evaluate(QualityGate gate, Measures measures, Configuration configuration) {
EvaluatedQualityGate.Builder result = EvaluatedQualityGate.newBuilder()
.setQualityGate(gate);
- boolean isSmallChangeset = isSmallChangeset(measures);
+ boolean ignoreSmallChanges = configuration.getBoolean(CoreProperties.QUALITY_GATE_IGNORE_SMALL_CHANGES).orElse(true);
+ boolean isSmallChangeset = ignoreSmallChanges && isSmallChangeset(measures);
+
gate.getConditions().forEach(condition -> {
String metricKey = condition.getMetricKey();
EvaluatedCondition evaluation = ConditionEvaluator.evaluate(condition, measures);
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.Optional;
-import java.util.OptionalDouble;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import static org.sonar.api.measures.Metric.ValueType.DATA;
import static org.sonar.api.measures.Metric.ValueType.DISTRIB;
import static org.sonar.api.measures.Metric.ValueType.STRING;
-import static org.sonar.server.qualitygate.ConditionEvaluatorTest.FakeMeasure.newFakeMeasureOnLeak;
+import static org.sonar.server.qualitygate.FakeMeasure.newMeasureOnLeak;
@RunWith(DataProviderRunner.class)
public class ConditionEvaluatorTest {
test(new FakeMeasure(10), Condition.Operator.GREATER_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
test(new FakeMeasure(10), Condition.Operator.GREATER_THAN, "11", EvaluatedCondition.EvaluationStatus.OK, "10");
- testOnLeak(newFakeMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "9", EvaluatedCondition.EvaluationStatus.ERROR, "10");
- testOnLeak(newFakeMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
- testOnLeak(newFakeMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "11", EvaluatedCondition.EvaluationStatus.OK, "10");
+ testOnLeak(newMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "9", EvaluatedCondition.EvaluationStatus.ERROR, "10");
+ testOnLeak(newMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
+ testOnLeak(newMeasureOnLeak(10), Condition.Operator.GREATER_THAN, "11", EvaluatedCondition.EvaluationStatus.OK, "10");
}
@Test
test(new FakeMeasure(10), Condition.Operator.LESS_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
test(new FakeMeasure(10), Condition.Operator.LESS_THAN, "11", EvaluatedCondition.EvaluationStatus.ERROR, "10");
- testOnLeak(newFakeMeasureOnLeak(10), Condition.Operator.LESS_THAN, "9", EvaluatedCondition.EvaluationStatus.OK, "10");
- testOnLeak(newFakeMeasureOnLeak(10), Condition.Operator.LESS_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
- testOnLeak(newFakeMeasureOnLeak(10), Condition.Operator.LESS_THAN, "11", EvaluatedCondition.EvaluationStatus.ERROR, "10");
+ testOnLeak(newMeasureOnLeak(10), Condition.Operator.LESS_THAN, "9", EvaluatedCondition.EvaluationStatus.OK, "10");
+ testOnLeak(newMeasureOnLeak(10), Condition.Operator.LESS_THAN, "10", EvaluatedCondition.EvaluationStatus.OK, "10");
+ testOnLeak(newMeasureOnLeak(10), Condition.Operator.LESS_THAN, "11", EvaluatedCondition.EvaluationStatus.ERROR, "10");
}
@Test
return Optional.ofNullable(measure);
}
}
-
- static class FakeMeasure implements QualityGateEvaluator.Measure {
- private Double leakValue;
- private Double value;
- private Metric.ValueType valueType;
-
- private FakeMeasure() {
-
- }
-
- FakeMeasure(Metric.ValueType valueType) {
- this.valueType = valueType;
- }
-
- FakeMeasure(@Nullable Double value) {
- this.value = value;
- this.valueType = Metric.ValueType.FLOAT;
- }
-
- FakeMeasure(@Nullable Integer value) {
- this.value = value == null ? null : value.doubleValue();
- this.valueType = Metric.ValueType.INT;
- }
-
- static FakeMeasure newFakeMeasureOnLeak(@Nullable Integer value) {
- FakeMeasure that = new FakeMeasure();
- that.leakValue = value == null ? null : value.doubleValue();
- that.valueType = Metric.ValueType.INT;
- return that;
- }
-
- @Override
- public Metric.ValueType getType() {
- return valueType;
- }
-
- @Override
- public OptionalDouble getValue() {
- return value == null ? OptionalDouble.empty() : OptionalDouble.of(value);
- }
-
- @Override
- public Optional<String> getStringValue() {
- return Optional.empty();
- }
-
- @Override
- public OptionalDouble getNewMetricValue() {
- return leakValue == null ? OptionalDouble.empty() : OptionalDouble.of(leakValue);
- }
- }
-
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.qualitygate;
+
+import java.util.Optional;
+import java.util.OptionalDouble;
+import javax.annotation.Nullable;
+import org.sonar.api.measures.Metric;
+
+public class FakeMeasure implements QualityGateEvaluator.Measure {
+ private Double leakValue;
+ private Double value;
+ private Metric.ValueType valueType;
+
+ private FakeMeasure() {
+ // nothing to do
+ }
+
+ public FakeMeasure(Metric.ValueType valueType) {
+ this.valueType = valueType;
+ }
+
+ public FakeMeasure(@Nullable Double value) {
+ this.value = value;
+ this.valueType = Metric.ValueType.FLOAT;
+ }
+
+ public FakeMeasure(@Nullable Integer value) {
+ this.value = value == null ? null : value.doubleValue();
+ this.valueType = Metric.ValueType.INT;
+ }
+
+ public static FakeMeasure newMeasureOnLeak(@Nullable Integer value) {
+ FakeMeasure measure = new FakeMeasure();
+ measure.leakValue = value == null ? null : value.doubleValue();
+ measure.valueType = Metric.ValueType.INT;
+ return measure;
+ }
+
+ @Override
+ public Metric.ValueType getType() {
+ return valueType;
+ }
+
+ @Override
+ public OptionalDouble getValue() {
+ return value == null ? OptionalDouble.empty() : OptionalDouble.of(value);
+ }
+
+ @Override
+ public Optional<String> getStringValue() {
+ return Optional.empty();
+ }
+
+ @Override
+ public OptionalDouble getNewMetricValue() {
+ return leakValue == null ? OptionalDouble.empty() : OptionalDouble.of(leakValue);
+ }
+}
package org.sonar.server.qualitygate;
import com.google.common.collect.ImmutableSet;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.Test;
-import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Configuration;
+import org.sonar.api.config.internal.ConfigurationBridge;
+import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.measures.Metric;
+import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_KEY;
import static org.sonar.api.measures.CoreMetrics.NEW_LINES_KEY;
+import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY;
+import static org.sonar.server.qualitygate.FakeMeasure.newMeasureOnLeak;
public class QualityGateEvaluatorImplTest {
-
+ private final MapSettings settings = new MapSettings();
+ private final Configuration configuration = new ConfigurationBridge(settings);
private final QualityGateEvaluator underTest = new QualityGateEvaluatorImpl();
@Test
@Test
public void evaluated_conditions_are_sorted() {
- Set<String> metricKeys = ImmutableSet.of("foo", "bar", CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY);
+ Set<String> metricKeys = ImmutableSet.of("foo", "bar", NEW_MAINTAINABILITY_RATING_KEY);
Set<Condition> conditions = metricKeys.stream().map(key -> {
Condition condition = mock(Condition.class);
when(condition.getMetricKey()).thenReturn(key);
when(gate.getConditions()).thenReturn(conditions);
QualityGateEvaluator.Measures measures = mock(QualityGateEvaluator.Measures.class);
- assertThat(underTest.evaluate(gate, measures).getEvaluatedConditions()).extracting(x -> x.getCondition().getMetricKey())
- .containsExactly(CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY, "bar", "foo");
+ assertThat(underTest.evaluate(gate, measures, configuration).getEvaluatedConditions()).extracting(x -> x.getCondition().getMetricKey())
+ .containsExactly(NEW_MAINTAINABILITY_RATING_KEY, "bar", "foo");
}
@Test
public void evaluate_is_OK_for_empty_qgate() {
QualityGate gate = mock(QualityGate.class);
QualityGateEvaluator.Measures measures = mock(QualityGateEvaluator.Measures.class);
- EvaluatedQualityGate evaluatedQualityGate = underTest.evaluate(gate, measures);
+ EvaluatedQualityGate evaluatedQualityGate = underTest.evaluate(gate, measures, configuration);
assertThat(evaluatedQualityGate.getStatus()).isEqualTo(Metric.Level.OK);
}
+
+ @Test
+ public void evaluate_is_ERROR() {
+ Condition condition = new Condition(NEW_MAINTAINABILITY_RATING_KEY, Condition.Operator.GREATER_THAN, "0");
+
+ QualityGate gate = mock(QualityGate.class);
+ when(gate.getConditions()).thenReturn(singleton(condition));
+ QualityGateEvaluator.Measures measures = key -> Optional.of(newMeasureOnLeak(1));
+
+ assertThat(underTest.evaluate(gate, measures, configuration).getStatus()).isEqualTo(Metric.Level.ERROR);
+ }
+
+ @Test
+ public void evaluate_for_small_changes() {
+ Condition condition = new Condition(NEW_DUPLICATED_LINES_KEY, Condition.Operator.GREATER_THAN, "0");
+
+ Map<String, QualityGateEvaluator.Measure> notSmallChange = new HashMap<>();
+ notSmallChange.put(NEW_DUPLICATED_LINES_KEY, newMeasureOnLeak(1));
+ notSmallChange.put(NEW_LINES_KEY, newMeasureOnLeak(1000));
+
+ Map<String, QualityGateEvaluator.Measure> smallChange = new HashMap<>();
+ smallChange.put(NEW_DUPLICATED_LINES_KEY, newMeasureOnLeak(1));
+ smallChange.put(NEW_LINES_KEY, newMeasureOnLeak(10));
+
+ QualityGate gate = mock(QualityGate.class);
+ when(gate.getConditions()).thenReturn(singleton(condition));
+ QualityGateEvaluator.Measures notSmallChangeMeasures = key -> Optional.ofNullable(notSmallChange.get(key));
+ QualityGateEvaluator.Measures smallChangeMeasures = key -> Optional.ofNullable(smallChange.get(key));
+
+ settings.setProperty(CoreProperties.QUALITY_GATE_IGNORE_SMALL_CHANGES, true);
+ assertThat(underTest.evaluate(gate, notSmallChangeMeasures, configuration).getStatus()).isEqualTo(Metric.Level.ERROR);
+ assertThat(underTest.evaluate(gate, smallChangeMeasures, configuration).getStatus()).isEqualTo(Metric.Level.OK);
+
+ settings.setProperty(CoreProperties.QUALITY_GATE_IGNORE_SMALL_CHANGES, false);
+ assertThat(underTest.evaluate(gate, smallChangeMeasures, configuration).getStatus()).isEqualTo(Metric.Level.ERROR);
+ }
}
}
});
- EvaluatedQualityGate evaluatedQualityGate = qGateComputer.refreshGateStatus(branchComponent, qualityGate, matrix);
+ EvaluatedQualityGate evaluatedQualityGate = qGateComputer.refreshGateStatus(branchComponent, qualityGate, matrix, config);
// persist the measures that have been created or updated
matrix.getChanged().sorted(LiveMeasureComparator.INSTANCE)
package org.sonar.server.measure.live;
import java.util.Set;
+import org.sonar.api.config.Configuration;
import org.sonar.api.server.ServerSide;
import org.sonar.db.DbSession;
import org.sonar.db.component.BranchDto;
QualityGate loadQualityGate(DbSession dbSession, ProjectDto project, BranchDto branch);
- EvaluatedQualityGate refreshGateStatus(ComponentDto project, QualityGate gate, MeasureMatrix measureMatrix);
+ EvaluatedQualityGate refreshGateStatus(ComponentDto project, QualityGate gate, MeasureMatrix measureMatrix, Configuration configuration);
Set<String> getMetricsRelatedTo(QualityGate gate);
import java.util.OptionalDouble;
import java.util.Set;
import java.util.stream.Stream;
+import org.sonar.api.config.Configuration;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.db.DbClient;
}
@Override
- public EvaluatedQualityGate refreshGateStatus(ComponentDto project, QualityGate gate, MeasureMatrix measureMatrix) {
+ 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.of(new LiveMeasure(liveMeasureDto.get(), metric));
};
- EvaluatedQualityGate evaluatedGate = evaluator.evaluate(gate, measures);
+ 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));
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
+import org.sonar.api.config.Configuration;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.measures.CoreMetrics;
.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));
+ verify(qGateComputer).refreshGateStatus(eq(project), same(qualityGate), any(MeasureMatrix.class), any(Configuration.class));
}
@Test
when(qGateComputer.loadQualityGate(any(DbSession.class), any(ProjectDto.class), any(BranchDto.class)))
.thenReturn(qualityGate);
when(qGateComputer.getMetricsRelatedTo(qualityGate)).thenReturn(singleton(CoreMetrics.ALERT_STATUS_KEY));
- when(qGateComputer.refreshGateStatus(eq(project), same(qualityGate), any(MeasureMatrix.class)))
+ when(qGateComputer.refreshGateStatus(eq(project), same(qualityGate), any(MeasureMatrix.class), any(Configuration.class)))
.thenReturn(newQualityGate);
MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, CorePropertyDefinitions.all()));
ProjectConfigurationLoader configurationLoader = new TestProjectConfigurationLoader(settings.asConfig());
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.config.Configuration;
+import org.sonar.api.config.internal.ConfigurationBridge;
+import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.db.DbTester;
@Rule
public DbTester db = DbTester.create();
+ private final MapSettings settings = new MapSettings();
+ private final Configuration configuration = new ConfigurationBridge(settings);
private final TestQualityGateEvaluator qualityGateEvaluator = new TestQualityGateEvaluator();
private final LiveQualityGateComputerImpl underTest = new LiveQualityGateComputerImpl(db.getDbClient(), new QualityGateFinder(db.getDbClient()), qualityGateEvaluator);
QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition));
MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(conditionMetric, statusMetric, detailsMetric), emptyList());
- EvaluatedQualityGate result = underTest.refreshGateStatus(project, gate, matrix);
+ EvaluatedQualityGate result = underTest.refreshGateStatus(project, gate, matrix, configuration);
QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
assertThat(measures.get(conditionMetric.getKey())).isEmpty();
MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(statusMetric, detailsMetric, numericMetric, numericNewMetric, stringMetric),
asList(numericMeasure, numericNewMeasure, stringMeasure));
- underTest.refreshGateStatus(project, gate, matrix);
+ underTest.refreshGateStatus(project, gate, matrix, configuration);
QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures();
private Measures measures;
@Override
- public EvaluatedQualityGate evaluate(QualityGate gate, Measures measures) {
+ public EvaluatedQualityGate evaluate(QualityGate gate, Measures measures, Configuration configuration) {
checkState(this.measures == null);
this.measures = measures;
EvaluatedQualityGate.Builder builder = EvaluatedQualityGate.newBuilder().setQualityGate(gate).setStatus(Metric.Level.OK);
.type(PropertyType.USER_LOGIN)
.build(),
+ // QUALITY GATE
+ PropertyDefinition.builder(CoreProperties.QUALITY_GATE_IGNORE_SMALL_CHANGES)
+ .name("Ignore duplication and coverage on small changes")
+ .description("Quality Gate conditions about duplications in new code and coverage on new code are ignored until the number of new lines is at least 20.")
+ .category(CoreProperties.CATEGORY_GENERAL)
+ .subCategory(CoreProperties.SUBCATEGORY_QUALITY_GATE)
+ .onQualifiers(Qualifiers.PROJECT)
+ .type(BOOLEAN)
+ .defaultValue(Boolean.toString(true))
+ .build(),
+
// CPD
PropertyDefinition.builder(CoreProperties.CPD_CROSS_PROJECT)
.defaultValue(Boolean.toString(false))
property.category.general.databaseCleaner=Database Cleaner
property.category.general.looknfeel=Look & Feel
property.category.general.issues=Issues
+property.category.general.qualityGate=Quality Gate
property.category.general.subProjects=Sub-projects
property.category.almintegration=ALM Integrations
property.category.almintegration.github=GitHub Authentication
overview.quality_gate_all_conditions_passed=All conditions passed.
overview.you_should_define_quality_gate=You should define a quality gate on this project.
overview.quality_gate.ignored_conditions=Some Quality Gate conditions on New Code were ignored because of the small number of New Lines
-overview.quality_gate.ignored_conditions.tooltip=At the start of a new code period, if very few lines have been added or modified, it might be difficult to reach the desired level of code coverage or duplications. To prevent Quality Gate failure when there's little that can be done about it, Quality Gate conditions about duplications in new code and coverage on new code are ignored until the number of new lines is at least 20.
+overview.quality_gate.ignored_conditions.tooltip=At the start of a new code period, if very few lines have been added or modified, it might be difficult to reach the desired level of code coverage or duplications. To prevent Quality Gate failure when there's little that can be done about it, Quality Gate conditions about duplications in new code and coverage on new code are ignored until the number of new lines is at least 20. An administrator can disable this in the general settings.
overview.quality_gate.conditions_on_new_code=Only conditions on new code that are defined in the Quality Gate are checked. See the {link} associated to the project for details.
overview.quality_profiles=Quality Profiles used
overview.new_code_period_x=New Code: {0}
@Test
public void all() {
List<PropertyDefinition> defs = CorePropertyDefinitions.all();
- assertThat(defs).hasSize(55);
+ assertThat(defs).hasSize(56);
}
@Test
*/
String SUBCATEGORY_ISSUES = "issues";
+ /**
+ * @since 8.9
+ */
+ String SUBCATEGORY_QUALITY_GATE = "qualityGate";
+
/**
* @since 4.0
*/
* @since 7.6
*/
String MODULE_LEVEL_ARCHIVED_SETTINGS = "sonar.subproject.settings.archived";
+
+ /**
+ * since 8.9
+ */
+ String QUALITY_GATE_IGNORE_SMALL_CHANGES = "sonar.qualitygate.ignoreSmallChanges";
}