3 * Copyright (C) 2009-2022 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.sonarsource.governance.projectdump.protobuf.ProjectDump;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import org.sonar.api.utils.log.Loggers;
27 import org.sonar.ce.task.projectexport.steps.DumpElement;
28 import org.sonar.ce.task.projectexport.steps.DumpWriter;
29 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
30 import org.sonar.ce.task.projectexport.steps.StreamWriter;
31 import org.sonar.ce.task.step.ComputationStep;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
35 import static java.lang.String.format;
36 import static org.sonar.ce.task.projectexport.util.ResultSetUtils.defaultIfNull;
37 import static org.sonar.ce.task.projectexport.util.ResultSetUtils.emptyIfNull;
39 public class ExportAdHocRulesStep implements ComputationStep {
40 private static final String RULE_STATUS_REMOVED = "REMOVED";
41 private static final String ISSUE_STATUS_CLOSED = "CLOSED";
43 private static final String QUERY = "select" +
44 " r.uuid, r.plugin_key, r.plugin_rule_key, r.plugin_name, r.name, r.status, r.rule_type, r.scope, rm.rule_uuid, rm.ad_hoc_name," +
45 " rm.ad_hoc_description,rm.ad_hoc_severity, rm.ad_hoc_type" +
47 " left join rules_metadata rm on rm.rule_uuid = r.uuid" +
48 " inner join issues i on r.uuid = i.rule_uuid and r.status <> ? and r.is_ad_hoc = ?" +
49 " left join components p on p.uuid = i.project_uuid" +
50 " left join project_branches pb on pb.uuid = p.uuid" +
51 " where pb.project_uuid = ? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge = ?" +
52 " and i.status <> ?" +
56 private final DbClient dbClient;
57 private final ProjectHolder projectHolder;
58 private final DumpWriter dumpWriter;
60 public ExportAdHocRulesStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter) {
61 this.dbClient = dbClient;
62 this.projectHolder = projectHolder;
63 this.dumpWriter = dumpWriter;
67 public void execute(Context context) {
70 StreamWriter<ProjectDump.AdHocRule> output = dumpWriter.newStreamWriter(DumpElement.AD_HOC_RULES);
71 DbSession dbSession = dbClient.openSession(false);
72 PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, QUERY)) {
73 stmt.setString(1, RULE_STATUS_REMOVED);
74 stmt.setBoolean(2, true);
75 stmt.setString(3, projectHolder.projectDto().getUuid());
76 stmt.setBoolean(4, true);
77 stmt.setString(5, ISSUE_STATUS_CLOSED);
78 try (ResultSet rs = stmt.executeQuery()) {
79 ProjectDump.AdHocRule.Builder adHocRuleBuilder = ProjectDump.AdHocRule.newBuilder();
81 ProjectDump.AdHocRule rule = convertToAdHocRule(rs, adHocRuleBuilder);
85 Loggers.get(getClass()).debug("{} ad-hoc rules exported", count);
87 } catch (Exception e) {
88 throw new IllegalStateException(format("Ad-hoc rules export failed after processing %d rules successfully", count), e);
92 private static ProjectDump.AdHocRule convertToAdHocRule(ResultSet rs, ProjectDump.AdHocRule.Builder builder) throws SQLException {
94 boolean metadataExistsForCurrentRule = !rs.wasNull();
98 .setRef(rs.getString(1))
99 .setPluginKey(emptyIfNull(rs, 2))
100 .setPluginRuleKey(rs.getString(3))
101 .setPluginName(rs.getString(4))
102 .setName(emptyIfNull(rs, 5))
103 .setStatus(emptyIfNull(rs, 6))
104 .setType(rs.getInt(7))
105 .setScope(rs.getString(8));
106 if (metadataExistsForCurrentRule) {
107 builder.setMetadata(ProjectDump.AdHocRule.RuleMetadata.newBuilder()
108 .setAdHocName(emptyIfNull(rs, 10))
109 .setAdHocDescription(emptyIfNull(rs, 11))
110 .setAdHocSeverity(emptyIfNull(rs, 12))
111 .setAdHocType(defaultIfNull(rs, 13, 0))
115 return builder.build();
119 public String getDescription() {
120 return "Export ad-hoc rules";