3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.measure;
22 import java.util.Optional;
23 import org.sonar.api.ce.ComputeEngineSide;
24 import org.sonar.api.measures.CoreMetrics;
25 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
26 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
27 import org.sonar.ce.task.projectanalysis.metric.Metric;
28 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
29 import org.sonar.ce.task.step.ComputationStep;
32 * Execute {@link PostMeasuresComputationCheck} instances in no specific order.
33 * If an extension fails (throws an exception), consecutive extensions
37 public class PostMeasuresComputationChecksStep implements ComputationStep {
39 private final TreeRootHolder treeRootHolder;
40 private final MetricRepository metricRepository;
41 private final MeasureRepository measureRepository;
42 private final AnalysisMetadataHolder analysisMetadataHolder;
43 private final PostMeasuresComputationCheck[] extensions;
45 public PostMeasuresComputationChecksStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository,
46 AnalysisMetadataHolder analysisMetadataHolder, PostMeasuresComputationCheck[] extensions) {
47 this.treeRootHolder = treeRootHolder;
48 this.metricRepository = metricRepository;
49 this.measureRepository = measureRepository;
50 this.analysisMetadataHolder = analysisMetadataHolder;
51 this.extensions = extensions;
55 * Used when zero {@link PostMeasuresComputationCheck} are registered into container.
57 public PostMeasuresComputationChecksStep(TreeRootHolder treeRootHolder, MetricRepository metricRepository, MeasureRepository measureRepository,
58 AnalysisMetadataHolder analysisMetadataHolder) {
59 this(treeRootHolder, metricRepository, measureRepository, analysisMetadataHolder, new PostMeasuresComputationCheck[0]);
63 public void execute(ComputationStep.Context context) {
64 PostMeasuresComputationCheck.Context extensionContext = new ContextImpl();
65 for (PostMeasuresComputationCheck extension : extensions) {
66 extension.onCheck(extensionContext);
71 public String getDescription() {
72 return "Checks executed after computation of measures";
75 private class ContextImpl implements PostMeasuresComputationCheck.Context {
78 public String getProjectUuid() {
79 return analysisMetadataHolder.getProject().getUuid();
83 public int getNcloc() {
84 Metric nclocMetric = metricRepository.getByKey(CoreMetrics.NCLOC_KEY);
85 Optional<Measure> nclocMeasure = measureRepository.getRawMeasure(treeRootHolder.getRoot(), nclocMetric);
86 return nclocMeasure.map(Measure::getIntValue).orElse(0);