Search in sources :

Example 61 with RuleKey

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;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDto(org.sonar.db.rule.RuleDto)

Example 62 with RuleKey

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());
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Example 63 with RuleKey

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;
}
Also used : RuleUpdate(org.sonar.server.rule.RuleUpdate) RuleKey(org.sonar.api.rule.RuleKey)

Example 64 with RuleKey

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);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey)

Example 65 with RuleKey

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;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) RuleDto(org.sonar.db.rule.RuleDto) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) RuleParamDto(org.sonar.db.rule.RuleParamDto)

Aggregations

RuleKey (org.sonar.api.rule.RuleKey)95 Test (org.junit.Test)48 RuleDto (org.sonar.db.rule.RuleDto)24 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)22 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)17 SearchOptions (org.sonar.server.es.SearchOptions)14 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)9 ArrayList (java.util.ArrayList)5 DbSession (org.sonar.db.DbSession)5 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ComponentDto (org.sonar.db.component.ComponentDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 Rule (org.sonar.api.batch.rule.Rule)3 WildcardPattern (org.sonar.api.utils.WildcardPattern)3 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)3