Search in sources :

Example 1 with RuleStatus

use of org.sonar.api.rule.RuleStatus in project sonarqube by SonarSource.

the class RuleIndex method buildFilters.

/* Build main filter (match based) */
private static Map<String, QueryBuilder> buildFilters(RuleQuery query) {
    Map<String, QueryBuilder> filters = new HashMap<>();
    /* Add enforced filter on rules that are REMOVED */
    filters.put(FIELD_RULE_STATUS, boolQuery().mustNot(QueryBuilders.termQuery(FIELD_RULE_STATUS, RuleStatus.REMOVED.toString())));
    if (StringUtils.isNotEmpty(query.getInternalKey())) {
        filters.put(FIELD_RULE_INTERNAL_KEY, QueryBuilders.termQuery(FIELD_RULE_INTERNAL_KEY, query.getInternalKey()));
    }
    if (StringUtils.isNotEmpty(query.getRuleKey())) {
        filters.put(FIELD_RULE_RULE_KEY, QueryBuilders.termQuery(FIELD_RULE_RULE_KEY, query.getRuleKey()));
    }
    if (isNotEmpty(query.getLanguages())) {
        filters.put(FIELD_RULE_LANGUAGE, QueryBuilders.termsQuery(FIELD_RULE_LANGUAGE, query.getLanguages()));
    }
    if (isNotEmpty(query.getRepositories())) {
        filters.put(FIELD_RULE_REPOSITORY, QueryBuilders.termsQuery(FIELD_RULE_REPOSITORY, query.getRepositories()));
    }
    if (isNotEmpty(query.getSeverities())) {
        filters.put(FIELD_RULE_SEVERITY, QueryBuilders.termsQuery(FIELD_RULE_SEVERITY, query.getSeverities()));
    }
    if (StringUtils.isNotEmpty(query.getKey())) {
        filters.put(FIELD_RULE_KEY, QueryBuilders.termQuery(FIELD_RULE_KEY, query.getKey()));
    }
    if (isNotEmpty(query.getTags())) {
        filters.put(FIELD_RULE_ALL_TAGS, QueryBuilders.termsQuery(FIELD_RULE_ALL_TAGS, query.getTags()));
    }
    if (isNotEmpty(query.getTypes())) {
        filters.put(FIELD_RULE_TYPE, QueryBuilders.termsQuery(FIELD_RULE_TYPE, query.getTypes()));
    }
    if (query.getAvailableSinceLong() != null) {
        filters.put("availableSince", QueryBuilders.rangeQuery(FIELD_RULE_CREATED_AT).gte(query.getAvailableSinceLong()));
    }
    if (isNotEmpty(query.getStatuses())) {
        Collection<String> stringStatus = new ArrayList<>();
        for (RuleStatus status : query.getStatuses()) {
            stringStatus.add(status.name());
        }
        filters.put(FIELD_RULE_STATUS, QueryBuilders.termsQuery(FIELD_RULE_STATUS, stringStatus));
    }
    Boolean isTemplate = query.isTemplate();
    if (isTemplate != null) {
        filters.put(FIELD_RULE_IS_TEMPLATE, QueryBuilders.termQuery(FIELD_RULE_IS_TEMPLATE, Boolean.toString(isTemplate)));
    }
    String template = query.templateKey();
    if (template != null) {
        filters.put(FIELD_RULE_TEMPLATE_KEY, QueryBuilders.termQuery(FIELD_RULE_TEMPLATE_KEY, template));
    }
    // ActiveRule Filter (profile and inheritance)
    BoolQueryBuilder childrenFilter = boolQuery();
    addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_PROFILE_KEY, query.getQProfileKey());
    addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_INHERITANCE, query.getInheritance());
    addTermFilter(childrenFilter, FIELD_ACTIVE_RULE_SEVERITY, query.getActiveSeverities());
    // ChildQuery
    QueryBuilder childQuery;
    if (childrenFilter.hasClauses()) {
        childQuery = childrenFilter;
    } else {
        childQuery = matchAllQuery();
    }
    /** Implementation of activation query */
    if (Boolean.TRUE.equals(query.getActivation())) {
        filters.put("activation", QueryBuilders.hasChildQuery(INDEX_TYPE_ACTIVE_RULE.getType(), childQuery));
    } else if (Boolean.FALSE.equals(query.getActivation())) {
        filters.put("activation", boolQuery().mustNot(QueryBuilders.hasChildQuery(INDEX_TYPE_ACTIVE_RULE.getType(), childQuery)));
    }
    return filters;
}
Also used : HashMap(java.util.HashMap) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ArrayList(java.util.ArrayList) RuleStatus(org.sonar.api.rule.RuleStatus) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 2 with RuleStatus

use of org.sonar.api.rule.RuleStatus in project sonarqube by SonarSource.

the class RuleUpdater method updateStatus.

private static void updateStatus(RuleUpdate update, Context context) {
    RuleStatus status = update.getStatus();
    if (status == null) {
        throw new IllegalArgumentException("The status is missing");
    }
    context.rule.setStatus(status);
}
Also used : RuleStatus(org.sonar.api.rule.RuleStatus)

Example 3 with RuleStatus

use of org.sonar.api.rule.RuleStatus in project sonarqube by SonarSource.

the class AppAction method addStatuses.

