aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/java/org/sonar/server
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-07-07 21:58:14 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-07-07 22:05:55 +0200
commit4b0f2cd14b2d20d2ed9c214b2110246ede391f2c (patch)
treed9d8f5116bd5f967ce1f4cfc8eabc517a2fa846d /sonar-server/src/main/java/org/sonar/server
parentd25d97de634f9591373e9d07d98f9c8b40757398 (diff)
downloadsonarqube-4b0f2cd14b2d20d2ed9c214b2110246ede391f2c.tar.gz
sonarqube-4b0f2cd14b2d20d2ed9c214b2110246ede391f2c.zip
SONAR-5007 fix bug related to synchronization of rule parameters
* removal of rule parameter must be propagated to active rules * addition of rule parameter with default value must be propagated to active rules
Diffstat (limited to 'sonar-server/src/main/java/org/sonar/server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java11
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java25
2 files changed, 29 insertions, 7 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java
index a79a79a863b..9538f515703 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/db/ActiveRuleDao.java
@@ -227,4 +227,15 @@ public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, Acti
}
return null;
}
+
+ public void deleteParamsByRuleParam(DbSession dbSession, RuleDto rule, String paramKey) {
+ List<ActiveRuleDto> activeRules = findByRule(dbSession, rule);
+ for (ActiveRuleDto activeRule : activeRules) {
+ for (ActiveRuleParamDto activeParam : findParamsByActiveRuleKey(dbSession, activeRule.getKey())) {
+ if (activeParam.getKey().equals(paramKey)) {
+ deleteParam(dbSession, activeRule, activeParam);
+ }
+ }
+ }
+ }
}
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java b/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java
index 6da4f4f4dd7..2fac09447af 100644
--- a/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java
+++ b/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java
@@ -23,6 +23,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@@ -37,6 +38,8 @@ import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.core.persistence.DbSession;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
@@ -290,30 +293,38 @@ public class RegisterRules implements Startable {
private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession session) {
List<RuleParamDto> paramDtos = dbClient.ruleDao().findRuleParamsByRuleKey(session, rule.getKey());
- List<String> existingParamDtoNames = new ArrayList<String>();
+ Map<String, RuleParamDto> existingParamsByName = Maps.newHashMap();
for (RuleParamDto paramDto : paramDtos) {
RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
if (paramDef == null) {
- // TODO cascade on the activeRule upon RuleDeletion
+ dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName());
dbClient.ruleDao().removeRuleParam(session, rule, paramDto);
} else {
- // TODO validate that existing active rules still match constraints
- // TODO store param name
if (mergeParam(paramDto, paramDef)) {
dbClient.ruleDao().updateRuleParam(session, rule, paramDto);
}
- existingParamDtoNames.add(paramDto.getName());
+ existingParamsByName.put(paramDto.getName(), paramDto);
}
}
+
+ // Create newly parameters
for (RulesDefinition.Param param : ruleDef.params()) {
- if (!existingParamDtoNames.contains(param.key())) {
- RuleParamDto paramDto = RuleParamDto.createFor(rule)
+ RuleParamDto paramDto = existingParamsByName.get(param.key());
+ if (paramDto == null) {
+ paramDto = RuleParamDto.createFor(rule)
.setName(param.key())
.setDescription(param.description())
.setDefaultValue(param.defaultValue())
.setType(param.type().toString());
dbClient.ruleDao().addRuleParam(session, rule, paramDto);
+ if (!StringUtils.isEmpty(param.defaultValue())) {
+ // Propagate the default value to existing active rules
+ for (ActiveRuleDto activeRule : dbClient.activeRuleDao().findByRule(session, rule)) {
+ ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
+ dbClient.activeRuleDao().addParam(session, activeRule, activeParam);
+ }
+ }
}
}
}