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