Search in sources :

Example 1 with Rule

use of org.sonar.api.batch.rule.Rule 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 2 with Rule

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

the class ModuleIssues method validateRule.

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;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) ActiveRule(org.sonar.api.batch.rule.ActiveRule) Rule(org.sonar.api.batch.rule.Rule)

Example 3 with Rule

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

the class RuleNameProviderTest method setUp.

@Before
public void setUp() {
    ruleKey = mock(RuleKey.class);
    rule = mock(Rule.class);
    rules = mock(Rules.class);
    provider = new RuleNameProvider(rules);
    when(ruleKey.rule()).thenReturn("ruleKey");
    when(ruleKey.repository()).thenReturn("repoKey");
    when(rule.name()).thenReturn("name");
    when(rule.key()).thenReturn(ruleKey);
    when(rules.find(any(RuleKey.class))).thenReturn(rule);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleNameProvider(org.sonar.scanner.scan.report.RuleNameProvider) Rule(org.sonar.api.batch.rule.Rule) Rules(org.sonar.api.batch.rule.Rules) Before(org.junit.Before)

Example 4 with Rule

use of org.sonar.api.batch.rule.Rule in project sonarlint-core by SonarSource.

the class DefaultSensorStorage method validateRule.

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 (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 (DefaultRule) rule;
}
Also used : DefaultRule(org.sonar.api.batch.rule.internal.DefaultRule) RuleKey(org.sonar.api.rule.RuleKey) ActiveRule(org.sonar.api.batch.rule.ActiveRule) Rule(org.sonar.api.batch.rule.Rule) DefaultRule(org.sonar.api.batch.rule.internal.DefaultRule)

Example 5 with Rule

use of org.sonar.api.batch.rule.Rule in project sonarlint-core by SonarSource.

the class SonarQubeActiveRulesProvider method createNewActiveRule.

private static void createNewActiveRule(ActiveRulesBuilder builder, ActiveRule activeRule, Sonarlint.Rules storageRules, String language, Rules rules) {
    RuleKey ruleKey = RuleKey.of(activeRule.getRepo(), activeRule.getKey());
    Rule rule = rules.find(ruleKey);
    Sonarlint.Rules.Rule storageRule;
    try {
        storageRule = storageRules.getRulesByKeyOrThrow(ruleKey.toString());
    } catch (IllegalArgumentException e) {
        throw new MessageException("Unknown active rule in the quality profile of the project. Please update the SonarQube server binding.");
    }
    NewActiveRule newActiveRule = builder.create(ruleKey).setLanguage(language).setName(rule.name()).setInternalKey(rule.internalKey()).setSeverity(activeRule.getSeverity());
    if (!StringUtils.isEmpty(storageRule.getTemplateKey())) {
        RuleKey templateRuleKey = RuleKey.parse(storageRule.getTemplateKey());
        newActiveRule.setTemplateRuleKey(templateRuleKey.rule());
    }
    for (Map.Entry<String, String> param : activeRule.getParamsMap().entrySet()) {
        newActiveRule.setParam(param.getKey(), param.getValue());
    }
    newActiveRule.activate();
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) MessageException(org.sonarsource.sonarlint.core.client.api.exceptions.MessageException) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) Rule(org.sonar.api.batch.rule.Rule) ActiveRule(org.sonarsource.sonarlint.core.proto.Sonarlint.ActiveRules.ActiveRule) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Rules(org.sonar.api.batch.rule.Rules) Map(java.util.Map)

Aggregations

Rule (org.sonar.api.batch.rule.Rule)8 RuleKey (org.sonar.api.rule.RuleKey)5 ActiveRule (org.sonar.api.batch.rule.ActiveRule)3 Rules (org.sonar.api.batch.rule.Rules)3 Before (org.junit.Before)2 TrackedIssue (org.sonar.scanner.issue.tracking.TrackedIssue)2 Map (java.util.Map)1 Test (org.junit.Test)1 InputComponent (org.sonar.api.batch.fs.InputComponent)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 DefaultRule (org.sonar.api.batch.rule.internal.DefaultRule)1 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)1 Issue (org.sonar.api.batch.sensor.issue.Issue)1 RulePriority (org.sonar.api.rules.RulePriority)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 RuleNameProvider (org.sonar.scanner.scan.report.RuleNameProvider)1