use of org.sonar.server.qualityprofile.builtin.RuleActivationContext in project sonarqube by SonarSource.
the class QProfileRulesImpl method doBulk.
private BulkChangeResult doBulk(DbSession dbSession, QProfileDto profile, RuleQuery ruleQuery, BiFunction<RuleActivationContext, RuleDefinitionDto, List<ActiveRuleChange>> fn) {
BulkChangeResult result = new BulkChangeResult();
Collection<String> ruleUuids = Sets.newHashSet(ruleIndex.searchAll(ruleQuery));
RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleUuids);
for (String ruleUuid : ruleUuids) {
try {
context.reset(ruleUuid);
RuleDefinitionDto ruleDefinition = context.getRule().get();
List<ActiveRuleChange> changes = fn.apply(context, ruleDefinition);
result.addChanges(changes);
if (!changes.isEmpty()) {
result.incrementSucceeded();
}
} catch (BadRequestException e) {
// other exceptions stop the bulk activation
result.incrementFailed();
result.getErrors().addAll(e.errors());
}
}
activeRuleIndexer.commitAndIndex(dbSession, result.getChanges());
return result;
}
use of org.sonar.server.qualityprofile.builtin.RuleActivationContext in project sonarqube by SonarSource.
the class QProfileRulesImpl method activateAndCommit.
@Override
public List<ActiveRuleChange> activateAndCommit(DbSession dbSession, QProfileDto profile, Collection<RuleActivation> activations) {
verifyNotBuiltIn(profile);
Set<String> ruleUuids = activations.stream().map(RuleActivation::getRuleUuid).collect(MoreCollectors.toHashSet(activations.size()));
RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleUuids);
List<ActiveRuleChange> changes = new ArrayList<>();
for (RuleActivation activation : activations) {
changes.addAll(ruleActivator.activate(dbSession, activation, context));
}
qualityProfileChangeEventService.distributeRuleChangeEvent(List.of(profile), changes, profile.getLanguage());
activeRuleIndexer.commitAndIndex(dbSession, changes);
return changes;
}
use of org.sonar.server.qualityprofile.builtin.RuleActivationContext in project sonarqube by SonarSource.
the class QProfileResetImpl method reset.
@Override
public BulkChangeResult reset(DbSession dbSession, QProfileDto profile, Collection<RuleActivation> activations) {
requireNonNull(profile.getRulesProfileUuid(), "Quality profile must be persisted");
checkArgument(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s'", profile.getKee());
BulkChangeResult result = new BulkChangeResult();
Set<String> rulesToBeDeactivated = new HashSet<>();
// Keep reference to all the activated rules before backup restore
for (ActiveRuleDto activeRuleDto : db.activeRuleDao().selectByProfile(dbSession, profile)) {
if (activeRuleDto.getInheritance() == null) {
// inherited rules can't be deactivated
rulesToBeDeactivated.add(activeRuleDto.getRuleUuid());
}
}
Set<String> ruleUuids = new HashSet<>(rulesToBeDeactivated.size() + activations.size());
ruleUuids.addAll(rulesToBeDeactivated);
activations.forEach(a -> ruleUuids.add(a.getRuleUuid()));
RuleActivationContext context = activator.createContextForUserProfile(dbSession, profile, ruleUuids);
for (RuleActivation activation : activations) {
try {
List<ActiveRuleChange> changes = activator.activate(dbSession, activation, context);
rulesToBeDeactivated.remove(activation.getRuleUuid());
result.incrementSucceeded();
result.addChanges(changes);
} catch (BadRequestException e) {
result.incrementFailed();
result.getErrors().addAll(e.errors());
}
}
List<ActiveRuleChange> changes = new ArrayList<>(result.getChanges());
for (String ruleUuid : rulesToBeDeactivated) {
try {
changes.addAll(activator.deactivate(dbSession, context, ruleUuid, false));
} catch (BadRequestException e) {
// ignore, probably a rule inherited from parent that can't be deactivated
}
}
qualityProfileChangeEventService.distributeRuleChangeEvent(List.of(profile), changes, profile.getLanguage());
activeRuleIndexer.commitAndIndex(dbSession, changes);
return result;
}
use of org.sonar.server.qualityprofile.builtin.RuleActivationContext 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.server.qualityprofile.builtin.RuleActivationContext in project sonarqube by SonarSource.
the class QProfileRulesImpl method deactivateAndCommit.
@Override
public List<ActiveRuleChange> deactivateAndCommit(DbSession dbSession, QProfileDto profile, Collection<String> ruleUuids) {
verifyNotBuiltIn(profile);
RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleUuids);
List<ActiveRuleChange> changes = new ArrayList<>();
for (String ruleUuid : ruleUuids) {
changes.addAll(ruleActivator.deactivate(dbSession, context, ruleUuid, false));
}
qualityProfileChangeEventService.distributeRuleChangeEvent(List.of(profile), changes, profile.getLanguage());
activeRuleIndexer.commitAndIndex(dbSession, changes);
return changes;
}
Aggregations