Search in sources :

Example 6 with RuleActivationContext

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

the class QProfileTreeImpl method removeParent.

private List<ActiveRuleChange> removeParent(DbSession dbSession, QProfileDto profile) {
    List<ActiveRuleChange> changes = new ArrayList<>();
    if (profile.getParentKee() == null) {
        return changes;
    }
    profile.setParentKee(null);
    db.qualityProfileDao().update(dbSession, profile);
    List<OrgActiveRuleDto> activeRules = db.activeRuleDao().selectByProfile(dbSession, profile);
    Collection<String> ruleUuids = activeRules.stream().map(ActiveRuleDto::getRuleUuid).collect(MoreCollectors.toArrayList());
    RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleUuids);
    for (OrgActiveRuleDto activeRule : activeRules) {
        if (ActiveRuleDto.INHERITED.equals(activeRule.getInheritance())) {
            changes.addAll(ruleActivator.deactivate(dbSession, context, activeRule.getRuleUuid(), true));
        } else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
            context.reset(activeRule.getRuleUuid());
            activeRule.setInheritance(null);
            activeRule.setUpdatedAt(system2.now());
            db.activeRuleDao().update(dbSession, activeRule);
            changes.add(new ActiveRuleChange(ActiveRuleChange.Type.UPDATED, activeRule, context.getRule().get()).setInheritance(null));
        }
    }
    qualityProfileChangeEventService.distributeRuleChangeEvent(List.of(profile), changes, profile.getLanguage());
    return changes;
}
Also used : ArrayList(java.util.ArrayList) RuleActivationContext(org.sonar.server.qualityprofile.builtin.RuleActivationContext) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto)

Aggregations

RuleActivationContext (org.sonar.server.qualityprofile.builtin.RuleActivationContext)6 ArrayList (java.util.ArrayList)5 BadRequestException (org.sonar.server.exceptions.BadRequestException)3 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)2 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)2 HashSet (java.util.HashSet)1 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)1