private void addStatuses(JsonWriter json) {
    json.name("statuses").beginObject();
    for (RuleStatus status : RuleStatus.values()) {
        if (status != RuleStatus.REMOVED) {
            json.prop(status.toString(), i18n.message(Locale.getDefault(), "rules.status." + status.toString().toLowerCase(Locale.ENGLISH), status.toString()));
        }
    }
    json.endObject();
}
Also used : RuleStatus(org.sonar.api.rule.RuleStatus)

Example 4 with RuleStatus

use of org.sonar.api.rule.RuleStatus in project sonarqube by SonarSource.

the class RulesDefinitionXmlLoader method processRule.

private static void processRule(RulesDefinition.NewRepository repo, SMInputCursor ruleC) throws XMLStreamException {
    String key = null;
    String name = null;
    String description = null;
    // enum is not used as variable type as we want to raise an exception with the rule key when format is not supported
    String descriptionFormat = DescriptionFormat.HTML.name();
    String internalKey = null;
    String severity = Severity.defaultSeverity();
    String type = null;
    RuleStatus status = RuleStatus.defaultStatus();
    boolean template = false;
    String gapDescription = null;
    String debtRemediationFunction = null;
    String debtRemediationFunctionGapMultiplier = null;
    String debtRemediationFunctionBaseEffort = null;
    List<ParamStruct> params = new ArrayList<>();
    List<String> tags = new ArrayList<>();
    /* BACKWARD COMPATIBILITY WITH VERY OLD FORMAT */
    String keyAttribute = ruleC.getAttrValue("key");
    if (isNotBlank(keyAttribute)) {
        key = trim(keyAttribute);
    }
    String priorityAttribute = ruleC.getAttrValue("priority");
    if (isNotBlank(priorityAttribute)) {
        severity = trim(priorityAttribute);
    }
    SMInputCursor cursor = ruleC.childElementCursor();
    while (cursor.getNext() != null) {
        String nodeName = cursor.getLocalName();
        if (equalsIgnoreCase("name", nodeName)) {
            name = nodeValue(cursor);
        } else if (equalsIgnoreCase("type", nodeName)) {
            type = nodeValue(cursor);
        } else if (equalsIgnoreCase("description", nodeName)) {
            description = nodeValue(cursor);
        } else if (equalsIgnoreCase("descriptionFormat", nodeName)) {
            descriptionFormat = nodeValue(cursor);
        } else if (equalsIgnoreCase("key", nodeName)) {
            key = nodeValue(cursor);
        } else if (equalsIgnoreCase("configKey", nodeName)) {
            // deprecated field, replaced by internalKey
            internalKey = nodeValue(cursor);
        } else if (equalsIgnoreCase("internalKey", nodeName)) {
            internalKey = nodeValue(cursor);
        } else if (equalsIgnoreCase("priority", nodeName) || equalsIgnoreCase("severity", nodeName)) {
            // "priority" is deprecated field and has been replaced by "severity"
            severity = nodeValue(cursor);
        } else if (equalsIgnoreCase("cardinality", nodeName)) {
            template = Cardinality.MULTIPLE == Cardinality.valueOf(nodeValue(cursor));
        } else if (equalsIgnoreCase("gapDescription", nodeName) || equalsIgnoreCase("effortToFixDescription", nodeName)) {
            gapDescription = nodeValue(cursor);
        } else if (equalsIgnoreCase("remediationFunction", nodeName) || equalsIgnoreCase("debtRemediationFunction", nodeName)) {
            debtRemediationFunction = nodeValue(cursor);
        } else if (equalsIgnoreCase("remediationFunctionBaseEffort", nodeName) || equalsIgnoreCase("debtRemediationFunctionOffset", nodeName)) {
            debtRemediationFunctionGapMultiplier = nodeValue(cursor);
        } else if (equalsIgnoreCase("remediationFunctionGapMultiplier", nodeName) || equalsIgnoreCase("debtRemediationFunctionCoefficient", nodeName)) {
            debtRemediationFunctionBaseEffort = nodeValue(cursor);
        } else if (equalsIgnoreCase("status", nodeName)) {
            String s = nodeValue(cursor);
            if (s != null) {
                status = RuleStatus.valueOf(s);
            }
        } else if (equalsIgnoreCase("param", nodeName)) {
            params.add(processParameter(cursor));
        } else if (equalsIgnoreCase("tag", nodeName)) {
            tags.add(nodeValue(cursor));
        }
    }
    try {
        RulesDefinition.NewRule rule = repo.createRule(key).setSeverity(severity).setName(name).setInternalKey(internalKey).setTags(tags.toArray(new String[tags.size()])).setTemplate(template).setStatus(status).setGapDescription(gapDescription);
        if (type != null) {
            rule.setType(RuleType.valueOf(type));
        }
        fillDescription(rule, descriptionFormat, description);
        fillRemediationFunction(rule, debtRemediationFunction, debtRemediationFunctionGapMultiplier, debtRemediationFunctionBaseEffort);
        fillParams(rule, params);
    } catch (Exception e) {
        throw new IllegalStateException(format("Fail to load the rule with key [%s:%s]", repo.key(), key), e);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) ArrayList(java.util.ArrayList) RuleStatus(org.sonar.api.rule.RuleStatus) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Aggregations

RuleStatus (org.sonar.api.rule.RuleStatus)4 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)1 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1