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.

BuiltInQProfileUpdateImpl.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.server.qualityprofile;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.stream.Stream;
  27. import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
  28. import org.sonar.core.util.stream.MoreCollectors;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.qualityprofile.ActiveRuleDto;
  32. import org.sonar.db.qualityprofile.RulesProfileDto;
  33. import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
  34. import static org.sonar.core.util.stream.MoreCollectors.toSet;
  35. public class BuiltInQProfileUpdateImpl implements BuiltInQProfileUpdate {
  36. private final DbClient dbClient;
  37. private final RuleActivator ruleActivator;
  38. private final ActiveRuleIndexer activeRuleIndexer;
  39. public BuiltInQProfileUpdateImpl(DbClient dbClient, RuleActivator ruleActivator, ActiveRuleIndexer activeRuleIndexer) {
  40. this.dbClient = dbClient;
  41. this.ruleActivator = ruleActivator;
  42. this.activeRuleIndexer = activeRuleIndexer;
  43. }
  44. public List<ActiveRuleChange> update(DbSession dbSession, BuiltInQProfile builtInDefinition, RulesProfileDto initialRuleProfile) {
  45. // Keep reference to all the activated rules before update
  46. Set<String> deactivatedRuleUuids = dbClient.activeRuleDao().selectByRuleProfile(dbSession, initialRuleProfile)
  47. .stream()
  48. .map(ActiveRuleDto::getRuleUuid)
  49. .collect(MoreCollectors.toHashSet());
  50. // all rules, including those which are removed from built-in profile
  51. Set<String> ruleUuids = Stream.concat(
  52. deactivatedRuleUuids.stream(),
  53. builtInDefinition.getActiveRules().stream().map(BuiltInQProfile.ActiveRule::getRuleUuid))
  54. .collect(toSet());
  55. Collection<RuleActivation> activations = new ArrayList<>();
  56. for (BuiltInQProfile.ActiveRule ar : builtInDefinition.getActiveRules()) {
  57. RuleActivation activation = convert(ar);
  58. activations.add(activation);
  59. deactivatedRuleUuids.remove(activation.getRuleUuid());
  60. }
  61. RuleActivationContext context = ruleActivator.createContextForBuiltInProfile(dbSession, initialRuleProfile, ruleUuids);
  62. List<ActiveRuleChange> changes = new ArrayList<>();
  63. for (RuleActivation activation : activations) {
  64. changes.addAll(ruleActivator.activate(dbSession, activation, context));
  65. }
  66. // these rules are no longer part of the built-in profile
  67. deactivatedRuleUuids.forEach(ruleUuid -> changes.addAll(ruleActivator.deactivate(dbSession, context, ruleUuid, false)));
  68. activeRuleIndexer.commitAndIndex(dbSession, changes);
  69. return changes;
  70. }
  71. private static RuleActivation convert(BuiltInQProfile.ActiveRule ar) {
  72. Map<String, String> params = ar.getParams().stream()
  73. .collect(MoreCollectors.uniqueIndex(BuiltInQualityProfilesDefinition.OverriddenParam::key, BuiltInQualityProfilesDefinition.OverriddenParam::overriddenValue));
  74. return RuleActivation.create(ar.getRuleUuid(), ar.getSeverity(), params);
  75. }
  76. }