aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2023-08-09 16:49:35 +0200
committersonartech <sonartech@sonarsource.com>2023-08-18 20:02:49 +0000
commitdddfee0f55914f56d168ae830038bd58fe9e1e8a (patch)
treef765f6a2ad6dcdf26106d39805c6404f7bb1c6e4 /sonar-scanner-engine
parent8b704e7f10a5525071975340cf97d0e5849beaaa (diff)
downloadsonarqube-dddfee0f55914f56d168ae830038bd58fe9e1e8a.tar.gz
sonarqube-dddfee0f55914f56d168ae830038bd58fe9e1e8a.zip
SONAR-20021 Manage external issues and adhoc rules
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java29
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java24
2 files changed, 43 insertions, 10 deletions
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 3b2f99fecaf..c8f4dce11e0 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
@@ -40,6 +40,8 @@ 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.api.rules.CleanCodeAttribute;
+import org.sonar.api.rules.RuleType;
import org.sonar.scanner.protocol.Constants.Severity;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.IssueLocation;
@@ -87,9 +89,9 @@ public class IssuePublisher {
private static boolean noSonar(DefaultInputComponent inputComponent, Issue issue) {
TextRange textRange = issue.primaryLocation().textRange();
return inputComponent.isFile()
- && textRange != null
- && ((DefaultInputFile) inputComponent).hasNoSonarAt(textRange.start().line())
- && !StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar");
+ && textRange != null
+ && ((DefaultInputFile) inputComponent).hasNoSonarAt(textRange.start().line())
+ && !StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar");
}
public void initAndAddExternalIssue(ExternalIssue issue) {
@@ -161,15 +163,11 @@ public class IssuePublisher {
private static ScannerReport.ExternalIssue createReportExternalIssue(ExternalIssue issue, int componentRef) {
// primary location of an external issue must have a message
String primaryMessage = issue.primaryLocation().message();
- Severity severity = Severity.valueOf(issue.severity().name());
- IssueType issueType = IssueType.valueOf(issue.type().name());
-
ScannerReport.ExternalIssue.Builder builder = ScannerReport.ExternalIssue.newBuilder();
ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder();
ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder();
+
// non-null fields
- builder.setSeverity(severity);
- builder.setType(issueType);
builder.setEngineId(issue.engineId());
builder.setRuleId(issue.ruleId());
builder.setMsg(primaryMessage);
@@ -178,6 +176,20 @@ public class IssuePublisher {
locationBuilder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings()));
locationBuilder.setComponentRef(componentRef);
TextRange primaryTextRange = issue.primaryLocation().textRange();
+
+ //nullable fields
+ CleanCodeAttribute cleanCodeAttribute = issue.cleanCodeAttribute();
+ if (cleanCodeAttribute != null) {
+ builder.setCleanCodeAttribute(cleanCodeAttribute.name());
+ }
+ org.sonar.api.batch.rule.Severity severity = issue.severity();
+ if (severity != null) {
+ builder.setSeverity(Severity.valueOf(severity.name()));
+ }
+ RuleType issueType = issue.type();
+ if (issueType != null) {
+ builder.setType(IssueType.valueOf(issueType.name()));
+ }
if (primaryTextRange != null) {
builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange));
}
@@ -186,6 +198,7 @@ public class IssuePublisher {
builder.setEffort(effort);
}
applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows());
+ builder.addAllImpacts(toProtobufImpacts(issue.impacts()));
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 c8bc5f78445..c399103480d 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
@@ -42,9 +42,9 @@ 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.CleanCodeAttribute;
import org.sonar.api.rules.RuleType;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.FlowType;
@@ -60,7 +60,8 @@ 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.*;
+import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY;
+import static org.sonar.api.issue.impact.SoftwareQuality.RELIABILITY;
@RunWith(MockitoJUnitRunner.class)
public class IssuePublisherTest {
@@ -211,6 +212,25 @@ public class IssuePublisherTest {
}
@Test
+ public void initAndAddExternalIssue_whenImpactAndCleanCodeAttributeProvided_shouldPopulateReportFields() {
+ initModuleIssues();
+
+ DefaultExternalIssue issue = new DefaultExternalIssue(project)
+ .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo"))
+ .cleanCodeAttribute(CleanCodeAttribute.CLEAR)
+ .forRule(JAVA_RULE_KEY)
+ .addImpact(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW);
+
+ moduleIssues.initAndAddExternalIssue(issue);
+
+ ArgumentCaptor<ScannerReport.ExternalIssue> argument = ArgumentCaptor.forClass(ScannerReport.ExternalIssue.class);
+ verify(reportPublisher.getWriter()).appendComponentExternalIssue(eq(file.scannerId()), argument.capture());
+ assertThat(argument.getValue().getImpactsList()).extracting(i -> i.getSoftwareQuality(), i -> i.getSeverity())
+ .containsExactly(tuple(MAINTAINABILITY.name(), org.sonar.api.issue.impact.Severity.LOW.name()));
+ assertThat(argument.getValue().getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.CLEAR.name());
+ }
+
+ @Test
public void use_severity_from_active_rule_if_no_severity_on_issue() {
initModuleIssues();