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.

ActiveRulesProvider.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.scanner.rule;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import org.sonar.api.batch.rule.LoadedActiveRule;
  28. import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
  29. import org.sonar.api.batch.rule.internal.DefaultActiveRules;
  30. import org.sonar.api.batch.rule.internal.NewActiveRule;
  31. import org.sonar.api.rule.RuleKey;
  32. import org.sonar.api.utils.log.Logger;
  33. import org.sonar.api.utils.log.Loggers;
  34. import org.sonar.api.utils.log.Profiler;
  35. import org.springframework.context.annotation.Bean;
  36. /**
  37. * Loads the rules that are activated on the Quality profiles
  38. * used by the current project and builds {@link org.sonar.api.batch.rule.ActiveRules}.
  39. */
  40. public class ActiveRulesProvider {
  41. private static final Logger LOG = Loggers.get(ActiveRulesProvider.class);
  42. private static final String LOG_MSG = "Load active rules";
  43. @Bean("ActiveRules")
  44. public DefaultActiveRules provide(ActiveRulesLoader loader, QualityProfiles qProfiles) {
  45. Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG);
  46. DefaultActiveRules activeRules = load(loader, qProfiles);
  47. profiler.stopInfo();
  48. return activeRules;
  49. }
  50. private static DefaultActiveRules load(ActiveRulesLoader loader, QualityProfiles qProfiles) {
  51. Collection<String> qProfileKeys = getKeys(qProfiles);
  52. Set<RuleKey> loadedRulesKey = new HashSet<>();
  53. ActiveRulesBuilder builder = new ActiveRulesBuilder();
  54. for (String qProfileKey : qProfileKeys) {
  55. Collection<LoadedActiveRule> qProfileRules = load(loader, qProfileKey);
  56. for (LoadedActiveRule r : qProfileRules) {
  57. if (!loadedRulesKey.contains(r.getRuleKey())) {
  58. loadedRulesKey.add(r.getRuleKey());
  59. builder.addRule(transform(r, qProfileKey, r.getDeprecatedKeys()));
  60. }
  61. }
  62. }
  63. return builder.build();
  64. }
  65. private static NewActiveRule transform(LoadedActiveRule activeRule, String qProfileKey, Set<RuleKey> deprecatedKeys) {
  66. NewActiveRule.Builder builder = new NewActiveRule.Builder();
  67. builder
  68. .setRuleKey(activeRule.getRuleKey())
  69. .setName(activeRule.getName())
  70. .setSeverity(activeRule.getSeverity())
  71. .setCreatedAt(activeRule.getCreatedAt())
  72. .setUpdatedAt(activeRule.getUpdatedAt())
  73. .setLanguage(activeRule.getLanguage())
  74. .setInternalKey(activeRule.getInternalKey())
  75. .setTemplateRuleKey(activeRule.getTemplateRuleKey())
  76. .setQProfileKey(qProfileKey)
  77. .setDeprecatedKeys(deprecatedKeys);
  78. // load parameters
  79. if (activeRule.getParams() != null) {
  80. for (Map.Entry<String, String> params : activeRule.getParams().entrySet()) {
  81. builder.setParam(params.getKey(), params.getValue());
  82. }
  83. }
  84. return builder.build();
  85. }
  86. private static List<LoadedActiveRule> load(ActiveRulesLoader loader, String qProfileKey) {
  87. return loader.load(qProfileKey);
  88. }
  89. private static Collection<String> getKeys(QualityProfiles qProfiles) {
  90. List<String> keys = new ArrayList<>(qProfiles.findAll().size());
  91. for (QProfile qp : qProfiles.findAll()) {
  92. keys.add(qp.getKey());
  93. }
  94. return keys;
  95. }
  96. }