From c502f859682071fc6d06776af77fc8866a9f57ca Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Wed, 29 Jan 2020 15:35:22 -0600 Subject: [PATCH] SONAR-13019 Keys of rules created from template are not renamed when template rule key is renamed --- .../org/sonar/server/rule/RegisterRules.java | 22 ++++----- .../sonar/server/rule/RegisterRulesTest.java | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java index 1b99932ed9b..081a12c8aba 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -70,7 +70,6 @@ import org.sonar.server.rule.index.RuleIndexer; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; -import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Sets.difference; import static com.google.common.collect.Sets.intersection; import static java.lang.String.format; @@ -163,16 +162,14 @@ public class RegisterRules implements Startable { } private RegisterRulesContext createRegisterRulesContext(DbSession dbSession) { - Map allRules = dbClient.ruleDao().selectAllDefinitions(dbSession) - .stream() + Map allRules = dbClient.ruleDao().selectAllDefinitions(dbSession).stream() .collect(uniqueIndex(RuleDefinitionDto::getKey)); Map> existingDeprecatedKeysById = loadDeprecatedRuleKeys(dbSession); return new RegisterRulesContext(allRules, existingDeprecatedKeysById); } private Map> loadDeprecatedRuleKeys(DbSession dbSession) { - return dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession) - .stream() + return dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream() .map(SingleDeprecatedRuleKey::from) .collect(Collectors.groupingBy(SingleDeprecatedRuleKey::getRuleId, Collectors.toSet())); } @@ -224,7 +221,7 @@ public class RegisterRules implements Startable { RuleDefinitionDto rule = dbRulesByRuleId.get(ruleId); if (rule == null) { LOG.warn("Could not retrieve rule with id %s referenced by a deprecated rule key. " + - "The following deprecated rule keys seem to be referencing a non-existing rule", + "The following deprecated rule keys seem to be referencing a non-existing rule", ruleId, entry.getValue()); } else { entry.getValue().forEach(d -> builder.put(d.getOldRuleKeyAsRuleKey(), rule)); @@ -631,8 +628,6 @@ public class RegisterRules implements Startable { } else if (dto.getSystemTags().size() != ruleDef.tags().size() || !dto.getSystemTags().containsAll(ruleDef.tags())) { dto.setSystemTags(ruleDef.tags()); - // FIXME this can't be implemented easily with organization support: remove end-user tags that are now declared as system - // RuleTagHelper.applyTags(dto, ImmutableSet.copyOf(dto.getTags())); changed = true; } return changed; @@ -654,7 +649,7 @@ public class RegisterRules implements Startable { private void processRemainingDbRules(RegisterRulesContext recorder, DbSession dbSession) { // custom rules check status of template, so they must be processed at the end - List customRules = newArrayList(); + List customRules = new ArrayList<>(); recorder.getRemaining().forEach(rule -> { if (rule.isCustomRule()) { @@ -670,6 +665,7 @@ public class RegisterRules implements Startable { Optional template = dbClient.ruleDao().selectDefinitionById(templateId, dbSession); if (template.isPresent() && template.get().getStatus() != RuleStatus.REMOVED) { if (updateCustomRuleFromTemplateRule(customRule, template.get())) { + recorder.updated(customRule); update(dbSession, customRule); } } else { @@ -732,6 +728,10 @@ public class RegisterRules implements Startable { customRule.setSeverity(templateRule.getSeverityString()); changed = true; } + if (!StringUtils.equals(customRule.getRepositoryKey(), templateRule.getRepositoryKey())) { + customRule.setRepositoryKey(templateRule.getRepositoryKey()); + changed = true; + } return changed; } @@ -802,7 +802,7 @@ public class RegisterRules implements Startable { .collect(Collectors.toSet()); checkState(incorrectRuleKeyMessage.isEmpty(), "An incorrect state of deprecated rule keys has been detected.\n %s", - lazyToString(() -> incorrectRuleKeyMessage.stream().collect(Collectors.joining("\n")))); + lazyToString(() -> String.join("\n", incorrectRuleKeyMessage))); } private static Stream filterInvalidDeprecatedRuleKeys(ImmutableMap dbDeprecatedRuleKeysByOldRuleKey, @@ -844,7 +844,7 @@ public class RegisterRules implements Startable { Set duplicates = new HashSet<>(); Set uniques = new HashSet<>(); - list.stream().forEach(t -> { + list.forEach(t -> { if (!uniques.add(t)) { duplicates.add(t); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java index a557963cee6..d875bf42807 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java @@ -452,6 +452,45 @@ public class RegisterRulesTest { assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions()).getTotal()).isEqualTo(0); } + @Test + public void update_template_rule_key_should_also_update_custom_rules() { + when(system.now()).thenReturn(DATE1.getTime()); + execute(context -> { + NewRepository repo = context.createRepository("squid", "java"); + repo.createRule("rule") + .setName("Name1") + .setHtmlDescription("Description") + .setTemplate(true); + repo.done(); + }); + + RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), defaultOrganization, RuleKey.of("squid", "rule")); + + // insert custom rule + dbClient.ruleDao().insert(db.getSession(), new RuleDefinitionDto() + .setRuleKey(RuleKey.of("squid", "custom")) + .setLanguage("java") + .setScope(Scope.ALL) + .setTemplateId(rule1.getId()) + .setName("custom1")); + db.commit(); + + // re-key rule + execute(context -> { + NewRepository repo = context.createRepository("java", "java"); + repo.createRule("rule") + .setName("Name1") + .setHtmlDescription("Description") + .addDeprecatedRuleKey("squid", "rule") + .setTemplate(true); + repo.done(); + }); + + // template rule and custom rule have been updated + rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), defaultOrganization, RuleKey.of("java", "rule")); + RuleDto custom = dbClient.ruleDao().selectOrFailByKey(db.getSession(), defaultOrganization, RuleKey.of("java", "custom")); + } + @Test public void update_if_rule_key_renamed_and_deprecated_key_declared() { String ruleKey1 = "rule1"; @@ -1048,6 +1087,13 @@ public class RegisterRulesTest { repo.done(); } + private void verifyIndicesMarkedAsInitialized() { + verify(metadataIndex).setInitialized(RuleIndexDefinition.TYPE_RULE, true); + verify(metadataIndex).setInitialized(RuleIndexDefinition.TYPE_RULE_EXTENSION, true); + verify(metadataIndex).setInitialized(RuleIndexDefinition.TYPE_ACTIVE_RULE, true); + reset(metadataIndex); + } + private RuleParamDto getParam(List params, String key) { for (RuleParamDto param : params) { if (param.getName().equals(key)) { -- 2.39.5