You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExportAnalysesStep.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.analysis;
  21. import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import org.slf4j.LoggerFactory;
  26. import org.sonar.ce.task.projectexport.component.ComponentRepository;
  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.DatabaseUtils;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbSession;
  35. import static java.lang.String.format;
  36. import static org.apache.commons.lang3.StringUtils.defaultString;
  37. import static org.sonar.db.DatabaseUtils.getString;
  38. import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
  39. public class ExportAnalysesStep implements ComputationStep {
  40. // contrary to naming convention used in other tables, the column snapshots.created_at is
  41. // the functional date (for instance when using property sonar.projectDate=2010-01-01). The
  42. // column "build_date" is the technical date of scanner execution. It must not be exported.
  43. private static final String QUERY = "select" +
  44. " p.uuid, s.version, s.created_at," +
  45. " s.period1_mode, s.period1_param, s.period1_date," +
  46. " s.uuid, s.build_string" +
  47. " from snapshots s" +
  48. " inner join components p on s.root_component_uuid=p.uuid" +
  49. " inner join project_branches pb on pb.uuid=p.uuid" +
  50. " where pb.project_uuid=? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge=? and s.status=? and p.enabled=?" +
  51. " order by s.analysis_date asc";
  52. private final DbClient dbClient;
  53. private final ProjectHolder projectHolder;
  54. private final ComponentRepository componentRepository;
  55. private final DumpWriter dumpWriter;
  56. public ExportAnalysesStep(DbClient dbClient, ProjectHolder projectHolder, ComponentRepository componentRepository, DumpWriter dumpWriter) {
  57. this.dbClient = dbClient;
  58. this.projectHolder = projectHolder;
  59. this.componentRepository = componentRepository;
  60. this.dumpWriter = dumpWriter;
  61. }
  62. @Override
  63. public void execute(Context context) {
  64. long count = 0L;
  65. try (
  66. StreamWriter<ProjectDump.Analysis> output = dumpWriter.newStreamWriter(DumpElement.ANALYSES);
  67. DbSession dbSession = dbClient.openSession(false);
  68. PreparedStatement stmt = buildSelectStatement(dbSession);
  69. ResultSet rs = stmt.executeQuery()) {
  70. ProjectDump.Analysis.Builder builder = ProjectDump.Analysis.newBuilder();
  71. while (rs.next()) {
  72. // Results are ordered by ascending id so that any parent is located
  73. // before its children.
  74. ProjectDump.Analysis analysis = convertToAnalysis(rs, builder);
  75. output.write(analysis);
  76. count++;
  77. }
  78. LoggerFactory.getLogger(getClass()).debug("{} analyses exported", count);
  79. } catch (Exception e) {
  80. throw new IllegalStateException(format("Analysis Export failed after processing %d analyses successfully", count), e);
  81. }
  82. }
  83. private PreparedStatement buildSelectStatement(DbSession dbSession) throws SQLException {
  84. PreparedStatement stmt = dbClient.getMyBatis().newScrollingSelectStatement(dbSession, QUERY);
  85. try {
  86. stmt.setString(1, projectHolder.projectDto().getUuid());
  87. stmt.setBoolean(2, true);
  88. stmt.setString(3, STATUS_PROCESSED);
  89. stmt.setBoolean(4, true);
  90. return stmt;
  91. } catch (Exception t) {
  92. DatabaseUtils.closeQuietly(stmt);
  93. throw t;
  94. }
  95. }
  96. private ProjectDump.Analysis convertToAnalysis(ResultSet rs, ProjectDump.Analysis.Builder builder) throws SQLException {
  97. builder.clear();
  98. long componentRef = componentRepository.getRef(rs.getString(1));
  99. return builder
  100. .setComponentRef(componentRef)
  101. .setProjectVersion(defaultString(getString(rs, 2)))
  102. .setDate(rs.getLong(3))
  103. .setPeriod1Mode(defaultString(getString(rs, 4)))
  104. .setPeriod1Param(defaultString(getString(rs, 5)))
  105. .setPeriod1Date(rs.getLong(6))
  106. .setUuid(rs.getString(7))
  107. .setBuildString(defaultString(getString(rs, 8)))
  108. .build();
  109. }
  110. @Override
  111. public String getDescription() {
  112. return "Export analyses";
  113. }
  114. }