diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-06-10 13:38:53 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-06-10 13:38:53 +0200 |
commit | c83044b1896c686b1d8283877c0414eb20238efb (patch) | |
tree | 691e7682f75b1ec739563d6e454d568441da3d46 /sonar-server/src/main | |
parent | ccc95a3673bfe28a048e5a3353d9e0e80a0fd12b (diff) | |
download | sonarqube-c83044b1896c686b1d8283877c0414eb20238efb.tar.gz sonarqube-c83044b1896c686b1d8283877c0414eb20238efb.zip |
SONAR-5362 When updating custom rule parameters, every parameters from active rules linked on the custom rule should be updated
Diffstat (limited to 'sonar-server/src/main')
4 files changed, 39 insertions, 7 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java index 422deba4482..ebc507aa31f 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java @@ -97,7 +97,7 @@ public class RuleActivator implements ServerComponent { } } - List<ActiveRuleChange> activate(DbSession dbSession, RuleActivation activation) { + public List<ActiveRuleChange> activate(DbSession dbSession, RuleActivation activation) { RuleActivatorContext context = contextFactory.create(activation.getKey(), dbSession); context.verifyForActivation(); List<ActiveRuleChange> changes = Lists.newArrayList(); @@ -255,7 +255,10 @@ public class RuleActivator implements ServerComponent { } } - private List<ActiveRuleChange> deactivate(DbSession dbSession, RuleDto ruleDto) { + /** + * 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().findByRule(dbSession, ruleDto); for (ActiveRuleDto activeRule : activeRules) { @@ -268,7 +271,7 @@ public class RuleActivator implements ServerComponent { /** * @param force if true then inherited rules are deactivated */ - private List<ActiveRuleChange> deactivate(DbSession dbSession, ActiveRuleKey key, boolean force) { + public List<ActiveRuleChange> deactivate(DbSession dbSession, ActiveRuleKey key, boolean force) { return cascadeDeactivation(key, dbSession, false, force); } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java index b103076d8c1..398ad66e6df 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java @@ -45,7 +45,7 @@ public class RuleDeleter implements ServerComponent { if (rule.getParentId() == null) { throw new IllegalStateException("Only custom rules can be deleted"); } - ruleActivator.deactivate(rule); + ruleActivator.deactivate(dbSession, rule); rule.setStatus(RuleStatus.REMOVED); dbClient.ruleDao().update(dbSession, rule); diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdate.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdate.java index cf6bddc1d2a..584053fa04f 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdate.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdate.java @@ -179,6 +179,10 @@ public class RuleUpdate { return parameters.get(paramKey); } + boolean isCustomRule() { + return isCustomRule; + } + public boolean isChangeTags() { return changeTags; } @@ -216,8 +220,11 @@ public class RuleUpdate { } public boolean isEmpty() { - return !changeMarkdownNote && !changeTags && !changeDebtSubCharacteristic && !changeDebtRemediationFunction && - !changeName && !changeDescription && !changeSeverity && !changeStatus && !changeParameters; + return !changeMarkdownNote && !changeTags && !changeDebtSubCharacteristic && !changeDebtRemediationFunction && isCustomRuleFieldsEmpty(); + } + + private boolean isCustomRuleFieldsEmpty(){ + return !changeName && !changeDescription && !changeSeverity && !changeStatus && !changeParameters; } private void checkCustomRule(){ diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java index 80dac2aa2e8..2087f840c6e 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java @@ -29,10 +29,13 @@ import org.sonar.api.rule.Severity; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.utils.System2; import org.sonar.core.persistence.DbSession; +import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.technicaldebt.db.CharacteristicDto; import org.sonar.server.db.DbClient; +import org.sonar.server.qualityprofile.RuleActivation; +import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.user.UserSession; import java.util.Collections; @@ -44,10 +47,12 @@ import static com.google.common.collect.Lists.newArrayList; public class RuleUpdater implements ServerComponent { + private final RuleActivator ruleActivator; private final DbClient dbClient; private final System2 system; - public RuleUpdater(DbClient dbClient, System2 system) { + public RuleUpdater(RuleActivator ruleActivator, DbClient dbClient, System2 system) { + this.ruleActivator = ruleActivator; this.dbClient = dbClient; this.system = system; } @@ -66,6 +71,8 @@ public class RuleUpdater implements ServerComponent { for (RuleParamDto ruleParamDto : context.parameters) { dbClient.ruleDao().updateRuleParam(dbSession, context.rule, ruleParamDto); } + // update related active rules (for custom rules only) + updateActiveRule(dbSession, update, context.rule); dbSession.commit(); return true; @@ -74,6 +81,17 @@ public class RuleUpdater implements ServerComponent { } } + private void updateActiveRule(DbSession dbSession, RuleUpdate update, RuleDto rule) { + if (update.isCustomRule() && update.isChangeParameters()) { + for (ActiveRuleDto activeRuleDto : dbClient.activeRuleDao().findByRule(dbSession, rule)) { + RuleActivation ruleActivation = new RuleActivation(activeRuleDto.getKey()) + .setSeverity(activeRuleDto.getSeverityString()) + .setParameters(update.getParameters()); + ruleActivator.activate(dbSession, ruleActivation); + } + } + } + /** * Load all the DTOs required for validating changes and updating rule */ @@ -176,12 +194,16 @@ public class RuleUpdater implements ServerComponent { } } + /** + * Only update existing parameters, ignore the ones that are not existing in the list + */ private void updateParameters(RuleUpdate update, Context context) { for (RuleParamDto ruleParamDto : context.parameters) { String value = update.parameter(ruleParamDto.getName()); if (!Strings.isNullOrEmpty(value)) { ruleParamDto.setDefaultValue(value); } + // Ignore parameter not existing in update.getParameters() } } |