Search in sources :

Example 6 with DeprecatedRuleKeyDto

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();
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) Test(org.junit.Test)

Example 7 with DeprecatedRuleKeyDto

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));
    }
}
Also used : DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) RuleDto(org.sonar.db.rule.RuleDto)

Example 8 with DeprecatedRuleKeyDto

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())));
}
Also used : DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) Sets(com.google.common.collect.Sets)

Example 9 with DeprecatedRuleKeyDto

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;
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleStatus(org.sonar.api.rule.RuleStatus) ExportRuleParamDto(org.sonar.db.qualityprofile.ExportRuleParamDto) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) Nullable(javax.annotation.Nullable) CompareToBuilder(org.apache.commons.lang.builder.CompareToBuilder) NewCustomRule(org.sonar.server.rule.NewCustomRule) Collection(java.util.Collection) Set(java.util.Set) QProfileName(org.sonar.server.qualityprofile.builtin.QProfileName) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) DbClient(org.sonar.db.DbClient) List(java.util.List) RuleCreator(org.sonar.server.rule.RuleCreator) ServerSide(org.sonar.api.server.ServerSide) Writer(java.io.Writer) RuleKey(org.sonar.api.rule.RuleKey) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) Function.identity(java.util.function.Function.identity) ExportRuleDto(org.sonar.db.qualityprofile.ExportRuleDto) Comparator(java.util.Comparator) Collections(java.util.Collections) DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) RuleKey(org.sonar.api.rule.RuleKey) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto)

Aggregations

DeprecatedRuleKeyDto (org.sonar.db.rule.DeprecatedRuleKeyDto)9 Test (org.junit.Test)4 DbSession (org.sonar.db.DbSession)3 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)3 RuleKey (org.sonar.api.rule.RuleKey)2 NewRepository (org.sonar.api.server.rule.RulesDefinition.NewRepository)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Sets (com.google.common.collect.Sets)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Function (java.util.function.Function)1