]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20021 passing overriden impacts from analyzer to scanner report
authorlukasz-jarocki-sonarsource <lukasz.jarocki@sonarsource.com>
Wed, 2 Aug 2023 10:07:25 +0000 (12:07 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 18 Aug 2023 20:02:47 +0000 (20:02 +0000)
sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java
sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java
sonar-scanner-protocol/src/main/protobuf/scanner_report.proto

index 4790c3f80a64d3dd67690ad9b9b58446841def23..38c0d61e0411fe5f3a57fcc701b29c3c92cb0aee 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.api.batch.sensor.issue.internal;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.EnumMap;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -42,6 +44,7 @@ import static org.sonar.api.utils.Preconditions.checkArgument;
 import static org.sonar.api.utils.Preconditions.checkState;
 
 public class DefaultIssue extends AbstractDefaultIssue<DefaultIssue> implements Issue, NewIssue {
+  private final Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> overridenImpacts = new EnumMap<>(SoftwareQuality.class);
   private RuleKey ruleKey;
   private Double gap;
   private Severity overriddenSeverity;
@@ -81,7 +84,8 @@ public class DefaultIssue extends AbstractDefaultIssue<DefaultIssue> implements
 
   @Override
   public DefaultIssue overrideImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity) {
-    return null;
+    overridenImpacts.put(softwareQuality, severity);
+    return this;
   }
 
   @Override
@@ -144,7 +148,7 @@ public class DefaultIssue extends AbstractDefaultIssue<DefaultIssue> implements
 
   @Override
   public Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> overridenImpacts() {
-    return Collections.emptyMap();
+    return overridenImpacts;
   }
 
   @Override
index f35583825ca64112f6e7d3235cb4cf0a6eb95bd7..ba3e9e48b94551273ea2a3d2474b6470127c15de 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.api.batch.sensor.issue.Issue.Flow;
 import org.sonar.api.batch.sensor.issue.MessageFormatting;
 import org.sonar.api.batch.sensor.issue.NewIssue.FlowType;
 import org.sonar.api.batch.sensor.issue.fix.NewQuickFix;
+import org.sonar.api.issue.impact.SoftwareQuality;
 import org.sonar.api.rule.RuleKey;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -234,6 +235,13 @@ public class DefaultIssueTest {
     assertThat(issue.isQuickFixAvailable()).isTrue();
   }
 
+  @Test
+  public void issue_can_override_impacts() {
+    DefaultIssue issue = new DefaultIssue(project, storage).overrideImpact(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW);
+
+    assertThat(issue.overridenImpacts()).containsEntry(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW);
+  }
+
   @Test
   public void quickfix_only_sets_flag_to_true() {
     DefaultIssue issue = new DefaultIssue(project);
index 5e3b7a6d254ad83819427048a8315f56fec351ac..3b2f99fecaf6e45a2206b2f7ab6a97c45b753a6e 100644 (file)
  */
 package org.sonar.scanner.issue;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 import javax.annotation.Nullable;
@@ -37,6 +39,7 @@ import org.sonar.api.batch.sensor.issue.Issue.Flow;
 import org.sonar.api.batch.sensor.issue.MessageFormatting;
 import org.sonar.api.batch.sensor.issue.NewIssue.FlowType;
 import org.sonar.api.batch.sensor.issue.internal.DefaultIssueFlow;
+import org.sonar.api.issue.impact.SoftwareQuality;
 import org.sonar.scanner.protocol.Constants.Severity;
 import org.sonar.scanner.protocol.output.ScannerReport;
 import org.sonar.scanner.protocol.output.ScannerReport.IssueLocation;
@@ -116,6 +119,7 @@ public class IssuePublisher {
     builder.setRuleKey(issue.ruleKey().rule());
     builder.setMsg(primaryMessage);
     builder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings()));
+    builder.addAllOverridenImpacts(toProtobufImpacts(issue.overridenImpacts()));
     locationBuilder.setMsg(primaryMessage);
     locationBuilder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings()));
 
