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.projectexport.rule;
22 import com.hazelcast.internal.util.MutableLong;
23 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
24 import java.util.List;
26 import org.apache.ibatis.cursor.Cursor;
27 import org.slf4j.LoggerFactory;
28 import org.sonar.api.rules.CleanCodeAttribute;
29 import org.sonar.ce.task.projectexport.steps.DumpElement;
30 import org.sonar.ce.task.projectexport.steps.DumpWriter;
31 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
32 import org.sonar.ce.task.projectexport.steps.StreamWriter;
33 import org.sonar.ce.task.step.ComputationStep;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.issue.ImpactDto;
37 import org.sonar.db.project.ProjectExportMapper;
38 import org.sonar.db.rule.RuleDto;
40 import static java.lang.String.format;
42 public class ExportAdHocRulesStep implements ComputationStep {
44 private final DbClient dbClient;
45 private final ProjectHolder projectHolder;
46 private final DumpWriter dumpWriter;
48 public ExportAdHocRulesStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter) {
49 this.dbClient = dbClient;
50 this.projectHolder = projectHolder;
51 this.dumpWriter = dumpWriter;
55 public void execute(Context context) {
56 MutableLong count = MutableLong.valueOf(0L);
58 StreamWriter<ProjectDump.AdHocRule> output = dumpWriter.newStreamWriter(DumpElement.AD_HOC_RULES);
59 DbSession dbSession = dbClient.openSession(false);
60 Cursor<RuleDto> ruleDtoCursor = dbSession.getMapper(ProjectExportMapper.class).scrollAdhocRulesForExport(projectHolder.projectDto().getUuid())
62 ProjectDump.AdHocRule.Builder adHocRuleBuilder = ProjectDump.AdHocRule.newBuilder();
65 ProjectDump.AdHocRule rule = convertToAdHocRule(ruleDto, adHocRuleBuilder);
69 LoggerFactory.getLogger(getClass()).debug("{} ad-hoc rules exported", count.value);
71 } catch (Exception e) {
72 throw new IllegalStateException(format("Ad-hoc rules export failed after processing %d rules successfully", count.value), e);
76 private static ProjectDump.AdHocRule convertToAdHocRule(RuleDto ruleDto, ProjectDump.AdHocRule.Builder builder) {
77 CleanCodeAttribute cleanCodeAttribute = ruleDto.getCleanCodeAttribute();
80 .setRef(ruleDto.getUuid())
81 .setPluginKey(ruleDto.getPluginKey())
82 .setPluginRuleKey(ruleDto.getKey().rule())
83 .setPluginName(ruleDto.getRepositoryKey())
84 .setName(ruleDto.getName())
85 .setStatus(ruleDto.getStatus().name())
86 .setType(ruleDto.getType())
87 .setScope(ruleDto.getScope().name())
88 .setMetadata(buildMetadata(ruleDto))
89 .setCleanCodeAttribute(cleanCodeAttribute != null ? cleanCodeAttribute.name() : null)
90 .addAllImpacts(buildImpacts(ruleDto.getDefaultImpacts()))
94 private static List<ProjectDump.Impact> buildImpacts(Set<ImpactDto> defaultImpacts) {
97 .map(i -> ProjectDump.Impact.newBuilder()
98 .setSoftwareQuality(ProjectDump.SoftwareQuality.valueOf(i.getSoftwareQuality().name()))
99 .setSeverity(ProjectDump.Severity.valueOf(i.getSeverity().name())).build())
103 private static ProjectDump.AdHocRule.RuleMetadata buildMetadata(RuleDto ruleDto) {
104 return ProjectDump.AdHocRule.RuleMetadata.newBuilder()
105 .setAdHocName(ruleDto.getAdHocName())
106 .setAdHocDescription(ruleDto.getAdHocDescription())
107 .setAdHocSeverity(ruleDto.getAdHocSeverity())
108 .setAdHocType(ruleDto.getAdHocType())
113 public String getDescription() {
114 return "Export ad-hoc rules";