diff options
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org')
6 files changed, 68 insertions, 23 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java index ddb665c7965..d070a464cb4 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java @@ -34,12 +34,14 @@ import org.sonar.scanner.protocol.output.ScannerReport.Issue; @ThreadSafe public class DefaultFilterableIssue implements FilterableIssue { private final Issue rawIssue; + private final String severity; private final InputComponent component; private DefaultInputProject project; - public DefaultFilterableIssue(DefaultInputProject project, Issue rawIssue, InputComponent component) { + public DefaultFilterableIssue(DefaultInputProject project, Issue rawIssue, String severity, InputComponent component) { this.project = project; this.rawIssue = rawIssue; + this.severity = severity; this.component = component; } @@ -55,7 +57,7 @@ public class DefaultFilterableIssue implements FilterableIssue { @Override public String severity() { - return rawIssue.getSeverity().name(); + return severity; } @Override diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ImpactMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ImpactMapper.java new file mode 100644 index 00000000000..76111b56055 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ImpactMapper.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.scanner.issue; + +import org.sonar.api.issue.impact.Severity; +import org.sonar.scanner.protocol.output.ScannerReport; + +public class ImpactMapper { + private ImpactMapper() { + } + + public static ScannerReport.ImpactSeverity mapImpactSeverity(Severity severity) { + return switch (severity) { + case BLOCKER -> ScannerReport.ImpactSeverity.ImpactSeverity_BLOCKER; + case HIGH -> ScannerReport.ImpactSeverity.ImpactSeverity_HIGH; + case MEDIUM -> ScannerReport.ImpactSeverity.ImpactSeverity_MEDIUM; + case LOW -> ScannerReport.ImpactSeverity.ImpactSeverity_LOW; + case INFO -> ScannerReport.ImpactSeverity.ImpactSeverity_INFO; + }; + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java index cce20df21d8..948e9552370 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java @@ -41,11 +41,11 @@ public class IssueFilters { this.project = project; } - public boolean accept(InputComponent component, ScannerReport.Issue rawIssue) { + public boolean accept(InputComponent component, ScannerReport.Issue rawIssue, String severity) { if (filterChain == null) { throw new IllegalStateException("Issue filters must be registered before this class can be used"); } - FilterableIssue fIssue = new DefaultFilterableIssue(project, rawIssue, component); + FilterableIssue fIssue = new DefaultFilterableIssue(project, rawIssue, severity, component); return filterChain.accept(fIssue); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java index 6de30db7829..da79ae46bd0 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java @@ -33,7 +33,6 @@ import org.sonar.api.batch.fs.internal.DefaultInputComponent; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.rule.ActiveRule; import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.rule.internal.DefaultActiveRule; import org.sonar.api.batch.sensor.issue.ExternalIssue; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.batch.sensor.issue.Issue.Flow; @@ -78,9 +77,9 @@ public class IssuePublisher { return false; } - ScannerReport.Issue rawIssue = createReportIssue(issue, inputComponent.scannerId(), activeRule.severity(), ((DefaultActiveRule) activeRule).impacts()); + ScannerReport.Issue rawIssue = createReportIssue(issue, inputComponent.scannerId()); - if (filters.accept(inputComponent, rawIssue)) { + if (filters.accept(inputComponent, rawIssue, activeRule.severity())) { write(inputComponent.scannerId(), rawIssue); return true; } @@ -108,29 +107,28 @@ public class IssuePublisher { return str; } - private static ScannerReport.Issue createReportIssue(Issue issue, int componentRef, String activeRuleSeverity, - Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> activeRuleImpacts) { + private static ScannerReport.Issue createReportIssue(Issue issue, int componentRef) { String primaryMessage = nullToEmpty(issue.primaryLocation().message()); - org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); - Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRuleSeverity); ScannerReport.Issue.Builder builder = ScannerReport.Issue.newBuilder(); ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder(); ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder(); // non-null fields - builder.setSeverity(severity); builder.setRuleRepository(issue.ruleKey().repository()); builder.setRuleKey(issue.ruleKey().rule()); builder.setMsg(primaryMessage); builder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings())); Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> overriddenImpacts = new EnumMap<>(issue.overridenImpacts()); - activeRuleImpacts.entrySet().forEach(e -> overriddenImpacts.putIfAbsent(e.getKey(), e.getValue())); - builder.addAllOverridenImpacts(toProtobufImpacts(overriddenImpacts)); + builder.addAllOverriddenImpacts(toProtobufImpacts(overriddenImpacts)); locationBuilder.setMsg(primaryMessage); locationBuilder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings())); locationBuilder.setComponentRef(componentRef); + org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); + if (overriddenSeverity != null) { + builder.setOverriddenSeverity(Severity.valueOf(overriddenSeverity.name())); + } TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange)); @@ -151,7 +149,9 @@ public class IssuePublisher { private static List<ScannerReport.Impact> toProtobufImpacts(Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> softwareQualitySeverityMap) { List<ScannerReport.Impact> impacts = new ArrayList<>(); - softwareQualitySeverityMap.forEach((q, s) -> impacts.add(ScannerReport.Impact.newBuilder().setSoftwareQuality(q.name()).setSeverity(s.name()).build())); + softwareQualitySeverityMap.forEach((q, s) -> impacts.add(ScannerReport.Impact.newBuilder() + .setSoftwareQuality(ScannerReport.SoftwareQuality.valueOf(q.name())) + .setSeverity(ImpactMapper.mapImpactSeverity(s)).build())); return impacts; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ActiveRulesPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ActiveRulesPublisher.java index 63672225c9e..dcad7da095b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ActiveRulesPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ActiveRulesPublisher.java @@ -21,6 +21,7 @@ package org.sonar.scanner.report; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.internal.DefaultActiveRule; +import org.sonar.scanner.issue.ImpactMapper; import org.sonar.scanner.protocol.Constants; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReportWriter; @@ -46,7 +47,12 @@ public class ActiveRulesPublisher implements ReportPublisherStep { builder.setCreatedAt(input.createdAt()); builder.setUpdatedAt(input.updatedAt()); builder.setQProfileKey(input.qpKey()); - builder.getMutableParamsByKey().putAll(input.params()); + builder.putAllParamsByKey(input.params()); + builder.addAllImpacts(input.impacts().entrySet().stream() + .map(entry -> ScannerReport.Impact.newBuilder() + .setSoftwareQuality(ScannerReport.SoftwareQuality.valueOf(entry.getKey().name())) + .setSeverity(ImpactMapper.mapImpactSeverity(entry.getValue())).build()) + .toList()); return builder.build(); }).toList()); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java index e0930fc5372..9ee364cf916 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java @@ -68,6 +68,7 @@ import org.sonar.core.util.CloseableIterator; import org.sonar.duplications.block.Block; import org.sonar.duplications.internal.pmd.PmdBlockChunker; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; +import org.sonar.scanner.issue.ImpactMapper; import org.sonar.scanner.issue.IssuePublisher; import org.sonar.scanner.protocol.Constants; import org.sonar.scanner.protocol.output.FileStructure; @@ -263,7 +264,6 @@ public class DefaultSensorStorage implements SensorStorage { builder.setDescription(description); } - org.sonar.api.batch.rule.Severity severity = adHocRule.severity(); if (severity != null) { builder.setSeverity(Constants.Severity.valueOf(severity.name())); @@ -285,8 +285,8 @@ public class DefaultSensorStorage implements SensorStorage { private static List<ScannerReport.Impact> mapImpacts(Map<SoftwareQuality, Severity> impactsMap) { return impactsMap.entrySet().stream() .map(e -> ScannerReport.Impact.newBuilder() - .setSoftwareQuality(e.getKey().name()) - .setSeverity(e.getValue().name()).build()) + .setSoftwareQuality(ScannerReport.SoftwareQuality.valueOf(e.getKey().name())) + .setSeverity(ImpactMapper.mapImpactSeverity(e.getValue())).build()) .toList(); } @@ -379,8 +379,7 @@ public class DefaultSensorStorage implements SensorStorage { private SortedMap<Integer, ScannerReport.LineCoverage.Builder> reloadExistingCoverage(DefaultInputFile inputFile) { SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = new TreeMap<>(); - try (CloseableIterator<ScannerReport.LineCoverage> lineCoverageCloseableIterator = - reportPublisher.getReader().readComponentCoverage(inputFile.scannerId())) { + try (CloseableIterator<ScannerReport.LineCoverage> lineCoverageCloseableIterator = reportPublisher.getReader().readComponentCoverage(inputFile.scannerId())) { while (lineCoverageCloseableIterator.hasNext()) { final ScannerReport.LineCoverage lineCoverage = lineCoverageCloseableIterator.next(); coveragePerLine.put(lineCoverage.getLine(), ScannerReport.LineCoverage.newBuilder(lineCoverage)); @@ -393,8 +392,8 @@ public class DefaultSensorStorage implements SensorStorage { void apply(Integer value, ScannerReport.LineCoverage.Builder builder); } - private static void mergeLineCoverageValues(int lineCount, SortedMap<Integer, Integer> valueByLine, SortedMap<Integer, - ScannerReport.LineCoverage.Builder> coveragePerLine, LineCoverageOperation op) { + private static void mergeLineCoverageValues(int lineCount, SortedMap<Integer, Integer> valueByLine, SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine, + LineCoverageOperation op) { for (Map.Entry<Integer, Integer> lineMeasure : valueByLine.entrySet()) { int lineIdx = lineMeasure.getKey(); if (lineIdx <= lineCount) { |