You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EducationRulesDefinition.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.education;
  21. import org.sonar.api.server.rule.RuleDescriptionSection;
  22. import org.sonar.api.server.rule.RulesDefinition;
  23. import org.sonar.education.sensors.EducationPrinciplesSensor;
  24. import org.sonar.education.sensors.EducationWithContextsSensor;
  25. import org.sonar.education.sensors.EducationWithDetectedContextSensor;
  26. import static org.sonar.education.sensors.EducationWithSingleContextSensor.EDUCATION_WITH_SINGLE_CONTEXT_RULE_KEY;
  27. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.ASSESS_THE_PROBLEM_SECTION_KEY;
  28. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.HOW_TO_FIX_SECTION_KEY;
  29. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.INTRODUCTION_SECTION_KEY;
  30. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.RESOURCES_SECTION_KEY;
  31. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.ROOT_CAUSE_SECTION_KEY;
  32. public class EducationRulesDefinition implements RulesDefinition {
  33. public static final String EDUCATION_RULE_REPOSITORY_KEY = "edu";
  34. public static final String EDUCATION_KEY = "education";
  35. private static final String IGNORED_FAKE_SECTION = "fake_section_to_be_ignored";
  36. public static final String[] CONTEXTS = {"spring", "hibernate", "apache-commons", "vaadin", "mybatis"};
  37. private static final String HTML_LOREM_IPSUM = "<a href=\"https://google.com\">Lorem</a> ipsum dolor sit amet, consectetur adipiscing " +
  38. "elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " +
  39. "laboris nisi ut aliquip ex ea commodo consequat.<br />Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu " +
  40. "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
  41. @Override
  42. public void define(Context context) {
  43. NewRepository repo = context.createRepository(EDUCATION_RULE_REPOSITORY_KEY, EDUCATION_KEY);
  44. createRuleWithDescriptionSectionsAndEducationPrinciples(repo);
  45. createRuleWithDescriptionSectionsAndSingleContext(repo);
  46. createRuleWithDescriptionSectionsAndMultipleContexts(repo);
  47. createRuleWithDescriptionSectionsAndMultipleContextsIncludingOneDetected(repo);
  48. repo.done();
  49. }
  50. private void createRuleWithDescriptionSectionsAndEducationPrinciples(NewRepository repo) {
  51. String[] educationPrinciples = {"defense_in_depth", "least_trust_principle"};
  52. String ruleKey = EducationPrinciplesSensor.EDUCATION_WITH_GENERIC_CONCEPTS_RULE_KEY;
  53. NewRule ruleWithSingleContext = repo.createRule(ruleKey).setName("Rule with description sections and education principles")
  54. .addTags(EDUCATION_KEY);
  55. ruleWithSingleContext
  56. .setHtmlDescription(String.format("This rule contains description sections. To trigger an issue using this rule you need to " +
  57. "scan any text file with a line containing %s key word. This rule also contains 2 education principles - %s and %s",
  58. ruleKey, educationPrinciples[0], educationPrinciples[1]))
  59. .addEducationPrincipleKeys(educationPrinciples);
  60. addNonContextualizedDescriptionSections(ruleWithSingleContext);
  61. descriptionSection(HOW_TO_FIX_SECTION_KEY);
  62. }
  63. private void createRuleWithDescriptionSectionsAndSingleContext(NewRepository repo) {
  64. String ruleKey = EDUCATION_WITH_SINGLE_CONTEXT_RULE_KEY;
  65. NewRule ruleWithSingleContext = repo.createRule(ruleKey).setName("Rule with description sections and single " +
  66. "contexts for 'how to fix'")
  67. .addTags(EDUCATION_KEY);
  68. ruleWithSingleContext
  69. .setHtmlDescription(String.format("This rule contains description sections and single context for 'how to fix' section. To " +
  70. "trigger an issue using this rule you need to scan any text file with a line containing %s key word.", ruleKey));
  71. addNonContextualizedDescriptionSections(ruleWithSingleContext);
  72. ruleWithSingleContext.addDescriptionSection(descriptionHowToFixSectionWithContext(CONTEXTS[0]));
  73. }
  74. private void createRuleWithDescriptionSectionsAndMultipleContexts(NewRepository repo) {
  75. NewRule ruleWithMultipleContexts = repo.createRule(EducationWithContextsSensor.EDUCATION_WITH_CONTEXTS_RULE_KEY)
  76. .setName("Rule with description sections and multiple contexts for 'how to fix'")
  77. .addTags(EDUCATION_KEY);
  78. ruleWithMultipleContexts
  79. .setHtmlDescription(String.format("This rule contains description sections and multiple contexts for 'how to fix' section. To trigger " +
  80. "an issue using this rule you need to scan any text file with a line containing %s keyword.",
  81. EducationWithContextsSensor.EDUCATION_WITH_CONTEXTS_RULE_KEY));
  82. addNonContextualizedDescriptionSections(ruleWithMultipleContexts);
  83. for (String context : CONTEXTS) {
  84. ruleWithMultipleContexts.addDescriptionSection(descriptionHowToFixSectionWithContext(context));
  85. }
  86. }
  87. private void createRuleWithDescriptionSectionsAndMultipleContextsIncludingOneDetected(NewRepository repo) {
  88. NewRule ruleWithMultipleContexts = repo.createRule(EducationWithDetectedContextSensor.EDUCATION_WITH_DETECTED_CONTEXT_RULE_KEY)
  89. .setName("Rule with description sections and multiple contexts (including one detected) for 'how to fix'")
  90. .addTags(EDUCATION_KEY);
  91. ruleWithMultipleContexts
  92. .setHtmlDescription(String.format("This rule contains description sections and multiple contexts (including one detected) for " +
  93. "'how to fix' section. To trigger an issue using this rule you need to scan any text file with a line containing %s keyword.",
  94. EducationWithDetectedContextSensor.EDUCATION_WITH_DETECTED_CONTEXT_RULE_KEY));
  95. addNonContextualizedDescriptionSections(ruleWithMultipleContexts);
  96. for (String context : CONTEXTS) {
  97. ruleWithMultipleContexts.addDescriptionSection(descriptionHowToFixSectionWithContext(context));
  98. }
  99. }
  100. private static void addNonContextualizedDescriptionSections(NewRule newRule) {
  101. newRule.addDescriptionSection(descriptionSection(INTRODUCTION_SECTION_KEY))
  102. .addDescriptionSection(descriptionSection(ROOT_CAUSE_SECTION_KEY))
  103. .addDescriptionSection(descriptionSection(ASSESS_THE_PROBLEM_SECTION_KEY))
  104. .addDescriptionSection(descriptionSection(RESOURCES_SECTION_KEY))
  105. .addDescriptionSection(descriptionSection(IGNORED_FAKE_SECTION));
  106. }
  107. private static RuleDescriptionSection descriptionHowToFixSectionWithContext(String context) {
  108. var ruleContext = new org.sonar.api.server.rule.Context(context, context);
  109. return RuleDescriptionSection.builder()
  110. .sectionKey(HOW_TO_FIX_SECTION_KEY)
  111. .context(ruleContext)
  112. .htmlContent(String.format("%s: %s", HOW_TO_FIX_SECTION_KEY, HTML_LOREM_IPSUM))
  113. .build();
  114. }
  115. private static RuleDescriptionSection descriptionSection(String sectionKey) {
  116. return RuleDescriptionSection.builder()
  117. .sectionKey(sectionKey)
  118. .htmlContent(String.format("%s: %s", sectionKey, HTML_LOREM_IPSUM))
  119. .build();
  120. }
  121. }