Search in sources :

Example 1 with ActiveRuleKey

use of org.sonar.db.qualityprofile.ActiveRuleKey in project sonarqube by SonarSource.

the class ActiveRuleCompleter method completeShow.

void completeShow(DbSession dbSession, RuleDto rule, ShowResponse.Builder response) {
    List<ActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByRuleId(dbSession, rule.getId());
    Map<Integer, ActiveRuleKey> activeRuleIdsByKey = new HashMap<>();
    for (ActiveRuleDto activeRuleDto : activeRuleDtos) {
        activeRuleIdsByKey.put(activeRuleDto.getId(), activeRuleDto.getKey());
    }
    List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId));
    ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRuleDtos.size(), 10);
    for (ActiveRuleParamDto activeRuleParamDto : activeRuleParamDtos) {
        ActiveRuleKey activeRuleKey = activeRuleIdsByKey.get(activeRuleParamDto.getActiveRuleId());
        activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParamDto);
    }
    for (ActiveRuleDto activeRule : activeRuleDtos) {
        response.addActives(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.getKey())));
    }
}
Also used : HashMap(java.util.HashMap) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 2 with ActiveRuleKey

use of org.sonar.db.qualityprofile.ActiveRuleKey in project sonarqube by SonarSource.

the class ActiveRuleResultSetIterator method read.

@Override
protected ActiveRuleDoc read(ResultSet rs) throws SQLException {
    RuleKey ruleKey = RuleKey.of(rs.getString(4), rs.getString(3));
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(rs.getString(5), ruleKey);
    ActiveRuleDoc doc = new ActiveRuleDoc(activeRuleKey);
    // all the fields must be present, even if value is null
    doc.setSeverity(SeverityUtil.getSeverityFromOrdinal(rs.getInt(1)));
    String inheritance = rs.getString(2);
    doc.setInheritance(inheritance == null ? ActiveRule.Inheritance.NONE.name() : inheritance);
    doc.setCreatedAt(rs.getLong(6));
    doc.setUpdatedAt(rs.getLong(7));
    return doc;
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 3 with ActiveRuleKey

use of org.sonar.db.qualityprofile.ActiveRuleKey in project sonarqube by SonarSource.

the class RuleActivator method cascadeDeactivation.

private List<ActiveRuleChange> cascadeDeactivation(ActiveRuleKey key, DbSession dbSession, boolean isCascade, boolean force) {
    List<ActiveRuleChange> changes = Lists.newArrayList();
    RuleActivatorContext context = contextFactory.create(key.qProfile(), key.ruleKey(), dbSession);
    ActiveRuleChange change;
    ActiveRuleDto activeRuleDto = context.activeRule();
    if (activeRuleDto == null) {
        return changes;
    }
    checkRequest(force || isCascade || activeRuleDto.getInheritance() == null, "Cannot deactivate inherited rule '%s'", key.ruleKey());
    change = ActiveRuleChange.createFor(ActiveRuleChange.Type.DEACTIVATED, key);
    changes.add(change);
    persist(change, context, dbSession);
    // get all inherited profiles
    List<QualityProfileDto> profiles = db.qualityProfileDao().selectChildren(dbSession, key.qProfile());
    for (QualityProfileDto profile : profiles) {
        ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), key.ruleKey());
        changes.addAll(cascadeDeactivation(activeRuleKey, dbSession, true, force));
    }
    if (!changes.isEmpty()) {
        updateProfileDates(dbSession, context);
    }
    return changes;
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 4 with ActiveRuleKey

use of org.sonar.db.qualityprofile.ActiveRuleKey in project sonarqube by SonarSource.

the class RuleActivator method bulkDeactivate.

BulkChangeResult bulkDeactivate(RuleQuery ruleQuery, String profile) {
    DbSession dbSession = db.openSession(false);
    BulkChangeResult result = new BulkChangeResult();
    try {
        Iterator<RuleKey> rules = ruleIndex.searchAll(ruleQuery);
        while (rules.hasNext()) {
            try {
                RuleKey ruleKey = rules.next();
                ActiveRuleKey key = ActiveRuleKey.of(profile, ruleKey);
                List<ActiveRuleChange> changes = deactivate(dbSession, key);
                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());
        return result;
    } finally {
        dbSession.close();
    }
}
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) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 5 with ActiveRuleKey

use of org.sonar.db.qualityprofile.ActiveRuleKey in project sonarqube by SonarSource.

the class RuleActivatorContextFactory method initActiveRules.

private void initActiveRules(String profileKey, RuleKey ruleKey, RuleActivatorContext context, DbSession session, boolean parent) {
    ActiveRuleKey key = ActiveRuleKey.of(profileKey, ruleKey);
    Optional<ActiveRuleDto> activeRule = db.activeRuleDao().selectByKey(session, key);
    Collection<ActiveRuleParamDto> activeRuleParams = null;
    if (activeRule.isPresent()) {
        activeRuleParams = db.activeRuleDao().selectParamsByActiveRuleId(session, activeRule.get().getId());
    }
    if (parent) {
        context.setParentActiveRule(activeRule.orNull());
        context.setParentActiveRuleParams(activeRuleParams);
    } else {
        context.setActiveRule(activeRule.orNull());
        context.setActiveRuleParams(activeRuleParams);
    }
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Aggregations

ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)30 Test (org.junit.Test)19 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)10 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)9 RuleKey (org.sonar.api.rule.RuleKey)7 HashMap (java.util.HashMap)4 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)4 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 BadRequestException (org.sonar.server.exceptions.BadRequestException)3 RuleQuery (org.sonar.server.rule.index.RuleQuery)3 Multimap (com.google.common.collect.Multimap)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 DbSession (org.sonar.db.DbSession)2