aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-core
diff options
context:
space:
mode:
authorMatteo Mara <matteo.mara@sonarsource.com>2023-02-24 12:01:45 +0100
committersonartech <sonartech@sonarsource.com>2023-02-27 20:03:16 +0000
commit576b4a83fba9f52406b602035bcc137fc3f69eaa (patch)
tree1ffa8843c20b2d6ee2361758c46b8a9ba1b507a3 /server/sonar-webserver-core
parent87159fbab9897cddd208732e3ea7c4ee10ff970a (diff)
downloadsonarqube-576b4a83fba9f52406b602035bcc137fc3f69eaa.tar.gz
sonarqube-576b4a83fba9f52406b602035bcc137fc3f69eaa.zip
SONAR-16198 removing the 6 built in common rules
Diffstat (limited to 'server/sonar-webserver-core')
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/rule/CommonRuleDefinitions.java26
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/rule/CommonRuleDefinitionsImpl.java166
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java9
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/rule/CommonRuleDefinitionsImplTest.java82
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java2
-rw-r--r--server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RuleDefinitionsLoaderTest.java57
6 files changed, 6 insertions, 336 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/CommonRuleDefinitions.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/CommonRuleDefinitions.java
deleted file mode 100644
index 518907618c4..00000000000
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/CommonRuleDefinitions.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.rule;
-
-import org.sonar.api.server.rule.RulesDefinition;
-
-public interface CommonRuleDefinitions {
- void define(RulesDefinition.Context context);
-}
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/CommonRuleDefinitionsImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/CommonRuleDefinitionsImpl.java
deleted file mode 100644
index 708dcf15109..00000000000
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/CommonRuleDefinitionsImpl.java
+++ /dev/null
@@ -1,166 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.rule;
-
-import java.util.Arrays;
-import java.util.List;
-import java.util.function.Predicate;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.api.rule.Severity;
-import org.sonar.api.server.rule.RuleParamType;
-import org.sonar.api.server.rule.RulesDefinition;
-
-import static org.sonar.api.rule.RuleStatus.DEPRECATED;
-import static org.sonar.server.rule.CommonRuleKeys.commonRepositoryForLang;
-
-/**
- * Declare the few rules that are automatically created by core for all languages. These rules
- * check measure values against thresholds defined in Quality profiles.
- */
-// this class must not be mixed with other RulesDefinition so it does implement the interface RulesDefinitions.
-// It replaces the common-rules that are still embedded within plugins.
-public class CommonRuleDefinitionsImpl implements CommonRuleDefinitions {
-
- private static final List<String> LANGUAGES_TO_SKIP = List.of(
- "terraform",
- "cloudformation",
- "kubernetes",
- "docker",
- "web",
- "css",
- "xml",
- "yaml",
- "json",
- "jsp",
- "text",
- "secrets");
- private static final String MINUTES_10 = "10min";
-
- private final Languages languages;
-
- public CommonRuleDefinitionsImpl(Languages languages) {
- this.languages = languages;
- }
-
- @Override
- public void define(RulesDefinition.Context context) {
- for (Language language : getActiveLanguages(languages.all())) {
- RulesDefinition.NewRepository repo = context.createRepository(commonRepositoryForLang(language.getKey()), language.getKey());
- repo.setName("Common " + language.getName());
- defineBranchCoverageRule(repo);
- defineLineCoverageRule(repo);
- defineCommentDensityRule(repo);
- defineDuplicatedBlocksRule(repo);
- defineFailedUnitTestRule(repo);
- defineSkippedUnitTestRule(repo);
- repo.done();
- }
- }
-
- private static List<Language> getActiveLanguages(Language[] allLanguages) {
- return Arrays.stream(allLanguages)
- .filter(Predicate.not(language -> LANGUAGES_TO_SKIP.contains(language.getKey())))
- .toList();
- }
-
- private static void defineBranchCoverageRule(RulesDefinition.NewRepository repo) {
- RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE);
- rule.setName("Branches should have sufficient coverage by tests")
- .addTags("bad-practice")
- .setHtmlDescription("An issue is created on a file as soon as the branch coverage on this file is less than the required threshold. "
- + "It gives the number of branches to be covered in order to reach the required threshold.")
- .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("5min"))
- .setGapDescription("number of uncovered conditions")
- .setSeverity(Severity.MAJOR);
- rule.createParam(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE_PROPERTY)
- .setName("The minimum required branch coverage ratio")
- .setDefaultValue("65")
- .setType(RuleParamType.FLOAT);
- rule.setStatus(DEPRECATED);
- }
-
- private static void defineLineCoverageRule(RulesDefinition.NewRepository repo) {
- RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE);
- rule.setName("Lines should have sufficient coverage by tests")
- .addTags("bad-practice")
- .setHtmlDescription("An issue is created on a file as soon as the line coverage on this file is less than the required threshold. " +
- "It gives the number of lines to be covered in order to reach the required threshold.")
- .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min"))
- .setGapDescription("number of lines under the coverage threshold")
- .setSeverity(Severity.MAJOR);
- rule.createParam(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE_PROPERTY)
- .setName("The minimum required line coverage ratio")
- .setDefaultValue("65")
- .setType(RuleParamType.FLOAT);
- rule.setStatus(DEPRECATED);
- }
-
- private static void defineCommentDensityRule(RulesDefinition.NewRepository repo) {
- RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY);
- rule.setName("Source files should have a sufficient density of comment lines")
- .addTags("convention")
- .setHtmlDescription("An issue is created on a file as soon as the density of comment lines on this file is less than the required threshold. " +
- "The number of comment lines to be written in order to reach the required threshold is provided by each issue message.")
- .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min"))
- .setGapDescription("number of lines required to meet minimum density")
- .setSeverity(Severity.MAJOR);
- rule.createParam(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY)
- .setName("The minimum required comment density")
- .setDefaultValue("25")
- .setType(RuleParamType.FLOAT);
- rule.setStatus(DEPRECATED);
- }
-
- private static void defineDuplicatedBlocksRule(RulesDefinition.NewRepository repo) {
- RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.DUPLICATED_BLOCKS);
- rule.setName("Source files should not have any duplicated blocks")
- .addTags("pitfall")
- .setHtmlDescription("An issue is created on a file as soon as there is at least one block of duplicated code on this file")
- .setDebtRemediationFunction(rule.debtRemediationFunctions().linearWithOffset(MINUTES_10, MINUTES_10))
- .setGapDescription("number of duplicate blocks")
- .setSeverity(Severity.MAJOR)
- .setStatus(DEPRECATED);
- }
-
- private static void defineFailedUnitTestRule(RulesDefinition.NewRepository repo) {
- RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.FAILED_UNIT_TESTS);
- rule
- .setName("Failed unit tests should be fixed")
- .addTags("bug")
- .setHtmlDescription("Test failures or errors generally indicate that regressions have been introduced. "
- + "Those tests should be handled as soon as possible to reduce the cost to fix the corresponding regressions.")
- .setDebtRemediationFunction(rule.debtRemediationFunctions().linear(MINUTES_10))
- .setGapDescription("number of failed tests")
- .setSeverity(Severity.MAJOR)
- .setStatus(DEPRECATED);
- }
-
- private static void defineSkippedUnitTestRule(RulesDefinition.NewRepository repo) {
- RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.SKIPPED_UNIT_TESTS);
- rule.setName("Skipped unit tests should be either removed or fixed")
- .addTags("pitfall")
- .setHtmlDescription("Skipped unit tests are considered as dead code. Either they should be activated again (and updated) or they should be removed.")
- .setDebtRemediationFunction(rule.debtRemediationFunctions().linear(MINUTES_10))
- .setGapDescription("number of skipped tests")
- .setSeverity(Severity.MAJOR)
- .setStatus(DEPRECATED);
- }
-}
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java
index 8defeca7cd2..d3e73b69f0f 100644
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RuleDefinitionsLoader.java
@@ -30,13 +30,11 @@ import org.springframework.beans.factory.annotation.Autowired;
*/
public class RuleDefinitionsLoader {
- private final CommonRuleDefinitions coreCommonDefs;
private final RulesDefinition[] pluginDefs;
private final ServerPluginRepository serverPluginRepository;
@Autowired(required = false)
- public RuleDefinitionsLoader(CommonRuleDefinitions coreCommonDefs, ServerPluginRepository serverPluginRepository, RulesDefinition[] pluginDefs) {
- this.coreCommonDefs = coreCommonDefs;
+ public RuleDefinitionsLoader(ServerPluginRepository serverPluginRepository, RulesDefinition[] pluginDefs) {
this.serverPluginRepository = serverPluginRepository;
this.pluginDefs = pluginDefs;
}
@@ -45,8 +43,8 @@ public class RuleDefinitionsLoader {
* Used when no definitions at all.
*/
@Autowired(required = false)
- public RuleDefinitionsLoader(CommonRuleDefinitions coreCommonDefs, ServerPluginRepository serverPluginRepository) {
- this(coreCommonDefs, serverPluginRepository, new RulesDefinition[0]);
+ public RuleDefinitionsLoader(ServerPluginRepository serverPluginRepository) {
+ this(serverPluginRepository, new RulesDefinition[0]);
}
public RulesDefinition.Context load() {
@@ -56,7 +54,6 @@ public class RuleDefinitionsLoader {
pluginDefinition.define(context);
}
context.setCurrentPluginKey(null);
- coreCommonDefs.define(context);
return context;
}
}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/CommonRuleDefinitionsImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/CommonRuleDefinitionsImplTest.java
deleted file mode 100644
index 5e2f77560f2..00000000000
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/CommonRuleDefinitionsImplTest.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.rule;
-
-import org.junit.Test;
-import org.sonar.api.impl.server.RulesDefinitionContext;
-import org.sonar.api.resources.AbstractLanguage;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.api.server.rule.RulesDefinition;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.api.rule.RuleStatus.DEPRECATED;
-
-public class CommonRuleDefinitionsImplTest {
-
- @Test
- public void instantiate_common_rules_for_correct_languages_only() {
- CommonRuleDefinitionsImpl commonRuleDefinitions = new CommonRuleDefinitionsImpl(getLanguages());
- RulesDefinition.Context underTest = new RulesDefinitionContext();
- commonRuleDefinitions.define(underTest);
-
- assertThat(underTest.repositories()).hasSize(3);
- assertThat(underTest.repository("common-java")).isNotNull();
- assertThat(underTest.repository("common-php")).isNotNull();
- assertThat(underTest.repository("common-js")).isNotNull();
-
- for (RulesDefinition.Repository repository : underTest.repositories()) {
- assertThat(repository.rules()).hasSize(6);
- for (RulesDefinition.Rule rule : repository.rules()) {
- assertThat(rule.status()).isEqualTo(DEPRECATED);
- }
- }
- }
-
- private Languages getLanguages() {
- return new Languages(
- createLanguage("java"),
- createLanguage("php"),
- createLanguage("js"),
- createLanguage("terraform"),
- createLanguage("cloudformation"),
- createLanguage("kubernetes"),
- createLanguage("docker"),
- createLanguage("web"),
- createLanguage("css"),
- createLanguage("xml"),
- createLanguage("yaml"),
- createLanguage("json"),
- createLanguage("jsp"),
- createLanguage("text"),
- createLanguage("secrets")
- );
- }
-
- private Language createLanguage(String languageKey) {
- return new AbstractLanguage(languageKey, languageKey + "_name") {
- @Override
- public String[] getFileSuffixes() {
- return new String[0];
- }
- };
- }
-
-}
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java
index 88d61fc1c88..b43a792f860 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java
@@ -1137,7 +1137,7 @@ public class RegisterRulesTest {
private void execute(RulesDefinition... defs) {
ServerPluginRepository pluginRepository = mock(ServerPluginRepository.class);
when(pluginRepository.getPluginKey(any(RulesDefinition.class))).thenReturn(FAKE_PLUGIN_KEY);
- RuleDefinitionsLoader loader = new RuleDefinitionsLoader(mock(CommonRuleDefinitionsImpl.class), pluginRepository, defs);
+ RuleDefinitionsLoader loader = new RuleDefinitionsLoader(pluginRepository, defs);
Languages languages = mock(Languages.class);
when(languages.get(any())).thenReturn(mock(Language.class));
reset(webServerRuleFinder);
diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RuleDefinitionsLoaderTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RuleDefinitionsLoaderTest.java
index d28687426da..2e03ec2db49 100644
--- a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RuleDefinitionsLoaderTest.java
+++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RuleDefinitionsLoaderTest.java
@@ -30,16 +30,14 @@ public class RuleDefinitionsLoaderTest {
@Test
public void no_definitions() {
- CommonRuleDefinitions commonRulesDefinitions = mock(CommonRuleDefinitions.class);
- RulesDefinition.Context context = new RuleDefinitionsLoader(commonRulesDefinitions, mock(ServerPluginRepository.class)).load();
+ RulesDefinition.Context context = new RuleDefinitionsLoader(mock(ServerPluginRepository.class)).load();
assertThat(context.repositories()).isEmpty();
}
@Test
public void load_definitions() {
- CommonRuleDefinitions commonRulesDefinitions = mock(CommonRuleDefinitions.class);
- RulesDefinition.Context context = new RuleDefinitionsLoader(commonRulesDefinitions, mock(ServerPluginRepository.class),
+ RulesDefinition.Context context = new RuleDefinitionsLoader(mock(ServerPluginRepository.class),
new RulesDefinition[] {
new FindbugsDefinitions(), new JavaDefinitions()
}).load();
@@ -49,35 +47,6 @@ public class RuleDefinitionsLoaderTest {
assertThat(context.repository("java")).isNotNull();
}
- @Test
- public void define_common_rules() {
- CommonRuleDefinitions commonRulesDefinitions = new FakeCommonRuleDefinitions();
- RulesDefinition.Context context = new RuleDefinitionsLoader(commonRulesDefinitions, mock(ServerPluginRepository.class),
- new RulesDefinition[] {
- new JavaDefinitions()
- }).load();
-
- assertThat(context.repositories()).extracting("key").containsOnly("java", "common-java");
- assertThat(context.repository("common-java").rules()).extracting("key").containsOnly("InsufficientBranchCoverage");
- }
-
- /**
- * "common-rules" are merged into core 5.2. Previously they were embedded by some plugins. Only the core definition
- * is taken into account. Others are ignored.
- */
- @Test
- public void plugin_common_rules_are_overridden() {
- CommonRuleDefinitions commonRulesDefinitions = new FakeCommonRuleDefinitions();
- RulesDefinition.Context context = new RuleDefinitionsLoader(commonRulesDefinitions, mock(ServerPluginRepository.class),
- new RulesDefinition[] {
- new PluginCommonRuleDefinitions()
- }).load();
-
- assertThat(context.repositories()).extracting("key").containsOnly("common-java");
- assertThat(context.repository("common-java").rules()).extracting("key").containsOnly("InsufficientBranchCoverage");
- assertThat(context.repository("common-java").rule("InsufficientBranchCoverage").name()).isEqualTo("The name as defined by core");
- }
-
static class FindbugsDefinitions implements RulesDefinition {
@Override
public void define(Context context) {
@@ -101,26 +70,4 @@ public class RuleDefinitionsLoaderTest {
repo.done();
}
}
-
- static class PluginCommonRuleDefinitions implements RulesDefinition {
- @Override
- public void define(RulesDefinition.Context context) {
- RulesDefinition.NewRepository repo = context.createRepository("common-java", "java");
- repo.createRule("InsufficientBranchCoverage")
- .setName("The name as defined by plugin")
- .setHtmlDescription("The description as defined by plugin");
- repo.done();
- }
- }
-
- static class FakeCommonRuleDefinitions implements CommonRuleDefinitions {
- @Override
- public void define(RulesDefinition.Context context) {
- RulesDefinition.NewRepository repo = context.createRepository("common-java", "java");
- repo.createRule("InsufficientBranchCoverage")
- .setName("The name as defined by core")
- .setHtmlDescription("The description as defined by core");
- repo.done();
- }
- }
}