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;
}
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;
}
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());
}
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;
}
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;
}
Aggregations