]> source.dussan.org Git - sonarqube.git/blob
e49c67498bb96bceae3e7e3fa9421d718dff4fed
[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.issue;
21
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.protobuf.ByteString;
24 import com.google.protobuf.InvalidProtocolBufferException;
25 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.Optional;
32 import javax.annotation.Nullable;
33 import org.slf4j.LoggerFactory;
34 import org.sonar.api.rule.RuleKey;
35 import org.sonar.ce.task.projectexport.component.ComponentRepository;
36 import org.sonar.ce.task.projectexport.rule.Rule;
37 import org.sonar.ce.task.projectexport.rule.RuleRepository;
38 import org.sonar.ce.task.projectexport.steps.DumpElement;
39 import org.sonar.ce.task.projectexport.steps.DumpElement.IssueDumpElement;
40 import org.sonar.ce.task.projectexport.steps.DumpWriter;
41 import org.sonar.ce.task.projectexport.steps.ProjectHolder;
42 import org.sonar.ce.task.projectexport.steps.StreamWriter;
43 import org.sonar.ce.task.step.ComputationStep;
44 import org.sonar.db.DatabaseUtils;
45 import org.sonar.db.DbClient;
46 import org.sonar.db.DbSession;
47 import org.sonar.db.protobuf.DbIssues;
48
49 import static java.lang.String.format;
50 import static org.sonar.ce.task.projectexport.util.ResultSetUtils.defaultIfNull;
51 import static org.sonar.ce.task.projectexport.util.ResultSetUtils.emptyIfNull;
52
53 public class ExportIssuesStep implements ComputationStep {
54   private static final String RULE_STATUS_REMOVED = "REMOVED";
55   private static final String ISSUE_STATUS_CLOSED = "CLOSED";
56
57   // ordered by rule_id to reduce calls to RuleRepository
58   private static final String QUERY = "select" +
59     " i.kee, r.uuid, r.plugin_rule_key, r.plugin_name, i.issue_type," +
60     " i.component_uuid, i.message, i.line, i.checksum, i.status," +
61     " i.resolution, i.severity, i.manual_severity, i.gap, effort," +
62     " i.assignee, i.author_login, i.tags, i.issue_creation_date," +
63     " i.issue_update_date, i.issue_close_date, i.locations, i.project_uuid," +
64     " i.rule_description_context_key, i.message_formattings, i.code_variants, " +
65     " ii.software_quality, ii.severity" +
66     " from issues i" +
67     " join rules r on r.uuid = i.rule_uuid and r.status <> ?" +
68     " join components p on p.uuid = i.project_uuid" +
69     " join project_branches pb on pb.uuid = p.uuid" +
70     " left outer join issues_impacts ii on i.kee = ii.issue_key" +
71     " where pb.project_uuid = ? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge=?" +
72     " and i.status <> ?" +
73     " order by" +
74     " i.rule_uuid asc, i.kee, i.created_at asc";
75
76   private final DbClient dbClient;
77   private final ProjectHolder projectHolder;
78   private final DumpWriter dumpWriter;
79   private final RuleRegistrar ruleRegistrar;
80   private final ComponentRepository componentRepository;
81
82   public ExportIssuesStep(DbClient dbClient, ProjectHolder projectHolder, DumpWriter dumpWriter, RuleRepository ruleRepository,
83     ComponentRepository componentRepository) {
84     this.dbClient = dbClient;
85     this.projectHolder = projectHolder;
86     this.dumpWriter = dumpWriter;
87     this.componentRepository = componentRepository;
88     this.ruleRegistrar = new RuleRegistrar(ruleRepository);
89   }
90
91   @Override
92   public String getDescription() {
93     return "Export issues";
94   }
95
96   @Override
97   public void execute(Context context) {
98     long count = 0;
99     try (
100       StreamWriter<ProjectDump.Issue> output = dumpWriter.newStreamWriter(DumpElement.ISSUES);
101       DbSession dbSession = dbClient.openSession(false);
102       PreparedStatement stmt = createStatement(dbSession);
103       ResultSet rs = stmt.executeQuery()) {
104       ProjectDump.Issue.Builder builder = ProjectDump.Issue.newBuilder();
105       ProjectDump.Issue previousIssue = null;
106       while (rs.next()) {
107         String issueUuid = rs.getString(1);
108         if ((!rs.isFirst() && !previousIssue.getUuid().equals(issueUuid))) {
109           output.write(previousIssue);
110           count++;
111         }
112         ProjectDump.Issue issue = mergeIssue(previousIssue, builder, rs);
113         if (rs.isLast()) {
114           output.write(issue);
115           count++;
116         }
117         previousIssue = issue;
118       }
119
120       LoggerFactory.getLogger(getClass()).debug("{} issues exported", count);
121     } catch (Exception e) {
122       throw new IllegalStateException(format("Issue export failed after processing %d issues successfully", count), e);
123     }
124   }
125
126   private PreparedStatement createStatement(DbSession dbSession) throws SQLException {
127     PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, QUERY);
128     try {
129       stmt.setString(1, RULE_STATUS_REMOVED);
130       stmt.setString(2, projectHolder.projectDto().getUuid());
131       stmt.setBoolean(3, true);
132       stmt.setString(4, ISSUE_STATUS_CLOSED);
133       return stmt;
134     } catch (Exception t) {
135       DatabaseUtils.closeQuietly(stmt);
136       throw t;
137     }
138   }
139
140   private ProjectDump.Issue mergeIssue(@Nullable ProjectDump.Issue previousIssue, ProjectDump.Issue.Builder builder, ResultSet rs) throws SQLException {
141     builder.clear();
142     String issueUuid = rs.getString(1);
143     setRule(builder, rs);
144     builder
145       .setUuid(issueUuid)
146       .setType(rs.getInt(5))
147       .setComponentRef(componentRepository.getRef(rs.getString(6)))
148       .setMessage(emptyIfNull(rs, 7))
149       .setLine(rs.getInt(8))
150       .setChecksum(emptyIfNull(rs, 9))
151       .setStatus(emptyIfNull(rs, 10))
152       .setResolution(emptyIfNull(rs, 11))
153       .setSeverity(emptyIfNull(rs, 12))
154       .setManualSeverity(rs.getBoolean(13))
155       .setGap(defaultIfNull(rs, 14, IssueDumpElement.NO_GAP))
156       .setEffort(defaultIfNull(rs, 15, IssueDumpElement.NO_EFFORT))
157       .setAssignee(emptyIfNull(rs, 16))
158       .setAuthor(emptyIfNull(rs, 17))
159       .setTags(emptyIfNull(rs, 18))
160       .setIssueCreatedAt(rs.getLong(19))
161       .setIssueUpdatedAt(rs.getLong(20))
162       .setIssueClosedAt(rs.getLong(21))
163       .setProjectUuid(rs.getString(23))
164       .setCodeVariants(emptyIfNull(rs, 26));
165     Optional.ofNullable(rs.getString(24)).ifPresent(builder::setRuleDescriptionContextKey);
166     setLocations(builder, rs, issueUuid);
167     setMessageFormattings(builder, rs, issueUuid);
168
169     mergeImpacts(builder, rs, previousIssue, issueUuid);
170
171     return builder.build();
172   }
173
174   private static void mergeImpacts(ProjectDump.Issue.Builder builder, ResultSet rs, @Nullable ProjectDump.Issue previousIssue, String issueUuid)
175     throws SQLException {
176     String softwareQualityFromDatabase = rs.getString(27);
177     if (softwareQualityFromDatabase == null) {
178       return;
179     }
180     ProjectDump.Impact impact = ProjectDump.Impact.newBuilder()
181       .setSoftwareQuality(ProjectDump.SoftwareQuality.valueOf(softwareQualityFromDatabase))
182       .setSeverity(ProjectDump.Severity.valueOf(rs.getString(28)))
183       .build();
184
185     builder.addImpacts(impact);
186     if (previousIssue != null && previousIssue.getUuid().equals(issueUuid)) {
187       builder.addAllImpacts(previousIssue.getImpactsList());
188     }
189   }
190
191   private void setRule(ProjectDump.Issue.Builder builder, ResultSet rs) throws SQLException {
192     String ruleUuid = rs.getString(2);
193     String ruleKey = rs.getString(3);
194     String repositoryKey = rs.getString(4);
195     builder.setRuleRef(ruleRegistrar.register(ruleUuid, repositoryKey, ruleKey).ref());
196   }
197
198   private static void setLocations(ProjectDump.Issue.Builder builder, ResultSet rs, String issueUuid) throws SQLException {
199     try {
200       byte[] bytes = rs.getBytes(22);
201       if (bytes != null) {
202         // fail fast, ensure we can read data from DB
203         DbIssues.Locations.parseFrom(bytes);
204         builder.setLocations(ByteString.copyFrom(bytes));
205       }
206     } catch (InvalidProtocolBufferException e) {
207       throw new IllegalStateException(format("Fail to read locations from DB for issue %s", issueUuid), e);
208     }
209   }
210
211   private static void setMessageFormattings(ProjectDump.Issue.Builder builder, ResultSet rs, String issueUuid) throws SQLException {
212     try {
213       byte[] bytes = rs.getBytes(25);
214       if (bytes != null) {
215         // fail fast, ensure we can read data from DB
216         DbIssues.MessageFormattings messageFormattings = DbIssues.MessageFormattings.parseFrom(bytes);
217         if (messageFormattings != null) {
218           builder.addAllMessageFormattings(dbToDumpMessageFormatting(messageFormattings.getMessageFormattingList()));
219         }
220       }
221     } catch (InvalidProtocolBufferException e) {
222       throw new IllegalStateException(format("Fail to read message formattings from DB for issue %s", issueUuid), e);
223     }
224   }
225
226   @VisibleForTesting
227   static List<ProjectDump.MessageFormatting> dbToDumpMessageFormatting(List<DbIssues.MessageFormatting> messageFormattingList) {
228     return messageFormattingList.stream()
229       .map(e -> ProjectDump.MessageFormatting.newBuilder()
230         .setStart(e.getStart())
231         .setEnd(e.getEnd())
232         .setType(ProjectDump.MessageFormattingType.valueOf(e.getType().name())).build())
233       .toList();
234   }
235
236   private static class RuleRegistrar {
237     private final RuleRepository ruleRepository;
238     private Rule previousRule = null;
239     private String previousRuleUuid = null;
240
241     private RuleRegistrar(RuleRepository ruleRepository) {
242       this.ruleRepository = ruleRepository;
243     }
244
245     public Rule register(String ruleUuid, String repositoryKey, String ruleKey) {
246       if (Objects.equals(previousRuleUuid, ruleUuid)) {
247         return previousRule;
248       }
249       return lookup(ruleUuid, RuleKey.of(repositoryKey, ruleKey));
250     }
251
252     private Rule lookup(String ruleUuid, RuleKey ruleKey) {
253       this.previousRule = ruleRepository.register(ruleUuid, ruleKey);
254       this.previousRuleUuid = ruleUuid;
255       return previousRule;
256     }
257   }
258
259 }