diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-03-25 10:59:50 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-03-25 15:20:10 +0100 |
commit | e157c182fb9f71f1a1bd16d48d4d09ac7b4fe92a (patch) | |
tree | 9119eb95b9e5b73e1914ba8a0f4639d8f1d14570 /sonar-batch/src/main/java | |
parent | ac44823d701c7ae3ea30ea8060c8fe28ca8ee6af (diff) | |
download | sonarqube-e157c182fb9f71f1a1bd16d48d4d09ac7b4fe92a.tar.gz sonarqube-e157c182fb9f71f1a1bd16d48d4d09ac7b4fe92a.zip |
SONAR-6280 Feed duplications in compute report
Diffstat (limited to 'sonar-batch/src/main/java')
3 files changed, 95 insertions, 28 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/DuplicationsPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/DuplicationsPublisher.java new file mode 100644 index 00000000000..3648f47cc12 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/report/DuplicationsPublisher.java @@ -0,0 +1,90 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.batch.report; + +import com.google.common.base.Function; +import com.google.common.collect.Iterables; +import org.sonar.api.batch.sensor.duplication.Duplication.Block; +import org.sonar.api.batch.sensor.duplication.internal.DefaultDuplication; +import org.sonar.batch.duplication.DuplicationCache; +import org.sonar.batch.index.BatchResource; +import org.sonar.batch.index.ResourceCache; +import org.sonar.batch.protocol.output.BatchReport; +import org.sonar.batch.protocol.output.BatchReport.Duplication; +import org.sonar.batch.protocol.output.BatchReport.DuplicationBlock; +import org.sonar.batch.protocol.output.BatchReportWriter; + +public class DuplicationsPublisher implements ReportPublisher { + + private final ResourceCache resourceCache; + private final DuplicationCache duplicationCache; + + public DuplicationsPublisher(ResourceCache resourceCache, DuplicationCache duplicationCache) { + this.resourceCache = resourceCache; + this.duplicationCache = duplicationCache; + } + + @Override + public void publish(BatchReportWriter writer) { + for (final BatchResource resource : resourceCache.all()) { + Iterable<DefaultDuplication> dups = duplicationCache.byComponent(resource.resource().getEffectiveKey()); + if (dups.iterator().hasNext()) { + Iterable<org.sonar.batch.protocol.output.BatchReport.Duplication> reportDuplications = Iterables.transform(dups, + new Function<DefaultDuplication, BatchReport.Duplication>() { + private final BatchReport.Duplication.Builder dupBuilder = BatchReport.Duplication.newBuilder(); + private final BatchReport.DuplicationBlock.Builder blockBuilder = BatchReport.DuplicationBlock.newBuilder(); + + @Override + public BatchReport.Duplication apply(DefaultDuplication input) { + return toReportDuplication(resource.key(), dupBuilder, blockBuilder, input); + } + + }); + writer.writeComponentDuplications(resource.batchId(), reportDuplications); + } + } + } + + private Duplication toReportDuplication(String currentComponentKey, Duplication.Builder dupBuilder, DuplicationBlock.Builder blockBuilder, DefaultDuplication input) { + dupBuilder.clear(); + Block originBlock = input.originBlock(); + blockBuilder.clear(); + dupBuilder.setOriginBlock(blockBuilder + .setStartLine(originBlock.startLine()) + .setEndLine(originBlock.startLine() + originBlock.length() - 1).build()); + for (Block duplicate : input.duplicates()) { + blockBuilder.clear(); + String componentKey = duplicate.resourceKey(); + if (!currentComponentKey.equals(componentKey)) { + BatchResource sameProjectComponent = resourceCache.get(componentKey); + if (sameProjectComponent != null) { + blockBuilder.setOtherComponentRef(sameProjectComponent.batchId()); + } else { + blockBuilder.setComponentKey(componentKey); + } + } + dupBuilder.addDuplicatedBy(blockBuilder + .setStartLine(duplicate.startLine()) + .setEndLine(duplicate.startLine() + duplicate.length() - 1).build()); + } + return dupBuilder.build(); + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java index e7c524ff52d..8aadc2180ee 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/MeasuresPublisher.java @@ -23,7 +23,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; -import org.sonar.api.batch.sensor.duplication.internal.DefaultDuplication; import org.sonar.api.measures.*; import org.sonar.api.measures.Metric.Level; import org.sonar.api.measures.Metric.ValueType; @@ -32,8 +31,6 @@ import org.sonar.api.resources.ResourceUtils; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RulePriority; import org.sonar.api.technicaldebt.batch.Characteristic; -import org.sonar.batch.duplication.DuplicationCache; -import org.sonar.batch.duplication.DuplicationUtils; import org.sonar.batch.index.BatchResource; import org.sonar.batch.index.ResourceCache; import org.sonar.batch.protocol.Constants; @@ -45,19 +42,16 @@ import org.sonar.batch.scan.measure.MeasureCache; import javax.annotation.Nullable; import java.io.Serializable; -import java.util.Arrays; public class MeasuresPublisher implements ReportPublisher { private final ResourceCache resourceCache; private final MeasureCache measureCache; - private final DuplicationCache duplicationCache; private final MetricFinder metricFinder; - public MeasuresPublisher(ResourceCache resourceCache, MeasureCache measureCache, DuplicationCache duplicationCache, MetricFinder metricFinder) { + public MeasuresPublisher(ResourceCache resourceCache, MeasureCache measureCache, MetricFinder metricFinder) { this.resourceCache = resourceCache; this.measureCache = measureCache; - this.duplicationCache = duplicationCache; this.metricFinder = metricFinder; } @@ -75,20 +69,14 @@ public class MeasuresPublisher implements ReportPublisher { }); Iterable<org.sonar.batch.protocol.output.BatchReport.Measure> reportMeasures = Iterables.transform(batchMeasures, new Function<Measure, BatchReport.Measure>() { - private BatchReport.Measure.Builder builder = BatchReport.Measure.newBuilder(); + private final BatchReport.Measure.Builder builder = BatchReport.Measure.newBuilder(); @Override public BatchReport.Measure apply(Measure input) { return toReportMeasure(builder, input); } }); - Iterable<DefaultDuplication> dups = duplicationCache.byComponent(resource.resource().getEffectiveKey()); - if (dups.iterator().hasNext()) { - org.sonar.batch.protocol.output.BatchReport.Measure dupMeasure = toReportMeasure(BatchReport.Measure.newBuilder(), dups); - writer.writeComponentMeasures(resource.batchId(), Iterables.concat(Arrays.asList(dupMeasure), reportMeasures)); - } else { - writer.writeComponentMeasures(resource.batchId(), reportMeasures); - } + writer.writeComponentMeasures(resource.batchId(), reportMeasures); } } @@ -109,15 +97,6 @@ public class MeasuresPublisher implements ReportPublisher { return measure.getValue() != null || measure.getData() != null || isNotEmpty; } - private BatchReport.Measure toReportMeasure(BatchReport.Measure.Builder builder, Iterable<DefaultDuplication> dups) { - builder.clear(); - - builder.setValueType(MeasureValueType.STRING); - builder.setStringValue(DuplicationUtils.toXml(dups)); - builder.setMetricKey(CoreMetrics.DUPLICATIONS_DATA_KEY); - return builder.build(); - } - private BatchReport.Measure toReportMeasure(BatchReport.Measure.Builder builder, Measure measure) { builder.clear(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java index aa35effd389..5df7f4c665d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java @@ -55,10 +55,7 @@ import org.sonar.batch.language.LanguageDistributionDecorator; import org.sonar.batch.phases.*; import org.sonar.batch.qualitygate.GenerateQualityGateEvents; import org.sonar.batch.qualitygate.QualityGateVerifier; -import org.sonar.batch.report.ComponentsPublisher; -import org.sonar.batch.report.IssuesPublisher; -import org.sonar.batch.report.MeasuresPublisher; -import org.sonar.batch.report.PublishReportJob; +import org.sonar.batch.report.*; import org.sonar.batch.rule.*; import org.sonar.batch.scan.filesystem.*; import org.sonar.batch.scan.report.IssuesReports; @@ -118,6 +115,7 @@ public class ModuleScanContainer extends ComponentContainer { ComponentsPublisher.class, IssuesPublisher.class, MeasuresPublisher.class, + DuplicationsPublisher.class, moduleDefinition.getContainerExtensions(), // file system |