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.

ComponentKeys.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.core.component;
  21. import javax.annotation.Nullable;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  24. import static com.google.common.base.Preconditions.checkArgument;
  25. public final class ComponentKeys {
  26. public static final int MAX_COMPONENT_KEY_LENGTH = 400;
  27. /*
  28. * Must not be blank or empty
  29. */
  30. private static final String VALID_PROJECT_KEY_REGEXP = "[^\\p{javaWhitespace}]+";
  31. private static final String VALID_PROJECT_KEY_ISSUES_MODE_REGEXP = "[\\p{Alnum}\\-_.:/]*[\\p{Alpha}\\-_.:/]+[\\p{Alnum}\\-_.:/]*";
  32. /*
  33. * Allowed characters are alphanumeric, '-', '_', '.' and '/'
  34. */
  35. private static final String VALID_BRANCH_REGEXP = "[\\p{Alnum}\\-_./]*";
  36. private static final String KEY_WITH_BRANCH_FORMAT = "%s:%s";
  37. private ComponentKeys() {
  38. // only static stuff
  39. }
  40. public static String createEffectiveKey(String projectKey, DefaultInputFile inputPath) {
  41. return createEffectiveKey(projectKey, inputPath.getProjectRelativePath());
  42. }
  43. public static String createEffectiveKey(String projectKey, @Nullable String path) {
  44. StringBuilder sb = new StringBuilder(MAX_COMPONENT_KEY_LENGTH);
  45. sb.append(projectKey);
  46. if (path != null) {
  47. sb.append(':').append(path);
  48. }
  49. return sb.toString();
  50. }
  51. /**
  52. * Test if given parameter is valid for a project. A key is valid if it doesn't contain whitespaces.
  53. *
  54. * @return <code>true</code> if <code>keyCandidate</code> can be used for a project
  55. */
  56. public static boolean isValidProjectKey(String keyCandidate) {
  57. return keyCandidate.matches(VALID_PROJECT_KEY_REGEXP);
  58. }
  59. /**
  60. * Checks if given parameter is valid for a project following {@link #isValidProjectKey(String)} contract.
  61. *
  62. * @throws IllegalArgumentException if the format is incorrect
  63. */
  64. public static void checkProjectKey(String keyCandidate) {
  65. checkArgument(isValidProjectKey(keyCandidate), "Malformed key for '%s'. %s", keyCandidate, "Project key cannot be empty nor contain whitespaces.");
  66. }
  67. /**
  68. * <p>Test if given parameter is valid for a project. Valid format is:</p>
  69. * <ul>
  70. * <li>Allowed characters:
  71. * <ul>
  72. * <li>Uppercase ASCII letters A-Z</li>
  73. * <li>Lowercase ASCII letters a-z</li>
  74. * <li>ASCII digits 0-9</li>
  75. * <li>Punctuation signs dash '-', underscore '_', period '.', colon ':' and slash '/'</li>
  76. * </ul>
  77. * </li>
  78. * <li>At least one non-digit</li>
  79. * </ul>
  80. *
  81. * @return <code>true</code> if <code>keyCandidate</code> can be used for a project in issues mode
  82. */
  83. public static boolean isValidProjectKeyIssuesMode(String keyCandidate) {
  84. return keyCandidate.matches(VALID_PROJECT_KEY_ISSUES_MODE_REGEXP);
  85. }
  86. /**
  87. * <p>Test if given parameter is valid for a branch. Valid format is:</p>
  88. * <ul>
  89. * <li>Allowed characters:
  90. * <ul>
  91. * <li>Uppercase ASCII letters A-Z</li>
  92. * <li>Lowercase ASCII letters a-z</li>
  93. * <li>ASCII digits 0-9</li>
  94. * <li>Punctuation signs dash '-', underscore '_', period '.', and '/'</li>
  95. * </ul>
  96. * </li>
  97. * </ul>
  98. *
  99. * @return <code>true</code> if <code>branchCandidate</code> can be used for a project
  100. */
  101. public static boolean isValidLegacyBranch(String branchCandidate) {
  102. return branchCandidate.matches(VALID_BRANCH_REGEXP);
  103. }
  104. /**
  105. * Return the project key with potential branch
  106. */
  107. public static String createKey(String keyWithoutBranch, @Nullable String branch) {
  108. if (StringUtils.isNotBlank(branch)) {
  109. return String.format(KEY_WITH_BRANCH_FORMAT, keyWithoutBranch, branch);
  110. } else {
  111. return keyWithoutBranch;
  112. }
  113. }
  114. public static String createKey(String projectKey, @Nullable String path, @Nullable String branch) {
  115. String key = createKey(projectKey, branch);
  116. return createEffectiveKey(key, path);
  117. }
  118. }