Search in sources :

Example 1 with ActiveRule

use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.

the class CreateIssueByInternalKeySensor method createIssues.

private static void createIssues(InputFile file, SensorContext context) {
    ActiveRule rule = context.activeRules().findByInternalKey(XooRulesDefinition.XOO_REPOSITORY, context.settings().getString(INTERNAL_KEY_PROPERTY));
    if (rule != null) {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(rule.ruleKey()).at(newIssue.newLocation().on(file).message("This issue is generated on each file")).save();
    }
}
Also used : NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) ActiveRule(org.sonar.api.batch.rule.ActiveRule)

Example 2 with ActiveRule

use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.

the class ActiveRulesBuilderTest method build_rules.

@Test
public void build_rules() {
    ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("squid", "S0001")).setName("My Rule").setSeverity(Severity.CRITICAL).setInternalKey("__S0001__").setParam("min", "20").activate().create(RuleKey.of("squid", "S0002")).activate().create(RuleKey.of("findbugs", "NPE")).setInternalKey(null).setSeverity(null).setParam("foo", null).activate().build();
    assertThat(activeRules.findAll()).hasSize(3);
    assertThat(activeRules.findByRepository("squid")).hasSize(2);
    assertThat(activeRules.findByRepository("findbugs")).hasSize(1);
    assertThat(activeRules.findByInternalKey("squid", "__S0001__")).isNotNull();
    assertThat(activeRules.findByRepository("unknown")).isEmpty();
    ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));
    assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
    assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
    assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
    assertThat(squid1.internalKey()).isEqualTo("__S0001__");
    assertThat(squid1.params()).hasSize(1);
    assertThat(squid1.param("min")).isEqualTo("20");
    ActiveRule squid2 = activeRules.find(RuleKey.of("squid", "S0002"));
    assertThat(squid2.ruleKey().repository()).isEqualTo("squid");
    assertThat(squid2.ruleKey().rule()).isEqualTo("S0002");
    assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
    assertThat(squid2.params()).isEmpty();
    ActiveRule findbugsRule = activeRules.find(RuleKey.of("findbugs", "NPE"));
    assertThat(findbugsRule.severity()).isEqualTo(Severity.defaultSeverity());
    assertThat(findbugsRule.internalKey()).isNull();
    assertThat(findbugsRule.params()).isEmpty();
}
Also used : ActiveRule(org.sonar.api.batch.rule.ActiveRule) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Test(org.junit.Test)

Example 3 with ActiveRule

use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.

the class ModuleIssues method initAndAddIssue.

public boolean initAndAddIssue(Issue issue) {
    DefaultInputComponent inputComponent = (DefaultInputComponent) issue.primaryLocation().inputComponent();
    Rule rule = validateRule(issue);
    ActiveRule activeRule = activeRules.find(issue.ruleKey());
    if (activeRule == null) {
        // rule does not exist or is not enabled -> ignore the issue
        return false;
    }
    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());
    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(inputComponent.batchId());
    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, locationBuilder, textRangeBuilder, issue);
    ScannerReport.Issue rawIssue = builder.build();
    if (filters.accept(inputComponent.key(), rawIssue)) {
        write(inputComponent.batchId(), rawIssue);
        return true;
    }
    return false;
}
Also used : Issue(org.sonar.api.batch.sensor.issue.Issue) ActiveRule(org.sonar.api.batch.rule.ActiveRule) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) Severity(org.sonar.scanner.protocol.Constants.Severity) TextRange(org.sonar.api.batch.fs.TextRange) IssueLocation(org.sonar.scanner.protocol.output.ScannerReport.IssueLocation) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) ActiveRule(org.sonar.api.batch.rule.ActiveRule) Rule(org.sonar.api.batch.rule.Rule)

Example 4 with ActiveRule

use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.

the class LocalIssueTracking method copyServerIssues.

private void copyServerIssues(Collection<ServerIssueFromWs> serverIssues, List<TrackedIssue> trackedIssues) {
    for (ServerIssueFromWs serverIssue : serverIssues) {
        org.sonar.scanner.protocol.input.ScannerInput.ServerIssue unmatchedPreviousIssue = serverIssue.getDto();
        TrackedIssue unmatched = IssueTransformer.toTrackedIssue(unmatchedPreviousIssue);
        ActiveRule activeRule = activeRules.find(unmatched.getRuleKey());
        unmatched.setNew(false);
        if (activeRule == null) {
            // rule removed
            IssueTransformer.resolveRemove(unmatched);
        }
        trackedIssues.add(unmatched);
    }
}
Also used : ActiveRule(org.sonar.api.batch.rule.ActiveRule)

Example 5 with ActiveRule

use of org.sonar.api.batch.rule.ActiveRule in project sonarqube by SonarSource.

the class LocalIssueTracking method updateUnmatchedIssue.

private void updateUnmatchedIssue(TrackedIssue issue) {
    ActiveRule activeRule = activeRules.find(issue.getRuleKey());
    issue.setNew(false);
    boolean isRemovedRule = activeRule == null;
    if (isRemovedRule) {
        IssueTransformer.resolveRemove(issue);
    } else {
        IssueTransformer.close(issue);
    }
}
Also used : ActiveRule(org.sonar.api.batch.rule.ActiveRule)

Aggregations

ActiveRule (org.sonar.api.batch.rule.ActiveRule)5 Test (org.junit.Test)1 TextRange (org.sonar.api.batch.fs.TextRange)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 ActiveRules (org.sonar.api.batch.rule.ActiveRules)1 Rule (org.sonar.api.batch.rule.Rule)1 Issue (org.sonar.api.batch.sensor.issue.Issue)1 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)1 Severity (org.sonar.scanner.protocol.Constants.Severity)1 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)1 IssueLocation (org.sonar.scanner.protocol.output.ScannerReport.IssueLocation)1