3 * Copyright (C) 2009-2024 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.steps;
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.slf4j.LoggerFactory;
27 import org.sonar.ce.task.projectexport.component.ComponentRepository;
28 import org.sonar.ce.task.step.ComputationStep;
29 import org.sonar.db.DatabaseUtils;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.component.SnapshotDto;
34 import static java.lang.String.format;
35 import static org.apache.commons.lang.StringUtils.defaultString;
36 import static org.sonar.db.DatabaseUtils.getString;
38 public class ExportEventsStep implements ComputationStep {
40 private static final String QUERY = "select" +
41 " p.uuid, e.name, e.analysis_uuid, e.category, e.description, e.event_data, e.event_date, e.uuid" +
43 " join snapshots s on s.uuid=e.analysis_uuid" +
44 " join components p on p.uuid=s.root_component_uuid" +
45 " join project_branches pb on pb.uuid=p.uuid" +
46 " where pb.project_uuid=? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge=? and s.status=? and p.enabled=?";
48 private final DbClient dbClient;
49 private final ProjectHolder projectHolder;
50 private final ComponentRepository componentRepository;
51 private final DumpWriter dumpWriter;
53 public ExportEventsStep(DbClient dbClient, ProjectHolder projectHolder, ComponentRepository componentRepository, DumpWriter dumpWriter) {
54 this.dbClient = dbClient;
55 this.projectHolder = projectHolder;
56 this.componentRepository = componentRepository;
57 this.dumpWriter = dumpWriter;
61 public void execute(Context context) {
64 StreamWriter<ProjectDump.Event> output = dumpWriter.newStreamWriter(DumpElement.EVENTS);
65 DbSession dbSession = dbClient.openSession(false);
66 PreparedStatement stmt = buildSelectStatement(dbSession);
67 ResultSet rs = stmt.executeQuery()) {
69 ProjectDump.Event.Builder builder = ProjectDump.Event.newBuilder();
71 ProjectDump.Event event = convertToEvent(rs, builder);
75 LoggerFactory.getLogger(getClass()).debug("{} events exported", count);
77 } catch (Exception e) {
78 throw new IllegalStateException(format("Event Export failed after processing %d events successfully", count), e);
82 private PreparedStatement buildSelectStatement(DbSession dbSession) throws SQLException {
83 PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, QUERY);
85 stmt.setString(1, projectHolder.projectDto().getUuid());
86 stmt.setBoolean(2, true);
87 stmt.setString(3, SnapshotDto.STATUS_PROCESSED);
88 stmt.setBoolean(4, true);
90 } catch (Exception e) {
91 DatabaseUtils.closeQuietly(stmt);
96 private ProjectDump.Event convertToEvent(ResultSet rs, ProjectDump.Event.Builder builder) throws SQLException {
97 long componentRef = componentRepository.getRef(rs.getString(1));
100 .setComponentRef(componentRef)
101 .setName(defaultString(getString(rs, 2)))
102 .setAnalysisUuid(getString(rs, 3))
103 .setCategory(defaultString(getString(rs, 4)))
104 .setDescription(defaultString(getString(rs, 5)))
105 .setData(defaultString(getString(rs, 6)))
106 .setDate(rs.getLong(7))
107 .setUuid(rs.getString(8))
113 public String getDescription() {
114 return "Export events";