Search in sources :

Example 6 with ActiveRuleDto

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

the class RuleActivator method cascadeDeactivation.

private List<ActiveRuleChange> cascadeDeactivation(ActiveRuleKey key, DbSession dbSession, boolean isCascade, boolean force) {
    List<ActiveRuleChange> changes = Lists.newArrayList();
    RuleActivatorContext context = contextFactory.create(key.qProfile(), key.ruleKey(), dbSession);
    ActiveRuleChange change;
    ActiveRuleDto activeRuleDto = context.activeRule();
    if (activeRuleDto == null) {
        return changes;
    }
    checkRequest(force || isCascade || activeRuleDto.getInheritance() == null, "Cannot deactivate inherited rule '%s'", key.ruleKey());
    change = ActiveRuleChange.createFor(ActiveRuleChange.Type.DEACTIVATED, key);
    changes.add(change);
    persist(change, context, dbSession);
    // get all inherited profiles
    List<QualityProfileDto> profiles = db.qualityProfileDao().selectChildren(dbSession, key.qProfile());
    for (QualityProfileDto profile : profiles) {
        ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), key.ruleKey());
        changes.addAll(cascadeDeactivation(activeRuleKey, dbSession, true, force));
    }
    if (!changes.isEmpty()) {
        updateProfileDates(dbSession, context);
    }
    return changes;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 7 with ActiveRuleDto

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

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

the class RuleActivator method setParent.

public List<ActiveRuleChange> setParent(DbSession dbSession, String profileKey, @Nullable String parentKey) {
    QualityProfileDto profile = db.qualityProfileDao().selectOrFailByKey(dbSession, profileKey);
    List<ActiveRuleChange> changes = new ArrayList<>();
    if (parentKey == null) {
        // unset if parent is defined, else nothing to do
        changes.addAll(removeParent(dbSession, profile));
    } else if (profile.getParentKee() == null || !parentKey.equals(profile.getParentKee())) {
        QualityProfileDto parentProfile = db.qualityProfileDao().selectOrFailByKey(dbSession, parentKey);
        checkRequest(!isDescendant(dbSession, profile, parentProfile), "Descendant profile '%s' can not be selected as parent of '%s'", parentKey, profileKey);
        changes.addAll(removeParent(dbSession, profile));
        // set new parent
        profile.setParentKee(parentKey);
        db.qualityProfileDao().update(dbSession, profile);
        for (ActiveRuleDto parentActiveRule : db.activeRuleDao().selectByProfileKey(dbSession, parentKey)) {
            try {
                RuleActivation activation = new RuleActivation(parentActiveRule.getKey().ruleKey());
                changes.addAll(activate(dbSession, activation, profileKey));
            } catch (BadRequestException e) {
            // for example because rule status is REMOVED
            // TODO return errors
            }
        }
    }
    dbSession.commit();
    activeRuleIndexer.index(changes);
    return changes;
}
Also used : ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) BadRequestException(org.sonar.server.exceptions.BadRequestException) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 9 with ActiveRuleDto

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

the class RuleActivatorContextFactory method initActiveRules.

private void initActiveRules(String profileKey, RuleKey ruleKey, RuleActivatorContext context, DbSession session, boolean parent) {
    ActiveRuleKey key = ActiveRuleKey.of(profileKey, ruleKey);
    Optional<ActiveRuleDto> activeRule = db.activeRuleDao().selectByKey(session, key);
    Collection<ActiveRuleParamDto> activeRuleParams = null;
    if (activeRule.isPresent()) {
        activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleId(session, activeRule.get().getId());
    }
    if (parent) {
        context.setParentActiveRule(activeRule.orNull());
        context.setParentActiveRuleParams(activeRuleParams);
    } else {
        context.setActiveRule(activeRule.orNull());
        context.setActiveRuleParams(activeRuleParams);
    }
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 10 with ActiveRuleDto

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

the class ActiveRuleCompleter method writeActiveRules.

private static Collection<String> writeActiveRules(RuleKey ruleKey, Collection<ActiveRuleDto> activeRules, ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey, Rules.Actives.Builder activesBuilder) {
    Collection<String> qProfileKeys = newHashSet();
    Rules.ActiveList.Builder activeRulesListResponse = Rules.ActiveList.newBuilder();
    for (ActiveRuleDto activeRule : activeRules) {
        activeRulesListResponse.addActiveList(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.getKey())));
        qProfileKeys.add(activeRule.getKey().qProfile());
    }
    activesBuilder.getMutableActives().put(ruleKey.toString(), activeRulesListResponse.build());
    return qProfileKeys;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Aggregations

ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)112 Test (org.junit.Test)51 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)49 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)26 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)21 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)21 RuleDto (org.sonar.db.rule.RuleDto)21 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)19 RuleParamDto (org.sonar.db.rule.RuleParamDto)19 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)18 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)15 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)14 RuleKey (org.sonar.api.rule.RuleKey)12 RuleQuery (org.sonar.server.rule.index.RuleQuery)11 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)10 ArrayList (java.util.ArrayList)9 BuiltInQualityProfilesDefinition (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition)9 NewBuiltInQualityProfile (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile)9 Map (java.util.Map)7 WsTester (org.sonar.server.ws.WsTester)6