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