@@ -138,6 +142,12 @@ public class IssuePublisher {
     return builder.build();
   }
 
+  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()));
+    return impacts;
+  }
+
   private static List<ScannerReport.MessageFormatting> toProtobufMessageFormattings(List<MessageFormatting> messageFormattings) {
     return messageFormattings.stream()
       .map(m -> ScannerReport.MessageFormatting.newBuilder()
index faaea60e5995ccd5fd8a028de77012dae9c8cc77..c8bc5f7844516e80098608162b885d16a4f85b8f 100644 (file)
@@ -42,6 +42,7 @@ import org.sonar.api.batch.sensor.issue.internal.DefaultExternalIssue;
 import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
 import org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation;
 import org.sonar.api.batch.sensor.issue.internal.DefaultMessageFormatting;
+import org.sonar.api.issue.impact.SoftwareQuality;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.rules.RuleType;
@@ -59,10 +60,11 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.when;
 import static org.sonar.api.batch.sensor.issue.MessageFormatting.Type.CODE;
+import static org.sonar.api.issue.impact.SoftwareQuality.*;
 
 @RunWith(MockitoJUnitRunner.class)
 public class IssuePublisherTest {
-  static final RuleKey JAVA_RULE_KEY = RuleKey.of("java", "AvoidCycle");
+  private static final RuleKey JAVA_RULE_KEY = RuleKey.of("java", "AvoidCycle");
   private static final RuleKey NOSONAR_RULE_KEY = RuleKey.of("java", "NoSonarCheck");
 
   private DefaultInputProject project;
@@ -128,7 +130,9 @@ public class IssuePublisherTest {
       .overrideSeverity(org.sonar.api.batch.rule.Severity.CRITICAL)
       .setQuickFixAvailable(true)
       .setRuleDescriptionContextKey(ruleDescriptionContextKey)
-      .setCodeVariants(List.of("variant1", "variant2"));
+      .setCodeVariants(List.of("variant1", "variant2"))
+      .overrideImpact(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH)
+      .overrideImpact(RELIABILITY, org.sonar.api.issue.impact.Severity.LOW);
 
     when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true);
 
@@ -141,6 +145,10 @@ public class IssuePublisherTest {
     assertThat(argument.getValue().getQuickFixAvailable()).isTrue();
     assertThat(argument.getValue().getRuleDescriptionContextKey()).isEqualTo(ruleDescriptionContextKey);
     assertThat(argument.getValue().getCodeVariantsList()).containsExactly("variant1", "variant2");
+
+    ScannerReport.Impact impact1 = ScannerReport.Impact.newBuilder().setSoftwareQuality(MAINTAINABILITY.name()).setSeverity("HIGH").build();
+    ScannerReport.Impact impact2 = ScannerReport.Impact.newBuilder().setSoftwareQuality(RELIABILITY.name()).setSeverity("LOW").build();
+    assertThat(argument.getValue().getOverridenImpactsList()).containsExactly(impact1, impact2);
   }
 
   @Test
index 335df02507f6d956e58eceb0f6ae5583ae6f32f6..5de3e742b2194ee9677a386096fea25fda56977e 100644 (file)
@@ -201,6 +201,7 @@ message Issue {
   optional string ruleDescriptionContextKey = 9;
   repeated MessageFormatting msgFormatting = 10;
   repeated string codeVariants = 11;
+  repeated Impact overridenImpacts = 12;
 }
 
 message ExternalIssue {
@@ -213,6 +214,7 @@ message ExternalIssue {
   repeated Flow flow = 7;
   IssueType type = 8;
   repeated MessageFormatting msgFormatting = 9;
+  repeated Impact overridenImpacts = 10;
 }
 
 message AdHocRule {
@@ -361,3 +363,8 @@ message AnalysisWarning {
   string text = 1;
   int64 timestamp = 2;
 }
+
+message Impact {
+  string software_quality = 1;
+  string severity = 2;
+}
\ No newline at end of file