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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.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.ComponentDto;
  33. import org.sonar.db.organization.OrganizationDto;
  34. import org.sonar.server.exceptions.NotFoundException;
  35. import static com.google.common.base.Preconditions.checkArgument;
  36. import static java.lang.String.format;
  37. import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
  38. import static org.sonar.server.ws.WsUtils.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 final DbClient dbClient;
  45. private final ResourceTypes resourceTypes;
  46. public ComponentFinder(DbClient dbClient, ResourceTypes resourceTypes) {
  47. this.dbClient = dbClient;
  48. this.resourceTypes = resourceTypes;
  49. }
  50. public ComponentDto getByUuidOrKey(DbSession dbSession, @Nullable String componentUuid, @Nullable String componentKey, ParamNames parameterNames) {
  51. checkByUuidOrKey(componentUuid, componentKey, parameterNames);
  52. if (componentUuid != null) {
  53. return getByUuid(dbSession, checkParamNotEmpty(componentUuid, parameterNames.getUuidParam()));
  54. }
  55. return getByKey(dbSession, checkParamNotEmpty(componentKey, parameterNames.getKeyParam()));
  56. }
  57. private static String checkParamNotEmpty(String value, String param) {
  58. checkArgument(!value.isEmpty(), MSG_PARAMETER_MUST_NOT_BE_EMPTY, param);
  59. return value;
  60. }
  61. private static void checkByUuidOrKey(@Nullable String componentUuid, @Nullable String componentKey, ParamNames parameterNames) {
  62. checkArgument(componentUuid != null ^ componentKey != null, MSG_COMPONENT_ID_OR_KEY_TEMPLATE, parameterNames.getUuidParam(), parameterNames.getKeyParam());
  63. }
  64. public ComponentDto getByKey(DbSession dbSession, String key) {
  65. return getByKey(dbSession, key, LABEL_COMPONENT);
  66. }
  67. private ComponentDto getByKey(DbSession dbSession, String key, String label) {
  68. return checkComponent(dbClient.componentDao().selectByKey(dbSession, key), "%s key '%s' not found", label, key);
  69. }
  70. public ComponentDto getByUuid(DbSession dbSession, String uuid) {
  71. return getByUuid(dbSession, uuid, LABEL_COMPONENT);
  72. }
  73. private ComponentDto getByUuid(DbSession dbSession, String uuid, String label) {
  74. return checkComponent(dbClient.componentDao().selectByUuid(dbSession, uuid), "%s id '%s' not found", label, uuid);
  75. }
  76. private static ComponentDto checkComponent(Optional<ComponentDto> componentDto, String message, Object... messageArguments) {
  77. if (componentDto.isPresent() && componentDto.get().isEnabled() && componentDto.get().getMainBranchProjectUuid() == null) {
  78. return componentDto.get();
  79. }
  80. throw new NotFoundException(format(message, messageArguments));
  81. }
  82. public ComponentDto getRootComponentByUuidOrKey(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey) {
  83. ComponentDto project;
  84. if (projectUuid != null) {
  85. project = getByUuid(dbSession, projectUuid, LABEL_PROJECT);
  86. } else {
  87. project = getByKey(dbSession, projectKey, LABEL_PROJECT);
  88. }
  89. checkIsProject(project);
  90. return project;
  91. }
  92. private ComponentDto checkIsProject(ComponentDto component) {
  93. Set<String> rootQualifiers = getRootQualifiers(resourceTypes);
  94. checkRequest(component.scope().equals(Scopes.PROJECT) && rootQualifiers.contains(component.qualifier()),
  95. format(
  96. "Component '%s' (id: %s) must be a project%s.",
  97. component.getDbKey(), component.uuid(),
  98. rootQualifiers.contains(Qualifiers.VIEW) ? " or a view" : ""));
  99. return component;
  100. }
  101. private static Set<String> getRootQualifiers(ResourceTypes resourceTypes) {
  102. Collection<ResourceType> rootTypes = resourceTypes.getRoots();
  103. return rootTypes
  104. .stream()
  105. .map(ResourceType::getQualifier)
  106. .collect(MoreCollectors.toSet(rootTypes.size()));
  107. }
  108. public OrganizationDto getOrganization(DbSession dbSession, ComponentDto component) {
  109. String organizationUuid = component.getOrganizationUuid();
  110. Optional<OrganizationDto> organizationDto = dbClient.organizationDao().selectByUuid(dbSession, organizationUuid);
  111. return checkFoundWithOptional(organizationDto, "Organization with uuid '%s' not found", organizationUuid);
  112. }
  113. public ComponentDto getByKeyAndBranch(DbSession dbSession, String key, String branch) {
  114. Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndBranch(dbSession, key, branch);
  115. if (componentDto.isPresent() && componentDto.get().isEnabled()) {
  116. return componentDto.get();
  117. }
  118. throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch));
  119. }
  120. public ComponentDto getByKeyAndPullRequest(DbSession dbSession, String key, String pullRequest) {
  121. Optional<ComponentDto> componentDto = dbClient.componentDao().selectByKeyAndPullRequest(dbSession, key, pullRequest);
  122. if (componentDto.isPresent() && componentDto.get().isEnabled()) {
  123. return componentDto.get();
  124. }
  125. throw new NotFoundException(format("Component '%s' of pull request '%s' not found", key, pullRequest));
  126. }
  127. public ComponentDto getByKeyAndOptionalBranchOrPullRequest(DbSession dbSession, String key, @Nullable String branch, @Nullable String pullRequest) {
  128. checkArgument(branch == null || pullRequest == null, "Either branch or pull request can be provided, not both");
  129. if (branch != null) {
  130. return getByKeyAndBranch(dbSession, key, branch);
  131. }
  132. if (pullRequest != null) {
  133. return getByKeyAndPullRequest(dbSession, key, pullRequest);
  134. }
  135. return getByKey(dbSession, key);
  136. }
  137. public enum ParamNames {
  138. PROJECT_ID_AND_KEY("projectId", "projectKey"),
  139. PROJECT_UUID_AND_KEY("projectUuid", "projectKey"),
  140. PROJECT_UUID_AND_PROJECT("projectUuid", "project"),
  141. UUID_AND_KEY("uuid", "key"),
  142. ID_AND_KEY("id", "key"),
  143. COMPONENT_ID_AND_KEY("componentId", "componentKey"),
  144. BASE_COMPONENT_ID_AND_KEY("baseComponentId", "component"),
  145. DEVELOPER_ID_AND_KEY("developerId", "developerKey"),
  146. COMPONENT_ID_AND_COMPONENT("componentId", "component"),
  147. PROJECT_ID_AND_PROJECT("projectId", "project"),
  148. PROJECT_ID_AND_FROM("projectId", "from");
  149. private final String uuidParamName;
  150. private final String keyParamName;
  151. ParamNames(String uuidParamName, String keyParamName) {
  152. this.uuidParamName = uuidParamName;
  153. this.keyParamName = keyParamName;
  154. }
  155. public String getUuidParam() {
  156. return uuidParamName;
  157. }
  158. public String getKeyParam() {
  159. return keyParamName;
  160. }
  161. }
  162. }