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(); }
@Test void issues_with_sonarqube_72() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.golint.reportPaths", REPORT_BASE_PATH.resolve("golint-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoLintReportSensor(), context); assertThat(externalIssues).hasSize(2); ExternalIssue first = externalIssues.get(0); assertThat(first.type()).isEqualTo(RuleType.CODE_SMELL); assertThat(first.severity()).isEqualTo(Severity.MAJOR); assertThat(first.ruleKey().repository()).isEqualTo("golint"); assertThat(first.ruleKey().rule()).isEqualTo("PackageComment"); assertThat(first.primaryLocation().message()).isEqualTo("package comment should be of the form \"Package samples ...\""); assertThat(first.primaryLocation().textRange().start().line()).isEqualTo(1); ExternalIssue second = externalIssues.get(1); assertThat(second.type()).isEqualTo(RuleType.CODE_SMELL); assertThat(second.severity()).isEqualTo(Severity.MAJOR); assertThat(second.ruleKey().repository()).isEqualTo("golint"); assertThat(second.ruleKey().rule()).isEqualTo("Exported"); assertThat(second.primaryLocation().message()).isEqualTo("exported type User should have comment or be unexported"); assertThat(second.primaryLocation().textRange().start().line()).isEqualTo(2); assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty(); }
@Test void issues_with_sonarqube_72() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("govet-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues).hasSize(3); ExternalIssue first = externalIssues.get(0); assertThat(first.ruleKey().rule()).isEqualTo("nilfunc"); assertThat(first.severity()).isEqualTo(Severity.MAJOR); assertThat(first.primaryLocation().message()).isEqualTo("comparison of function Foo == nil is always false"); assertThat(first.primaryLocation().textRange().start().line()).isEqualTo(1); ExternalIssue second = externalIssues.get(1); assertThat(second.ruleKey().rule()).isEqualTo("printf"); assertThat(second.severity()).isEqualTo(Severity.MAJOR); assertThat(second.primaryLocation().message()).isEqualTo("Printf format %s has arg &str of wrong type *string"); assertThat(second.primaryLocation().textRange().start().line()).isEqualTo(2); ExternalIssue third = externalIssues.get(2); assertThat(third.ruleKey().rule()).isEqualTo("unreachable"); assertThat(third.severity()).isEqualTo(Severity.MAJOR); assertThat(third.primaryLocation().message()).isEqualTo("unreachable code"); assertThat(third.primaryLocation().textRange().start().line()).isEqualTo(2); assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty(); }
public void initAndAddExternalIssue(ExternalIssue issue) { DefaultInputComponent inputComponent = (DefaultInputComponent) issue.primaryLocation().inputComponent(); ScannerReport.ExternalIssue rawExternalIssue = createReportExternalIssue(issue, inputComponent.scannerId()); write(inputComponent.scannerId(), rawExternalIssue); }
@Test void should_match_govet_all_keys() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("all-govet-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues).hasSize(263); Stream<String> uniqueKeys = externalIssues.stream().map(externalIssue -> externalIssue.ruleKey().rule()).distinct(); assertThat(uniqueKeys).hasSize(19); // all messages are associated to a rule key assertThat(externalIssues).filteredOn("ruleKey.rule", GENERIC_ISSUE_KEY).hasSize(0); }
@Test void issues_with_sonarqube_72() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.gometalinter.reportPaths", REPORT_BASE_PATH.resolve("gometalinter-report.txt").toString()); List<org.sonar.api.batch.sensor.issue.ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoMetaLinterReportSensor(), context); assertThat(externalIssues).hasSize(3); org.sonar.api.batch.sensor.issue.ExternalIssue first = externalIssues.get(0); assertThat(first.type()).isEqualTo(RuleType.BUG); assertThat(first.severity()).isEqualTo(Severity.MAJOR); assertThat(first.ruleKey().repository()).isEqualTo("govet"); assertThat(first.ruleKey().rule()).isEqualTo("assign"); assertThat(first.primaryLocation().message()).isEqualTo("self-assignment of name to name"); assertThat(first.primaryLocation().textRange().start().line()).isEqualTo(1); org.sonar.api.batch.sensor.issue.ExternalIssue second = externalIssues.get(1); assertThat(second.type()).isEqualTo(RuleType.CODE_SMELL); assertThat(second.severity()).isEqualTo(Severity.MAJOR); assertThat(second.ruleKey().repository()).isEqualTo("interfacer"); assertThat(second.ruleKey().rule()).isEqualTo("issue"); assertThat(second.primaryLocation().message()).isEqualTo("other (declaration) of ascii_allowed"); assertThat(second.primaryLocation().textRange().start().line()).isEqualTo(2); org.sonar.api.batch.sensor.issue.ExternalIssue third = externalIssues.get(2); assertThat(third.type()).isEqualTo(RuleType.CODE_SMELL); assertThat(third.severity()).isEqualTo(Severity.MAJOR); assertThat(third.ruleKey().repository()).isEqualTo("golint"); assertThat(third.ruleKey().rule()).isEqualTo("Exported"); assertThat(third.primaryLocation().message()).isEqualTo("exported type User should have comment or be unexported"); assertThat(third.primaryLocation().textRange().start().line()).isEqualTo(3); assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty(); }
public void initAndAddExternalIssue(ExternalIssue issue) { DefaultInputComponent inputComponent = (DefaultInputComponent) issue.primaryLocation().inputComponent(); ScannerReport.ExternalIssue rawExternalIssue = createReportExternalIssue(issue, inputComponent.scannerId()); write(inputComponent.scannerId(), rawExternalIssue); }
@Test void should_match_to_generic_issue_if_match_not_found() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("govet-with-unknown-message.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues.get(0).ruleKey().rule()).isEqualTo(GENERIC_ISSUE_KEY); } }
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(); }
@Test void should_match_golint_all_keys() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.golint.reportPaths", REPORT_BASE_PATH.resolve("all-golint-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoLintReportSensor(), context); assertThat(externalIssues).hasSize(102); Stream<String> uniqueKeys = externalIssues.stream().map(externalIssue -> externalIssue.ruleKey().rule()).distinct(); assertThat(uniqueKeys).hasSize(18); // all messages are associated to a rule key assertThat(externalIssues).filteredOn("ruleKey.rule", GENERIC_ISSUE_KEY).hasSize(0); }
@Test void should_match_to_generic_issue_if_match_not_found() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.golint.reportPaths", REPORT_BASE_PATH.resolve("golint-with-unknown-message.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoLintReportSensor(), context); assertThat(externalIssues.get(0).ruleKey().rule()).isEqualTo(GENERIC_ISSUE_KEY); }