3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.component;
22 import java.util.Date;
23 import javax.annotation.Nullable;
24 import org.sonar.api.utils.System2;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.component.BranchDto;
28 import org.sonar.db.component.ComponentDto;
29 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
32 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
33 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
35 public class BranchPersister {
36 private final DbClient dbClient;
37 private final System2 system2;
38 private final TreeRootHolder treeRootHolder;
39 private final AnalysisMetadataHolder analysisMetadataHolder;
41 public BranchPersister(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder) {
42 this.dbClient = dbClient;
43 this.system2 = system2;
44 this.treeRootHolder = treeRootHolder;
45 this.analysisMetadataHolder = analysisMetadataHolder;
48 public void persist(DbSession dbSession) {
49 Branch branch = analysisMetadataHolder.getBranch();
50 String branchUuid = treeRootHolder.getRoot().getUuid();
52 com.google.common.base.Optional<ComponentDto> branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid);
54 ComponentDto branchComponentDto;
55 if (branch.isMain()) {
56 checkState(branchComponentDtoOpt.isPresent(), "Project has been deleted by end-user during analysis");
57 branchComponentDto = branchComponentDtoOpt.get();
60 // inserts new row in table projects if it's the first time branch is analyzed
61 branchComponentDto = branchComponentDtoOpt.or(() -> insertIntoProjectsTable(dbSession, branchUuid));
64 // insert or update in table project_branches
65 dbClient.branchDao().upsert(dbSession, toBranchDto(branchComponentDto, branch));
68 private static void checkState(boolean condition, String msg) {
70 throw new IllegalStateException(msg);
74 private static <T> T firstNonNull(@Nullable T first, T second) {
75 return (first != null) ? first : second;
78 private static BranchDto toBranchDto(ComponentDto componentDto, Branch branch) {
79 BranchDto dto = new BranchDto();
80 dto.setUuid(componentDto.uuid());
81 // MainBranchProjectUuid will be null if it's a main branch
82 dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid()));
83 dto.setKey(branch.getName());
84 dto.setBranchType(branch.getType());
85 // merge branch is only present if it's a short living branch
86 dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null));
90 private ComponentDto insertIntoProjectsTable(DbSession dbSession, String branchUuid) {
91 String mainBranchProjectUuid = analysisMetadataHolder.getProject().getUuid();
92 ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, mainBranchProjectUuid);
93 ComponentDto branchDto = project.copy();
94 branchDto.setUuid(branchUuid);
95 branchDto.setProjectUuid(branchUuid);
96 branchDto.setRootUuid(branchUuid);
97 branchDto.setUuidPath(UUID_PATH_OF_ROOT);
98 branchDto.setModuleUuidPath(UUID_PATH_SEPARATOR + branchUuid + UUID_PATH_SEPARATOR);
99 branchDto.setMainBranchProjectUuid(mainBranchProjectUuid);
100 branchDto.setDbKey(treeRootHolder.getRoot().getKey());
101 branchDto.setCreatedAt(new Date(system2.now()));
102 dbClient.componentDao().insert(dbSession, branchDto);