Search in sources :

Example 11 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 12 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)

Example 13 with RuleKey

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

the class ActiveRuleCompleter method writeActiveRules.

private Collection<String> writeActiveRules(DbSession dbSession, SearchResponse.Builder response, RuleQuery query, List<RuleDto> rules) {
    Collection<String> qProfileKeys = newHashSet();
    Rules.Actives.Builder activesBuilder = response.getActivesBuilder();
    String profileKey = query.getQProfileKey();
    if (profileKey != null) {
        // Load details of active rules on the selected profile
        List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profileKey);
        Map<RuleKey, ActiveRuleDto> activeRuleByRuleKey = from(activeRuleDtos).uniqueIndex(ActiveRuleToRuleKey.INSTANCE);
        ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
        for (RuleDto rule : rules) {
            ActiveRuleDto activeRule = activeRuleByRuleKey.get(rule.getKey());
            if (activeRule != null) {
                qProfileKeys = writeActiveRules(rule.getKey(), singletonList(activeRule), activeRuleParamsByActiveRuleKey, activesBuilder);
            }
        }
    } else {
        // Load details of all active rules
        List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByRuleIds(dbSession, Lists.transform(rules, RuleDto::getId));
        Multimap<RuleKey, ActiveRuleDto> activeRulesByRuleKey = from(activeRuleDtos).index(ActiveRuleToRuleKey.INSTANCE);
        ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = activeRuleDtosToActiveRuleParamDtos(dbSession, activeRuleDtos);
        for (RuleDto rule : rules) {
            qProfileKeys = writeActiveRules(rule.getKey(), activeRulesByRuleKey.get(rule.getKey()), activeRuleParamsByActiveRuleKey, activesBuilder);
        }
    }
    response.setActives(activesBuilder);
    return qProfileKeys;
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 14 with RuleKey

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

the class RegisterRules method start.

@Override
public void start() {
    Profiler profiler = Profiler.create(LOG).startInfo("Register rules");
    DbSession session = dbClient.openSession(false);
    try {
        Map<RuleKey, RuleDto> allRules = loadRules(session);
        RulesDefinition.Context context = defLoader.load();
        for (RulesDefinition.ExtendedRepository repoDef : getRepositories(context)) {
            if (languages.get(repoDef.language()) != null) {
                for (RulesDefinition.Rule ruleDef : repoDef.rules()) {
                    registerRule(ruleDef, allRules, session);
                }
                session.commit();
            }
        }
        List<RuleDto> activeRules = processRemainingDbRules(allRules.values(), session);
        List<ActiveRuleChange> changes = removeActiveRulesOnStillExistingRepositories(session, activeRules, context);
        session.commit();
        persistRepositories(session, context.repositories());
        ruleIndexer.index();
        activeRuleIndexer.index(changes);
        profiler.stopDebug();
    } finally {
        session.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) Profiler(org.sonar.api.utils.log.Profiler) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange)

Example 15 with RuleKey

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

the class ActiveRuleResultSetIterator method read.

@Override
protected ActiveRuleDoc read(ResultSet rs) throws SQLException {
    RuleKey ruleKey = RuleKey.of(rs.getString(4), rs.getString(3));
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(rs.getString(5), ruleKey);
    ActiveRuleDoc doc = new ActiveRuleDoc(activeRuleKey);
    // all the fields must be present, even if value is null
    doc.setSeverity(SeverityUtil.getSeverityFromOrdinal(rs.getInt(1)));
    String inheritance = rs.getString(2);
    doc.setInheritance(inheritance == null ? ActiveRule.Inheritance.NONE.name() : inheritance);
    doc.setCreatedAt(rs.getLong(6));
    doc.setUpdatedAt(rs.getLong(7));
    return doc;
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

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