Search in sources :

Example 71 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto 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 72 with ActiveRuleDto

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

the class QProfileFactory method doDelete.

private List<ActiveRuleChange> doDelete(DbSession session, QualityProfileDto profile) {
    db.qualityProfileDao().deleteAllProjectProfileAssociation(profile.getKey(), session);
    List<ActiveRuleChange> changes = new ArrayList<>();
    for (ActiveRuleDto activeRule : db.activeRuleDao().selectByProfileKey(session, profile.getKey())) {
        db.activeRuleDao().delete(session, activeRule.getKey());
        changes.add(ActiveRuleChange.createFor(DEACTIVATED, activeRule.getKey()));
    }
    db.qualityProfileDao().delete(session, profile.getId());
    return changes;
}
Also used : ArrayList(java.util.ArrayList) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 73 with ActiveRuleDto

use of org.sonar.db.qualityprofile.ActiveRuleDto 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)

Example 74 with ActiveRuleDto

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

the class RegisterRules method mergeParams.

private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession session) {
    List<RuleParamDto> paramDtos = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey());
    Map<String, RuleParamDto> existingParamsByName = Maps.newHashMap();
    for (RuleParamDto paramDto : paramDtos) {
        RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
        if (paramDef == null) {
            dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule.getId(), paramDto.getName());
            dbClient.ruleDao().deleteRuleParam(session, paramDto.getId());
        } else {
            if (mergeParam(paramDto, paramDef)) {
                dbClient.ruleDao().updateRuleParam(session, rule, paramDto);
            }
            existingParamsByName.put(paramDto.getName(), paramDto);
        }
    }
    // Create newly parameters
    for (RulesDefinition.Param param : ruleDef.params()) {
        RuleParamDto paramDto = existingParamsByName.get(param.key());
        if (paramDto != null) {
            continue;
        }
        paramDto = RuleParamDto.createFor(rule).setName(param.key()).setDescription(param.description()).setDefaultValue(param.defaultValue()).setType(param.type().toString());
        dbClient.ruleDao().insertRuleParam(session, rule, paramDto);
        if (StringUtils.isEmpty(param.defaultValue())) {
            continue;
        }
        // Propagate the default value to existing active rule parameters
        for (ActiveRuleDto activeRule : dbClient.activeRuleDao().selectByRuleId(session, rule.getId())) {
            ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
            dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
        }
    }
}
Also used : RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 75 with ActiveRuleDto

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

the class ActiveRuleCompleter method activeRuleDtosToActiveRuleParamDtos.

private ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleDtosToActiveRuleParamDtos(DbSession dbSession, List<ActiveRuleDto> activeRuleDtos) {
    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);
    }
    return activeRuleParamsByActiveRuleKey;
}
Also used : HashMap(java.util.HashMap) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Aggregations

ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)112 Test (org.junit.Test)51 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)49 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)26 OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)21 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)21 RuleDto (org.sonar.db.rule.RuleDto)21 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)19 RuleParamDto (org.sonar.db.rule.RuleParamDto)19 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)18 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)15 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)14 RuleKey (org.sonar.api.rule.RuleKey)12 RuleQuery (org.sonar.server.rule.index.RuleQuery)11 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)10 ArrayList (java.util.ArrayList)9 BuiltInQualityProfilesDefinition (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition)9 NewBuiltInQualityProfile (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile)9 Map (java.util.Map)7 WsTester (org.sonar.server.ws.WsTester)6