Search in sources :

Example 1 with OrgActiveRuleDto

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

the class QProfileComparison method compare.

public QProfileComparisonResult compare(DbSession dbSession, QProfileDto left, QProfileDto right) {
    Map<RuleKey, OrgActiveRuleDto> leftActiveRulesByRuleKey = loadActiveRules(dbSession, left);
    Map<RuleKey, OrgActiveRuleDto> rightActiveRulesByRuleKey = loadActiveRules(dbSession, right);
    Set<RuleKey> allRules = new HashSet<>();
    allRules.addAll(leftActiveRulesByRuleKey.keySet());
    allRules.addAll(rightActiveRulesByRuleKey.keySet());
    QProfileComparisonResult result = new QProfileComparisonResult(left, right);
    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(dbSession, leftActiveRulesByRuleKey.get(ruleKey), rightActiveRulesByRuleKey.get(ruleKey), result);
        }
    }
    return result;
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) HashSet(java.util.HashSet)

Example 2 with OrgActiveRuleDto

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

the class QProfileTreeImpl method setParent.

private List<ActiveRuleChange> setParent(DbSession dbSession, QProfileDto profile, QProfileDto parent) {
    checkRequest(parent.getLanguage().equals(profile.getLanguage()), "Cannot set the profile '%s' as the parent of profile '%s' since their languages differ ('%s' != '%s')", parent.getKee(), profile.getKee(), parent.getLanguage(), profile.getLanguage());
    List<ActiveRuleChange> changes = new ArrayList<>();
    if (parent.getKee().equals(profile.getParentKee())) {
        return changes;
    }
    checkRequest(!isDescendant(dbSession, profile, parent), "Descendant profile '%s' can not be selected as parent of '%s'", parent.getKee(), profile.getKee());
    changes.addAll(removeParent(dbSession, profile));
    // set new parent
    profile.setParentKee(parent.getKee());
    db.qualityProfileDao().update(dbSession, profile);
    List<OrgActiveRuleDto> parentActiveRules = db.activeRuleDao().selectByProfile(dbSession, parent);
    Collection<String> ruleUuids = parentActiveRules.stream().map(ActiveRuleDto::getRuleUuid).collect(MoreCollectors.toArrayList());
    RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleUuids);
    for (ActiveRuleDto parentActiveRule : parentActiveRules) {
        try {
            RuleActivation activation = RuleActivation.create(parentActiveRule.getRuleUuid(), null, null);
            changes.addAll(ruleActivator.activate(dbSession, activation, context));
        } catch (BadRequestException e) {
        // for example because rule status is REMOVED
        // TODO return errors
        }
    }
    qualityProfileChangeEventService.distributeRuleChangeEvent(List.of(profile), changes, profile.getLanguage());
    return changes;
}
Also used : ArrayList(java.util.ArrayList) RuleActivationContext(org.sonar.server.qualityprofile.builtin.RuleActivationContext) BadRequestException(org.sonar.server.exceptions.BadRequestException) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto)

Example 3 with OrgActiveRuleDto

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

the class ActiveRuleCompleter method completeShow.

List<Rules.Active> completeShow(DbSession dbSession, RuleDefinitionDto rule) {
    List<OrgActiveRuleDto> activeRules = dbClient.activeRuleDao().selectByOrgRuleUuid(dbSession, rule.getUuid());
    Map<String, ActiveRuleKey> activeRuleUuidsByKey = new HashMap<>();
    for (OrgActiveRuleDto activeRuleDto : activeRules) {
        activeRuleUuidsByKey.put(activeRuleDto.getUuid(), activeRuleDto.getKey());
    }
    List<String> activeRuleUuids = activeRules.stream().map(ActiveRuleDto::getUuid).collect(Collectors.toList());
    List<ActiveRuleParamDto> activeRuleParams = dbClient.activeRuleDao().selectParamsByActiveRuleUuids(dbSession, activeRuleUuids);
    ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = ArrayListMultimap.create(activeRules.size(), 10);
    for (ActiveRuleParamDto activeRuleParamDto : activeRuleParams) {
        ActiveRuleKey activeRuleKey = activeRuleUuidsByKey.get(activeRuleParamDto.getActiveRuleUuid());
        activeRuleParamsByActiveRuleKey.put(activeRuleKey, activeRuleParamDto);
    }
    return activeRules.stream().map(activeRule -> buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.getKey()))).collect(Collectors.toList());
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) StringUtils(org.apache.commons.lang.StringUtils) SearchResponse(org.sonarqube.ws.Rules.SearchResponse) ListMultimap(com.google.common.collect.ListMultimap) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) HashSet(java.util.HashSet) Rules(org.sonarqube.ws.Rules) Languages(org.sonar.api.resources.Languages) Lists(com.google.common.collect.Lists) DateUtils(org.sonar.api.utils.DateUtils) Map(java.util.Map) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Language(org.sonar.api.resources.Language) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) Set(java.util.Set) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) Collectors(java.util.stream.Collectors) DbClient(org.sonar.db.DbClient) List(java.util.List) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleDto(org.sonar.db.rule.RuleDto) ServerSide(org.sonar.api.server.ServerSide) RuleKey(org.sonar.api.rule.RuleKey) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) ActiveRuleInheritance(org.sonar.server.qualityprofile.ActiveRuleInheritance) HashMap(java.util.HashMap) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey)

