3 * Copyright (C) 2009-2019 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.Optional;
23 import javax.annotation.Nullable;
24 import org.sonar.api.utils.MessageException;
25 import org.sonar.ce.task.projectanalysis.analysis.Branch;
26 import org.sonar.core.component.ComponentKeys;
27 import org.sonar.db.component.BranchDto;
28 import org.sonar.db.component.BranchType;
30 import static java.lang.String.format;
31 import static org.apache.commons.lang.StringUtils.isEmpty;
32 import static org.apache.commons.lang.StringUtils.trimToNull;
35 * The default (and legacy) implementation of {@link Branch}. It is used
36 * when scanner is configured with parameter "sonar.branch" or when no branch is provided and the branch plugin is not installed.
37 * A legacy branch is implemented as a fork of the project, so any branch is considered as "main".
39 public class DefaultBranchImpl implements Branch {
40 private final String branchName;
41 private final boolean isLegacyBranch;
43 public DefaultBranchImpl() {
47 public DefaultBranchImpl(@Nullable String name) {
48 this.isLegacyBranch = (name != null);
49 this.branchName = (name == null) ? BranchDto.DEFAULT_MAIN_BRANCH_NAME : name;
50 if (!ComponentKeys.isValidBranch(branchName)) {
51 throw MessageException.of(format("\"%s\" is not a valid branch name. "
52 + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", branchName));
57 public BranchType getType() {
58 return BranchType.LONG;
62 public boolean isMain() {
67 public Optional<String> getMergeBranchUuid() {
68 return Optional.empty();
72 public boolean isLegacyFeature() {
73 return isLegacyBranch;
77 public String getName() {
82 public boolean supportsCrossProjectCpd() {
83 // only on regular project, not on branches
84 return !isLegacyBranch;
88 public String getPullRequestKey() {
89 throw new IllegalStateException("Only a branch of type PULL_REQUEST can have a pull request id.");
93 public String generateKey(String projectKey, @Nullable String fileOrDirPath) {
95 projectKey = ComponentKeys.createKey(projectKey, branchName);
97 if (isEmpty(fileOrDirPath)) {
100 return ComponentKeys.createEffectiveKey(projectKey, trimToNull(fileOrDirPath));