import org.sonar.core.log.Log;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.preview.PreviewCache;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.ActiveRuleKey;
-import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.core.qualityprofile.db.QualityProfileKey;
+import org.sonar.core.qualityprofile.db.*;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.util.TypeValidations;
import javax.annotation.Nullable;
+
import java.util.Iterator;
import java.util.List;
import java.util.Map;
*/
private void applySeverityAndParamToChange(RuleActivation activation, RuleActivatorContext context, ActiveRuleChange change) {
change.setSeverity(StringUtils.defaultIfEmpty(activation.getSeverity(), context.defaultSeverity()));
+ verifyParametersAreNotSetOnCustomRule(context, activation, change);
for (RuleParamDto ruleParamDto : context.ruleParams()) {
String value = StringUtils.defaultIfEmpty(
activation.getParameters().get(ruleParamDto.getName()),
}
return false;
}
+
+ private void verifyParametersAreNotSetOnCustomRule(RuleActivatorContext context, RuleActivation activation, ActiveRuleChange change){
+ if (!activation.getParameters().isEmpty() && context.rule().getParentId() != null) {
+ throw new IllegalStateException(String.format("Parameters cannot be set when activating the custom rule '%s'", activation.getKey().ruleKey()));
+ }
+ }
+
}
import org.sonar.api.utils.System2;
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.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;
public class RuleUpdater implements ServerComponent {
- private final RuleActivator ruleActivator;
private final DbClient dbClient;
private final System2 system;
- public RuleUpdater(RuleActivator ruleActivator, DbClient dbClient, System2 system) {
- this.ruleActivator = ruleActivator;
+ public RuleUpdater(DbClient dbClient, System2 system) {
this.dbClient = dbClient;
this.system = system;
}
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);
+ for (ActiveRuleParamDto activeRuleParamDto : dbClient.activeRuleDao().findParamsByActiveRuleKey(dbSession, activeRuleDto.getKey())) {
+ String newValue = update.getParameters().get(activeRuleParamDto.getKey());
+ if (!Strings.isNullOrEmpty(newValue)) {
+ activeRuleParamDto.setValue(newValue);
+ dbClient.activeRuleDao().updateParam(dbSession, activeRuleDto, activeRuleParamDto);
+ }
+ }
}
}
}
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.rule.RuleParamType;
-import org.sonar.check.Cardinality;
import org.sonar.core.persistence.DbSession;
-import org.sonar.core.qualityprofile.db.ActiveRuleDto;
-import org.sonar.core.qualityprofile.db.ActiveRuleKey;
-import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
-import org.sonar.core.qualityprofile.db.QualityProfileDto;
-import org.sonar.core.qualityprofile.db.QualityProfileKey;
+import org.sonar.core.qualityprofile.db.*;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.tester.ServerTester;
import javax.annotation.Nullable;
+
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
static final QualityProfileKey XOO_CHILD_PROFILE_KEY = QualityProfileKey.of("P2", "xoo");
static final QualityProfileKey XOO_GRAND_CHILD_PROFILE_KEY = QualityProfileKey.of("P3", "xoo");
static final RuleKey MANUAL_RULE_KEY = RuleKey.of(Rule.MANUAL_REPOSITORY_KEY, "m1");
+ static final RuleKey TEMPLATE_RULE_KEY = RuleKey.of("xoo", "template1");
+ static final RuleKey CUSTOM_RULE_KEY = RuleKey.of(TEMPLATE_RULE_KEY.repository(), "custom1");
static final RuleKey XOO_RULE_1 = RuleKey.of("xoo", "x1");
static final RuleKey XOO_RULE_2 = RuleKey.of("xoo", "x2");
RuleDto xooRule1 = RuleTesting.newDto(XOO_RULE_1)
.setSeverity("MINOR").setLanguage("xoo");
RuleDto xooRule2 = RuleTesting.newDto(XOO_RULE_2).setLanguage("xoo");
- RuleDto xooTemplateRule1 = RuleTesting.newDto(RuleKey.of("xoo", "template1"))
- .setSeverity("MINOR").setLanguage("xoo").setCardinality(Cardinality.MULTIPLE);
+ RuleDto xooTemplateRule1 = RuleTesting.newTemplateRule(TEMPLATE_RULE_KEY)
+ .setSeverity("MINOR").setLanguage("xoo");
RuleDto manualRule = RuleTesting.newDto(MANUAL_RULE_KEY);
db.ruleDao().insert(dbSession, javaRule, xooRule1, xooRule2, xooTemplateRule1, manualRule);
db.ruleDao().addRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("max").setDefaultValue("10").setType(RuleParamType.INTEGER.type()));
db.ruleDao().addRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("min").setType(RuleParamType.INTEGER.type()));
+ db.ruleDao().addRuleParam(dbSession, xooTemplateRule1, RuleParamDto.createFor(xooTemplateRule1)
+ .setName("format").setType(RuleParamType.STRING.type()));
+
+ RuleDto xooCustomRule1 = RuleTesting.newCustomRule(xooTemplateRule1).setRuleKey(CUSTOM_RULE_KEY.rule())
+ .setSeverity("MINOR").setLanguage("xoo");
+ db.ruleDao().insert(dbSession, xooCustomRule1);
+ db.ruleDao().addRuleParam(dbSession, xooTemplateRule1, RuleParamDto.createFor(xooTemplateRule1)
+ .setName("format").setDefaultValue("txt").setType(RuleParamType.STRING.type()));
// create pre-defined profile
db.qualityProfileDao().insert(dbSession, QualityProfileDto.createFor(XOO_PROFILE_KEY));
@Test
public void fail_to_activate_if_template() throws Exception {
- RuleActivation activation = new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_KEY, RuleKey.of("xoo", "template1")));
+ RuleActivation activation = new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_KEY, TEMPLATE_RULE_KEY));
try {
ruleActivator.activate(activation);
}
}
+ @Test
+ public void fail_to_activate_if_custom_rule_template_and_parameters_are_set() throws Exception {
+ RuleActivation activation = new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_KEY, CUSTOM_RULE_KEY))
+ .setParameter("format", "xls");
+
+ try {
+ ruleActivator.activate(activation);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Parameters cannot be set when activating the custom rule 'xoo:custom1'");
+ verifyZeroActiveRules(XOO_PROFILE_KEY);
+ }
+ }
+
@Test
public void deactivate() throws Exception {
// activation