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.

ComponentFinder.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.component;
  21. import java.util.Collection;
  22. import java.util.Optional;
  23. import java.util.Set;
  24. import javax.annotation.Nullable;
  25. import org.sonar.api.resources.Qualifiers;
  26. import org.sonar.api.resources.ResourceType;
  27. import org.sonar.api.resources.ResourceTypes;
  28. import org.sonar.api.resources.Scopes;
  29. import org.sonar.core.util.stream.MoreCollectors;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.component.BranchDto;
  33. import org.sonar.db.component.ComponentDto;
  34. import org.sonar.db.project.ProjectDto;
  35. import org.sonar.server.exceptions.NotFoundException;
  36. import static com.google.common.base.Preconditions.checkArgument;
  37. import static java.lang.String.format;
  38. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  39. public class ComponentFinder {
  40. private static final String MSG_COMPONENT_ID_OR_KEY_TEMPLATE = "Either '%s' or '%s' must be provided";
  41. private static final String MSG_PARAMETER_MUST_NOT_BE_EMPTY = "The '%s' parameter must not be empty";
  42. private static final String LABEL_PROJECT = "Project";
  43. private static final String LABEL_COMPONENT = "Component";
  44. private static final String LABEL_PROJECT_NOT_FOUND = "Project '%s' not found";
  45. private final DbClient dbClient;
  46. private final ResourceTypes resourceTypes;
  47. public ComponentFinder(DbClient dbClient, ResourceTypes resourceTypes) {
  48. this.dbClient = dbClient;
  49. this.resourceTypes = resourceTypes;
  50. }
  51. public ComponentDto getByUuidOrKey(DbSession dbSession, @Nullable String componentUuid, @Nullable String componentKey, ParamNames parameterNames) {
  52. checkByUuidOrKey(componentUuid, componentKey, parameterNames);
  53. if (componentUuid != null) {
  54. return getByUuid(dbSession, checkParamNotEmpty(componentUuid, parameterNames.getUuidParam()));
  55. }
  56. return getByKey(dbSession, checkParamNotEmpty(componentKey, parameterNames.getKeyParam()));
  57. }
  58. public ProjectDto getProjectByKey(DbSession dbSession, String projectKey) {
  59. return dbClient.projectDao().selectProjectByKey(dbSession, projectKey)
  60. .orElseThrow(() -> new NotFoundException(String.format(LABEL_PROJECT_NOT_FOUND, projectKey)));
  61. }
  62. public ProjectDto getApplicationByKey(DbSession dbSession, String applicationKey) {
  63. return dbClient.projectDao().selectApplicationByKey(dbSession, applicationKey)
  64. .orElseThrow(() -> new NotFoundException(String.format("Application '%s' not found", applicationKey)));
  65. }
  66. public ProjectDto getProjectOrApplicationByKey(DbSession dbSession, String projectKey) {
  67. return dbClient.projectDao().selectProjectOrAppByKey(dbSession, projectKey)
  68. .orElseThrow(() -> new NotFoundException(String.format(LABEL_PROJECT_NOT_FOUND, projectKey)));
  69. }
  70. public ProjectDto getProjectByUuid(DbSession dbSession, String projectUuid) {
  71. return dbClient.projectDao().selectByUuid(dbSession, projectUuid)
  72. .filter(p -> Qualifiers.PROJECT.equals(p.getQualifier()))
  73. .orElseThrow(() -> new NotFoundException(String.format(LABEL_PROJECT_NOT_FOUND, projectUuid)));
  74. }
  75. public ProjectDto getProjectByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey, ParamNames parameterNames) {
  76. checkByUuidOrKey(projectUuid, projectKey, parameterNames);
  77. if (projectUuid != null) {
  78. return getProjectByUuid(dbSession, checkParamNotEmpty(projectUuid, parameterNames.getUuidParam()));
  79. }
  80. return getProjectByKey(dbSession, checkParamNotEmpty(projectKey, parameterNames.getKeyParam()));
  81. }
  82. private static String checkParamNotEmpty(String value, String param) {
  83. checkArgument(!value.isEmpty(), MSG_PARAMETER_MUST_NOT_BE_EMPTY, param);
  84. return value;
  85. }
  86. public BranchDto getBranchOrPullRequest(DbSession dbSession, ComponentDto project, @Nullable String branchKey, @Nullable String pullRequestKey) {
  87. return getBranchOrPullRequest(dbSession, project.uuid(), project.getKey(), branchKey, pullRequestKey);
  88. }
  89. public BranchDto getBranchOrPullRequest(DbSession dbSession, ProjectDto project, @Nullable String branchKey, @Nullable String pullRequestKey) {
  90. return getBranchOrPullRequest(dbSession, project.getUuid(), project.getKey(), branchKey, pullRequestKey);
  91. }
  92. public BranchDto getBranchOrPullRequest(DbSession dbSession, String projectUuid, String projectKey,
  93. @Nullable String branchKey, @Nullable String pullRequestKey) {
  94. if (branchKey != null) {
  95. return dbClient.branchDao().selectByBranchKey(dbSession, projectUuid, branchKey)
  96. .orElseThrow(() -> new NotFoundException(String.format("Branch '%s' in project '%s' not found", branchKey, projectKey)));
  97. } else if (pullRequestKey != null) {
  98. return dbClient.branchDao().selectByPullRequestKey(dbSession, projectUuid, pullRequestKey)
  99. .orElseThrow(() -> new NotFoundException(String.format("Pull request '%s' in project '%s' not found", pullRequestKey, projectKey)));
  100. }
  101. return dbClient.branchDao().selectByUuid(dbSession, projectUuid)
  102. .orElseThrow(() -> new NotFoundException(String.format("Main branch in project '%s' not found", projectKey)));
  103. }
  104. public BranchDto getMainBranch(DbSession dbSession, ProjectDto projectDto) {
  105. return dbClient.branchDao().selectByUuid(dbSession, projectDto.getUuid())
  106. .orElseThrow(() -> new IllegalStateException(String.format("Can't find main branch for project '%s'", projectDto.getKey())));
  107. }
  108. public BranchDto getMainBranch(DbSession dbSession, ComponentDto projectDto) {
  109. return dbClient.branchDao().selectByUuid(dbSession, projectDto.uuid())
  110. .orElseThrow(() -> new IllegalStateException(String.format("Can't find main branch for project '%s'", projectDto.getKey())));
  111. }
  112. private static void checkByUuidOrKey(@Nullable String componentUuid, @Nullable String componentKey, ParamNames parameterNames) {
  113. checkArgument(componentUuid != null ^ componentKey != null, MSG_COMPONENT_ID_OR_KEY_TEMPLATE, parameterNames.getUuidParam(), parameterNames.getKeyParam());
  114. }
  115. public ComponentDto getByKey(DbSession dbSession, String key) {
  116. return getByKey(dbSession, key, LABEL_COMPONENT);
  117. }
  118. private ComponentDto getByKey(DbSession dbSession, String key, String label) {
  119. return checkComponent(dbClient.componentDao().selectByKey(dbSession, key), "%s key '%s' not found", label, key);
  120. }
  121. public ComponentDto getByUuid(DbSession dbSession, String uuid) {
  122. return getByUuid(dbSession, uuid, LABEL_COMPONENT);
  123. }
  124. private ComponentDto getByUuid(DbSession dbSession, String uuid, String label) {
  125. return checkComponent(dbClient.componentDao().selectByUuid(dbSession, uuid), "%s id '%s' not found", label, uuid);
  126. }
  127. private static ComponentDto checkComponent(Optional<ComponentDto> componentDto, String message, Object... messageArguments) {
  128. if (componentDto.isPresent() && componentDto.get().isEnabled() && componentDto.get().getMainBranchProjectUuid() == null) {
  129. return componentDto.get();
  130. }
  131. throw new NotFoundException(format(message, messageArguments));
  132. }
  133. public ComponentDto getRootComponentByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey) {
  134. ComponentDto project;
  135. if (projectUuid != null) {
  136. project = getByUuid(dbSession, projectUuid, LABEL_PROJECT);
  137. } else {
  138. project = getByKey(dbSession, projectKey, LABEL_PROJECT);
  139. }
  140. checkIsProject(project);
  141. return project;
  142. }
  143. private ComponentDto checkIsProject(ComponentDto component) {
  144. Set<String> rootQualifiers = getRootQualifiers(resourceTypes);
  145. checkRequest(component.scope().equals(Scopes.PROJECT) && rootQualifiers.contains(component.qualifier()),
  146. format(
  147. "Component '%s' (id: %s) must be a project%s.",
  148. component.getDbKey(), component.uuid(),
  149. rootQualifiers.contains(Qualifiers.VIEW) ? " or a view" : ""));
  150. return component;
  151. }
  152. private static Set<String> getRootQualifiers(ResourceTypes resourceTypes) {
  153. Collection<ResourceType> rootTypes = resourceTypes.getRoots();
  154. return rootTypes
  155. .stream()
  156. .map(ResourceType::getQualifier)
  157. .collect(MoreCollectors.toSet(rootTypes.size()));
  158. }
  159. public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) {
  160. Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch);
  161. if (componentDto.isPresent() && componentDto.get().isEnabled()) {
  162. return componentDto.get();
  163. }
  164. throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch));
  165. }
  166. public ComponentDto getByKeyAndPullRequest(DbSession dbSession, String key, String pullRequest) {
  167. Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndPullRequest(dbSession, key, pullRequest);
  168. if (componentDto.isPresent() && componentDto.get().isEnabled()) {
  169. return componentDto.get();
  170. }
  171. throw new NotFoundException(format("Component '%s' of pull request '%s' not found", key, pullRequest));
  172. }
  173. public ComponentDto getByKeyAndOptionalBranchOrPullRequest(DbSession dbSession, String key, @Nullable String branch, @Nullable String pullRequest) {
  174. checkArgument(branch == null || pullRequest == null, "Either branch or pull request can be provided, not both");
  175. if (branch != null) {
  176. return getByKeyAndBranch(dbSession, key, branch);
  177. }
  178. if (pullRequest != null) {
  179. return getByKeyAndPullRequest(dbSession, key, pullRequest);
  180. }
  181. return getByKey(dbSession, key);
  182. }
  183. public enum ParamNames {
  184. PROJECT_ID_AND_KEY("projectId", "projectKey"),
  185. PROJECT_UUID_AND_KEY("projectUuid", "projectKey"),
  186. PROJECT_UUID_AND_PROJECT("projectUuid", "project"),
  187. UUID_AND_KEY("uuid", "key"),
  188. ID_AND_KEY("id", "key"),
  189. COMPONENT_ID_AND_KEY("componentId", "componentKey"),
  190. BASE_COMPONENT_ID_AND_KEY("baseComponentId", "component"),
  191. DEVELOPER_ID_AND_KEY("developerId", "developerKey"),
  192. COMPONENT_ID_AND_COMPONENT("componentId", "component"),
  193. PROJECT_ID_AND_PROJECT("projectId", "project"),
  194. PROJECT_ID_AND_FROM("projectId", "from");
  195. private final String uuidParamName;
  196. private final String keyParamName;
  197. ParamNames(String uuidParamName, String keyParamName) {
  198. this.uuidParamName = uuidParamName;
  199. this.keyParamName = keyParamName;
  200. }
  201. public String getUuidParam() {
  202. return uuidParamName;
  203. }
  204. public String getKeyParam() {
  205. return keyParamName;
  206. }
  207. }
  208. }