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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.server.computation.task.projectanalysis.component;
  21. import java.util.Optional;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.utils.MessageException;
  24. import org.sonar.core.component.ComponentKeys;
  25. import org.sonar.db.component.BranchDto;
  26. import org.sonar.db.component.BranchType;
  27. import org.sonar.scanner.protocol.output.ScannerReport;
  28. import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
  29. import static java.lang.String.format;
  30. import static org.apache.commons.lang.StringUtils.isEmpty;
  31. import static org.apache.commons.lang.StringUtils.trimToNull;
  32. /**
  33. * The default (and legacy) implementation of {@link Branch}. It is used
  34. * when scanner is configured with parameter "sonar.branch" or when no branch is provided and the branch plugin is not installed.
  35. * A legacy branch is implemented as a fork of the project, so any branch is considered as "main".
  36. */
  37. public class DefaultBranchImpl implements Branch {
  38. private final String branchName;
  39. private final boolean isLegacyBranch;
  40. public DefaultBranchImpl() {
  41. this(null);
  42. }
  43. public DefaultBranchImpl(@Nullable String name) {
  44. this.isLegacyBranch = (name != null);
  45. this.branchName = (name == null) ? BranchDto.DEFAULT_MAIN_BRANCH_NAME : name;
  46. if (!ComponentKeys.isValidBranch(branchName)) {
  47. throw MessageException.of(format("\"%s\" is not a valid branch name. "
  48. + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", branchName));
  49. }
  50. }
  51. @Override
  52. public BranchType getType() {
  53. return BranchType.LONG;
  54. }
  55. @Override
  56. public boolean isMain() {
  57. return true;
  58. }
  59. @Override
  60. public Optional<String> getMergeBranchUuid() {
  61. return Optional.empty();
  62. }
  63. @Override
  64. public boolean isLegacyFeature() {
  65. return isLegacyBranch;
  66. }
  67. @Override
  68. public String getName() {
  69. return branchName;
  70. }
  71. @Override
  72. public boolean supportsCrossProjectCpd() {
  73. // only on regular project, not on branches
  74. return !isLegacyBranch;
  75. }
  76. @Override
  77. public String getPullRequestId() {
  78. throw new IllegalStateException("Only a branch of type PULL_REQUEST can have a pull request id.");
  79. }
  80. @Override
  81. public String generateKey(ScannerReport.Component module, @Nullable ScannerReport.Component fileOrDir) {
  82. String moduleWithBranch = module.getKey();
  83. if (isLegacyBranch) {
  84. moduleWithBranch = ComponentKeys.createKey(module.getKey(), branchName);
  85. }
  86. if (fileOrDir == null || isEmpty(fileOrDir.getPath())) {
  87. return moduleWithBranch;
  88. }
  89. return ComponentKeys.createEffectiveKey(moduleWithBranch, trimToNull(fileOrDir.getPath()));
  90. }
  91. }