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.

ShowAction.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.component.ws;
  21. import java.util.List;
  22. import java.util.Optional;
  23. import java.util.stream.IntStream;
  24. import javax.annotation.CheckForNull;
  25. import javax.annotation.Nullable;
  26. import org.sonar.api.server.ws.Change;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.api.web.UserRole;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.component.SnapshotDto;
  34. import org.sonar.db.organization.OrganizationDto;
  35. import org.sonar.server.component.ComponentFinder;
  36. import org.sonar.server.user.UserSession;
  37. import org.sonarqube.ws.Components.ShowWsResponse;
  38. import static com.google.common.base.Preconditions.checkArgument;
  39. import static java.lang.String.format;
  40. import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
  41. import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_COMPONENT;
  42. import static org.sonar.server.component.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
  43. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH;
  44. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST;
  45. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  46. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  47. import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
  48. import static org.sonar.server.ws.WsUtils.checkRequest;
  49. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  50. import static org.sonarqube.ws.client.component.ComponentsWsParameters.ACTION_SHOW;
  51. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_COMPONENT;
  52. import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_COMPONENT_ID;
  53. public class ShowAction implements ComponentsWsAction {
  54. private final UserSession userSession;
  55. private final DbClient dbClient;
  56. private final ComponentFinder componentFinder;
  57. public ShowAction(UserSession userSession, DbClient dbClient, ComponentFinder componentFinder) {
  58. this.userSession = userSession;
  59. this.dbClient = dbClient;
  60. this.componentFinder = componentFinder;
  61. }
  62. @Override
  63. public void define(WebService.NewController context) {
  64. WebService.NewAction action = context.createAction(ACTION_SHOW)
  65. .setDescription(format("Returns a component (file, directory, project, view…) and its ancestors. " +
  66. "The ancestors are ordered from the parent to the root project. " +
  67. "The '%s' or '%s' parameter must be provided.<br>" +
  68. "Requires the following permission: 'Browse' on the project of the specified component.",
  69. PARAM_COMPONENT_ID, PARAM_COMPONENT))
  70. .setResponseExample(getClass().getResource("show-example.json"))
  71. .setSince("5.4")
  72. .setChangelog(
  73. new Change("6.4", "Analysis date has been added to the response"),
  74. new Change("6.4", "The field 'id' is deprecated in the response"),
  75. new Change("6.4", "The 'visibility' field is added to the response"),
  76. new Change("6.5", "Leak period date is added to the response"),
  77. new Change("6.6", "'branch' is added to the response"),
  78. new Change("6.6", "'version' is added to the response"))
  79. .setHandler(this);
  80. action.createParam(PARAM_COMPONENT_ID)
  81. .setDescription("Component id")
  82. .setDeprecatedKey("id", "6.4")
  83. .setDeprecatedSince("6.4")
  84. .setExampleValue(UUID_EXAMPLE_01);
  85. action.createParam(PARAM_COMPONENT)
  86. .setDescription("Component key")
  87. .setDeprecatedKey("key", "6.4")
  88. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  89. action.createParam(PARAM_BRANCH)
  90. .setDescription("Branch key")
  91. .setExampleValue(KEY_BRANCH_EXAMPLE_001)
  92. .setInternal(true)
  93. .setSince("6.6");
  94. action.createParam(PARAM_PULL_REQUEST)
  95. .setDescription("Pull request id")
  96. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001)
  97. .setInternal(true)
  98. .setSince("7.1");
  99. }
  100. @Override
  101. public void handle(org.sonar.api.server.ws.Request request, Response response) throws Exception {
  102. Request showRequest = toShowWsRequest(request);
  103. ShowWsResponse showWsResponse = doHandle(showRequest);
  104. writeProtobuf(showWsResponse, request, response);
  105. }
  106. private ShowWsResponse doHandle(Request request) {
  107. try (DbSession dbSession = dbClient.openSession(false)) {
  108. ComponentDto component = loadComponent(dbSession, request);
  109. userSession.checkComponentPermission(UserRole.USER, component);
  110. Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByComponentUuid(dbSession, component.projectUuid());
  111. List<ComponentDto> ancestors = dbClient.componentDao().selectAncestors(dbSession, component);
  112. OrganizationDto organizationDto = componentFinder.getOrganization(dbSession, component);
  113. return buildResponse(component, organizationDto, ancestors, lastAnalysis);
  114. }
  115. }
  116. private ComponentDto loadComponent(DbSession dbSession, Request request) {
  117. String componentId = request.getId();
  118. String componentKey = request.getKey();
  119. String branch = request.getBranch();
  120. String pullRequest = request.getPullRequest();
  121. checkArgument(componentId == null || (branch == null && pullRequest == null), "Parameter '%s' cannot be used at the same time as '%s' or '%s'", PARAM_COMPONENT_ID,
  122. PARAM_BRANCH, PARAM_PULL_REQUEST);
  123. if (branch == null && pullRequest == null) {
  124. return componentFinder.getByUuidOrKey(dbSession, componentId, componentKey, COMPONENT_ID_AND_COMPONENT);
  125. }
  126. checkRequest(componentKey!=null, "The '%s' parameter is missing", PARAM_COMPONENT);
  127. return componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, componentKey, branch, pullRequest);
  128. }
  129. private static ShowWsResponse buildResponse(ComponentDto component, OrganizationDto organizationDto, List<ComponentDto> orderedAncestors, Optional<SnapshotDto> lastAnalysis) {
  130. ShowWsResponse.Builder response = ShowWsResponse.newBuilder();
  131. response.setComponent(componentDtoToWsComponent(component, organizationDto, lastAnalysis));
  132. // ancestors are ordered from root to leaf, whereas it's the opposite in WS response
  133. int size = orderedAncestors.size() - 1;
  134. IntStream.rangeClosed(0, size).forEach(
  135. index -> response.addAncestors(componentDtoToWsComponent(orderedAncestors.get(size - index), organizationDto, lastAnalysis)));
  136. return response.build();
  137. }
  138. private static Request toShowWsRequest(org.sonar.api.server.ws.Request request) {
  139. return new Request()
  140. .setId(request.param(PARAM_COMPONENT_ID))
  141. .setKey(request.param(PARAM_COMPONENT))
  142. .setBranch(request.param(PARAM_BRANCH))
  143. .setPullRequest(request.param(PARAM_PULL_REQUEST));
  144. }
  145. private static class Request {
  146. private String id;
  147. private String key;
  148. private String branch;
  149. private String pullRequest;
  150. @CheckForNull
  151. public String getId() {
  152. return id;
  153. }
  154. public Request setId(@Nullable String id) {
  155. this.id = id;
  156. return this;
  157. }
  158. @CheckForNull
  159. public String getKey() {
  160. return key;
  161. }
  162. public Request setKey(@Nullable String key) {
  163. this.key = key;
  164. return this;
  165. }
  166. @CheckForNull
  167. public String getBranch() {
  168. return branch;
  169. }
  170. public Request setBranch(@Nullable String branch) {
  171. this.branch = branch;
  172. return this;
  173. }
  174. @CheckForNull
  175. public String getPullRequest() {
  176. return pullRequest;
  177. }
  178. public Request setPullRequest(@Nullable String pullRequest) {
  179. this.pullRequest = pullRequest;
  180. return this;
  181. }
  182. }
  183. }