use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class RuleActivator method bulkActivate.
BulkChangeResult bulkActivate(RuleQuery ruleQuery, String profileKey, @Nullable String severity) {
DbSession dbSession = db.openSession(false);
BulkChangeResult result = new BulkChangeResult();
try {
Iterator<RuleKey> rules = ruleIndex.searchAll(ruleQuery);
while (rules.hasNext()) {
RuleKey ruleKey = rules.next();
try {
RuleActivation activation = new RuleActivation(ruleKey);
activation.setSeverity(severity);
List<ActiveRuleChange> changes = activate(dbSession, activation, profileKey);
result.addChanges(changes);
if (!changes.isEmpty()) {
result.incrementSucceeded();
}
} catch (BadRequestException e) {
// other exceptions stop the bulk activation
result.incrementFailed();
result.getErrors().addAll(e.errors());
}
}
dbSession.commit();
activeRuleIndexer.index(result.getChanges());
} finally {
dbSession.close();
}
return result;
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class CompareAction method writeDifferences.
private void writeDifferences(JsonWriter json, Map<RuleKey, ActiveRuleDiff> modified, Map<RuleKey, RuleDto> rulesByKey, Map<String, RuleRepositoryDto> repositoriesByKey) {
json.beginArray();
for (Entry<RuleKey, ActiveRuleDiff> diffEntry : modified.entrySet()) {
RuleKey key = diffEntry.getKey();
ActiveRuleDiff value = diffEntry.getValue();
json.beginObject();
RuleDto rule = rulesByKey.get(key);
writeRule(json, rule, repositoriesByKey.get(rule.getRepositoryKey()));
json.name(ATTRIBUTE_LEFT).beginObject();
json.prop(ATTRIBUTE_SEVERITY, value.leftSeverity());
json.name(ATTRIBUTE_PARAMS).beginObject();
for (Entry<String, ValueDifference<String>> valueDiff : value.paramDifference().entriesDiffering().entrySet()) {
json.prop(valueDiff.getKey(), valueDiff.getValue().leftValue());
}
for (Entry<String, String> valueDiff : value.paramDifference().entriesOnlyOnLeft().entrySet()) {
json.prop(valueDiff.getKey(), valueDiff.getValue());
}
// params
json.endObject();
// left
json.endObject();
json.name(ATTRIBUTE_RIGHT).beginObject();
json.prop(ATTRIBUTE_SEVERITY, value.rightSeverity());
json.name(ATTRIBUTE_PARAMS).beginObject();
for (Entry<String, ValueDifference<String>> valueDiff : value.paramDifference().entriesDiffering().entrySet()) {
json.prop(valueDiff.getKey(), valueDiff.getValue().rightValue());
}
for (Entry<String, String> valueDiff : value.paramDifference().entriesOnlyOnRight().entrySet()) {
json.prop(valueDiff.getKey(), valueDiff.getValue());
}
// params
json.endObject();
// right
json.endObject();
// rule
json.endObject();
}
json.endArray();
}
use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.
the class CompareAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
String leftKey = request.mandatoryParam(PARAM_LEFT_KEY);
String rightKey = request.mandatoryParam(PARAM_RIGHT_KEY);
QProfileComparisonResult result = comparator.compare(leftKey, rightKey);
try (DbSession dbSession = dbClient.openSession(false)) {
List<RuleDto> referencedRules = dbClient.ruleDao().selectByKeys(dbSession, new ArrayList<>(result.collectRuleKeys()));
Map<RuleKey, RuleDto> rulesByKey = Maps.uniqueIndex(referencedRules, RuleDtoToRuleKey.INSTANCE);
Map<String, RuleRepositoryDto> repositoriesByKey = Maps.uniqueIndex(dbClient.ruleRepositoryDao().selectAll(dbSession), RuleRepositoryDto::getKey);
writeResult(response.newJsonWriter(), result, rulesByKey, repositoriesByKey);
}
}
use of org.sonar.api.rule.RuleKey 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);
}
}
}
use of org.sonar.api.rule.RuleKey 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;
}
Aggregations