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.

AdHocRuleCreator.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.ce.task.projectanalysis.issue;
  21. import com.google.common.base.Preconditions;
  22. import java.util.Map;
  23. import java.util.Objects;
  24. import java.util.Optional;
  25. import java.util.stream.Collectors;
  26. import org.sonar.api.issue.impact.Severity;
  27. import org.sonar.api.issue.impact.SoftwareQuality;
  28. import org.sonar.api.rules.CleanCodeAttribute;
  29. import org.sonar.api.rules.RuleType;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.core.util.UuidFactory;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.issue.ImpactDto;
  35. import org.sonar.db.rule.RuleDao;
  36. import org.sonar.db.rule.RuleDto;
  37. import org.sonar.server.rule.index.RuleIndexer;
  38. import static java.util.Objects.requireNonNull;
  39. import static org.apache.commons.lang3.StringUtils.substring;
  40. import static org.sonar.api.rule.RuleStatus.READY;
  41. import static org.sonar.db.rule.RuleDto.Scope.ALL;
  42. public class AdHocRuleCreator {
  43. private static final int MAX_LENGTH_AD_HOC_NAME = 200;
  44. private static final int MAX_LENGTH_AD_HOC_DESC = 16_777_215;
  45. private final DbClient dbClient;
  46. private final System2 system2;
  47. private final RuleIndexer ruleIndexer;
  48. private final UuidFactory uuidFactory;
  49. public AdHocRuleCreator(DbClient dbClient, System2 system2, RuleIndexer ruleIndexer, UuidFactory uuidFactory) {
  50. this.dbClient = dbClient;
  51. this.system2 = system2;
  52. this.ruleIndexer = ruleIndexer;
  53. this.uuidFactory = uuidFactory;
  54. }
  55. /**
  56. * Persists a new add hoc rule in the DB and indexes it.
  57. * @return the rule that was inserted in the DB, which <b>includes the generated ID</b>.
  58. */
  59. public RuleDto persistAndIndex(DbSession dbSession, NewAdHocRule adHoc) {
  60. RuleDao dao = dbClient.ruleDao();
  61. long now = system2.now();
  62. RuleDto ruleDtoToUpdate = findOrCreateRuleDto(dbSession, adHoc, dao, now);
  63. boolean changed = false;
  64. if (adHoc.hasDetails()) {
  65. if (!Objects.equals(ruleDtoToUpdate.getAdHocName(), adHoc.getName())) {
  66. ruleDtoToUpdate.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME));
  67. changed = true;
  68. }
  69. if (!Objects.equals(ruleDtoToUpdate.getAdHocDescription(), adHoc.getDescription())) {
  70. ruleDtoToUpdate.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC));
  71. changed = true;
  72. }
  73. if (!Objects.equals(ruleDtoToUpdate.getAdHocSeverity(), adHoc.getSeverity())) {
  74. ruleDtoToUpdate.setAdHocSeverity(adHoc.getSeverity());
  75. changed = true;
  76. }
  77. RuleType ruleType = requireNonNull(adHoc.getRuleType(), "Rule type should not be null");
  78. if (!Objects.equals(ruleDtoToUpdate.getAdHocType(), ruleType.getDbConstant())) {
  79. ruleDtoToUpdate.setAdHocType(ruleType);
  80. changed = true;
  81. }
  82. }
  83. CleanCodeAttribute cleanCodeAttribute = adHoc.getCleanCodeAttribute();
  84. if (!Objects.equals(ruleDtoToUpdate.getCleanCodeAttribute(), cleanCodeAttribute)) {
  85. ruleDtoToUpdate.setCleanCodeAttribute(cleanCodeAttribute);
  86. changed = true;
  87. }
  88. Map<SoftwareQuality, Severity> currentImpacts = ruleDtoToUpdate.getDefaultImpacts().stream()
  89. .collect(Collectors.toMap(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity));
  90. if (!Objects.equals(currentImpacts, adHoc.getDefaultImpacts())) {
  91. ruleDtoToUpdate.replaceAllDefaultImpacts(adHoc.getDefaultImpacts().entrySet().stream()
  92. .map(i -> createImpactDto(i.getKey(), i.getValue()))
  93. .toList());
  94. changed = true;
  95. }
  96. if (changed) {
  97. ruleDtoToUpdate.setUpdatedAt(now);
  98. dao.update(dbSession, ruleDtoToUpdate);
  99. }
  100. RuleDto ruleDto = dao.selectOrFailByKey(dbSession, adHoc.getKey());
  101. ruleIndexer.commitAndIndex(dbSession, ruleDto.getUuid());
  102. return ruleDto;
  103. }
  104. private static ImpactDto createImpactDto(SoftwareQuality softwareQuality, Severity severity) {
  105. return new ImpactDto().setSoftwareQuality(softwareQuality).setSeverity(severity);
  106. }
  107. private RuleDto findOrCreateRuleDto(DbSession dbSession, NewAdHocRule adHoc, RuleDao dao, long now) {
  108. Optional<RuleDto> existingRuleDtoOpt = dbClient.ruleDao().selectByKey(dbSession, adHoc.getKey());
  109. if (existingRuleDtoOpt.isEmpty()) {
  110. RuleDto ruleDto = new RuleDto()
  111. .setUuid(uuidFactory.create())
  112. .setRuleKey(adHoc.getKey())
  113. .setIsExternal(true)
  114. .setIsAdHoc(true)
  115. .setName(adHoc.getEngineId() + ":" + adHoc.getRuleId())
  116. .setScope(ALL)
  117. .setStatus(READY)
  118. .setCreatedAt(now)
  119. .setUpdatedAt(now);
  120. dao.insert(dbSession, ruleDto);
  121. return ruleDto;
  122. } else {
  123. RuleDto ruleDto = existingRuleDtoOpt.get();
  124. Preconditions.checkState(ruleDto.isExternal() && ruleDto.isAdHoc());
  125. return ruleDto;
  126. }
  127. }
  128. }