Search in sources :

Example 1 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class QProfileBackuper method writeXml.

private void writeXml(DbSession dbSession, Writer writer, QualityProfileDto profile, Iterator<ActiveRuleDto> activeRules) {
    XmlWriter xml = XmlWriter.of(writer).declaration();
    xml.begin("profile");
    xml.prop("name", profile.getName());
    xml.prop("language", profile.getLanguage());
    xml.begin("rules");
    while (activeRules.hasNext()) {
        ActiveRuleDto activeRule = activeRules.next();
        xml.begin("rule");
        xml.prop("repositoryKey", activeRule.getKey().ruleKey().repository());
        xml.prop("key", activeRule.getKey().ruleKey().rule());
        xml.prop("priority", activeRule.getSeverityString());
        xml.begin("parameters");
        for (ActiveRuleParamDto param : db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId())) {
            xml.begin("parameter").prop("key", param.getKey()).prop("value", param.getValue()).end();
        }
        xml.end("parameters");
        xml.end("rule");
    }
    xml.end("rules").end("profile").close();
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) XmlWriter(org.sonar.api.utils.text.XmlWriter)

Example 2 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class QProfileExporters method wrap.

private RulesProfile wrap(QualityProfileDto profile) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage());
        List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfileKey(dbSession, profile.getKey());
        List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId));
        ListMultimap<Integer, ActiveRuleParamDto> activeRuleParamsByActiveRuleId = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleId);
        for (ActiveRuleDto activeRule : activeRuleDtos) {
            // TODO all rules should be loaded by using one query with all active rule keys as parameter
            Rule rule = ruleFinder.findByKey(activeRule.getKey().ruleKey());
            org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString()));
            List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleId.get(activeRule.getId());
            for (ActiveRuleParamDto activeRuleParamDto : paramDtos) {
                wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue());
            }
        }
        return target;
    }
}
Also used : DbSession(org.sonar.db.DbSession) RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) Rule(org.sonar.api.rules.Rule) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 3 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class ActiveRuleCompleter method completeShow.

void completeShow(DbSession dbSession, RuleDto rule, ShowResponse.Builder response) {
    List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByRuleId(dbSession, rule.getId());
    Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
    for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
        activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
    }
    List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId));
    ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRuleDtos.size(), 10);
    for (ActiveRuleParamDto activeRuleParamDto : activeRuleParamDtos) {
        ActiveRuleKey activeRuleKey = activeRuleIdsByKey.get(activeRuleParamDto.getActiveRuleId());
        activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParamDto);
    }
    for (ActiveRuleDto activeRule : activeRuleDtos) {
        response.addActives(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.getKey())));
    }
}
Also used : HashMap(java.util.HashMap) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 4 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto 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 5 with ActiveRuleParamDto

use of org.sonar.db.qualityprofile.ActiveRuleParamDto in project sonarqube by SonarSource.

the class RuleActivator method doInsert.

private ActiveRuleDto doInsert(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
    ActiveRuleDto activeRule;
    ActiveRuleDao dao = db.activeRuleDao();
    activeRule = ActiveRuleDto.createFor(context.profile(), context.rule());
    String severity = change.getSeverity();
    if (severity != null) {
        activeRule.setSeverity(severity);
    }
    ActiveRule.Inheritance inheritance = change.getInheritance();
    if (inheritance != null) {
        activeRule.setInheritance(inheritance.name());
    }
    activeRule.setUpdatedAt(system2.now());
    activeRule.setCreatedAt(system2.now());
    dao.insert(dbSession, activeRule);
    for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
        if (param.getValue() != null) {
            ActiveRuleParamDto paramDto = ActiveRuleParamDto.createFor(context.ruleParamsByKeys().get(param.getKey()));
            paramDto.setValue(param.getValue());
            dao.insertParam(dbSession, activeRule, paramDto);
        }
    }
    return activeRule;
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Map(java.util.Map)

Aggregations

ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)29 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)26 Test (org.junit.Test)13 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)13 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)9 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)8 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)7 RuleParamDto (org.sonar.db.rule.RuleParamDto)7 RuleDto (org.sonar.db.rule.RuleDto)6 Map (java.util.Map)5 StringReader (java.io.StringReader)4 RuleKey (org.sonar.api.rule.RuleKey)4 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 RuleQuery (org.sonar.server.rule.index.RuleQuery)4 RulesProfile (org.sonar.api.profiles.RulesProfile)3 RuleIndex (org.sonar.server.rule.index.RuleIndex)3 WsTester (org.sonar.server.ws.WsTester)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashMap (java.util.HashMap)2 RuleActivation (org.sonar.server.qualityprofile.RuleActivation)2