use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleCreator method createCustomRule.
private RuleKey createCustomRule(NewCustomRule newRule, DbSession dbSession) {
RuleKey templateKey = newRule.templateKey();
if (templateKey == null) {
throw new IllegalArgumentException("Rule template key should not be null");
}
RuleDto templateRule = dbClient.ruleDao().selectOrFailByKey(dbSession, templateKey);
if (!templateRule.isTemplate()) {
throw new IllegalArgumentException("This rule is not a template rule: " + templateKey.toString());
}
validateCustomRule(newRule, dbSession, templateKey);
RuleKey customRuleKey = RuleKey.of(templateRule.getRepositoryKey(), newRule.ruleKey());
Optional<RuleDto> existingRule = loadRule(customRuleKey, dbSession);
if (existingRule.isPresent()) {
updateExistingRule(existingRule.get(), newRule, dbSession);
} else {
createCustomRule(customRuleKey, newRule, templateRule, dbSession);
}
dbSession.commit();
ruleIndexer.index();
return customRuleKey;
}
use of org.sonar.api.rule.RuleKey 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.api.rule.RuleKey in project sonarqube by SonarSource.
the class UpdateAction method readRequest.
private RuleUpdate readRequest(DbSession dbSession, Request request) {
RuleKey key = RuleKey.parse(request.mandatoryParam(PARAM_KEY));
RuleUpdate update = createRuleUpdate(dbSession, key);
readTags(request, update);
readMarkdownNote(request, update);
readDebt(request, update);
String name = request.param(PARAM_NAME);
if (name != null) {
update.setName(name);
}
String description = request.param(PARAM_DESCRIPTION);
if (description != null) {
update.setMarkdownDescription(description);
}
String severity = request.param(PARAM_SEVERITY);
if (severity != null) {
update.setSeverity(severity);
}
String status = request.param(PARAM_STATUS);
if (status != null) {
update.setStatus(RuleStatus.valueOf(status));
}
String params = request.param(PARAMS);
if (params != null) {
update.setParameters(KeyValueFormat.parse(params));
}
return update;
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class DeleteAction method handle.
@Override
public void handle(Request request, Response response) {
ruleWsSupport.checkQProfileAdminPermission();
RuleKey key = RuleKey.parse(request.mandatoryParam(PARAM_KEY));
ruleDeleter.delete(key);
}
use of org.sonar.api.rule.RuleKey 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;
}
Aggregations