選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ComponentFinder.java 14KB

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