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 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"); }
public boolean initAndAddIssue(Issue issue) { InputComponent inputComponent = issue.primaryLocation().inputComponent(); BatchComponent component = componentCache.get(inputComponent); ActiveRule activeRule = activeRules.find(issue.ruleKey()); if (activeRule == null) { String primaryMessage = Strings.isNullOrEmpty(issue.primaryLocation().message()) ? rule.name() : issue.primaryLocation().message(); org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRule.severity()); builder.setRuleRepository(issue.ruleKey().repository()); builder.setRuleKey(issue.ruleKey().rule()); builder.setMsg(primaryMessage); locationBuilder.setMsg(primaryMessage); TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setLine(primaryTextRange.start().line()); builder.setTextRange(toProtobufTextRange(primaryTextRange)); Double effortToFix = issue.effortToFix(); if (effortToFix != null) { builder.setEffortToFix(effortToFix);
public static DefaultIssue toDefaultIssue(String projectKey, String componentKey, Issue issue) { Severity overridenSeverity = issue.overridenSeverity(); return new org.sonar.core.issue.DefaultIssueBuilder() .componentKey(componentKey) .projectKey(projectKey) .ruleKey(RuleKey.of(issue.ruleKey().repository(), issue.ruleKey().rule())) .effortToFix(issue.effortToFix()) .line(issue.line()) .message(issue.message()) .severity(overridenSeverity != null ? overridenSeverity.name() : null) .build(); }
@Test public void testForceSeverity() throws IOException { DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo") .setLanguage(Xoo.KEY) .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n") .build(); SensorContextTester context = SensorContextTester.create(temp.newFolder()); context.fileSystem().add(inputFile); context.settings().setProperty(OneIssuePerLineSensor.FORCE_SEVERITY_PROPERTY, "MINOR"); sensor.execute(context); assertThat(context.allIssues()).hasSize(10); // One issue per line for (Issue issue : context.allIssues()) { assertThat(issue.overriddenSeverity()).isEqualTo(Severity.MINOR); } }
/** * Thread safe assuming that each issues for each file are only written once. */ @Override public void store(Issue issue) { if (issue.primaryLocation().inputComponent() instanceof DefaultInputFile) { DefaultInputFile defaultInputFile = (DefaultInputFile) issue.primaryLocation().inputComponent(); if (shouldSkipStorage(defaultInputFile)) { return; } defaultInputFile.setPublished(true); } moduleIssues.initAndAddIssue(issue); }
@Test public void testRule() throws IOException { DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo") .setLanguage(Xoo.KEY) .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n") .build(); SensorContextTester context = SensorContextTester.create(temp.newFolder()); context.fileSystem().add(inputFile); sensor.execute(context); assertThat(context.allIssues()).hasSize(10); // One issue per line for (Issue issue : context.allIssues()) { assertThat(issue.gap()).isNull(); } }
private void applyFlows(Issue issue) { for (Flow flow : issue.flows()) { if (!flow.locations().isEmpty()) { flowBuilder.clear(); for (org.sonar.api.batch.sensor.issue.IssueLocation location : flow.locations()) { locationBuilder.clear(); locationBuilder.setComponentRef(componentCache.get(location.inputComponent()).batchId()); String message = location.message(); if (message != null) { locationBuilder.setMsg(message); } TextRange textRange = location.textRange(); if (textRange != null) { locationBuilder.setTextRange(toProtobufTextRange(textRange)); } flowBuilder.addLocation(locationBuilder.build()); } builder.addFlow(flowBuilder.build()); } } }
@Override public void store(Issue issue) { String key = getKey(issue.inputPath()); if (key == null) { projectIssues.add(issue); } else { if (!issuesByComponent.containsKey(key)) { issuesByComponent.put(key, new ArrayList<Issue>()); } issuesByComponent.get(key).add(issue); } }
public boolean initAndAddIssue(Issue issue) { DefaultInputComponent inputComponent = (DefaultInputComponent) issue.primaryLocation().inputComponent(); if (noSonar(inputComponent, issue)) { return false; } ActiveRule activeRule = activeRules.find(issue.ruleKey()); if (activeRule == null) { // rule does not exist or is not enabled -> ignore the issue return false; } ScannerReport.Issue rawIssue = createReportIssue(issue, inputComponent.scannerId(), activeRule.severity()); if (filters.accept(inputComponent, rawIssue)) { write(inputComponent.scannerId(), rawIssue); return true; } return false; }
/** * Thread safe assuming that each issues for each file are only written once. */ @Override public void store(Issue issue) { if (issue.primaryLocation().inputComponent() instanceof DefaultInputFile) { DefaultInputFile defaultInputFile = (DefaultInputFile) issue.primaryLocation().inputComponent(); if (shouldSkipStorage(defaultInputFile)) { return; } defaultInputFile.setPublished(true); } moduleIssues.initAndAddIssue(issue); }
@Test public void testProvideGap() throws IOException { DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo") .setLanguage(Xoo.KEY) .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n") .build(); SensorContextTester context = SensorContextTester.create(temp.newFolder()); context.fileSystem().add(inputFile); context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2"); sensor.execute(context); assertThat(context.allIssues()).hasSize(10); // One issue per line for (Issue issue : context.allIssues()) { assertThat(issue.gap()).isEqualTo(1.2d); } }
@Override public void store(Issue issue) { Resource r; InputPath inputPath = issue.inputPath(); if (inputPath != null) { if (inputPath instanceof InputDir) { r = Directory.create(inputPath.relativePath()); } else { r = File.create(inputPath.relativePath()); } } else { r = project; } Issuable issuable = perspectives.as(Issuable.class, r); if (issuable == null) { return; } issuable.addIssue(toDefaultIssue(project.getKey(), ComponentKeys.createEffectiveKey(project, r), issue)); }
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 Rule validateRule(Issue issue) { RuleKey ruleKey = issue.ruleKey(); Rule rule = rules.find(ruleKey); if (rule == null) { throw MessageException.of(String.format("The rule '%s' does not exist.", ruleKey)); } if (Strings.isNullOrEmpty(rule.name()) && Strings.isNullOrEmpty(issue.primaryLocation().message())) { throw MessageException.of(String.format("The rule '%s' has no name and the related issue has no message.", ruleKey)); } return rule; }
@Test public void testProvideGap_before_5_5() throws IOException { DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo") .setLanguage(Xoo.KEY) .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n") .build(); SensorContextTester context = SensorContextTester.create(temp.newFolder()); context.fileSystem().add(inputFile); context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2"); context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.parse("5.4"), SonarQubeSide.SCANNER)); sensor.execute(context); assertThat(context.allIssues()).hasSize(10); // One issue per line for (Issue issue : context.allIssues()) { assertThat(issue.gap()).isEqualTo(1.2d); } }
@Override public void store(Issue issue) { InputComponent inputComponent = issue.primaryLocation().inputComponent(); DefaultRule rule = validateRule(issue); ActiveRule activeRule = activeRules.find(issue.ruleKey()); if (activeRule == null) { // rule does not exist or is not enabled -> ignore the issue return; } if (noSonar(inputComponent, issue)) { return; } String primaryMessage = defaultIfEmpty(issue.primaryLocation().message(), rule.name()); org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); String severity = overriddenSeverity != null ? overriddenSeverity.name() : activeRule.severity(); String type = rule.type(); List<org.sonarsource.sonarlint.core.client.api.common.analysis.Issue.Flow> flows = mapFlows(issue.flows()); DefaultClientIssue newIssue = new DefaultClientIssue(severity, type, activeRule, rules.find(activeRule.ruleKey()), primaryMessage, issue.primaryLocation().textRange(), inputComponent.isFile() ? ((SonarLintInputFile) inputComponent).getClientInputFile() : null, flows); if (filters.accept(inputComponent, newIssue)) { issueListener.handle(newIssue); } }
private DefaultRule validateRule(Issue issue) { RuleKey ruleKey = issue.ruleKey(); Rule rule = rules.find(ruleKey); if (rule == null) { throw MessageException.of(String.format("The rule '%s' does not exist.", ruleKey)); } if (isEmpty(rule.name()) && isEmpty(issue.primaryLocation().message())) { throw MessageException.of(String.format("The rule '%s' has no name and the related issue has no message.", ruleKey)); } return (DefaultRule) rule; }
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"); }
private static boolean noSonar(InputComponent inputComponent, Issue issue) { TextRange textRange = issue.primaryLocation().textRange(); return inputComponent.isFile() && textRange != null && ((SonarLintInputFile) inputComponent).hasNoSonarAt(textRange.start().line()) && !StringUtils.containsIgnoreCase(issue.ruleKey().rule(), "nosonar"); }