3 * Copyright (C) 2009-2020 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.projectanalysis.component;
22 import java.util.List;
23 import java.util.regex.Pattern;
24 import javax.annotation.Nullable;
25 import org.sonar.api.config.Configuration;
26 import org.sonar.api.resources.Qualifiers;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.analysis.Branch;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.component.BranchDto;
32 import org.sonar.db.component.BranchType;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.protobuf.DbProjectBranches;
36 import static java.util.Arrays.asList;
37 import static java.util.Optional.ofNullable;
38 import static org.sonar.core.config.PurgeConstants.BRANCHES_TO_KEEP_WHEN_INACTIVE;
41 * Creates or updates the data in table {@code PROJECT_BRANCHES} for the current root.
43 public class BranchPersisterImpl implements BranchPersister {
44 private final DbClient dbClient;
45 private final TreeRootHolder treeRootHolder;
46 private final AnalysisMetadataHolder analysisMetadataHolder;
47 private final Configuration configuration;
49 public BranchPersisterImpl(DbClient dbClient, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
50 Configuration configuration) {
51 this.dbClient = dbClient;
52 this.treeRootHolder = treeRootHolder;
53 this.analysisMetadataHolder = analysisMetadataHolder;
54 this.configuration = configuration;
57 public void persist(DbSession dbSession) {
58 Branch branch = analysisMetadataHolder.getBranch();
59 String branchUuid = treeRootHolder.getRoot().getUuid();
61 ComponentDto branchComponentDto = dbClient.componentDao().selectByUuid(dbSession, branchUuid)
62 .orElseThrow(() -> new IllegalStateException("Component has been deleted by end-user during analysis"));
64 // insert or update in table project_branches
65 dbClient.branchDao().upsert(dbSession, toBranchDto(dbSession, branchComponentDto, branch, checkIfExcludedFromPurge()));
68 private boolean checkIfExcludedFromPurge() {
69 if (analysisMetadataHolder.getBranch().isMain()) {
73 List<String> excludeFromPurgeEntries = asList(ofNullable(configuration.getStringArray(BRANCHES_TO_KEEP_WHEN_INACTIVE)).orElse(new String[0]));
74 return excludeFromPurgeEntries.stream()
75 .map(Pattern::compile)
76 .anyMatch(excludePattern -> excludePattern.matcher(analysisMetadataHolder.getBranch().getName()).matches());
79 protected BranchDto toBranchDto(DbSession dbSession, ComponentDto componentDto, Branch branch, boolean excludeFromPurge) {
80 BranchDto dto = new BranchDto();
81 dto.setUuid(componentDto.uuid());
83 // MainBranchProjectUuid will be null if it's a main branch
84 String projectUuid = firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid());
85 dto.setProjectUuid(projectUuid);
86 dto.setBranchType(branch.getType());
87 dto.setExcludeFromPurge(excludeFromPurge);
89 // merge branch is only present if it's not a main branch and not an application
90 if (!branch.isMain() && !Qualifiers.APP.equals(componentDto.qualifier())) {
91 dto.setMergeBranchUuid(branch.getReferenceBranchUuid());
94 if (branch.getType() == BranchType.PULL_REQUEST) {
95 String pullRequestKey = analysisMetadataHolder.getPullRequestKey();
96 dto.setKey(pullRequestKey);
98 DbProjectBranches.PullRequestData pullRequestData = getBuilder(dbSession, projectUuid, pullRequestKey)
99 .setBranch(branch.getName())
100 .setTitle(branch.getName())
101 .setTarget(branch.getTargetBranchName())
103 dto.setPullRequestData(pullRequestData);
105 dto.setKey(branch.getName());
111 private DbProjectBranches.PullRequestData.Builder getBuilder(DbSession dbSession, String projectUuid, String pullRequestKey) {
112 return dbClient.branchDao().selectByPullRequestKey(dbSession, projectUuid, pullRequestKey)
113 .map(BranchDto::getPullRequestData)
114 .map(DbProjectBranches.PullRequestData::toBuilder)
115 .orElse(DbProjectBranches.PullRequestData.newBuilder());
118 private static <T> T firstNonNull(@Nullable T first, T second) {
119 return (first != null) ? first : second;