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.BranchKeyType;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
32 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
34 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
35 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
37 public class BranchPersister {
38 private final DbClient dbClient;
39 private final System2 system2;
40 private final TreeRootHolder treeRootHolder;
41 private final AnalysisMetadataHolder analysisMetadataHolder;
43 public BranchPersister(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder) {
44 this.dbClient = dbClient;
45 this.system2 = system2;
46 this.treeRootHolder = treeRootHolder;
47 this.analysisMetadataHolder = analysisMetadataHolder;
50 public void persist(DbSession dbSession) {
51 Optional<Branch> branchOpt = analysisMetadataHolder.getBranch();
52 if (!branchOpt.isPresent()) {
56 Branch branch = branchOpt.get();
57 String branchUuid = treeRootHolder.getRoot().getUuid();
59 com.google.common.base.Optional<ComponentDto> branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid);
61 ComponentDto branchComponentDto;
62 if (branch.isMain()) {
63 checkState(branchComponentDtoOpt.isPresent(), "Project has been deleted by end-user during analysis");
64 branchComponentDto = branchComponentDtoOpt.get();
67 // inserts new row in table projects if it's the first time branch is analyzed
68 branchComponentDto = branchComponentDtoOpt.or(() -> insertIntoProjectsTable(dbSession, branchUuid));
71 // insert or update in table project_branches
72 dbClient.branchDao().upsert(dbSession, toBranchDto(branchComponentDto, branch));
75 private static void checkState(boolean condition, String msg) {
77 throw new IllegalStateException(msg);
81 private static <T> T firstNonNull(@Nullable T first, T second) {
82 return (first != null) ? first : second;
85 private static BranchDto toBranchDto(ComponentDto componentDto, Branch branch) {
86 BranchDto dto = new BranchDto();
87 dto.setUuid(componentDto.uuid());
88 // MainBranchProjectUuid will be null if it's a main branch
89 dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid()));
90 dto.setKeeType(BranchKeyType.BRANCH);
91 dto.setKey(branch.getName().orElse(null));
92 dto.setBranchType(branch.getType());
93 // merge branch is only present if it's a short living branch
94 dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null));
95 dto.setPullRequestTitle(null);
99 private ComponentDto insertIntoProjectsTable(DbSession dbSession, String branchUuid) {
100 String mainBranchProjectUuid = analysisMetadataHolder.getProject().getUuid();
101 ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, mainBranchProjectUuid);
102 ComponentDto branchDto = project.copy();
103 branchDto.setUuid(branchUuid);
104 branchDto.setProjectUuid(branchUuid);
105 branchDto.setRootUuid(branchUuid);
106 branchDto.setUuidPath(UUID_PATH_OF_ROOT);
107 branchDto.setModuleUuidPath(UUID_PATH_SEPARATOR + branchUuid + UUID_PATH_SEPARATOR);
108 branchDto.setMainBranchProjectUuid(mainBranchProjectUuid);
109 branchDto.setDbKey(treeRootHolder.getRoot().getKey());
110 branchDto.setCreatedAt(new Date(system2.now()));
111 dbClient.componentDao().insert(dbSession, branchDto);