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;
}
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);
}
}
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;
}
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);
}
}
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;
}
Aggregations