From 39f4503d689f20e83edfbd48d24d1d37acc46c20 Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Wed, 6 Jul 2022 09:09:17 +0200 Subject: [PATCH] SONAR-16614 saving detected context in the scanner report --- build.gradle | 2 +- .../batch/sensor/issue/internal/DefaultIssue.java | 13 +++++++++++++ .../sensor/issue/internal/DefaultIssueTest.java | 4 +++- .../org/sonar/scanner/issue/IssuePublisher.java | 1 + .../org/sonar/scanner/issue/IssuePublisherTest.java | 5 ++++- .../src/main/protobuf/scanner_report.proto | 1 + 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index d0d96d9824a..eb7be4f63b3 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java index c5189dd2d58..b36f7125b8c 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssue.java @@ -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 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 implements return this; } + @Override + public DefaultIssue setRuleDescriptionContextKey(@Nullable String ruleDescriptionContextKey) { + this.ruleDescriptionContextKey = ruleDescriptionContextKey; + return this; + } + @Override public boolean isQuickFixAvailable() { return quickFixAvailable; } + @Override + public Optional ruleDescriptionContextKey() { + return Optional.ofNullable(ruleDescriptionContextKey); + } + @Override public Severity overriddenSeverity() { return this.overriddenSeverity; diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java index d0901893e24..686f804812e 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/issue/internal/DefaultIssueTest.java @@ -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(); 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 849810dc2a7..48ac551dd1c 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 @@ -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(); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java index 279a28b7c19..f55559e9100 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java @@ -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 diff --git a/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto b/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto index dda0893f219..9b2b159dc31 100644 --- a/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto +++ b/sonar-scanner-protocol/src/main/protobuf/scanner_report.proto @@ -195,6 +195,7 @@ message Issue { TextRange text_range = 6; repeated Flow flow = 7; bool quickFixAvailable = 8; + optional string ruleDescriptionContextKey = 9; } message ExternalIssue { -- 2.39.5