Search in sources :

Example 56 with RuleKey

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

the class RuleActivator method bulkActivate.

BulkChangeResult bulkActivate(RuleQuery ruleQuery, String profileKey, @Nullable String severity) {
    DbSession dbSession = db.openSession(false);
    BulkChangeResult result = new BulkChangeResult();
    try {
        Iterator<RuleKey> rules = ruleIndex.searchAll(ruleQuery);
        while (rules.hasNext()) {
            RuleKey ruleKey = rules.next();
            try {
                RuleActivation activation = new RuleActivation(ruleKey);
                activation.setSeverity(severity);
                List<ActiveRuleChange> changes = activate(dbSession, activation, profileKey);
                result.addChanges(changes);
                if (!changes.isEmpty()) {
                    result.incrementSucceeded();
                }
            } catch (BadRequestException e) {
                // other exceptions stop the bulk activation
                result.incrementFailed();
                result.getErrors().addAll(e.errors());
            }
        }
        dbSession.commit();
        activeRuleIndexer.index(result.getChanges());
    } finally {
        dbSession.close();
    }
    return result;
}
Also used : DbSession(org.sonar.db.DbSession) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) BadRequestException(org.sonar.server.exceptions.BadRequestException)

Example 57 with RuleKey

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

the class CompareAction method writeDifferences.

private void writeDifferences(JsonWriter json, Map<RuleKey, ActiveRuleDiff> modified, Map<RuleKey, RuleDto> rulesByKey, Map<String, RuleRepositoryDto> repositoriesByKey) {
    json.beginArray();
    for (Entry<RuleKey, ActiveRuleDiff> diffEntry : modified.entrySet()) {
        RuleKey key = diffEntry.getKey();
        ActiveRuleDiff value = diffEntry.getValue();
        json.beginObject();
        RuleDto rule = rulesByKey.get(key);
        writeRule(json, rule, repositoriesByKey.get(rule.getRepositoryKey()));
        json.name(ATTRIBUTE_LEFT).beginObject();
        json.prop(ATTRIBUTE_SEVERITY, value.leftSeverity());
        json.name(ATTRIBUTE_PARAMS).beginObject();
        for (Entry<String, ValueDifference<String>> valueDiff : value.paramDifference().entriesDiffering().entrySet()) {
            json.prop(valueDiff.getKey(), valueDiff.getValue().leftValue());
        }
        for (Entry<String, String> valueDiff : value.paramDifference().entriesOnlyOnLeft().entrySet()) {
            json.prop(valueDiff.getKey(), valueDiff.getValue());
        }
        // params
        json.endObject();
        // left
        json.endObject();
        json.name(ATTRIBUTE_RIGHT).beginObject();
        json.prop(ATTRIBUTE_SEVERITY, value.rightSeverity());
        json.name(ATTRIBUTE_PARAMS).beginObject();
        for (Entry<String, ValueDifference<String>> valueDiff : value.paramDifference().entriesDiffering().entrySet()) {
            json.prop(valueDiff.getKey(), valueDiff.getValue().rightValue());
        }
        for (Entry<String, String> valueDiff : value.paramDifference().entriesOnlyOnRight().entrySet()) {
            json.prop(valueDiff.getKey(), valueDiff.getValue());
        }
        // params
        json.endObject();
        // right
        json.endObject();
        // rule
        json.endObject();
    }
    json.endArray();
}
Also used : ActiveRuleDiff(org.sonar.server.qualityprofile.QProfileComparison.ActiveRuleDiff) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ValueDifference(com.google.common.collect.MapDifference.ValueDifference)

Example 58 with RuleKey

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

