use of org.sonar.db.rule.DeprecatedRuleKeyDto in project sonarqube by SonarSource.
the class RegisterRulesTest method rules_that_remove_deprecated_key_must_remove_records.
@Test
public void rules_that_remove_deprecated_key_must_remove_records() {
execute(context -> {
NewRepository repo = context.createRepository("fake", "java");
createRule(repo, "rule1");
repo.done();
});
execute(context -> {
NewRepository repo = context.createRepository("fake", "java");
createRule(repo, "newKey").addDeprecatedRuleKey("fake", "rule1").addDeprecatedRuleKey("fake", "rule2");
repo.done();
});
assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).hasSize(1);
Set<DeprecatedRuleKeyDto> deprecatedRuleKeys = dbClient.ruleDao().selectAllDeprecatedRuleKeys(db.getSession());
assertThat(deprecatedRuleKeys).hasSize(2);
execute(context -> {
NewRepository repo = context.createRepository("fake", "java");
createRule(repo, "newKey");
repo.done();
});
assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).hasSize(1);
deprecatedRuleKeys = dbClient.ruleDao().selectAllDeprecatedRuleKeys(db.getSession());
assertThat(deprecatedRuleKeys).isEmpty();
}
use of org.sonar.db.rule.DeprecatedRuleKeyDto in project sonarqube by SonarSource.
the class RuleRepositoryImpl method loadRulesFromDb.
private void loadRulesFromDb(DbSession dbSession) {
this.rulesByKey = new HashMap<>();
this.rulesByUuid = new HashMap<>();
Multimap<String, DeprecatedRuleKeyDto> deprecatedRuleKeysByRuleUuid = dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream().collect(MoreCollectors.index(DeprecatedRuleKeyDto::getRuleUuid));
for (RuleDto ruleDto : dbClient.ruleDao().selectAll(dbSession)) {
Rule rule = new RuleImpl(ruleDto);
rulesByKey.put(ruleDto.getKey(), rule);
rulesByUuid.put(ruleDto.getUuid(), rule);
deprecatedRuleKeysByRuleUuid.get(ruleDto.getUuid()).forEach(t -> rulesByKey.put(RuleKey.of(t.getOldRepositoryKey(), t.getOldRuleKey()), rule));
}
}
use of org.sonar.db.rule.DeprecatedRuleKeyDto in project sonarqube by SonarSource.
the class RegisterRules method updateDeprecatedKeys.
private void updateDeprecatedKeys(RegisterRulesContext context, RulesDefinition.Rule ruleDef, RuleDefinitionDto rule, DbSession dbSession) {
Set<SingleDeprecatedRuleKey> deprecatedRuleKeysFromDefinition = SingleDeprecatedRuleKey.from(ruleDef);
Set<SingleDeprecatedRuleKey> deprecatedRuleKeysFromDB = context.getDBDeprecatedKeysFor(rule);
// DeprecatedKeys that must be deleted
List<String> uuidsToBeDeleted = difference(deprecatedRuleKeysFromDB, deprecatedRuleKeysFromDefinition).stream().map(SingleDeprecatedRuleKey::getUuid).collect(toList());
dbClient.ruleDao().deleteDeprecatedRuleKeys(dbSession, uuidsToBeDeleted);
// DeprecatedKeys that must be created
Sets.SetView<SingleDeprecatedRuleKey> deprecatedRuleKeysToBeCreated = difference(deprecatedRuleKeysFromDefinition, deprecatedRuleKeysFromDB);
deprecatedRuleKeysToBeCreated.forEach(r -> dbClient.ruleDao().insert(dbSession, new DeprecatedRuleKeyDto().setUuid(uuidFactory.create()).setRuleUuid(rule.getUuid()).setOldRepositoryKey(r.getOldRepositoryKey()).setOldRuleKey(r.getOldRuleKey()).setCreatedAt(system2.now())));
}
use of org.sonar.db.rule.DeprecatedRuleKeyDto in project sonarqube by SonarSource.
the class QProfileBackuperImpl method getImportedRulesDefinitions.
/**
* Returns map of rule definition for an imported rule key.
* The imported rule key may refer to a deprecated rule key, in which case the the RuleDefinitionDto will correspond to a different key (the new key).
*/
private Map<RuleKey, RuleDefinitionDto> getImportedRulesDefinitions(DbSession dbSession, List<ImportedRule> rules) {
Set<RuleKey> ruleKeys = rules.stream().map(ImportedRule::getRuleKey).collect(Collectors.toSet());
Map<RuleKey, RuleDefinitionDto> rulesDefinitions = db.ruleDao().selectDefinitionByKeys(dbSession, ruleKeys).stream().collect(Collectors.toMap(RuleDefinitionDto::getKey, identity()));
Set<RuleKey> unrecognizedRuleKeys = ruleKeys.stream().filter(r -> !rulesDefinitions.containsKey(r)).collect(Collectors.toSet());
if (!unrecognizedRuleKeys.isEmpty()) {
Map<String, DeprecatedRuleKeyDto> deprecatedRuleKeysByUuid = db.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream().filter(r -> r.getNewRepositoryKey() != null && r.getNewRuleKey() != null).filter(r -> unrecognizedRuleKeys.contains(RuleKey.of(r.getOldRepositoryKey(), r.getOldRuleKey()))).filter(r -> !ruleKeys.contains(RuleKey.of(r.getNewRepositoryKey(), r.getNewRuleKey()))).collect(Collectors.toMap(DeprecatedRuleKeyDto::getRuleUuid, identity()));
List<RuleDefinitionDto> rulesBasedOnDeprecatedKeys = db.ruleDao().selectDefinitionByUuids(dbSession, deprecatedRuleKeysByUuid.keySet());
for (RuleDefinitionDto rule : rulesBasedOnDeprecatedKeys) {
DeprecatedRuleKeyDto deprecatedRuleKey = deprecatedRuleKeysByUuid.get(rule.getUuid());
RuleKey oldRuleKey = RuleKey.of(deprecatedRuleKey.getOldRepositoryKey(), deprecatedRuleKey.getOldRuleKey());
rulesDefinitions.put(oldRuleKey, rule);
}
}
return rulesDefinitions;
}
Aggregations