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.

SearchAction.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.project.ws;
  21. import java.util.HashSet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.stream.Collectors;
  26. import javax.annotation.Nullable;
  27. import org.sonar.api.server.ws.Change;
  28. import org.sonar.api.server.ws.Request;
  29. import org.sonar.api.server.ws.Response;
  30. import org.sonar.api.server.ws.WebService;
  31. import org.sonar.api.server.ws.WebService.Param;
  32. import org.sonar.api.utils.Paging;
  33. import org.sonar.db.DatabaseUtils;
  34. import org.sonar.db.DbClient;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.component.BranchDto;
  37. import org.sonar.db.component.ComponentDto;
  38. import org.sonar.db.component.ComponentQuery;
  39. import org.sonar.db.component.ProjectLastAnalysisDateDto;
  40. import org.sonar.db.component.SnapshotDto;
  41. import org.sonar.db.permission.GlobalPermission;
  42. import org.sonar.server.management.ManagedProjectService;
  43. import org.sonar.server.project.Visibility;
  44. import org.sonar.server.user.UserSession;
  45. import org.sonarqube.ws.Projects.SearchWsResponse;
  46. import static java.util.Optional.ofNullable;
  47. import static java.util.function.Function.identity;
  48. import static java.util.stream.Collectors.toMap;
  49. import static org.sonar.api.resources.Qualifiers.APP;
  50. import static org.sonar.api.resources.Qualifiers.PROJECT;
  51. import static org.sonar.api.resources.Qualifiers.VIEW;
  52. import static org.sonar.api.utils.DateUtils.formatDateTime;
  53. import static org.sonar.api.utils.DateUtils.parseDateOrDateTime;
  54. import static org.sonar.server.project.Visibility.PRIVATE;
  55. import static org.sonar.server.project.Visibility.PUBLIC;
  56. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  57. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_002;
  58. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  59. import static org.sonarqube.ws.Projects.SearchWsResponse.Component;
  60. import static org.sonarqube.ws.Projects.SearchWsResponse.newBuilder;
  61. import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_SEARCH;
  62. import static org.sonarqube.ws.client.project.ProjectsWsParameters.MAX_PAGE_SIZE;
  63. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_ANALYZED_BEFORE;
  64. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_ON_PROVISIONED_ONLY;
  65. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECTS;
  66. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS;
  67. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_VISIBILITY;
  68. public class SearchAction implements ProjectsWsAction {
  69. private final DbClient dbClient;
  70. private final UserSession userSession;
  71. private final ManagedProjectService managedProjectService;
  72. public SearchAction(DbClient dbClient, UserSession userSession, ManagedProjectService managedProjectService) {
  73. this.dbClient = dbClient;
  74. this.userSession = userSession;
  75. this.managedProjectService = managedProjectService;
  76. }
  77. @Override
  78. public void define(WebService.NewController context) {
  79. WebService.NewAction action = context.createAction(ACTION_SEARCH)
  80. .setSince("6.3")
  81. .setDescription("Search for projects or views to administrate them.<br>" +
  82. "Requires 'Administer System' permission")
  83. .addPagingParams(100, MAX_PAGE_SIZE)
  84. .setResponseExample(getClass().getResource("search-example.json"))
  85. .setChangelog(new Change("10.2", "Response includes 'managed' field."))
  86. .setChangelog(new Change("9.1", "The parameter '" + PARAM_ANALYZED_BEFORE + "' and the field 'lastAnalysisDate' of the returned projects "
  87. + "take into account the analysis of all branches and pull requests, not only the main branch."))
  88. .setHandler(this);
  89. action.createParam(Param.TEXT_QUERY)
  90. .setDescription("Limit search to: <ul>" +
  91. "<li>component names that contain the supplied string</li>" +
  92. "<li>component keys that contain the supplied string</li>" +
  93. "</ul>")
  94. .setExampleValue("sonar");
  95. action.createParam(PARAM_QUALIFIERS)
  96. .setDescription("Comma-separated list of component qualifiers. Filter the results with the specified qualifiers")
  97. .setPossibleValues(PROJECT, VIEW, APP)
  98. .setDefaultValue(PROJECT);
  99. action.createParam(PARAM_VISIBILITY)
  100. .setDescription("Filter the projects that should be visible to everyone (%s), or only specific user/groups (%s).<br/>" +
  101. "If no visibility is specified, the default project visibility will be used.",
  102. Visibility.PUBLIC.getLabel(), Visibility.PRIVATE.getLabel())
  103. .setRequired(false)
  104. .setInternal(true)
  105. .setSince("6.4")
  106. .setPossibleValues(Visibility.getLabels());
  107. action.createParam(PARAM_ANALYZED_BEFORE)
  108. .setDescription("Filter the projects for which the last analysis of all branches are older than the given date (exclusive).<br> " +
  109. "Either a date (server timezone) or datetime can be provided.")
  110. .setSince("6.6")
  111. .setExampleValue("2017-10-19 or 2017-10-19T13:00:00+0200");
  112. action.createParam(PARAM_ON_PROVISIONED_ONLY)
  113. .setDescription("Filter the projects that are provisioned")
  114. .setBooleanPossibleValues()
  115. .setDefaultValue("false")
  116. .setSince("6.6");
  117. action
  118. .createParam(PARAM_PROJECTS)
  119. .setDescription("Comma-separated list of project keys")
  120. .setSince("6.6")
  121. // Limitation of ComponentDao#selectByQuery(), max 1000 values are accepted.
  122. // Restricting size of HTTP parameter allows to not fail with SQL error
  123. .setMaxValuesAllowed(DatabaseUtils.PARTITION_SIZE_FOR_ORACLE)
  124. .setExampleValue(String.join(",", KEY_PROJECT_EXAMPLE_001, KEY_PROJECT_EXAMPLE_002));
  125. }
  126. @Override
  127. public void handle(Request wsRequest, Response wsResponse) throws Exception {
  128. SearchWsResponse searchWsResponse = doHandle(toSearchWsRequest(wsRequest));
  129. writeProtobuf(searchWsResponse, wsRequest, wsResponse);
  130. }
  131. private static SearchRequest toSearchWsRequest(Request request) {
  132. return SearchRequest.builder()
  133. .setQualifiers(request.mandatoryParamAsStrings(PARAM_QUALIFIERS))
  134. .setQuery(request.param(Param.TEXT_QUERY))
  135. .setPage(request.mandatoryParamAsInt(Param.PAGE))
  136. .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
  137. .setVisibility(request.param(PARAM_VISIBILITY))
  138. .setAnalyzedBefore(request.param(PARAM_ANALYZED_BEFORE))
  139. .setOnProvisionedOnly(request.mandatoryParamAsBoolean(PARAM_ON_PROVISIONED_ONLY))
  140. .setProjects(request.paramAsStrings(PARAM_PROJECTS))
  141. .build();
  142. }
  143. private SearchWsResponse doHandle(SearchRequest request) {
  144. try (DbSession dbSession = dbClient.openSession(false)) {
  145. userSession.checkPermission(GlobalPermission.ADMINISTER);
  146. ComponentQuery query = buildDbQuery(request);
  147. Paging paging = buildPaging(dbSession, request, query);
  148. List<ComponentDto> components = dbClient.componentDao().selectByQuery(dbSession, query, paging.offset(), paging.pageSize());
  149. Set<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(Collectors.toSet());
  150. List<BranchDto> branchDtos = dbClient.branchDao().selectByUuids(dbSession, componentUuids);
  151. Map<String, String> componentUuidToProjectUuid = branchDtos.stream().collect(Collectors.toMap(BranchDto::getUuid,BranchDto::getProjectUuid));
  152. Map<String, Boolean> projectUuidToManaged = managedProjectService.getProjectUuidToManaged(dbSession, new HashSet<>(componentUuidToProjectUuid.values()));
  153. Map<String, Boolean> componentUuidToManaged = toComponentUuidToManaged(componentUuidToProjectUuid, projectUuidToManaged);
  154. Map<String, Long> lastAnalysisDateByComponentUuid = dbClient.snapshotDao().selectLastAnalysisDateByProjectUuids(dbSession, componentUuidToProjectUuid.values()).stream()
  155. .collect(Collectors.toMap(ProjectLastAnalysisDateDto::getProjectUuid, ProjectLastAnalysisDateDto::getDate));
  156. Map<String, SnapshotDto> snapshotsByComponentUuid = dbClient.snapshotDao()
  157. .selectLastAnalysesByRootComponentUuids(dbSession, componentUuids).stream()
  158. .collect(Collectors.toMap(SnapshotDto::getRootComponentUuid, identity()));
  159. return buildResponse(components, snapshotsByComponentUuid, lastAnalysisDateByComponentUuid, componentUuidToProjectUuid, componentUuidToManaged, paging);
  160. }
  161. }
  162. private Map<String, Boolean> toComponentUuidToManaged(Map<String, String> componentUuidToProjectUuid, Map<String, Boolean> projectUuidToManaged) {
  163. return componentUuidToProjectUuid.keySet().stream()
  164. .collect(toMap(identity(), componentUuid -> isComponentManaged(
  165. componentUuidToProjectUuid.get(componentUuid),
  166. projectUuidToManaged))
  167. );
  168. }
  169. private boolean isComponentManaged(String projectUuid, Map<String, Boolean> projectUuidToManaged) {
  170. return ofNullable(projectUuidToManaged.get(projectUuid)).orElse(false);
  171. }
  172. static ComponentQuery buildDbQuery(SearchRequest request) {
  173. List<String> qualifiers = request.getQualifiers();
  174. ComponentQuery.Builder query = ComponentQuery.builder()
  175. .setQualifiers(qualifiers.toArray(new String[qualifiers.size()]));
  176. ofNullable(request.getQuery()).ifPresent(q -> {
  177. query.setNameOrKeyQuery(q);
  178. query.setPartialMatchOnKey(true);
  179. });
  180. ofNullable(request.getVisibility()).ifPresent(v -> query.setPrivate(Visibility.isPrivate(v)));
  181. ofNullable(request.getAnalyzedBefore()).ifPresent(d -> query.setAllBranchesAnalyzedBefore(parseDateOrDateTime(d).getTime()));
  182. query.setOnProvisionedOnly(request.isOnProvisionedOnly());
  183. ofNullable(request.getProjects()).ifPresent(keys -> query.setComponentKeys(new HashSet<>(keys)));
  184. return query.build();
  185. }
  186. private Paging buildPaging(DbSession dbSession, SearchRequest request, ComponentQuery query) {
  187. int total = dbClient.componentDao().countByQuery(dbSession, query);
  188. return Paging.forPageIndex(request.getPage())
  189. .withPageSize(request.getPageSize())
  190. .andTotal(total);
  191. }
  192. private static SearchWsResponse buildResponse(List<ComponentDto> components, Map<String, SnapshotDto> snapshotsByComponentUuid,
  193. Map<String, Long> lastAnalysisDateByComponentUuid, Map<String, String> projectUuidByComponentUuid, Map<String, Boolean> componentUuidToManaged, Paging paging) {
  194. SearchWsResponse.Builder responseBuilder = newBuilder();
  195. responseBuilder.getPagingBuilder()
  196. .setPageIndex(paging.pageIndex())
  197. .setPageSize(paging.pageSize())
  198. .setTotal(paging.total())
  199. .build();
  200. components.stream()
  201. .map(dto -> dtoToProject(dto, snapshotsByComponentUuid.get(dto.uuid()), lastAnalysisDateByComponentUuid.get(projectUuidByComponentUuid.get(dto.uuid())),
  202. PROJECT.equals(dto.qualifier()) ? componentUuidToManaged.get(dto.uuid()) : null))
  203. .forEach(responseBuilder::addComponents);
  204. return responseBuilder.build();
  205. }
  206. private static Component dtoToProject(ComponentDto dto, @Nullable SnapshotDto snapshot, @Nullable Long lastAnalysisDate, @Nullable Boolean isManaged) {
  207. Component.Builder builder = Component.newBuilder()
  208. .setKey(dto.getKey())
  209. .setName(dto.name())
  210. .setQualifier(dto.qualifier())
  211. .setVisibility(dto.isPrivate() ? PRIVATE.getLabel() : PUBLIC.getLabel());
  212. if (snapshot != null) {
  213. ofNullable(lastAnalysisDate).ifPresent(d -> builder.setLastAnalysisDate(formatDateTime(d)));
  214. ofNullable(snapshot.getRevision()).ifPresent(builder::setRevision);
  215. }
  216. ofNullable(isManaged).ifPresent(builder::setManaged);
  217. return builder.build();
  218. }
  219. }