use of org.sonar.db.qualityprofile.ActiveRuleDto in project sonarqube by SonarSource.
the class RuleActivationContext method register.
private void register(Collection<ActiveRuleDto> activeRules, Collection<ActiveRuleParamDto> activeRuleParams) {
ListMultimap<String, ActiveRuleParamDto> paramsByActiveRuleUuid = activeRuleParams.stream().collect(index(ActiveRuleParamDto::getActiveRuleUuid));
for (ActiveRuleDto activeRule : activeRules) {
ActiveRuleWrapper wrapper = new ActiveRuleWrapper(activeRule, paramsByActiveRuleUuid.get(activeRule.getUuid()));
this.activeRulesByKey.put(activeRule.getKey(), wrapper);
}
}
use of org.sonar.db.qualityprofile.ActiveRuleDto 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.ActiveRuleDto 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.ActiveRuleDto 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.ActiveRuleDto in project sonarqube by SonarSource.
the class QProfileFactoryImplTest method createCustomProfile.
private QProfileDto createCustomProfile() {
QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage("xoo").setIsBuiltIn(false));
ActiveRuleDto activeRuleDto = db.qualityProfiles().activateRule(profile, rule);
ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setRulesParameterUuid(ruleParam.getUuid()).setKey("foo").setValue("bar");
db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, activeRuleParam);
db.getDbClient().qProfileChangeDao().insert(dbSession, new QProfileChangeDto().setChangeType(ActiveRuleChange.Type.ACTIVATED.name()).setRulesProfileUuid(profile.getRulesProfileUuid()));
db.commit();
return profile;
}
Aggregations