]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16614 saving detected context in the scanner report
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Wed, 6 Jul 2022 07:09:17 +0000 (09:09 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 8 Jul 2022 20:02:48 +0000 (20:02 +0000)
build.gradle
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 d0d96d9824afcfdcfb8dc10b9606145108e59335..eb7be4f63b3129f3b432fb5a0955995ce424c4db 100644 (file)
@@ -178,7 +178,7 @@ subprojects {
       dependency 'org.sonarsource.kotlin:sonar-kotlin-plugin:2.9.0.1147'
       dependency 'org.sonarsource.slang:sonar-ruby-plugin:1.9.0.3429'
       dependency 'org.sonarsource.slang:sonar-scala-plugin:1.9.0.3429'
-      dependency 'org.sonarsource.api.plugin:sonar-plugin-api:9.7.0.160'
+      dependency 'org.sonarsource.api.plugin:sonar-plugin-api:9.8.0.196'
       dependency 'org.sonarsource.xml:sonar-xml-plugin:2.5.0.3376'
       dependency 'org.sonarsource.iac:sonar-iac-plugin:1.7.0.2012'
       dependency 'org.sonarsource.text:sonar-text-plugin:1.1.0.282'
index c5189dd2d58816fb15b91169009f1c1e9307bd40..b36f7125b8ca1f0eec24ed33feb562fda040d4b3 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.api.batch.sensor.issue.internal;
 
+import java.util.Optional;
 import javax.annotation.Nullable;
 import org.sonar.api.batch.rule.Severity;
 import org.sonar.api.batch.sensor.internal.SensorStorage;
@@ -38,6 +39,7 @@ public class DefaultIssue extends AbstractDefaultIssue<DefaultIssue> implements
   private Double gap;
   private Severity overriddenSeverity;
   private boolean quickFixAvailable = false;
+  private String ruleDescriptionContextKey;
 
   public DefaultIssue(DefaultInputProject project) {
     this(project, null);
@@ -75,11 +77,22 @@ public class DefaultIssue extends AbstractDefaultIssue<DefaultIssue> implements
     return this;
   }
 
+  @Override
+  public DefaultIssue setRuleDescriptionContextKey(@Nullable String ruleDescriptionContextKey) {
+    this.ruleDescriptionContextKey = ruleDescriptionContextKey;
+    return this;
+  }
+
   @Override
   public boolean isQuickFixAvailable() {
     return quickFixAvailable;
   }
 
+  @Override
+  public Optional<String> ruleDescriptionContextKey() {
+    return Optional.ofNullable(ruleDescriptionContextKey);
+  }
+
   @Override
   public Severity overriddenSeverity() {
     return this.overriddenSeverity;
index d0901893e24472497d310f6acc603deda65a4d3f..686f804812ed42a6d07db8e5ee0a58484118e1ba 100644 (file)
@@ -67,13 +67,15 @@ public class DefaultIssueTest {
         .at(inputFile.selectLine(1))
         .message("Wrong way!"))
       .forRule(RuleKey.of("repo", "rule"))
-      .gap(10.0);
+      .gap(10.0)
+      .setRuleDescriptionContextKey("spring");
 
     assertThat(issue.primaryLocation().inputComponent()).isEqualTo(inputFile);
     assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
     assertThat(issue.primaryLocation().textRange().start().line()).isOne();
     assertThat(issue.gap()).isEqualTo(10.0);
     assertThat(issue.primaryLocation().message()).isEqualTo("Wrong way!");
+    assertThat(issue.ruleDescriptionContextKey()).contains("spring");
 
     issue.save();
 
index 849810dc2a79da0cd00a5a7a2b64967d93b5cdbd..48ac551dd1c494c2195d4d91a1cc8e8923172116 100644 (file)
@@ -123,6 +123,7 @@ public class IssuePublisher {
     }
     applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows());
     builder.setQuickFixAvailable(issue.isQuickFixAvailable());
+    issue.ruleDescriptionContextKey().ifPresent(builder::setRuleDescriptionContextKey);
     return builder.build();
   }
 
index 279a28b7c1982493f6188b4d140f16d15b22809c..f55559e9100034d3c2b9dcd42d475ec4a283a795 100644 (file)
@@ -125,11 +125,13 @@ public class IssuePublisherTest {
       .build());
     initModuleIssues();
 
+    final String ruleDescriptionContextKey = "spring";
     DefaultIssue issue = new DefaultIssue(project)
       .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo"))
       .forRule(JAVA_RULE_KEY)
       .overrideSeverity(org.sonar.api.batch.rule.Severity.CRITICAL)
-      .setQuickFixAvailable(true);
+      .setQuickFixAvailable(true)
+      .setRuleDescriptionContextKey(ruleDescriptionContextKey);
 
     when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true);
 
@@ -140,6 +142,7 @@ public class IssuePublisherTest {
     verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture());
     assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.CRITICAL);
     assertThat(argument.getValue().getQuickFixAvailable()).isTrue();
+    assertThat(argument.getValue().getRuleDescriptionContextKey()).isEqualTo(ruleDescriptionContextKey);
   }
 
   @Test
index dda0893f219311ce9294f14f4f75341e67614f4d..9b2b159dc31c9d4777f1944ce728719f06ed0ee4 100644 (file)
@@ -195,6 +195,7 @@ message Issue {
   TextRange text_range = 6;
   repeated Flow flow = 7;
   bool quickFixAvailable = 8;
+  optional string ruleDescriptionContextKey = 9;
 }
 
 message ExternalIssue {