Search in sources :

Example 66 with ActiveRuleDto

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

the class RuleActivator method doActivate.

private List<ActiveRuleChange> doActivate(DbSession dbSession, RuleActivation activation, RuleActivatorContext context) {
    context.verifyForActivation();
    List<ActiveRuleChange> changes = Lists.newArrayList();
    ActiveRuleChange change;
    boolean stopPropagation = false;
    ActiveRuleDto activeRule = context.activeRule();
    if (activeRule == null) {
        if (activation.isReset()) {
            // ignore reset when rule is not activated
            return changes;
        }
        // new activation
        change = ActiveRuleChange.createFor(ActiveRuleChange.Type.ACTIVATED, context.activeRuleKey());
        applySeverityAndParamToChange(activation, context, change);
        if (activation.isCascade() || context.isSameAsParent(change)) {
            change.setInheritance(ActiveRule.Inheritance.INHERITED);
        }
    } else {
        // already activated
        if (activation.isCascade() && activeRule.doesOverride()) {
            // propagating to descendants, but child profile already overrides rule -> stop propagation
            return changes;
        }
        change = ActiveRuleChange.createFor(ActiveRuleChange.Type.UPDATED, context.activeRuleKey());
        if (activation.isCascade() && activeRule.getInheritance() == null) {
            // activate on child, then on parent -> mark child as overriding parent
            change.setInheritance(ActiveRule.Inheritance.OVERRIDES);
            change.setSeverity(context.currentSeverity());
            change.setParameters(context.activeRuleParamsAsStringMap());
            stopPropagation = true;
        } else {
            applySeverityAndParamToChange(activation, context, change);
            if (!activation.isCascade() && context.parentActiveRule() != null) {
                // override rule which is already declared on parents
                change.setInheritance(context.isSameAsParent(change) ? ActiveRule.Inheritance.INHERITED : ActiveRule.Inheritance.OVERRIDES);
            }
        }
        if (context.isSame(change)) {
            change = null;
        }
    }
    if (change != null) {
        changes.add(change);
        persist(change, context, dbSession);
    }
    if (!stopPropagation) {
        changes.addAll(cascadeActivation(dbSession, activation, context.profile().getKey()));
    }
    if (!changes.isEmpty()) {
        updateProfileDates(dbSession, context);
    }
    return changes;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 67 with ActiveRuleDto

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

the class RuleActivator method doUpdate.

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

Example 68 with ActiveRuleDto

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

the class RuleActivator method persist.

private ActiveRuleDto persist(ActiveRuleChange change, RuleActivatorContext context, DbSession dbSession) {
    ActiveRuleDto activeRule = null;
    if (change.getType() == ActiveRuleChange.Type.ACTIVATED) {
        activeRule = doInsert(change, context, dbSession);
    } else if (change.getType() == ActiveRuleChange.Type.DEACTIVATED) {
        ActiveRuleDao dao = db.activeRuleDao();
        dao.delete(dbSession, change.getKey());
    } else if (change.getType() == ActiveRuleChange.Type.UPDATED) {
        activeRule = doUpdate(change, context, dbSession);
    }
    db.qProfileChangeDao().insert(dbSession, change.toDto(userSession.getLogin()));
    return activeRule;
}
Also used : ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 69 with ActiveRuleDto

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

the class RuleActivator method deactivate.

/**
   * Deactivate a rule on a Quality profile WITHOUT committing db session, WITHOUT checking permissions, and forcing removal of inherited rules
   */
public List<ActiveRuleChange> deactivate(DbSession dbSession, RuleDto ruleDto) {
    List<ActiveRuleChange> changes = Lists.newArrayList();
    List<ActiveRuleDto> activeRules = db.activeRuleDao().selectByRuleId(dbSession, ruleDto.getId());
    for (ActiveRuleDto activeRule : activeRules) {
        changes.addAll(deactivate(dbSession, activeRule.getKey(), true));
    }
    return changes;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 70 with ActiveRuleDto

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

the class RuleActivator method removeParent.

/**
   * Does not commit
   */
private List<ActiveRuleChange> removeParent(DbSession dbSession, QualityProfileDto profileDto) {
    if (profileDto.getParentKee() != null) {
        List<ActiveRuleChange> changes = new ArrayList<>();
        profileDto.setParentKee(null);
        db.qualityProfileDao().update(dbSession, profileDto);
        for (ActiveRuleDto activeRule : db.activeRuleDao().selectByProfileKey(dbSession, profileDto.getKey())) {
            if (ActiveRuleDto.INHERITED.equals(activeRule.getInheritance())) {
                changes.addAll(deactivate(dbSession, activeRule.getKey(), true));
            } else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
                activeRule.setInheritance(null);
                activeRule.setUpdatedAt(system2.now());
                db.activeRuleDao().update(dbSession, activeRule);
                changes.add(ActiveRuleChange.createFor(ActiveRuleChange.Type.UPDATED, activeRule.getKey()).setInheritance(null));
            }
        }
        return changes;
    }
    return Collections.emptyList();
}
Also used : ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) 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