diff options
author | Léo Geoffroy <leo.geoffroy@sonarsource.com> | 2022-11-23 15:57:02 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-12-01 20:03:11 +0000 |
commit | 66b8bff0dcc778eb93da92cd408214bbfca82c2a (patch) | |
tree | e60388bdf0bdbc60434d00eae81b8e6dffe39fad /sonar-scanner-engine/src | |
parent | 24d4519ea1b8a4426dd90e0bb80b2b09bd7a1c12 (diff) | |
download | sonarqube-66b8bff0dcc778eb93da92cd408214bbfca82c2a.tar.gz sonarqube-66b8bff0dcc778eb93da92cd408214bbfca82c2a.zip |
SONAR-17592 Persist Message formatting from plugin api
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java | 19 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java | 7 |
2 files changed, 24 insertions, 2 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 8688c6a3ebe..d3a26a55d95 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 @@ -20,7 +20,9 @@ package org.sonar.scanner.issue; import java.util.Collection; +import java.util.List; import java.util.function.Consumer; +import java.util.stream.Collectors; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; import org.apache.commons.lang.StringUtils; @@ -32,6 +34,7 @@ import org.sonar.api.batch.rule.ActiveRules; 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; +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.scanner.protocol.Constants.Severity; @@ -112,7 +115,9 @@ public class IssuePublisher { builder.setRuleRepository(issue.ruleKey().repository()); builder.setRuleKey(issue.ruleKey().rule()); builder.setMsg(primaryMessage); + builder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings())); locationBuilder.setMsg(primaryMessage); + locationBuilder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings())); locationBuilder.setComponentRef(componentRef); TextRange primaryTextRange = issue.primaryLocation().textRange(); @@ -129,6 +134,16 @@ public class IssuePublisher { return builder.build(); } + private static List<ScannerReport.MessageFormatting> toProtobufMessageFormattings(List<MessageFormatting> messageFormattings) { + return messageFormattings.stream() + .map(m -> ScannerReport.MessageFormatting.newBuilder() + .setStart(m.start()) + .setEnd(m.end()) + .setType(ScannerReport.MessageFormattingType.valueOf(m.type().name())) + .build()) + .collect(Collectors.toList()); + } + private static ScannerReport.ExternalIssue createReportExternalIssue(ExternalIssue issue, int componentRef) { // primary location of an external issue must have a message String primaryMessage = issue.primaryLocation().message(); @@ -144,8 +159,9 @@ public class IssuePublisher { builder.setEngineId(issue.engineId()); builder.setRuleId(issue.ruleId()); builder.setMsg(primaryMessage); + builder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings())); locationBuilder.setMsg(primaryMessage); - + locationBuilder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings())); locationBuilder.setComponentRef(componentRef); TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { @@ -175,6 +191,7 @@ public class IssuePublisher { String message = location.message(); if (message != null) { locationBuilder.setMsg(message); + locationBuilder.addAllMsgFormatting(toProtobufMessageFormattings(location.messageFormattings())); } TextRange textRange = location.textRange(); if (textRange != null) { 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 becb9a339f1..ada4b74e15b 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 @@ -41,6 +41,7 @@ import org.sonar.api.batch.sensor.issue.NewIssue; 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.rule.RuleKey; import org.sonar.api.rule.Severity; import org.sonar.api.rules.RuleType; @@ -57,6 +58,7 @@ import static org.mockito.Mockito.mock; 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; @RunWith(MockitoJUnitRunner.class) public class IssuePublisherTest { @@ -143,10 +145,12 @@ public class IssuePublisherTest { public void add_issue_flows_to_cache() { initModuleIssues(); + DefaultMessageFormatting messageFormatting = new DefaultMessageFormatting().start(0).end(4).type(CODE); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file)) // Flow without type - .addFlow(List.of(new DefaultIssueLocation().on(file).at(file.selectLine(1)).message("Foo1"), new DefaultIssueLocation().on(file).at(file.selectLine(2)).message("Foo2"))) + .addFlow(List.of(new DefaultIssueLocation().on(file).at(file.selectLine(1)).message("Foo1", List.of(messageFormatting)), + new DefaultIssueLocation().on(file).at(file.selectLine(2)).message("Foo2"))) // Flow with type and description .addFlow(List.of(new DefaultIssueLocation().on(file)), NewIssue.FlowType.DATA, "description") // Flow with execution type and no description @@ -169,6 +173,7 @@ public class IssuePublisherTest { ScannerReport.IssueLocation.newBuilder() .setComponentRef(file.scannerId()) .setMsg("Foo1") + .addMsgFormatting(ScannerReport.MessageFormatting.newBuilder().setStart(0).setEnd(4).setType(ScannerReport.MessageFormattingType.CODE).build()) .setTextRange(ScannerReport.TextRange.newBuilder().setStartLine(1).setEndLine(1).setEndOffset(3).build()) .build(), ScannerReport.IssueLocation.newBuilder() |