diff options
author | Javier García Orduña <javier.garcia@sonarsource.com> | 2024-12-12 15:06:37 +0100 |
---|---|---|
committer | Steve Marion <steve.marion@sonarsource.com> | 2024-12-18 11:13:23 +0100 |
commit | 2f1464af44ca3ad98be345c7ce72b925d72069f2 (patch) | |
tree | 82f3fb3f6955f747afd0d87e8448f74eb498ff3b | |
parent | 8268050c7f73d63480bf10a7b9b406b181748142 (diff) | |
download | sonarqube-2f1464af44ca3ad98be345c7ce72b925d72069f2.tar.gz sonarqube-2f1464af44ca3ad98be345c7ce72b925d72069f2.zip |
SONAR-22876 Exclude metrics from being persisted to measures
2 files changed, 40 insertions, 0 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStepIT.java index 4ff9babee3c..d9564b8b699 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStepIT.java @@ -53,6 +53,10 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.sonar.api.measures.CoreMetrics.EXECUTABLE_LINES_DATA; +import static org.sonar.api.measures.CoreMetrics.EXECUTABLE_LINES_DATA_KEY; +import static org.sonar.api.measures.CoreMetrics.NCLOC_DATA; +import static org.sonar.api.measures.CoreMetrics.NCLOC_DATA_KEY; import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY; import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT; @@ -257,6 +261,26 @@ class PersistMeasuresStepIT { verifyUnchanged(0); } + @Test + void do_not_persist_excluded_metrics() { + MetricDto nclocDto = db.measures().insertMetric(m -> m.setKey(NCLOC_DATA.getKey()).setValueType(Metric.ValueType.DATA.name())); + MetricDto executableDto = db.measures().insertMetric(m -> m.setKey(EXECUTABLE_LINES_DATA.getKey()).setValueType(Metric.ValueType.DATA.name())); + + metricRepository.add(nclocDto.getUuid(), NCLOC_DATA); + metricRepository.add(executableDto.getUuid(), EXECUTABLE_LINES_DATA); + + prepareProject(); + + // the computed measures + measureRepository.addRawMeasure(REF_1, NCLOC_DATA_KEY, newMeasureBuilder().create(10_000)); + measureRepository.addRawMeasure(REF_1, EXECUTABLE_LINES_DATA_KEY, newMeasureBuilder().create(5_000)); + + step().execute(context); + + // no excluded measures are persisted + verifyInsertsOrUpdates(0); + } + private void insertMeasure(String componentUuid, String projectUuid, Metric<?> metric, Object obj) { MeasureDto measure = new MeasureDto() .setComponentUuid(componentUuid) diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStep.java index 46094e08fd0..0bfa6dba574 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistMeasuresStep.java @@ -51,6 +51,8 @@ import org.sonar.db.measure.MeasureHash; import org.springframework.beans.factory.annotation.Autowired; import static org.sonar.api.measures.CoreMetrics.DUPLICATIONS_DATA_KEY; +import static org.sonar.api.measures.CoreMetrics.EXECUTABLE_LINES_DATA_KEY; +import static org.sonar.api.measures.CoreMetrics.NCLOC_DATA_KEY; import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER; public class PersistMeasuresStep implements ComputationStep { @@ -60,6 +62,13 @@ public class PersistMeasuresStep implements ComputationStep { private static final Predicate<Measure> NON_EMPTY_MEASURE = measure -> measure.getValueType() != ValueType.NO_VALUE || measure.getData() != null; + /** + * List of metrics that should not be persisted + */ + private static final Set<String> NOT_TO_PERSIST = Set.of( + EXECUTABLE_LINES_DATA_KEY, + NCLOC_DATA_KEY); + private final DbClient dbClient; private final MetricRepository metricRepository; private final TreeRootHolder treeRootHolder; @@ -146,6 +155,9 @@ public class PersistMeasuresStep implements ComputationStep { Map<String, Measure> measures = measureRepository.getRawMeasures(component); for (Map.Entry<String, Measure> measuresByMetricKey : measures.entrySet()) { String metricKey = measuresByMetricKey.getKey(); + if (shouldNotPersist(metricKey)) { + continue; + } Metric metric = metricRepository.getByKey(metricKey); Predicate<Measure> notBestValueOptimized = BestValueOptimization.from(metric, component).negate(); Measure measure = measuresByMetricKey.getValue(); @@ -168,6 +180,10 @@ public class PersistMeasuresStep implements ComputationStep { return measureDto; } + private static boolean shouldNotPersist(String metricKey) { + return NOT_TO_PERSIST.contains(metricKey); + } + private void persist(Collection<MeasureDto> inserts, Collection<MeasureDto> updates) { if (inserts.isEmpty() && updates.isEmpty()) { return; |