use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class RuleCreatorMediumTest method create_custom_rule_with_no_parameter_value.
@Test
public void create_custom_rule_with_no_parameter_value() {
// insert template rule
RuleDto templateRule = createTemplateRuleWithIntArrayParam();
NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY);
RuleKey customRuleKey = creator.create(newRule);
dbSession.clearCache();
List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
assertThat(params).hasSize(1);
RuleParamDto param = params.get(0);
assertThat(param.getName()).isEqualTo("myIntegers");
assertThat(param.getDescription()).isEqualTo("My Integers");
assertThat(param.getType()).isEqualTo("INTEGER,multiple=true,values=1;2;3");
assertThat(param.getDefaultValue()).isNull();
}
use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class ShowActionMediumTest method show_rule.
@Test
public void show_rule() throws Exception {
RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("java", "S001")).setName("Rule S001").setDescription("Rule S001 <b>description</b>").setDescriptionFormat(Format.HTML).setSeverity(MINOR).setStatus(RuleStatus.BETA).setConfigKey("InternalKeyS001").setLanguage("xoo").setTags(newHashSet("tag1", "tag2")).setSystemTags(newHashSet("systag1", "systag2")).setType(RuleType.BUG);
ruleDao.insert(session, ruleDto);
RuleParamDto param = RuleParamDto.createFor(ruleDto).setName("regex").setType("STRING").setDescription("Reg *exp*").setDefaultValue(".*");
ruleDao.insertRuleParam(session, ruleDto, param);
session.commit();
session.clearCache();
WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "show").setParam("key", ruleDto.getKey().toString());
request.execute().assertJson(getClass(), "show_rule.json");
}
use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class QProfileReset method resetLanguage.
/**
* Reset built-in profiles for the given language. Missing profiles are created and
* existing ones are updated.
*/
public void resetLanguage(String language) {
DbSession dbSession = db.openSession(false);
try {
ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language);
for (Map.Entry<QProfileName, Collection<RulesProfile>> entry : profilesByName.asMap().entrySet()) {
QProfileName profileName = entry.getKey();
QualityProfileDto profile = factory.getOrCreate(dbSession, profileName);
List<RuleActivation> activations = Lists.newArrayList();
for (RulesProfile def : entry.getValue()) {
for (ActiveRule activeRule : def.getActiveRules()) {
RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey()));
activation.setSeverity(activeRule.getSeverity().name());
if (!activeRule.getActiveRuleParams().isEmpty()) {
for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
activation.setParameter(param.getParamKey(), param.getValue());
}
} else {
for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
activation.setParameter(param.getName(), param.getDefaultValue());
}
}
activations.add(activation);
}
}
doReset(dbSession, profile, activations);
}
} finally {
dbSession.close();
}
}
use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class RuleActivator method applySeverityAndParamToChange.
/**
* Severity and parameter values are :
* 1. defined by end-user
* 2. else inherited from parent profile
* 3. else defined by rule defaults
* <p/>
* On custom rules, it's always rule parameters that are used
*/
private void applySeverityAndParamToChange(RuleActivation request, RuleActivatorContext context, ActiveRuleChange change) {
if (request.isReset()) {
// load severity and params from parent profile, else from default values
change.setSeverity(firstNonNull(context.parentSeverity(), context.defaultSeverity()));
for (RuleParamDto ruleParamDto : context.ruleParams()) {
String paramKey = ruleParamDto.getName();
change.setParameter(paramKey, validateParam(ruleParamDto, firstNonNull(context.parentParamValue(paramKey), context.defaultParamValue(paramKey))));
}
} else if (context.activeRule() != null) {
// already activated -> load severity and parameters from request, else keep existing ones, else from parent,
// else from default
change.setSeverity(firstNonNull(request.getSeverity(), context.currentSeverity(), context.parentSeverity(), context.defaultSeverity()));
for (RuleParamDto ruleParamDto : context.ruleParams()) {
String paramKey = ruleParamDto.getName();
String paramValue = context.hasRequestParamValue(request, paramKey) ? // If the request contains the parameter then we're using either value from request, or parent value, or default value
firstNonNull(context.requestParamValue(request, paramKey), context.parentParamValue(paramKey), context.defaultParamValue(paramKey)) : // If the request doesn't contain the parameter, then we're using either value in DB, or parent value, or default value
firstNonNull(context.currentParamValue(paramKey), context.parentParamValue(paramKey), context.defaultParamValue(paramKey));
change.setParameter(paramKey, validateParam(ruleParamDto, paramValue));
}
} else if (context.activeRule() == null) {
// not activated -> load severity and parameters from request, else from parent, else from defaults
change.setSeverity(firstNonNull(request.getSeverity(), context.parentSeverity(), context.defaultSeverity()));
for (RuleParamDto ruleParamDto : context.ruleParams()) {
String paramKey = ruleParamDto.getName();
change.setParameter(paramKey, validateParam(ruleParamDto, firstNonNull(context.requestParamValue(request, paramKey), context.parentParamValue(paramKey), context.defaultParamValue(paramKey))));
}
}
}
use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class RuleUpdater method deleteOrUpdateParameters.
private void deleteOrUpdateParameters(DbSession dbSession, RuleUpdate update, RuleDto customRule, List<String> paramKeys, Multimap<ActiveRuleDto, ActiveRuleParamDto> activeRuleParamsByActiveRule) {
for (RuleParamDto ruleParamDto : dbClient.ruleDao().selectRuleParamsByRuleKey(dbSession, update.getRuleKey())) {
String key = ruleParamDto.getName();
String value = Strings.emptyToNull(update.parameter(key));
// Update rule param
ruleParamDto.setDefaultValue(value);
dbClient.ruleDao().updateRuleParam(dbSession, customRule, ruleParamDto);
if (value != null) {
// Update linked active rule params or create new one
updateOrInsertActiveRuleParams(dbSession, ruleParamDto, activeRuleParamsByActiveRule);
} else {
// Delete linked active rule params
deleteActiveRuleParams(dbSession, key, activeRuleParamsByActiveRule.values());
}
paramKeys.add(key);
}
}
Aggregations