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.ce.task.projectexport.steps.DumpElement;
29 import org.sonar.ce.task.projectexport.steps.DumpWriter;
30 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
31 import org.sonar.ce.task.projectexport.steps.StreamWriter;
32 import org.sonar.ce.task.step.ComputationStep;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.issue.ImpactDto;
36 import org.sonar.db.project.ProjectExportMapper;
37 import org.sonar.db.rule.RuleDto;
39 import static java.lang.String.format;
41 public class ExportAdHocRulesStep implements ComputationStep {
43 private final DbClient dbClient;
44 private final ProjectHolder projectHolder;
45 private final DumpWriter dumpWriter;
47 public ExportAdHocRulesStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter) {
48 this.dbClient = dbClient;
49 this.projectHolder = projectHolder;
50 this.dumpWriter = dumpWriter;
54 public void execute(Context context) {
55 MutableLong count = MutableLong.valueOf(0L);
57 StreamWriter<ProjectDump.AdHocRule> output = dumpWriter.newStreamWriter(DumpElement.AD_HOC_RULES);
58 DbSession dbSession = dbClient.openSession(false);
59 Cursor<RuleDto> ruleDtoCursor = dbSession.getMapper(ProjectExportMapper.class).scrollAdhocRulesForExport(projectHolder.projectDto().getUuid())
61 ProjectDump.AdHocRule.Builder adHocRuleBuilder = ProjectDump.AdHocRule.newBuilder();
64 ProjectDump.AdHocRule rule = convertToAdHocRule(ruleDto, adHocRuleBuilder);
68 LoggerFactory.getLogger(getClass()).debug("{} ad-hoc rules exported", count.value);
70 } catch (Exception e) {
71 throw new IllegalStateException(format("Ad-hoc rules export failed after processing %d rules successfully", count.value), e);
75 private static ProjectDump.AdHocRule convertToAdHocRule(RuleDto ruleDto, ProjectDump.AdHocRule.Builder builder) {
78 .setRef(ruleDto.getUuid())
79 .setPluginKey(ruleDto.getPluginKey())
80 .setPluginRuleKey(ruleDto.getKey().rule())
81 .setPluginName(ruleDto.getRepositoryKey())
82 .setName(ruleDto.getName())
83 .setStatus(ruleDto.getStatus().name())
84 .setType(ruleDto.getType())
85 .setScope(ruleDto.getScope().name())
86 .setMetadata(buildMetadata(ruleDto))
87 .setCleanCodeAttribute(ruleDto.getCleanCodeAttribute() != null ? ruleDto.getCleanCodeAttribute().name() : null)
88 .addAllImpacts(buildImpacts(ruleDto.getDefaultImpacts()))
92 private static List<ProjectDump.Impact> buildImpacts(Set<ImpactDto> defaultImpacts) {
95 .map(i -> ProjectDump.Impact.newBuilder()
96 .setSoftwareQuality(ProjectDump.SoftwareQuality.valueOf(i.getSoftwareQuality().name()))
97 .setSeverity(ProjectDump.Severity.valueOf(i.getSeverity().name())).build())
101 private static ProjectDump.AdHocRule.RuleMetadata buildMetadata(RuleDto ruleDto) {
102 return ProjectDump.AdHocRule.RuleMetadata.newBuilder()
103 .setAdHocName(ruleDto.getAdHocName())
104 .setAdHocDescription(ruleDto.getAdHocDescription())
105 .setAdHocSeverity(ruleDto.getAdHocSeverity())
106 .setAdHocType(ruleDto.getAdHocType())
111 public String getDescription() {
112 return "Export ad-hoc rules";