the class CompareAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String leftKey = request.mandatoryParam(PARAM_LEFT_KEY);
    String rightKey = request.mandatoryParam(PARAM_RIGHT_KEY);
    QProfileComparisonResult result = comparator.compare(leftKey, rightKey);
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<RuleDto> referencedRules = dbClient.ruleDao().selectByKeys(dbSession, new ArrayList<>(result.collectRuleKeys()));
        Map<RuleKey, RuleDto> rulesByKey = Maps.uniqueIndex(referencedRules, RuleDtoToRuleKey.INSTANCE);
        Map<String, RuleRepositoryDto> repositoriesByKey = Maps.uniqueIndex(dbClient.ruleRepositoryDao().selectAll(dbSession), RuleRepositoryDto::getKey);
        writeResult(response.newJsonWriter(), result, rulesByKey, repositoriesByKey);
    }
}
Also used : DbSession(org.sonar.db.DbSession) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) RuleRepositoryDto(org.sonar.db.rule.RuleRepositoryDto) QProfileComparisonResult(org.sonar.server.qualityprofile.QProfileComparison.QProfileComparisonResult)

Example 59 with RuleKey

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

the class QProfileComparison method compare.

private void compare(DbSession session, String leftKey, String rightKey, QProfileComparisonResult result) {
    result.left = dbClient.qualityProfileDao().selectByKey(session, leftKey);
    Preconditions.checkArgument(result.left != null, String.format("Could not find left profile '%s'", leftKey));
    result.right = dbClient.qualityProfileDao().selectByKey(session, rightKey);
    Preconditions.checkArgument(result.right != null, String.format("Could not find right profile '%s'", leftKey));
    Map<RuleKey, ActiveRuleDto> leftActiveRulesByRuleKey = loadActiveRules(session, leftKey);
    Map<RuleKey, ActiveRuleDto> rightActiveRulesByRuleKey = loadActiveRules(session, rightKey);
    Set<RuleKey> allRules = Sets.newHashSet();
    allRules.addAll(leftActiveRulesByRuleKey.keySet());
    allRules.addAll(rightActiveRulesByRuleKey.keySet());
    for (RuleKey ruleKey : allRules) {
        if (!leftActiveRulesByRuleKey.containsKey(ruleKey)) {
            result.inRight.put(ruleKey, rightActiveRulesByRuleKey.get(ruleKey));
        } else if (!rightActiveRulesByRuleKey.containsKey(ruleKey)) {
            result.inLeft.put(ruleKey, leftActiveRulesByRuleKey.get(ruleKey));
        } else {
            compareActivationParams(session, leftActiveRulesByRuleKey.get(ruleKey), rightActiveRulesByRuleKey.get(ruleKey), result);
        }
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 60 with RuleKey

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

the class QProfileReset method doReset.

/**
   * @param dbSession
   * @param profile must exist
   */
private BulkChangeResult doReset(DbSession dbSession, QualityProfileDto profile, Collection<RuleActivation> activations) {
    Preconditions.checkNotNull(profile.getId(), "Quality profile must be persisted");
    BulkChangeResult result = new BulkChangeResult(profile);
    Set<RuleKey> ruleToBeDeactivated = Sets.newHashSet();
    // Keep reference to all the activated rules before backup restore
    for (ActiveRuleDto activeRuleDto : db.activeRuleDao().selectByProfileKey(dbSession, profile.getKee())) {
        if (activeRuleDto.getInheritance() == null) {
            // inherited rules can't be deactivated
            ruleToBeDeactivated.add(activeRuleDto.getKey().ruleKey());
        }
    }
    for (RuleActivation activation : activations) {
        try {
            List<ActiveRuleChange> changes = activator.activate(dbSession, activation, profile.getKey());
            ruleToBeDeactivated.remove(activation.getRuleKey());
            result.incrementSucceeded();
            result.addChanges(changes);
        } catch (BadRequestException e) {
            result.incrementFailed();
            result.getErrors().addAll(e.errors());
        }
    }
    List<ActiveRuleChange> changes = new ArrayList<>();
    changes.addAll(result.getChanges());
    for (RuleKey ruleKey : ruleToBeDeactivated) {
        try {
            changes.addAll(activator.deactivate(dbSession, ActiveRuleKey.of(profile.getKee(), ruleKey)));
        } catch (BadRequestException e) {
        // ignore, probably a rule inherited from parent that can't be deactivated
        }
    }
    dbSession.commit();
    activeRuleIndexer.index(changes);
    return result;
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ArrayList(java.util.ArrayList) BadRequestException(org.sonar.server.exceptions.BadRequestException) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

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