Search in sources :

Example 6 with RuleKey

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

the class OneIssueOnDirPerFileSensor method createIssues.

private static void createIssues(InputFile file, SensorContext context) {
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY);
    InputDir inputDir = context.fileSystem().inputDir(file.file().getParentFile());
    if (inputDir != null) {
        NewIssue newIssue = context.newIssue();
        newIssue.forRule(ruleKey).at(newIssue.newLocation().on(inputDir).message("This issue is generated for file " + file.relativePath())).save();
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) InputDir(org.sonar.api.batch.fs.InputDir)

Example 7 with RuleKey

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

the class ActiveRuleKeyTest method rule_key_can_contain_colons.

@Test
public void rule_key_can_contain_colons() {
    RuleKey ruleKey = RuleKey.of("squid", "Key:With:Some::Colons");
    ActiveRuleKey key = ActiveRuleKey.of("P1", ruleKey);
    assertThat(key.qProfile()).isEqualTo("P1");
    assertThat(key.ruleKey()).isSameAs(ruleKey);
    assertThat(key.toString()).isEqualTo("P1:squid:Key:With:Some::Colons");
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 8 with RuleKey

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

the class LoadQualityProfilesStep method convert.

private static ActiveRule convert(ScannerReport.ActiveRule input) {
    RuleKey key = RuleKey.of(input.getRuleRepository(), input.getRuleKey());
    Map<String, String> params = new HashMap<>(input.getParamsByKey());
    return new ActiveRule(key, input.getSeverity().name(), params, input.getCreatedAt());
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) HashMap(java.util.HashMap) ActiveRule(org.sonar.server.computation.task.projectanalysis.qualityprofile.ActiveRule)

Example 9 with RuleKey

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

the class RuleRepositoryImpl method loadRulesFromDb.

private void loadRulesFromDb(DbSession dbSession) {
    ImmutableMap.Builder<RuleKey, Rule> rulesByKeyBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Integer, Rule> rulesByIdBuilder = ImmutableMap.builder();
    for (RuleDto ruleDto : dbClient.ruleDao().selectAll(dbSession)) {
        Rule rule = new RuleImpl(ruleDto);
        rulesByKeyBuilder.put(ruleDto.getKey(), rule);
        rulesByIdBuilder.put(ruleDto.getId(), rule);
    }
    this.rulesByKey = rulesByKeyBuilder.build();
    this.rulesById = rulesByIdBuilder.build();
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDto(org.sonar.db.rule.RuleDto) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 10 with RuleKey

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

the class QProfileBackuper method parseRuleActivations.

private List<RuleActivation> parseRuleActivations(SMInputCursor rulesCursor) throws XMLStreamException {
    List<RuleActivation> activations = Lists.newArrayList();
    Set<RuleKey> activatedKeys = Sets.newHashSet();
    List<RuleKey> duplicatedKeys = Lists.newArrayList();
    while (rulesCursor.getNext() != null) {
        SMInputCursor ruleCursor = rulesCursor.childElementCursor();
        String repositoryKey = null;
        String key = null;
        String severity = null;
        Map<String, String> parameters = Maps.newHashMap();
        while (ruleCursor.getNext() != null) {
            String nodeName = ruleCursor.getLocalName();
            if (StringUtils.equals("repositoryKey", nodeName)) {
                repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals("key", nodeName)) {
                key = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals("priority", nodeName)) {
                severity = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals("parameters", nodeName)) {
                SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
                readParameters(propsCursor, parameters);
            }
        }
        RuleKey ruleKey = RuleKey.of(repositoryKey, key);
        if (activatedKeys.contains(ruleKey)) {
            duplicatedKeys.add(ruleKey);
        }
        activatedKeys.add(ruleKey);
        RuleActivation activation = new RuleActivation(ruleKey);
        activation.setSeverity(severity);
        activation.setParameters(parameters);
        activations.add(activation);
    }
    if (!duplicatedKeys.isEmpty()) {
        throw new IllegalArgumentException("The quality profile cannot be restored as it contains duplicates for the following rules: " + RULE_KEY_JOINER.join(duplicatedKeys));
    }
    return activations;
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) RuleKey(org.sonar.api.rule.RuleKey)

Aggregations

RuleKey (org.sonar.api.rule.RuleKey)95 Test (org.junit.Test)48 RuleDto (org.sonar.db.rule.RuleDto)24 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)22 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)17 SearchOptions (org.sonar.server.es.SearchOptions)14 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)9 ArrayList (java.util.ArrayList)5 DbSession (org.sonar.db.DbSession)5 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ComponentDto (org.sonar.db.component.ComponentDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 Rule (org.sonar.api.batch.rule.Rule)3 WildcardPattern (org.sonar.api.utils.WildcardPattern)3 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)3