.setGap(3.14) .addFlow(ScannerReport.Flow.newBuilder() .addLocation(ScannerReport.IssueLocation.newBuilder() .setComponentRef(FILE_REF) .setMsg("Secondary location in same file") .setTextRange(TextRange.newBuilder().setStartLine(2).build())) .addLocation(ScannerReport.IssueLocation.newBuilder() .setComponentRef(NOT_IN_REPORT_FILE_REF) .setMsg("Secondary location in a missing file") .setTextRange(TextRange.newBuilder().setStartLine(3).build())) .addLocation(ScannerReport.IssueLocation.newBuilder() .setComponentRef(ANOTHER_FILE_REF) .setMsg("Secondary location in another file")
@Test public void testFlowWithSingleLocation() throws Exception { List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/Multiple.xoo")); assertThat(issues).hasSize(1); Issue issue = issues.get(0); assertThat(issue.getMsg()).isEqualTo("Primary location"); assertThat(issue.getTextRange().getStartLine()).isEqualTo(6); assertThat(issue.getTextRange().getStartOffset()).isEqualTo(23); assertThat(issue.getTextRange().getEndLine()).isEqualTo(6); assertThat(issue.getTextRange().getEndOffset()).isEqualTo(50); assertThat(issue.getFlowList()).hasSize(1); Flow flow = issue.getFlow(0); assertThat(flow.getLocationList()).hasSize(1); IssueLocation additionalLocation = flow.getLocation(0); assertThat(additionalLocation.getMsg()).isEqualTo("Flow step #1"); assertThat(additionalLocation.getTextRange().getStartLine()).isEqualTo(7); assertThat(additionalLocation.getTextRange().getStartOffset()).isEqualTo(26); assertThat(additionalLocation.getTextRange().getEndLine()).isEqualTo(7); assertThat(additionalLocation.getTextRange().getEndOffset()).isEqualTo(53); }
private Optional<DbIssues.Location> convertLocation(ScannerReport.IssueLocation source) { DbIssues.Location.Builder target = DbIssues.Location.newBuilder(); if (source.getComponentRef() != 0 && source.getComponentRef() != component.getReportAttributes().getRef()) { // SONAR-10781 Component might not exist because on short living branch and PR, only changed components are included in the report Optional<Component> optionalComponent = treeRootHolder.getOptionalComponentByRef(source.getComponentRef()); if (!optionalComponent.isPresent()) { return Optional.empty(); } target.setComponentId(optionalComponent.get().getUuid()); } if (isNotEmpty(source.getMsg())) { target.setMsg(source.getMsg()); } if (source.hasTextRange()) { ScannerReport.TextRange sourceRange = source.getTextRange(); DbCommons.TextRange.Builder targetRange = convertTextRange(sourceRange); target.setTextRange(targetRange); } return Optional.of(target.build()); }
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); locationBuilder.setMsg(primaryMessage); locationBuilder.setComponentRef(componentRef); TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange)); } Long effort = issue.remediationEffort(); if (effort != null) { builder.setEffort(effort); } applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows()); return builder.build(); }
private static ScannerReport.Issue createReportIssue(Issue issue, int componentRef, String activeRuleSeverity) { String primaryMessage = nullToEmpty(issue.primaryLocation().message()); org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRuleSeverity); ScannerReport.Issue.Builder builder = ScannerReport.Issue.newBuilder(); ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder(); ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder(); // non-null fields builder.setSeverity(severity); builder.setRuleRepository(issue.ruleKey().repository()); builder.setRuleKey(issue.ruleKey().rule()); builder.setMsg(primaryMessage); locationBuilder.setMsg(primaryMessage); locationBuilder.setComponentRef(componentRef); TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange)); } Double gap = issue.gap(); if (gap != null) { builder.setGap(gap); } applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows()); return builder.build(); }
private Optional<DbIssues.Location> convertLocation(ScannerReport.IssueLocation source) { DbIssues.Location.Builder target = DbIssues.Location.newBuilder(); if (source.getComponentRef() != 0 && source.getComponentRef() != component.getReportAttributes().getRef()) { // SONAR-10781 Component might not exist because on short living branch and PR, only changed components are included in the report Optional<Component> optionalComponent = treeRootHolder.getOptionalComponentByRef(source.getComponentRef()); if (!optionalComponent.isPresent()) { return Optional.empty(); } target.setComponentId(optionalComponent.get().getUuid()); } if (isNotEmpty(source.getMsg())) { target.setMsg(source.getMsg()); } if (source.hasTextRange()) { ScannerReport.TextRange sourceRange = source.getTextRange(); DbCommons.TextRange.Builder targetRange = convertTextRange(sourceRange); target.setTextRange(targetRange); } return Optional.of(target.build()); }
assertThat(issue.hasTextRange()).isFalse(); assertThat(issue.getFlow(0).getLocationCount()).isOne(); assertThat(issue.getFlow(0).getLocation(0).getTextRange().getStartLine()).isOne(); assertThat(issue.getFlow(1).getLocationCount()).isOne(); assertThat(issue.getFlow(1).getLocation(0).getTextRange().getStartLine()).isEqualTo(3);
private static ScannerReport.Issue createReportIssue(Issue issue, int componentRef, String activeRuleSeverity) { String primaryMessage = nullToEmpty(issue.primaryLocation().message()); org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRuleSeverity); ScannerReport.Issue.Builder builder = ScannerReport.Issue.newBuilder(); ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder(); ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder(); // non-null fields builder.setSeverity(severity); builder.setRuleRepository(issue.ruleKey().repository()); builder.setRuleKey(issue.ruleKey().rule()); builder.setMsg(primaryMessage); locationBuilder.setMsg(primaryMessage); locationBuilder.setComponentRef(componentRef); TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange)); } Double gap = issue.gap(); if (gap != null) { builder.setGap(gap); } applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows()); return builder.build(); }
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); locationBuilder.setMsg(primaryMessage); locationBuilder.setComponentRef(componentRef); TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange)); } Long effort = issue.remediationEffort(); if (effort != null) { builder.setEffort(effort); } applyFlows(builder::addFlow, locationBuilder, textRangeBuilder, issue.flows()); return builder.build(); }
private static ScannerReport.IssueLocation moveLocation(ScannerReport.Component root, ScannerReport.Component component, ScannerReport.IssueLocation location) { String msg = "[" + component.getProjectRelativePath() + "] " + location.getMsg(); return ScannerReport.IssueLocation.newBuilder() .setComponentRef(root.getRef()) .setMsg(msg) .build(); }