3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.issue;
22 import com.google.common.base.Preconditions;
24 import java.util.Objects;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import org.sonar.api.issue.impact.Severity;
28 import org.sonar.api.issue.impact.SoftwareQuality;
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.core.util.Uuids;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.issue.ImpactDto;
36 import org.sonar.db.rule.RuleDao;
37 import org.sonar.db.rule.RuleDto;
38 import org.sonar.server.rule.index.RuleIndexer;
40 import static java.util.Objects.requireNonNull;
41 import static org.apache.commons.lang.StringUtils.substring;
42 import static org.sonar.api.rule.RuleStatus.READY;
43 import static org.sonar.db.rule.RuleDto.Scope.ALL;
45 public class AdHocRuleCreator {
47 private static final int MAX_LENGTH_AD_HOC_NAME = 200;
48 private static final int MAX_LENGTH_AD_HOC_DESC = 16_777_215;
49 private final DbClient dbClient;
50 private final System2 system2;
51 private final RuleIndexer ruleIndexer;
52 private final UuidFactory uuidFactory;
54 public AdHocRuleCreator(DbClient dbClient, System2 system2, RuleIndexer ruleIndexer, UuidFactory uuidFactory) {
55 this.dbClient = dbClient;
56 this.system2 = system2;
57 this.ruleIndexer = ruleIndexer;
58 this.uuidFactory = uuidFactory;
62 * Persists a new add hoc rule in the DB and indexes it.
63 * @return the rule that was inserted in the DB, which <b>includes the generated ID</b>.
65 public RuleDto persistAndIndex(DbSession dbSession, NewAdHocRule adHoc) {
66 RuleDao dao = dbClient.ruleDao();
67 long now = system2.now();
69 RuleDto ruleDtoToUpdate = findOrCreateRuleDto(dbSession, adHoc, dao, now);
71 boolean changed = false;
72 if (adHoc.hasDetails()) {
73 if (!Objects.equals(ruleDtoToUpdate.getAdHocName(), adHoc.getName())) {
74 ruleDtoToUpdate.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME));
77 if (!Objects.equals(ruleDtoToUpdate.getAdHocDescription(), adHoc.getDescription())) {
78 ruleDtoToUpdate.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC));
81 if (!Objects.equals(ruleDtoToUpdate.getAdHocSeverity(), adHoc.getSeverity())) {
82 ruleDtoToUpdate.setAdHocSeverity(adHoc.getSeverity());
85 RuleType ruleType = requireNonNull(adHoc.getRuleType(), "Rule type should not be null");
86 if (!Objects.equals(ruleDtoToUpdate.getAdHocType(), ruleType.getDbConstant())) {
87 ruleDtoToUpdate.setAdHocType(ruleType);
92 if (!Objects.equals(ruleDtoToUpdate.getCleanCodeAttribute(), adHoc.getCleanCodeAttribute())) {
93 ruleDtoToUpdate.setCleanCodeAttribute(adHoc.getCleanCodeAttribute());
97 Map<SoftwareQuality, Severity> currentImpacts = ruleDtoToUpdate.getDefaultImpacts().stream()
98 .collect(Collectors.toMap(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity));
99 if (!Objects.equals(currentImpacts, adHoc.getDefaultImpacts())) {
100 ruleDtoToUpdate.replaceAllDefaultImpacts(adHoc.getDefaultImpacts().entrySet().stream()
101 .map(i -> createImpactDto(i.getKey(), i.getValue()))
107 ruleDtoToUpdate.setUpdatedAt(now);
108 dao.update(dbSession, ruleDtoToUpdate);
111 RuleDto ruleDto = dao.selectOrFailByKey(dbSession, adHoc.getKey());
112 ruleIndexer.commitAndIndex(dbSession, ruleDto.getUuid());
116 private static ImpactDto createImpactDto(SoftwareQuality softwareQuality, Severity severity) {
117 return new ImpactDto().setUuid(Uuids.create()).setSoftwareQuality(softwareQuality).setSeverity(severity);
120 private RuleDto findOrCreateRuleDto(DbSession dbSession, NewAdHocRule adHoc, RuleDao dao, long now) {
121 Optional<RuleDto> existingRuleDtoOpt = dbClient.ruleDao().selectByKey(dbSession, adHoc.getKey());
122 if (existingRuleDtoOpt.isEmpty()) {
123 RuleDto ruleDto = new RuleDto()
124 .setUuid(uuidFactory.create())
125 .setRuleKey(adHoc.getKey())
128 .setName(adHoc.getEngineId() + ":" + adHoc.getRuleId())
133 dao.insert(dbSession, ruleDto);
136 RuleDto ruleDto = existingRuleDtoOpt.get();
137 Preconditions.checkState(ruleDto.isExternal() && ruleDto.isAdHoc());