Search in sources :

Example 1 with ActiveRuleWrapper

use of org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper in project sonarqube by SonarSource.

the class RuleActivator method applySeverityAndParamToChange.

/**
 * Update severity and params
 */
private void applySeverityAndParamToChange(RuleActivation request, RuleActivationContext context, ActiveRuleChange change) {
    RuleWrapper rule = context.getRule();
    ActiveRuleWrapper activeRule = context.getActiveRule();
    ActiveRuleWrapper parentActiveRule = context.getParentActiveRule();
    if (request.isReset()) {
        applySeverityAndParamsWhenResetRequested(change, rule, parentActiveRule);
    } else if (context.getRulesProfile().isBuiltIn()) {
        applySeverityAndParamsWhenBuiltInProfile(request, context, change, rule);
    } else {
        applySeverityAndParamsWhenNonBuiltInProfile(request, context, change, rule, activeRule, parentActiveRule);
    }
}
Also used : ActiveRuleWrapper(org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper) RuleWrapper(org.sonar.server.qualityprofile.builtin.RuleActivationContext.RuleWrapper) ActiveRuleWrapper(org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper)

Example 2 with ActiveRuleWrapper

use of org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper in project sonarqube by SonarSource.

the class RuleActivator method doUpdate.

private ActiveRuleDto doUpdate(ActiveRuleChange change, RuleActivationContext context, DbSession dbSession) {
    ActiveRuleWrapper activeRule = context.getActiveRule();
    if (activeRule == null) {
        return null;
    }
    ActiveRuleDao dao = db.activeRuleDao();
    String severity = change.getSeverity();
    if (severity != null) {
        activeRule.get().setSeverity(severity);
    }
    ActiveRuleInheritance inheritance = change.getInheritance();
    if (inheritance != null) {
        activeRule.get().setInheritance(inheritance.name());
    }
    activeRule.get().setUpdatedAt(system2.now());
    dao.update(dbSession, activeRule.get());
    for (Map.Entry<String, String> param : change.getParameters().entrySet()) {
        ActiveRuleParamDto activeRuleParamDto = activeRule.getParam(param.getKey());
        if (activeRuleParamDto == null) {
            // did not exist
            if (param.getValue() != null) {
                activeRuleParamDto = ActiveRuleParamDto.createFor(context.getRule().getParam(param.getKey()));
                activeRuleParamDto.setValue(param.getValue());
                dao.insertParam(dbSession, activeRule.get(), activeRuleParamDto);
            }
        } else {
            if (param.getValue() != null) {
                activeRuleParamDto.setValue(param.getValue());
                dao.updateParam(dbSession, activeRuleParamDto);
            } else {
                dao.deleteParam(dbSession, activeRuleParamDto);
            }
        }
    }
    return activeRule.get();
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleWrapper(org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper) ActiveRuleInheritance(org.sonar.server.qualityprofile.ActiveRuleInheritance) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) Map(java.util.Map)

Example 3 with ActiveRuleWrapper

use of org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper in project sonarqube by SonarSource.

the class RuleActivator method doDeactivate.

private List<ActiveRuleChange> doDeactivate(DbSession dbSession, RuleActivationContext context, boolean force) {
    List<ActiveRuleChange> changes = new ArrayList<>();
    ActiveRuleWrapper activeRule = context.getActiveRule();
    if (activeRule == null) {
        return changes;
    }
    ActiveRuleChange change;
    checkRequest(force || context.isCascading() || activeRule.get().getInheritance() == null, "Cannot deactivate inherited rule '%s'", context.getRule().get().getKey());
    change = new ActiveRuleChange(ActiveRuleChange.Type.DEACTIVATED, activeRule.get(), context.getRule().get());
    changes.add(change);
    persist(change, context, dbSession);
    // get all inherited profiles (they are not built-in by design)
    context.getChildProfiles().forEach(child -> {
        context.selectChild(child);
        changes.addAll(doDeactivate(dbSession, context, force));
    });
    if (!changes.isEmpty()) {
        updateProfileDates(dbSession, context);
    }
    return changes;
}
Also used : ActiveRuleWrapper(org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) ArrayList(java.util.ArrayList)

Example 4 with ActiveRuleWrapper

use of org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper in project sonarqube by SonarSource.

the class RuleActivator method doActivate.

private List<ActiveRuleChange> doActivate(DbSession dbSession, RuleActivation activation, RuleActivationContext context) {
    RuleDefinitionDto rule = context.getRule().get();
    checkRequest(RuleStatus.REMOVED != rule.getStatus(), "Rule was removed: %s", rule.getKey());
    checkRequest(!rule.isTemplate(), "Rule template can't be activated on a Quality profile: %s", rule.getKey());
    checkRequest(context.getRulesProfile().getLanguage().equals(rule.getLanguage()), "%s rule %s cannot be activated on %s profile %s", rule.getLanguage(), rule.getKey(), context.getRulesProfile().getLanguage(), context.getRulesProfile().getName());
    List<ActiveRuleChange> changes = new ArrayList<>();
    ActiveRuleChange change;
    boolean stopCascading = false;
    ActiveRuleWrapper activeRule = context.getActiveRule();
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(context.getRulesProfile(), rule.getKey());
    if (activeRule == null) {
        if (activation.isReset()) {
            // ignore reset when rule is not activated
            return changes;
        }
        change = handleNewRuleActivation(activation, context, rule, activeRuleKey);
    } else {
        // already activated
        if (context.isCascading() && activeRule.get().doesOverride()) {
            // propagating to descendants, but child profile already overrides rule -> stop propagation
            return changes;
        }
        change = new ActiveRuleChange(ActiveRuleChange.Type.UPDATED, activeRuleKey, rule);
        stopCascading = handleUpdatedRuleActivation(activation, context, change, stopCascading, activeRule);
        if (isSame(change, activeRule)) {
            change = null;
            stopCascading = true;
        }
    }
    if (change != null) {
        changes.add(change);
        persist(change, context, dbSession);
    }
    if (!changes.isEmpty()) {
        updateProfileDates(dbSession, context);
    }
    if (!stopCascading) {
        changes.addAll(propagateActivationToDescendants(dbSession, activation, context));
    }
    return changes;
}
Also used : ActiveRuleWrapper(org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) ArrayList(java.util.ArrayList) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Aggregations

ActiveRuleWrapper (org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper)4 ArrayList (java.util.ArrayList)2 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)2 Map (java.util.Map)1 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)1 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)1 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)1 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)1 ActiveRuleInheritance (org.sonar.server.qualityprofile.ActiveRuleInheritance)1 RuleWrapper (org.sonar.server.qualityprofile.builtin.RuleActivationContext.RuleWrapper)1