]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16598 Populate Generic concepts from sonar-plugin api
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Wed, 6 Jul 2022 15:26:32 +0000 (17:26 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 12 Jul 2022 14:30:04 +0000 (14:30 +0000)
(cherry picked from commit 1d1feb6ab5dcf0d89f63ec0e1c67778f22da0978)

plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRulesDefinition.java
server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java
server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/AddTagsActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/RemoveTagsActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleTagHelperTest.java

index 6656e76003404b2c224c38aacff9707ab358121e..17860487d381ad00626096fe99ffec7e1f741d0d 100644 (file)
@@ -119,7 +119,8 @@ public class XooRulesDefinition implements RulesDefinition {
     addHowToFixSectionsWithContexts(oneIssuePerLine);
     oneIssuePerLine
       .setDebtRemediationFunction(oneIssuePerLine.debtRemediationFunctions().linear("1min"))
-      .setGapDescription("It takes about 1 minute to an experienced software craftsman to remove a line of code");
+      .setGapDescription("It takes about 1 minute to an experienced software craftsman to remove a line of code")
+      .addGenericConceptKeys("defense_in_depth", "least_trust_principle");
 
     NewRule oneQuickFixPerLine = repo.createRule(OneQuickFixPerLineSensor.RULE_KEY).setName("One Quick Fix Per Line")
       .setTags("line");
index fc3463987b61d5fd93cb5ec6e0816d7b19099b46..b7324ab3a3ab04c44967e98b34587d03ef764e39 100644 (file)
@@ -396,7 +396,8 @@ public class RegisterRules implements Startable {
       .setIsExternal(ruleDef.repository().isExternal())
       .setIsAdHoc(false)
       .setCreatedAt(system2.now())
-      .setUpdatedAt(system2.now());
+      .setUpdatedAt(system2.now())
+      .setGenericConcepts(ruleDef.genericConceptKeys());
 
     if (isNotEmpty(ruleDef.htmlDescription())) {
       ruleDto.setDescriptionFormat(Format.HTML);
@@ -442,7 +443,8 @@ public class RegisterRules implements Startable {
     boolean debtDefinitionsMerged = mergeDebtDefinitions(ruleDef, ruleDto);
     boolean tagsMerged = mergeTags(ruleDef, ruleDto);
     boolean securityStandardsMerged = mergeSecurityStandards(ruleDef, ruleDto);
-    return ruleMerged || debtDefinitionsMerged || tagsMerged || securityStandardsMerged;
+    boolean genericConceptsMerged = mergeGenericConcepts(ruleDef, ruleDto);
+    return ruleMerged || debtDefinitionsMerged || tagsMerged || securityStandardsMerged || genericConceptsMerged;
   }
 
   private boolean mergeRule(RulesDefinition.Rule def, RuleDto dto) {
@@ -681,6 +683,16 @@ public class RegisterRules implements Startable {
     return changed;
   }
 
+  private static boolean mergeGenericConcepts(RulesDefinition.Rule ruleDef, RuleDto dto) {
+    boolean changed = false;
+    if (dto.getGenericConcepts().size() != ruleDef.genericConceptKeys().size() ||
+      !dto.getGenericConcepts().containsAll(ruleDef.genericConceptKeys())) {
+      dto.setGenericConcepts(ruleDef.genericConceptKeys());
+      changed = true;
+    }
+    return changed;
+  }
+
   private void processRemainingDbRules(RegisterRulesContext recorder, DbSession dbSession) {
     // custom rules check status of template, so they must be processed at the end
     List<RuleDto> customRules = new ArrayList<>();
index 3144162449b2159e6d092247d78feb9a4a68e38c..164949cb9e44d69223f967812efd923ff4ef0b85 100644 (file)
@@ -244,6 +244,7 @@ public class RegisterRulesTest {
     assertThat(rule.getType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());
     assertThat(rule.getPluginKey()).isEqualTo(FAKE_PLUGIN_KEY);
     assertThat(rule.isAdHoc()).isFalse();
+    assertThat(rule.getGenericConcepts()).containsOnly("concept1", "concept2", "concept3");
   }
 
   @Test
@@ -413,6 +414,7 @@ public class RegisterRulesTest {
     assertThat(rule1.getType()).isEqualTo(RuleType.BUG.getDbConstant());
     assertThat(rule1.getCreatedAt()).isEqualTo(DATE1.getTime());
     assertThat(rule1.getUpdatedAt()).isEqualTo(DATE2.getTime());
+    assertThat(rule1.getGenericConcepts()).containsOnly("concept1","concept4");
   }
 
   @Test
@@ -663,7 +665,7 @@ public class RegisterRulesTest {
 
   @DataProvider
   public static Object[][] allRenamingCases() {
-    return new Object[][] {
+    return new Object[][]{
       {"repo1", "rule1", "repo1", "rule2"},
       {"repo1", "rule1", "repo2", "rule1"},
       {"repo1", "rule1", "repo2", "rule2"},
@@ -744,11 +746,11 @@ public class RegisterRulesTest {
     system.setNow(DATE1.getTime());
 
     RuleDescriptionSection section1context1 = createRuleDescriptionSection(HOW_TO_FIX_SECTION_KEY, "section1 ctx1 content", "CTX_1");
-    RuleDescriptionSection section1context2 = createRuleDescriptionSection(HOW_TO_FIX_SECTION_KEY,"section1 ctx2 content", "CTX_2");
-    RuleDescriptionSection section2context1 = createRuleDescriptionSection(RESOURCES_SECTION_KEY,"section2 content", "CTX_1");
+    RuleDescriptionSection section1context2 = createRuleDescriptionSection(HOW_TO_FIX_SECTION_KEY, "section1 ctx2 content", "CTX_2");
+    RuleDescriptionSection section2context1 = createRuleDescriptionSection(RESOURCES_SECTION_KEY, "section2 content", "CTX_1");
     RuleDescriptionSection section2context2 = createRuleDescriptionSection(RESOURCES_SECTION_KEY,"section2 ctx2 content", "CTX_2");
-    RuleDescriptionSection section3noContext = createRuleDescriptionSection(ASSESS_THE_PROBLEM_SECTION_KEY,"section3 content", null);
-    RuleDescriptionSection section4noContext = createRuleDescriptionSection(ROOT_CAUSE_SECTION_KEY,"section4 content", null);
+    RuleDescriptionSection section3noContext = createRuleDescriptionSection(ASSESS_THE_PROBLEM_SECTION_KEY, "section3 content", null);
+    RuleDescriptionSection section4noContext = createRuleDescriptionSection(ROOT_CAUSE_SECTION_KEY, "section4 content", null);
     execute(context -> {
       NewRepository repo = context.createRepository("fake", "java");
       repo.createRule("rule")
@@ -1206,7 +1208,8 @@ public class RegisterRulesTest {
         .setScope(RuleScope.ALL)
         .setType(RuleType.CODE_SMELL)
         .setStatus(RuleStatus.BETA)
-        .setGapDescription("java.S115.effortToFix");
+        .setGapDescription("java.S115.effortToFix")
+        .addGenericConceptKeys("concept1", "concept2", "concept3");
       rule1.setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("5d", "10h"));
 
       rule1.createParam("param1").setDescription("parameter one").setDefaultValue("default1");
@@ -1244,7 +1247,8 @@ public class RegisterRulesTest {
         .setTags("tag1", "tag4")
         .setType(RuleType.BUG)
         .setStatus(READY)
-        .setGapDescription("java.S115.effortToFix.v2");
+        .setGapDescription("java.S115.effortToFix.v2")
+        .addGenericConceptKeys("concept1", "concept4");
       rule1.setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("6d", "2h"));
       rule1.createParam("param1").setDescription("parameter one v2").setDefaultValue("default1 v2");
       rule1.createParam("param2").setDescription("parameter two v2").setDefaultValue("default2 v2");
@@ -1283,7 +1287,8 @@ public class RegisterRulesTest {
         .setTags("tag1", "tag2", "tag3")
         .setScope(RuleScope.ALL)
         .setType(RuleType.CODE_SMELL)
-        .setStatus(RuleStatus.BETA);
+        .setStatus(RuleStatus.BETA)
+        .addGenericConceptKeys("concept1", "concept2", "concept3");
 
       repo.createRule(EXTERNAL_HOTSPOT_RULE_KEY.rule())
         .setName("Hotspot")
index a7a75e6f2e07b7fb35ebe9de0a242681f38db728..e716d9b2e8ae5ff41ae631ab4edceabfdf2f7f0f 100644 (file)
@@ -67,6 +67,6 @@ public class AddTagsActionTest {
       underTest.execute(properties, context);
     })
       .isInstanceOf(IllegalArgumentException.class)
-      .hasMessage("Tag 'th ag' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
+      .hasMessage("Entry 'th ag' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
   }
 }
index 4a8d7573c6ba7d69752703289a10f612a81db69c..6c35531d6e4be023554449f1df530f6458e19db9 100644 (file)
@@ -67,6 +67,6 @@ public class RemoveTagsActionTest {
       action.execute(properties, context);
     })
       .isInstanceOf(IllegalArgumentException.class)
-      .hasMessage("Tag 'th ag' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
+      .hasMessage("Entry 'th ag' is invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
   }
 }
index b4585b5b519fe1bb64bf361d87be606960d1cbca..9dfd0f41558e9e2f9bbb9ec9297fcdb01877f23b 100644 (file)
@@ -175,7 +175,7 @@ public class SetTagsActionTest {
 
     assertThatThrownBy(() -> call(issueDto.getKey(), "pol op"))
       .isInstanceOf(IllegalArgumentException.class)
-      .hasMessage("Tags 'pol op' are invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
+      .hasMessage("Entries 'pol op' are invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
   }
 
   @Test
index ac40696249338d82f447c297199d0715b247f5e1..77e12bf069b921252256481d708a0bc201c05013 100644 (file)
@@ -65,7 +65,7 @@ public class RuleTagHelperTest {
       RuleTagHelper.applyTags(rule, Sets.newHashSet("Java Eight"));
       fail();
     } catch (IllegalArgumentException e) {
-      assertThat(e.getMessage()).startsWith("Tag 'Java Eight' is invalid");
+      assertThat(e.getMessage()).startsWith("Entry 'Java Eight' is invalid");
     }
   }