Search in sources :

Example 1 with RuleParamDto

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

the class ActiveRuleDaoTest method setUp.

@Before
public void setUp() {
    when(system.now()).thenReturn(NOW);
    dbClient.qualityProfileDao().insert(dbSession, profile1);
    dbClient.qualityProfileDao().insert(dbSession, profile2);
    dbClient.ruleDao().insert(dbSession, rule1);
    dbClient.ruleDao().insert(dbSession, rule2);
    dbClient.ruleDao().insert(dbSession, rule3);
    rule1Param1 = new RuleParamDto().setName("param1").setDefaultValue("value1").setType(RuleParamType.STRING.toString());
    dbClient.ruleDao().insertRuleParam(dbSession, rule1, rule1Param1);
    rule1Param2 = new RuleParamDto().setRuleId(rule1.getId()).setName("param2").setDefaultValue("2").setType(RuleParamType.INTEGER.toString());
    dbClient.ruleDao().insertRuleParam(dbSession, rule1, rule1Param2);
    rule2Param1 = new RuleParamDto().setRuleId(rule2.getId()).setName("param1").setDefaultValue("1").setType(RuleParamType.INTEGER.toString());
    dbClient.ruleDao().insertRuleParam(dbSession, rule2, rule2Param1);
    dbSession.commit();
}
Also used : RuleParamDto(org.sonar.db.rule.RuleParamDto) Before(org.junit.Before)

Example 2 with RuleParamDto

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

the class RuleCreator method validateCustomRule.

private void validateCustomRule(NewCustomRule newRule, DbSession dbSession, RuleKey templateKey) {
    List<String> errors = new ArrayList<>();
    validateRuleKey(errors, newRule.ruleKey());
    validateName(errors, newRule);
    validateDescription(errors, newRule);
    String severity = newRule.severity();
    if (Strings.isNullOrEmpty(severity)) {
        errors.add("The severity is missing");
    } else if (!Severity.ALL.contains(severity)) {
        errors.add(format("Severity \"%s\" is invalid", severity));
    }
    if (newRule.status() == null) {
        errors.add("The status is missing");
    }
    for (RuleParamDto ruleParam : dbClient.ruleDao().selectRuleParamsByRuleKey(dbSession, templateKey)) {
        try {
            validateParam(ruleParam, newRule.parameter(ruleParam.getName()));
        } catch (BadRequestException validationError) {
            errors.addAll(validationError.errors());
        }
    }
    checkRequest(errors.isEmpty(), errors);
}
Also used : ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) BadRequestException(org.sonar.server.exceptions.BadRequestException) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 3 with RuleParamDto

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

the class RuleCreator method createCustomRule.

private RuleKey createCustomRule(RuleKey ruleKey, NewCustomRule newRule, RuleDto templateRuleDto, DbSession dbSession) {
    RuleDto ruleDto = RuleDto.createFor(ruleKey).setTemplateId(templateRuleDto.getId()).setConfigKey(templateRuleDto.getConfigKey()).setName(newRule.name()).setDescription(newRule.markdownDescription()).setDescriptionFormat(Format.MARKDOWN).setSeverity(newRule.severity()).setStatus(newRule.status()).setLanguage(templateRuleDto.getLanguage()).setDefaultRemediationFunction(templateRuleDto.getDefaultRemediationFunction()).setDefaultRemediationGapMultiplier(templateRuleDto.getDefaultRemediationGapMultiplier()).setDefaultRemediationBaseEffort(templateRuleDto.getDefaultRemediationBaseEffort()).setGapDescription(templateRuleDto.getGapDescription()).setTags(templateRuleDto.getTags()).setSystemTags(templateRuleDto.getSystemTags()).setType(templateRuleDto.getType()).setCreatedAt(system2.now()).setUpdatedAt(system2.now());
    dbClient.ruleDao().insert(dbSession, ruleDto);
    for (RuleParamDto templateRuleParamDto : dbClient.ruleDao().selectRuleParamsByRuleKey(dbSession, templateRuleDto.getKey())) {
        String customRuleParamValue = Strings.emptyToNull(newRule.parameter(templateRuleParamDto.getName()));
        createCustomRuleParams(customRuleParamValue, ruleDto, templateRuleParamDto, dbSession);
    }
    return ruleKey;
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 4 with RuleParamDto

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

the class RuleCreator method createCustomRuleParams.

private void createCustomRuleParams(@Nullable String paramValue, RuleDto ruleDto, RuleParamDto templateRuleParam, DbSession dbSession) {
    RuleParamDto ruleParamDto = RuleParamDto.createFor(ruleDto).setName(templateRuleParam.getName()).setType(templateRuleParam.getType()).setDescription(templateRuleParam.getDescription()).setDefaultValue(paramValue);
    dbClient.ruleDao().insertRuleParam(dbSession, ruleDto, ruleParamDto);
}
Also used : RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 5 with RuleParamDto

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

the class CreateAction method createResponse.

private Rules.CreateResponse createResponse(DbSession dbSession, RuleKey ruleKey) {
    RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbSession, ruleKey);
    List<RuleDto> templateRules = new ArrayList<>();
    if (rule.getTemplateId() != null) {
        Optional<RuleDto> templateRule = dbClient.ruleDao().selectById(rule.getTemplateId(), dbSession);
        if (templateRule.isPresent()) {
            templateRules.add(templateRule.get());
        }
    }
    List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, singletonList(rule.getId()));
    SearchAction.SearchResult searchResult = new SearchAction.SearchResult().setRules(singletonList(rule)).setRuleParameters(ruleParameters).setTemplateRules(templateRules).setTotal(1L);
    return Rules.CreateResponse.newBuilder().setRule(ruleMapper.toWsRule(rule, searchResult, Collections.<String>emptySet())).build();
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) 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