]> source.dussan.org Git - sonarqube.git/blob
a20a5e5b8cd0072432b743bcd6551609efdc3858
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.projectexport.rule;
21
22 import com.hazelcast.internal.util.MutableLong;
23 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.Set;
27 import org.apache.ibatis.cursor.Cursor;
28 import org.slf4j.LoggerFactory;
29 import org.sonar.api.rules.CleanCodeAttribute;
30 import org.sonar.ce.task.projectexport.steps.DumpElement;
31 import org.sonar.ce.task.projectexport.steps.DumpWriter;
32 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
33 import org.sonar.ce.task.projectexport.steps.StreamWriter;
34 import org.sonar.ce.task.step.ComputationStep;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.issue.ImpactDto;
38 import org.sonar.db.project.ProjectExportMapper;
39 import org.sonar.db.rule.RuleDto;
40
41 import static java.lang.String.format;
42
43 public class ExportAdHocRulesStep implements ComputationStep {
44
45   private final DbClient dbClient;
46   private final ProjectHolder projectHolder;
47   private final DumpWriter dumpWriter;
48
49   public ExportAdHocRulesStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter) {
50     this.dbClient = dbClient;
51     this.projectHolder = projectHolder;
52     this.dumpWriter = dumpWriter;
53   }
54
55   @Override
56   public void execute(Context context) {
57     MutableLong count = MutableLong.valueOf(0L);
58     try (
59       StreamWriter<ProjectDump.AdHocRule> output = dumpWriter.newStreamWriter(DumpElement.AD_HOC_RULES);
60       DbSession dbSession = dbClient.openSession(false);
61       Cursor<RuleDto> ruleDtoCursor = dbSession.getMapper(ProjectExportMapper.class).scrollAdhocRulesForExport(projectHolder.projectDto().getUuid())) {
62       ProjectDump.AdHocRule.Builder adHocRuleBuilder = ProjectDump.AdHocRule.newBuilder();
63       ruleDtoCursor
64         .forEach(ruleDto -> {
65           ProjectDump.AdHocRule rule = convertToAdHocRule(ruleDto, adHocRuleBuilder);
66           output.write(rule);
67           count.getAndInc();
68         });
69       LoggerFactory.getLogger(getClass()).debug("{} ad-hoc rules exported", count.value);
70
71     } catch (Exception e) {
72       throw new IllegalStateException(format("Ad-hoc rules export failed after processing %d rules successfully", count.value), e);
73     }
74   }
75
76   private static ProjectDump.AdHocRule convertToAdHocRule(RuleDto ruleDto, ProjectDump.AdHocRule.Builder builder) {
77     CleanCodeAttribute cleanCodeAttribute = ruleDto.getCleanCodeAttribute();
78     return builder
79       .clear()
80       .setRef(ruleDto.getUuid())
81       .setPluginKey(Optional.of(ruleDto).map(RuleDto::getPluginKey).orElse(""))
82       .setPluginRuleKey(ruleDto.getKey().rule())
83       .setPluginName(ruleDto.getRepositoryKey())
84       .setName(Optional.of(ruleDto).map(RuleDto::getName).orElse(""))
85       .setStatus(Optional.of(ruleDto).map(RuleDto::getStatus).map(Enum::name).orElse(""))
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()))
91       .build();
92   }
93
94   private static List<ProjectDump.Impact> buildImpacts(Set<ImpactDto> defaultImpacts) {
95     return defaultImpacts
96       .stream()
97       .map(i -> ProjectDump.Impact.newBuilder()
98         .setSoftwareQuality(ProjectDump.SoftwareQuality.valueOf(i.getSoftwareQuality().name()))
99         .setSeverity(ProjectDump.Severity.valueOf(i.getSeverity().name())).build())
100       .toList();
101   }
102
103   private static ProjectDump.AdHocRule.RuleMetadata buildMetadata(RuleDto ruleDto) {
104     Optional<RuleDto> rule = Optional.of(ruleDto);
105     return ProjectDump.AdHocRule.RuleMetadata.newBuilder()
106       .setAdHocName(rule.map(RuleDto::getAdHocName).orElse(""))
107       .setAdHocDescription(rule.map(RuleDto::getAdHocDescription).orElse(""))
108       .setAdHocSeverity(rule.map(RuleDto::getAdHocSeverity).orElse(""))
109       .setAdHocType(rule.map(RuleDto::getAdHocType).orElse(0))
110       .build();
111   }
112
113   @Override
114   public String getDescription() {
115     return "Export ad-hoc rules";
116   }
117 }