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.Optional;
23 import javax.annotation.Nullable;
24 import org.sonar.api.utils.MessageException;
25 import org.sonar.core.component.ComponentKeys;
26 import org.sonar.db.component.BranchDto;
27 import org.sonar.db.component.BranchType;
28 import org.sonar.scanner.protocol.output.ScannerReport;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
31 import static java.lang.String.format;
32 import static org.apache.commons.lang.StringUtils.isEmpty;
33 import static org.apache.commons.lang.StringUtils.trimToNull;
36 * The default (and legacy) implementation of {@link Branch}. It is used
37 * when scanner is configured with parameter "sonar.branch" or when no branch is provided and the branch plugin is not installed.
38 * A legacy branch is implemented as a fork of the project, so any branch
39 * is considered as "main".
41 public class DefaultBranchImpl implements Branch {
42 private final String branchName;
43 private final boolean isLegacyBranch;
45 public DefaultBranchImpl() {
49 public DefaultBranchImpl(@Nullable String name) {
50 this.isLegacyBranch = (name != null);
51 this.branchName = (name == null) ? BranchDto.DEFAULT_MAIN_BRANCH_NAME : name;
52 if (!ComponentKeys.isValidBranch(branchName)) {
53 throw MessageException.of(format("\"%s\" is not a valid branch name. "
54 + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", branchName));
59 public BranchType getType() {
60 return BranchType.LONG;
64 public boolean isMain() {
69 public Optional<String> getMergeBranchUuid() {
70 return Optional.empty();
74 public boolean isLegacyFeature() {
75 return isLegacyBranch;
79 public Optional<String> getName() {
80 return Optional.ofNullable(branchName);
84 public boolean supportsCrossProjectCpd() {
85 // only on regular project, not on branches
86 return !isLegacyBranch;
90 public String generateKey(ScannerReport.Component module, @Nullable ScannerReport.Component fileOrDir) {
91 String moduleWithBranch = module.getKey();
93 moduleWithBranch = ComponentKeys.createKey(module.getKey(), branchName);
95 if (fileOrDir == null || isEmpty(fileOrDir.getPath())) {
96 return moduleWithBranch;
98 return ComponentKeys.createEffectiveKey(moduleWithBranch, trimToNull(fileOrDir.getPath()));