use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class SearchAction method doSearch.
protected SearchResult doSearch(DbSession dbSession, RuleQuery query, SearchOptions context) {
SearchIdResult<RuleKey> result = ruleIndex.search(query, context);
List<RuleKey> ruleKeys = result.getIds();
// rule order is managed by ES
Map<RuleKey, RuleDto> rulesByRuleKey = Maps.uniqueIndex(dbClient.ruleDao().selectByKeys(dbSession, ruleKeys), RuleDto::getKey);
List<RuleDto> rules = new ArrayList<>();
for (RuleKey ruleKey : ruleKeys) {
RuleDto rule = rulesByRuleKey.get(ruleKey);
if (rule != null) {
rules.add(rule);
}
}
List<Integer> ruleIds = from(rules).transform(RuleDtoToId.INSTANCE).toList();
List<Integer> templateRuleIds = from(rules).transform(RuleDtoToTemplateId.INSTANCE).filter(Predicates.notNull()).toList();
List<RuleDto> templateRules = dbClient.ruleDao().selectByIds(dbSession, templateRuleIds);
List<RuleParamDto> ruleParamDtos = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, ruleIds);
return new SearchResult().setRules(rules).setRuleParameters(ruleParamDtos).setTemplateRules(templateRules).setFacets(result.getFacets()).setTotal(result.getTotal());
}
use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class DefaultRuleFinder method toRule.
private static org.sonar.api.rules.Rule toRule(RuleDto rule, List<RuleParamDto> params) {
String severity = rule.getSeverityString();
String description = rule.getDescription();
RuleDto.Format descriptionFormat = rule.getDescriptionFormat();
org.sonar.api.rules.Rule apiRule = new org.sonar.api.rules.Rule();
apiRule.setName(rule.getName()).setLanguage(rule.getLanguage()).setKey(rule.getRuleKey()).setConfigKey(rule.getConfigKey()).setIsTemplate(rule.isTemplate()).setCreatedAt(new Date(rule.getCreatedAt())).setUpdatedAt(new Date(rule.getUpdatedAt())).setRepositoryKey(rule.getRepositoryKey()).setSeverity(severity != null ? RulePriority.valueOf(severity) : null).setStatus(rule.getStatus().name()).setTags(rule.getTags().toArray(new String[rule.getTags().size()])).setId(rule.getId());
if (description != null && descriptionFormat != null) {
if (RuleDto.Format.HTML.equals(descriptionFormat)) {
apiRule.setDescription(description);
} else {
apiRule.setDescription(Markdown.convertToHtml(description));
}
}
List<org.sonar.api.rules.RuleParam> apiParams = newArrayList();
for (RuleParamDto param : params) {
apiParams.add(new org.sonar.api.rules.RuleParam(apiRule, param.getName(), param.getDescription(), param.getType()).setDefaultValue(param.getDefaultValue()));
}
apiRule.setParams(apiParams);
return apiRule;
}
use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class DefaultRuleFinder method convertToRuleApi.
private Collection<org.sonar.api.rules.Rule> convertToRuleApi(DbSession dbSession, List<RuleDto> ruleDtos) {
List<org.sonar.api.rules.Rule> rules = new ArrayList<>();
List<RuleKey> ruleKeys = FluentIterable.from(ruleDtos).transform(RuleDtoToKey.INSTANCE).toList();
List<RuleParamDto> ruleParamDtos = ruleDao.selectRuleParamsByRuleKeys(dbSession, ruleKeys);
ImmutableListMultimap<Integer, RuleParamDto> ruleParamByRuleId = FluentIterable.from(ruleParamDtos).index(RuleParamDtoToRuleId.INSTANCE);
for (RuleDto rule : ruleDtos) {
rules.add(toRule(rule, ruleParamByRuleId.get(rule.getId())));
}
return rules;
}
use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class RegisterRules method mergeParams.
private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession session) {
List<RuleParamDto> paramDtos = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey());
Map<String, RuleParamDto> existingParamsByName = Maps.newHashMap();
for (RuleParamDto paramDto : paramDtos) {
RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
if (paramDef == null) {
dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule.getId(), paramDto.getName());
dbClient.ruleDao().deleteRuleParam(session, paramDto.getId());
} else {
if (mergeParam(paramDto, paramDef)) {
dbClient.ruleDao().updateRuleParam(session, rule, paramDto);
}
existingParamsByName.put(paramDto.getName(), paramDto);
}
}
// Create newly parameters
for (RulesDefinition.Param param : ruleDef.params()) {
RuleParamDto paramDto = existingParamsByName.get(param.key());
if (paramDto != null) {
continue;
}
paramDto = RuleParamDto.createFor(rule).setName(param.key()).setDescription(param.description()).setDefaultValue(param.defaultValue()).setType(param.type().toString());
dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
if (StringUtils.isEmpty(param.defaultValue())) {
continue;
}
// Propagate the default value to existing active rule parameters
for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRuleId(session, rule.getId())) {
ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
}
}
}
use of org.sonar.db.rule.RuleParamDto in project sonarqube by SonarSource.
the class CompareActionMediumTest method createRuleWithParam.
private RuleDto createRuleWithParam(String lang, String id) {
RuleDto rule = createRule(lang, id);
RuleParamDto param = RuleParamDto.createFor(rule).setName("param_" + id).setType(RuleParamType.STRING.toString());
db.ruleDao().insertRuleParam(session, rule, param);
return rule;
}
Aggregations