Example 4 with OrgActiveRuleDto

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

the class ActiveRuleCompleter method writeActiveRules.

private Set<String> writeActiveRules(DbSession dbSession, SearchResponse.Builder response, RuleQuery query, List<RuleDto> rules) {
    final Set<String> profileUuids = new HashSet<>();
    Rules.Actives.Builder activesBuilder = response.getActivesBuilder();
    QProfileDto profile = query.getQProfile();
    if (profile != null) {
        // Load details of active rules on the selected profile
        List<OrgActiveRuleDto> activeRules = dbClient.activeRuleDao().selectByProfile(dbSession, profile);
        Map<RuleKey, OrgActiveRuleDto> activeRuleByRuleKey = activeRules.stream().collect(uniqueIndex(ActiveRuleDto::getRuleKey));
        ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = loadParams(dbSession, activeRules);
        for (RuleDto rule : rules) {
            OrgActiveRuleDto activeRule = activeRuleByRuleKey.get(rule.getKey());
            if (activeRule != null) {
                profileUuids.addAll(writeActiveRules(rule.getKey(), singletonList(activeRule), activeRuleParamsByActiveRuleKey, activesBuilder));
            }
        }
    } else {
        // Load details of all active rules
        List<String> ruleUuids = Lists.transform(rules, RuleDto::getUuid);
        List<OrgActiveRuleDto> activeRules = dbClient.activeRuleDao().selectByRuleUuids(dbSession, ruleUuids);
        Multimap<RuleKey, OrgActiveRuleDto> activeRulesByRuleKey = activeRules.stream().collect(MoreCollectors.index(OrgActiveRuleDto::getRuleKey));
        ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey = loadParams(dbSession, activeRules);
        rules.forEach(rule -> profileUuids.addAll(writeActiveRules(rule.getKey(), activeRulesByRuleKey.get(rule.getKey()), activeRuleParamsByActiveRuleKey, activesBuilder)));
    }
    response.setActives(activesBuilder);
    return profileUuids;
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) HashSet(java.util.HashSet)

Example 5 with OrgActiveRuleDto

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

the class ActiveRuleCompleter method writeActiveRules.

private static Set<String> writeActiveRules(RuleKey ruleKey, Collection<OrgActiveRuleDto> activeRules, ListMultimap<ActiveRuleKey, ActiveRuleParamDto> activeRuleParamsByActiveRuleKey, Rules.Actives.Builder activesBuilder) {
    final Set<String> profileUuids = new HashSet<>();
    Rules.ActiveList.Builder activeRulesListResponse = Rules.ActiveList.newBuilder();
    for (OrgActiveRuleDto activeRule : activeRules) {
        activeRulesListResponse.addActiveList(buildActiveRuleResponse(activeRule, activeRuleParamsByActiveRuleKey.get(activeRule.getKey())));
        profileUuids.add(activeRule.getOrgProfileUuid());
    }
    activesBuilder.getMutableActives().put(ruleKey.toString(), activeRulesListResponse.build());
    return profileUuids;
}
Also used : OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) HashSet(java.util.HashSet)

Aggregations

OrgActiveRuleDto (org.sonar.db.qualityprofile.OrgActiveRuleDto)18 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)10 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)6 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)5 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)5 RuleQuery (org.sonar.server.rule.index.RuleQuery)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 SearchOptions (org.sonar.server.es.SearchOptions)4 RuleKey (org.sonar.api.rule.RuleKey)3 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)3 RuleDto (org.sonar.db.rule.RuleDto)3 HashMap (java.util.HashMap)2 List (java.util.List)2 DbSession (org.sonar.db.DbSession)2 ActiveRuleChange (org.sonar.server.qualityprofile.ActiveRuleChange)2 Strings.nullToEmpty (com.google.common.base.Strings.nullToEmpty)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1