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.

ExportBranchesStep.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.branches;
  21. import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
  22. import java.util.List;
  23. import org.slf4j.LoggerFactory;
  24. import org.sonar.ce.task.projectexport.steps.DumpElement;
  25. import org.sonar.ce.task.projectexport.steps.DumpWriter;
  26. import org.sonar.ce.task.projectexport.steps.ProjectHolder;
  27. import org.sonar.ce.task.projectexport.steps.StreamWriter;
  28. import org.sonar.ce.task.step.ComputationStep;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.component.BranchDto;
  32. import static java.lang.String.format;
  33. import static org.apache.commons.lang3.StringUtils.defaultString;
  34. public class ExportBranchesStep implements ComputationStep {
  35. private final DumpWriter dumpWriter;
  36. private final DbClient dbClient;
  37. private final ProjectHolder projectHolder;
  38. public ExportBranchesStep(DumpWriter dumpWriter, DbClient dbClient, ProjectHolder projectHolder) {
  39. this.dumpWriter = dumpWriter;
  40. this.dbClient = dbClient;
  41. this.projectHolder = projectHolder;
  42. }
  43. @Override
  44. public void execute(Context context) {
  45. long count = 0L;
  46. try {
  47. try (DbSession dbSession = dbClient.openSession(false);
  48. StreamWriter<ProjectDump.Branch> output = dumpWriter.newStreamWriter(DumpElement.BRANCHES)) {
  49. ProjectDump.Branch.Builder builder = ProjectDump.Branch.newBuilder();
  50. List<BranchDto> branches = dbClient.projectExportDao()
  51. .selectBranchesForExport(dbSession, projectHolder.projectDto().getUuid());
  52. for (BranchDto branch : branches) {
  53. builder
  54. .clear()
  55. .setUuid(branch.getUuid())
  56. .setProjectUuid(branch.getProjectUuid())
  57. .setKee(branch.getKey())
  58. .setIsMain(branch.isMain())
  59. .setBranchType(branch.getBranchType().name())
  60. .setMergeBranchUuid(defaultString(branch.getMergeBranchUuid()));
  61. output.write(builder.build());
  62. ++count;
  63. }
  64. LoggerFactory.getLogger(getClass()).debug("{} branches exported", count);
  65. }
  66. } catch (Exception e) {
  67. throw new IllegalStateException(format("Branch export failed after processing %d branch(es) successfully", count), e);
  68. }
  69. }
  70. @Override
  71. public String getDescription() {
  72. return "Export branches";
  73. }
  74. }