3 * Copyright (C) 2009-2022 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 javax.annotation.Nullable;
23 import org.sonar.ce.task.projectanalysis.analysis.Branch;
24 import org.sonar.core.component.ComponentKeys;
25 import org.sonar.db.component.BranchDto;
26 import org.sonar.db.component.BranchType;
28 import static org.apache.commons.lang.StringUtils.isEmpty;
29 import static org.apache.commons.lang.StringUtils.trimToNull;
32 * Implementation of {@link Branch} for default/main branch. It is used
33 * when no branch is provided as a scanner parameter or if the branch plugin is not installed.
35 public class DefaultBranchImpl implements Branch {
36 private final String branchName;
38 public DefaultBranchImpl() {
39 this.branchName = BranchDto.DEFAULT_MAIN_BRANCH_NAME;
43 public BranchType getType() {
44 return BranchType.BRANCH;
48 public boolean isMain() {
53 public String getReferenceBranchUuid() {
54 throw new IllegalStateException("Not valid for the main branch");
58 public String getName() {
63 public boolean supportsCrossProjectCpd() {
68 public String getPullRequestKey() {
69 throw new IllegalStateException("Only a branch of type PULL_REQUEST can have a pull request id.");
73 public String getTargetBranchName() {
74 throw new IllegalStateException("Only on a pull request");
78 public String generateKey(String projectKey, @Nullable String fileOrDirPath) {
79 if (isEmpty(fileOrDirPath)) {
82 return ComponentKeys.createEffectiveKey(projectKey, trimToNull(fileOrDirPath));