3 * Copyright (C) 2009-2021 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;
35 import org.sonar.server.setting.ProjectConfigurationLoader;
37 import static java.lang.String.format;
38 import static java.util.Arrays.asList;
39 import static java.util.Optional.ofNullable;
40 import static org.sonar.core.config.PurgeConstants.BRANCHES_TO_KEEP_WHEN_INACTIVE;
43 * Creates or updates the data in table {@code PROJECT_BRANCHES} for the current root.
45 public class BranchPersisterImpl implements BranchPersister {
46 private final DbClient dbClient;
47 private final TreeRootHolder treeRootHolder;
48 private final AnalysisMetadataHolder analysisMetadataHolder;
49 private final ProjectConfigurationLoader projectConfigurationLoader;
51 public BranchPersisterImpl(DbClient dbClient, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
52 ProjectConfigurationLoader projectConfigurationLoader) {
53 this.dbClient = dbClient;
54 this.treeRootHolder = treeRootHolder;
55 this.analysisMetadataHolder = analysisMetadataHolder;
56 this.projectConfigurationLoader = projectConfigurationLoader;
59 public void persist(DbSession dbSession) {
60 Branch branch = analysisMetadataHolder.getBranch();
61 String branchUuid = treeRootHolder.getRoot().getUuid();
63 ComponentDto branchComponentDto = dbClient.componentDao().selectByUuid(dbSession, branchUuid)
64 .orElseThrow(() -> new IllegalStateException("Component has been deleted by end-user during analysis"));
66 // insert or update in table project_branches
67 dbClient.branchDao().upsert(dbSession, toBranchDto(dbSession, branchComponentDto, branch, checkIfExcludedFromPurge(dbSession)));
70 private boolean checkIfExcludedFromPurge(DbSession dbSession) {
71 if (analysisMetadataHolder.getBranch().isMain()) {
75 if (BranchType.PULL_REQUEST.equals(analysisMetadataHolder.getBranch().getType())) {
79 ComponentDto projectDto = dbClient.componentDao().selectByUuid(dbSession, analysisMetadataHolder.getProject().getUuid())
80 .orElseThrow(() -> new IllegalStateException(format("Component '%s' is missing.", analysisMetadataHolder.getProject().getKey())));
81 Configuration projectConfiguration = projectConfigurationLoader.loadProjectConfiguration(dbSession, projectDto);
82 String[] branchesToKeep = projectConfiguration.getStringArray(BRANCHES_TO_KEEP_WHEN_INACTIVE);
83 List<String> excludeFromPurgeEntries = asList(ofNullable(branchesToKeep).orElse(new String[0]));
84 return excludeFromPurgeEntries.stream()
85 .map(Pattern::compile)
86 .anyMatch(excludePattern -> excludePattern.matcher(analysisMetadataHolder.getBranch().getName()).matches());
89 protected BranchDto toBranchDto(DbSession dbSession, ComponentDto componentDto, Branch branch, boolean excludeFromPurge) {
90 BranchDto dto = new BranchDto();
91 dto.setUuid(componentDto.uuid());
93 // MainBranchProjectUuid will be null if it's a main branch
94 String projectUuid = firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid());
95 dto.setProjectUuid(projectUuid);
96 dto.setBranchType(branch.getType());
97 dto.setExcludeFromPurge(excludeFromPurge);
99 // merge branch is only present if it's not a main branch and not an application
100 if (!branch.isMain() && !Qualifiers.APP.equals(componentDto.qualifier())) {
101 dto.setMergeBranchUuid(branch.getReferenceBranchUuid());
104 if (branch.getType() == BranchType.PULL_REQUEST) {
105 String pullRequestKey = analysisMetadataHolder.getPullRequestKey();
106 dto.setKey(pullRequestKey);
108 DbProjectBranches.PullRequestData pullRequestData = getBuilder(dbSession, projectUuid, pullRequestKey)
109 .setBranch(branch.getName())
110 .setTitle(branch.getName())
111 .setTarget(branch.getTargetBranchName())
113 dto.setPullRequestData(pullRequestData);
115 dto.setKey(branch.getName());
121 private DbProjectBranches.PullRequestData.Builder getBuilder(DbSession dbSession, String projectUuid, String pullRequestKey) {
122 return dbClient.branchDao().selectByPullRequestKey(dbSession, projectUuid, pullRequestKey)
123 .map(BranchDto::getPullRequestData)
124 .map(DbProjectBranches.PullRequestData::toBuilder)
125 .orElse(DbProjectBranches.PullRequestData.newBuilder());
128 private static <T> T firstNonNull(@Nullable T first, T second) {
129 return (first != null) ? first : second;