Search in sources :

Example 21 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class SearchAction method doSearch.

protected SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) {
    SearchIdResult<RuleKey> result = ruleIndex.search(query, context);
    List<RuleKey> ruleKeys = result.getIds();
    // rule order is managed by ES
    Map<RuleKey, RuleDto> rulesByRuleKey = Maps.uniqueIndex(dbClient.ruleDao().selectByKeys(dbSession, ruleKeys), RuleDto::getKey);
    List<RuleDto> rules = new ArrayList<>();
    for (RuleKey ruleKey : ruleKeys) {
        RuleDto rule = rulesByRuleKey.get(ruleKey);
        if (rule != null) {
            rules.add(rule);
        }
    }
    List<Integer> ruleIds = from(rules).transform(RuleDtoToId.INSTANCE).toList();
    List<Integer> templateRuleIds = from(rules).transform(RuleDtoToTemplateId.INSTANCE).filter(Predicates.notNull()).toList();
    List<RuleDto> templateRules = dbClient.ruleDao().selectByIds(dbSession, templateRuleIds);
    List<RuleParamDto> ruleParamDtos = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, ruleIds);
    return new SearchResult().setRules(rules).setRuleParameters(ruleParamDtos).setTemplateRules(templateRules).setFacets(result.getFacets()).setTotal(result.getTotal());
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 22 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class DefaultRuleFinder method toRule.

private static org.sonar.api.rules.Rule toRule(RuleDto rule, List<RuleParamDto> params) {
    String severity = rule.getSeverityString();
    String description = rule.getDescription();
    RuleDto.Format descriptionFormat = rule.getDescriptionFormat();
    org.sonar.api.rules.Rule apiRule = new org.sonar.api.rules.Rule();
    apiRule.setName(rule.getName()).setLanguage(rule.getLanguage()).setKey(rule.getRuleKey()).setConfigKey(rule.getConfigKey()).setIsTemplate(rule.isTemplate()).setCreatedAt(new Date(rule.getCreatedAt())).setUpdatedAt(new Date(rule.getUpdatedAt())).setRepositoryKey(rule.getRepositoryKey()).setSeverity(severity != null ? RulePriority.valueOf(severity) : null).setStatus(rule.getStatus().name()).setTags(rule.getTags().toArray(new String[rule.getTags().size()])).setId(rule.getId());
    if (description != null && descriptionFormat != null) {
        if (RuleDto.Format.HTML.equals(descriptionFormat)) {
            apiRule.setDescription(description);
        } else {
            apiRule.setDescription(Markdown.convertToHtml(description));
        }
    }
    List<org.sonar.api.rules.RuleParam> apiParams = newArrayList();
    for (RuleParamDto param : params) {
        apiParams.add(new org.sonar.api.rules.RuleParam(apiRule, param.getName(), param.getDescription(), param.getType()).setDefaultValue(param.getDefaultValue()));
    }
    apiRule.setParams(apiParams);
    return apiRule;
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) Date(java.util.Date) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 23 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class DefaultRuleFinder method convertToRuleApi.

private Collection<org.sonar.api.rules.Rule> convertToRuleApi(DbSession dbSession, List<RuleDto> ruleDtos) {
    List<org.sonar.api.rules.Rule> rules = new ArrayList<>();
    List<RuleKey> ruleKeys = FluentIterable.from(ruleDtos).transform(RuleDtoToKey.INSTANCE).toList();
    List<RuleParamDto> ruleParamDtos = ruleDao.selectRuleParamsByRuleKeys(dbSession, ruleKeys);
    ImmutableListMultimap<Integer, RuleParamDto> ruleParamByRuleId = FluentIterable.from(ruleParamDtos).index(RuleParamDtoToRuleId.INSTANCE);
    for (RuleDto rule : ruleDtos) {
        rules.add(toRule(rule, ruleParamByRuleId.get(rule.getId())));
    }
    return rules;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 24 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class RegisterRules method mergeParams.

private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession session) {
    List<RuleParamDto> paramDtos = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey());
    Map<String, RuleParamDto> existingParamsByName = Maps.newHashMap();
    for (RuleParamDto paramDto : paramDtos) {
        RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
        if (paramDef == null) {
            dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule.getId(), paramDto.getName());
            dbClient.ruleDao().deleteRuleParam(session, paramDto.getId());
        } else {
            if (mergeParam(paramDto, paramDef)) {
                dbClient.ruleDao().updateRuleParam(session, rule, paramDto);
            }
            existingParamsByName.put(paramDto.getName(), paramDto);
        }
    }
    // Create newly parameters
    for (RulesDefinition.Param param : ruleDef.params()) {
        RuleParamDto paramDto = existingParamsByName.get(param.key());
        if (paramDto != null) {
            continue;
        }
        paramDto = RuleParamDto.createFor(rule).setName(param.key()).setDescription(param.description()).setDefaultValue(param.defaultValue()).setType(param.type().toString());
        dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
        if (StringUtils.isEmpty(param.defaultValue())) {
            continue;
        }
        // Propagate the default value to existing active rule parameters
        for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRuleId(session, rule.getId())) {
            ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
            dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
        }
    }
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 25 with RuleParamDto

use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.

the class CompareActionMediumTest method createRuleWithParam.

private RuleDto createRuleWithParam(String lang, String id) {
    RuleDto rule = createRule(lang, id);
    RuleParamDto param = RuleParamDto.createFor(rule).setName("param_" + id).setType(RuleParamType.STRING.toString());
    db.ruleDao().insertRuleParam(session, rule, param);
    return rule;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Aggregations

RuleParamDto (org.sonar.db.rule.RuleParamDto)37 RuleDto (org.sonar.db.rule.RuleDto)29 Test (org.junit.Test)17 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)14 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)12 RuleKey (org.sonar.api.rule.RuleKey)10 ArrayList (java.util.ArrayList)6 WsTester (org.sonar.server.ws.WsTester)6 Date (java.util.Date)5 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)5 SearchOptions (org.sonar.server.es.SearchOptions)4 RuleQuery (org.sonar.server.rule.index.RuleQuery)4 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 DbSession (org.sonar.db.DbSession)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Before (org.junit.Before)1