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.issue;
22 import com.google.protobuf.ByteString;
23 import com.google.protobuf.InvalidProtocolBufferException;
24 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.util.Objects;
29 import org.sonar.api.rule.RuleKey;
30 import org.sonar.api.utils.log.Loggers;
31 import org.sonar.ce.task.projectexport.component.ComponentRepository;
32 import org.sonar.ce.task.projectexport.rule.Rule;
33 import org.sonar.ce.task.projectexport.rule.RuleRepository;
34 import org.sonar.ce.task.projectexport.steps.DumpElement;
35 import org.sonar.ce.task.projectexport.steps.DumpElement.IssueDumpElement;
36 import org.sonar.ce.task.projectexport.steps.DumpWriter;
37 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
38 import org.sonar.ce.task.projectexport.steps.StreamWriter;
39 import org.sonar.ce.task.step.ComputationStep;
40 import org.sonar.db.DatabaseUtils;
41 import org.sonar.db.DbClient;
42 import org.sonar.db.DbSession;
43 import org.sonar.db.protobuf.DbIssues;
45 import static java.lang.String.format;
46 import static org.sonar.ce.task.projectexport.util.ResultSetUtils.defaultIfNull;
47 import static org.sonar.ce.task.projectexport.util.ResultSetUtils.emptyIfNull;
49 public class ExportIssuesStep implements ComputationStep {
50 private static final String RULE_STATUS_REMOVED = "REMOVED";
51 private static final String ISSUE_STATUS_CLOSED = "CLOSED";
53 // ordered by rule_id to reduce calls to RuleRepository
54 private static final String QUERY = "select" +
55 " i.kee, r.uuid, r.plugin_rule_key, r.plugin_name, i.issue_type," +
56 " i.component_uuid, i.message, i.line, i.checksum, i.status," +
57 " i.resolution, i.severity, i.manual_severity, i.gap, effort," +
58 " i.assignee, i.author_login, i.tags, i.issue_creation_date," +
59 " i.issue_update_date, i.issue_close_date, i.locations, i.project_uuid" +
61 " join rules r on r.uuid = i.rule_uuid and r.status <> ?" +
62 " join components p on p.uuid = i.project_uuid" +
63 " join project_branches pb on pb.uuid = p.uuid" +
64 " where pb.project_uuid = ? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge=?" +
65 " and i.status <> ?" +
67 " i.rule_uuid asc, i.created_at asc";
69 private final DbClient dbClient;
70 private final ProjectHolder projectHolder;
71 private final DumpWriter dumpWriter;
72 private final RuleRegistrar ruleRegistrar;
73 private final ComponentRepository componentRepository;
75 public ExportIssuesStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter, RuleRepository ruleRepository,
76 ComponentRepository componentRepository) {
77 this.dbClient = dbClient;
78 this.projectHolder = projectHolder;
79 this.dumpWriter = dumpWriter;
80 this.componentRepository = componentRepository;
81 this.ruleRegistrar = new RuleRegistrar(ruleRepository);
85 public String getDescription() {
86 return "Export issues";
90 public void execute(Context context) {
93 StreamWriter<ProjectDump.Issue> output = dumpWriter.newStreamWriter(DumpElement.ISSUES);
94 DbSession dbSession = dbClient.openSession(false);
95 PreparedStatement stmt = createStatement(dbSession);
96 ResultSet rs = stmt.executeQuery()) {
97 ProjectDump.Issue.Builder builder = ProjectDump.Issue.newBuilder();
99 ProjectDump.Issue issue = toIssue(builder, rs);
103 Loggers.get(getClass()).debug("{} issues exported", count);
104 } catch (Exception e) {
105 throw new IllegalStateException(format("Issue export failed after processing %d issues successfully", count), e);
109 private PreparedStatement createStatement(DbSession dbSession) throws SQLException {
110 PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, QUERY);
112 stmt.setString(1, RULE_STATUS_REMOVED);
113 stmt.setString(2, projectHolder.projectDto().getUuid());
114 stmt.setBoolean(3, true);
115 stmt.setString(4, ISSUE_STATUS_CLOSED);
117 } catch (Exception t) {
118 DatabaseUtils.closeQuietly(stmt);
123 private ProjectDump.Issue toIssue(ProjectDump.Issue.Builder builder, ResultSet rs) throws SQLException {
125 String issueUuid = rs.getString(1);
126 setRule(builder, rs);
129 .setType(rs.getInt(5))
130 .setComponentRef(componentRepository.getRef(rs.getString(6)))
131 .setMessage(emptyIfNull(rs, 7))
132 .setLine(rs.getInt(8))
133 .setChecksum(emptyIfNull(rs, 9))
134 .setStatus(emptyIfNull(rs, 10))
135 .setResolution(emptyIfNull(rs, 11))
136 .setSeverity(emptyIfNull(rs, 12))
137 .setManualSeverity(rs.getBoolean(13))
138 .setGap(defaultIfNull(rs, 14, IssueDumpElement.NO_GAP))
139 .setEffort(defaultIfNull(rs, 15, IssueDumpElement.NO_EFFORT))
140 .setAssignee(emptyIfNull(rs, 16))
141 .setAuthor(emptyIfNull(rs, 17))
142 .setTags(emptyIfNull(rs, 18))
143 .setIssueCreatedAt(rs.getLong(19))
144 .setIssueUpdatedAt(rs.getLong(20))
145 .setIssueClosedAt(rs.getLong(21))
146 .setProjectUuid(rs.getString(23));
147 setLocations(builder, rs, issueUuid);
148 return builder.build();
151 private void setRule(ProjectDump.Issue.Builder builder, ResultSet rs) throws SQLException {
152 String ruleUuid = rs.getString(2);
153 String ruleKey = rs.getString(3);
154 String repositoryKey = rs.getString(4);
155 builder.setRuleRef(ruleRegistrar.register(ruleUuid, repositoryKey, ruleKey).getRef());
158 private static void setLocations(ProjectDump.Issue.Builder builder, ResultSet rs, String issueUuid) throws SQLException {
160 byte[] bytes = rs.getBytes(22);
162 // fail fast, ensure we can read data from DB
163 DbIssues.Locations.parseFrom(bytes);
164 builder.setLocations(ByteString.copyFrom(bytes));
166 } catch (InvalidProtocolBufferException e) {
167 throw new IllegalStateException(format("Fail to read locations from DB for issue %s", issueUuid), e);
171 private static class RuleRegistrar {
172 private final RuleRepository ruleRepository;
173 private Rule previousRule = null;
174 private String previousRuleUuid = null;
176 private RuleRegistrar(RuleRepository ruleRepository) {
177 this.ruleRepository = ruleRepository;
180 public Rule register(String ruleUuid, String repositoryKey, String ruleKey) {
181 if (Objects.equals(previousRuleUuid, ruleUuid)) {
184 return lookup(ruleUuid, RuleKey.of(repositoryKey, ruleKey));
187 private Rule lookup(String ruleUuid, RuleKey ruleKey) {
188 this.previousRule = ruleRepository.register(ruleUuid, ruleKey);
189 this.previousRuleUuid = ruleUuid;