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