Search in sources :

Example 21 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 22 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 23 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)

Example 24 with RuleKey

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

the class QProfileComparison method compareActivationParams.

private void compareActivationParams(DbSession session, ActiveRuleDto leftRule, ActiveRuleDto rightRule, QProfileComparisonResult result) {
    RuleKey key = leftRule.getKey().ruleKey();
    Map<String, String> leftParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleId(session, leftRule.getId()));
    Map<String, String> rightParams = paramDtoToMap(dbClient.activeRuleDao().selectParamsByActiveRuleId(session, rightRule.getId()));
    if (leftParams.equals(rightParams) && leftRule.getSeverityString().equals(rightRule.getSeverityString())) {
        result.same.put(key, leftRule);
    } else {
        ActiveRuleDiff diff = new ActiveRuleDiff();
        diff.leftSeverity = leftRule.getSeverityString();
        diff.rightSeverity = rightRule.getSeverityString();
        diff.paramDifference = Maps.difference(leftParams, rightParams);
        result.modified.put(key, diff);
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey)

Example 25 with RuleKey

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

the class RuleResultSetIterator method read.

@Override
protected RuleDoc read(ResultSet rs) throws SQLException {
    RuleDoc doc = new RuleDoc(Maps.<String, Object>newHashMapWithExpectedSize(16));
    String ruleKey = rs.getString(1);
    String repositoryKey = rs.getString(2);
    RuleKey key = RuleKey.of(repositoryKey, ruleKey);
    // all the fields must be present, even if value is null
    doc.setKey(key.toString());
    doc.setRuleKey(ruleKey);
    doc.setRepository(repositoryKey);
    doc.setName(rs.getString(3));
    String description = rs.getString(4);
    String descriptionFormat = rs.getString(5);
    if (descriptionFormat != null) {
        if (RuleDto.Format.HTML.equals(RuleDto.Format.valueOf(descriptionFormat))) {
            doc.setHtmlDescription(description);
        } else {
            doc.setHtmlDescription(description == null ? null : Markdown.convertToHtml(description));
        }
    }
    doc.setSeverity(SeverityUtil.getSeverityFromOrdinal(rs.getInt(6)));
    doc.setStatus(rs.getString(7));
    doc.setIsTemplate(rs.getBoolean(8));
    doc.setAllTags(Sets.union(stringTagsToSet(rs.getString(9)), stringTagsToSet(rs.getString(10))));
    String templateRuleKey = rs.getString(11);
    String templateRepoKey = rs.getString(12);
    if (templateRepoKey != null && templateRuleKey != null) {
        doc.setTemplateKey(RuleKey.of(templateRepoKey, templateRuleKey).toString());
    } else {
        doc.setTemplateKey(null);
    }
    doc.setInternalKey(rs.getString(13));
    doc.setLanguage(rs.getString(14));
    doc.setType(RuleType.valueOf(rs.getInt(15)));
    doc.setCreatedAt(rs.getLong(16));
    doc.setUpdatedAt(rs.getLong(17));
    return doc;
}
Also used : 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