You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultBranchImpl.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.component;
  21. import javax.annotation.Nullable;
  22. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  23. import org.sonar.core.component.ComponentKeys;
  24. import org.sonar.db.component.BranchDto;
  25. import org.sonar.db.component.BranchType;
  26. import static org.apache.commons.lang.StringUtils.isEmpty;
  27. import static org.apache.commons.lang.StringUtils.trimToNull;
  28. /**
  29. * Implementation of {@link Branch} for default/main branch. It is used
  30. * when no branch is provided as a scanner parameter or if the branch plugin is not installed.
  31. */
  32. public class DefaultBranchImpl implements Branch {
  33. private final String branchName;
  34. public DefaultBranchImpl() {
  35. this.branchName = BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  36. }
  37. @Override
  38. public BranchType getType() {
  39. return BranchType.LONG;
  40. }
  41. @Override
  42. public boolean isMain() {
  43. return true;
  44. }
  45. @Override
  46. public String getMergeBranchUuid() {
  47. throw new IllegalStateException("Not valid for the main branch");
  48. }
  49. @Override
  50. public String getName() {
  51. return branchName;
  52. }
  53. @Override
  54. public boolean supportsCrossProjectCpd() {
  55. return true;
  56. }
  57. @Override
  58. public String getPullRequestKey() {
  59. throw new IllegalStateException("Only a branch of type PULL_REQUEST can have a pull request id.");
  60. }
  61. @Override
  62. public String getTargetBranchName() {
  63. throw new IllegalStateException("Only on a short lived branch or pull request");
  64. }
  65. @Override
  66. public String generateKey(String projectKey, @Nullable String fileOrDirPath) {
  67. if (isEmpty(fileOrDirPath)) {
  68. return projectKey;
  69. }
  70. return ComponentKeys.createEffectiveKey(projectKey, trimToNull(fileOrDirPath));
  71. }
  72. }