Search in sources :

Example 6 with RuleDto

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

the class RegisterRules method processRemainingDbRules.

private List<RuleDto> processRemainingDbRules(Collection<RuleDto> existingRules, DbSession session) {
    // custom rules check status of template, so they must be processed at the end
    List<RuleDto> customRules = newArrayList();
    List<RuleDto> removedRules = newArrayList();
    for (RuleDto rule : existingRules) {
        if (rule.getTemplateId() != null) {
            customRules.add(rule);
        } else if (rule.getStatus() != RuleStatus.REMOVED) {
            removeRule(session, removedRules, rule);
        }
    }
    for (RuleDto customRule : customRules) {
        Integer templateId = customRule.getTemplateId();
        checkNotNull(templateId, "Template id of the custom rule '%s' is null", customRule);
        Optional<RuleDto> template = dbClient.ruleDao().selectById(templateId, session);
        if (template.isPresent() && template.get().getStatus() != RuleStatus.REMOVED) {
            if (updateCustomRuleFromTemplateRule(customRule, template.get())) {
                update(session, customRule);
            }
        } else {
            removeRule(session, removedRules, customRule);
        }
    }
    session.commit();
    return removedRules;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto)

Example 7 with RuleDto

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

the class CompareAction method writeRules.

private void writeRules(JsonWriter json, Map<RuleKey, ActiveRuleDto> activeRules, Map<RuleKey, RuleDto> rulesByKey, Map<String, RuleRepositoryDto> repositoriesByKey) {
    json.beginArray();
    for (Entry<RuleKey, ActiveRuleDto> activeRule : activeRules.entrySet()) {
        RuleKey key = activeRule.getKey();
        ActiveRuleDto value = activeRule.getValue();
        json.beginObject();
        RuleDto rule = rulesByKey.get(key);
        writeRule(json, rule, repositoriesByKey.get(rule.getRepositoryKey()));
        json.prop(ATTRIBUTE_SEVERITY, value.getSeverityString());
        json.endObject();
    }
    json.endArray();
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 8 with RuleDto

use of org.sonar.db.rule.RuleDto 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 9 with RuleDto

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

the class RuleUpdater method updateParameters.

private void updateParameters(DbSession dbSession, RuleUpdate update, Context context) {
    if (update.isChangeParameters() && update.isCustomRule()) {
        RuleDto customRule = context.rule;
        Integer templateId = customRule.getTemplateId();
        checkNotNull(templateId, "Rule '%s' has no persisted template!", customRule);
        Optional<RuleDto> templateRule = dbClient.ruleDao().selectById(templateId, dbSession);
        if (!templateRule.isPresent()) {
            throw new IllegalStateException(String.format("Template %s of rule %s does not exist", customRule.getTemplateId(), customRule.getKey()));
        }
        List<String> paramKeys = newArrayList();
        // Load active rules and its parameters in cache
        Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParamsByActiveRule = getActiveRuleParamsByActiveRule(dbSession, customRule);
        // Browse custom rule parameters to create, update or delete them
        deleteOrUpdateParameters(dbSession, update, customRule, paramKeys, activeRuleParamsByActiveRule);
    }
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 10 with RuleDto

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

the class ClearRulesOverloadedDebt method clearDebt.

private void clearDebt(DbSession session) {
    int countClearedRules = 0;
    for (RuleDto rule : dbClient.ruleDao().selectAll(session)) {
        if (isDebtOverridden(rule)) {
            rule.setRemediationFunction(null);
            rule.setRemediationGapMultiplier(null);
            rule.setRemediationBaseEffort(null);
            rule.setUpdatedAt(system2.now());
            dbClient.ruleDao().update(session, rule);
            countClearedRules++;
        }
    }
    if (countClearedRules > 0) {
        LOG.warn("The SQALE model has been cleaned to remove any redundant data left over from previous migrations.");
        LOG.warn("=> As a result, the technical debt of existing issues in your projects may change slightly when those projects are reanalyzed.");
    }
}
Also used : RuleDto(org.sonar.db.rule.RuleDto)

Aggregations

RuleDto (org.sonar.db.rule.RuleDto)197 Test (org.junit.Test)140 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)80 ComponentDto (org.sonar.db.component.ComponentDto)47 WsTester (org.sonar.server.ws.WsTester)38 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)29 RuleParamDto (org.sonar.db.rule.RuleParamDto)29 IssueDto (org.sonar.db.issue.IssueDto)28 RuleKey (org.sonar.api.rule.RuleKey)24 SearchOptions (org.sonar.server.es.SearchOptions)16 RuleQuery (org.sonar.server.rule.index.RuleQuery)16 BadRequestException (org.sonar.server.exceptions.BadRequestException)15 RuleTesting.newRuleDto (org.sonar.db.rule.RuleTesting.newRuleDto)14 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)12 IssueIndexer (org.sonar.server.issue.index.IssueIndexer)10 Date (java.util.Date)9 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)8 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)8 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)7 ArrayList (java.util.ArrayList)6