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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.qualitygate.ws;
  21. import com.google.common.io.Resources;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.api.utils.Paging;
  28. import org.sonar.api.web.UserRole;
  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.organization.OrganizationDto;
  33. import org.sonar.db.qualitygate.ProjectQgateAssociation;
  34. import org.sonar.db.qualitygate.ProjectQgateAssociationDto;
  35. import org.sonar.db.qualitygate.ProjectQgateAssociationQuery;
  36. import org.sonar.db.qualitygate.QGateWithOrgDto;
  37. import org.sonar.server.user.UserSession;
  38. import org.sonarqube.ws.Qualitygates;
  39. import static org.sonar.api.server.ws.WebService.Param.SELECTED;
  40. import static org.sonar.api.utils.Paging.forPageIndex;
  41. import static org.sonar.db.qualitygate.ProjectQgateAssociationQuery.ANY;
  42. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_GATE_ID;
  43. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PAGE;
  44. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PAGE_SIZE;
  45. import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_QUERY;
  46. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  47. public class SearchAction implements QualityGatesWsAction {
  48. private final DbClient dbClient;
  49. private final UserSession userSession;
  50. private final QualityGatesWsSupport wsSupport;
  51. public SearchAction(DbClient dbClient, UserSession userSession, QualityGatesWsSupport wsSupport) {
  52. this.dbClient = dbClient;
  53. this.userSession = userSession;
  54. this.wsSupport = wsSupport;
  55. }
  56. @Override
  57. public void define(WebService.NewController controller) {
  58. WebService.NewAction action = controller.createAction("search")
  59. .setDescription("Search for projects associated (or not) to a quality gate.<br/>" +
  60. "Only authorized projects for current user will be returned.")
  61. .setSince("4.3")
  62. .setResponseExample(Resources.getResource(this.getClass(), "search-example.json"))
  63. .setHandler(this);
  64. action.createParam(PARAM_GATE_ID)
  65. .setDescription("Quality Gate ID")
  66. .setRequired(true)
  67. .setExampleValue("1");
  68. action.createParam(PARAM_QUERY)
  69. .setDescription("To search for projects containing this string. If this parameter is set, \"selected\" is set to \"all\".")
  70. .setExampleValue("abc");
  71. action.addSelectionModeParam();
  72. action.createParam(PARAM_PAGE)
  73. .setDescription("Page number")
  74. .setDefaultValue("1")
  75. .setExampleValue("2");
  76. action.createParam(PARAM_PAGE_SIZE)
  77. .setDescription("Page size")
  78. .setExampleValue("10");
  79. wsSupport.createOrganizationParam(action);
  80. }
  81. @Override
  82. public void handle(Request request, Response response) {
  83. try (DbSession dbSession = dbClient.openSession(false)) {
  84. OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
  85. QGateWithOrgDto qualityGate = wsSupport.getByOrganizationAndId(dbSession, organization, request.mandatoryParamAsLong(PARAM_GATE_ID));
  86. Association associations = find(dbSession,
  87. ProjectQgateAssociationQuery.builder()
  88. .qualityGate(qualityGate)
  89. .membership(request.param(PARAM_QUERY) == null ? request.param(SELECTED) : ANY)
  90. .projectSearch(request.param(PARAM_QUERY))
  91. .pageIndex(request.paramAsInt(PARAM_PAGE))
  92. .pageSize(request.paramAsInt(PARAM_PAGE_SIZE))
  93. .build());
  94. Qualitygates.SearchResponse.Builder createResponse = Qualitygates.SearchResponse.newBuilder()
  95. .setMore(associations.hasMoreResults());
  96. for (ProjectQgateAssociation project : associations.projects()) {
  97. createResponse.addResultsBuilder()
  98. .setId(project.id())
  99. .setName(project.name())
  100. .setSelected(project.isMember());
  101. }
  102. writeProtobuf(createResponse.build(), request, response);
  103. }
  104. }
  105. private SearchAction.Association find(DbSession dbSession, ProjectQgateAssociationQuery query) {
  106. List<ProjectQgateAssociationDto> projects = dbClient.projectQgateAssociationDao().selectProjects(dbSession, query);
  107. List<ProjectQgateAssociationDto> authorizedProjects = keepAuthorizedProjects(dbSession, projects);
  108. Paging paging = forPageIndex(query.pageIndex())
  109. .withPageSize(query.pageSize())
  110. .andTotal(authorizedProjects.size());
  111. return new SearchAction.Association(toProjectAssociations(getPaginatedProjects(authorizedProjects, paging)), paging.hasNextPage());
  112. }
  113. private static List<ProjectQgateAssociationDto> getPaginatedProjects(List<ProjectQgateAssociationDto> projects, Paging paging) {
  114. return projects.stream().skip(paging.offset()).limit(paging.pageSize()).collect(MoreCollectors.toList());
  115. }
  116. private static List<ProjectQgateAssociation> toProjectAssociations(List<ProjectQgateAssociationDto> dtos) {
  117. return dtos.stream().map(ProjectQgateAssociationDto::toQgateAssociation).collect(MoreCollectors.toList());
  118. }
  119. private List<ProjectQgateAssociationDto> keepAuthorizedProjects(DbSession dbSession, List<ProjectQgateAssociationDto> projects) {
  120. if (userSession.isRoot()) {
  121. // the method AuthorizationDao#keepAuthorizedProjectIds() should be replaced by
  122. // a call to UserSession, which would transparently support roots.
  123. // Meanwhile root is explicitly handled.
  124. return projects;
  125. }
  126. List<Long> projectIds = projects.stream().map(ProjectQgateAssociationDto::getId).collect(MoreCollectors.toList());
  127. Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(dbSession, projectIds, userSession.getUserId(), UserRole.USER);
  128. return projects.stream().filter(project -> authorizedProjectIds.contains(project.getId())).collect(MoreCollectors.toList());
  129. }
  130. private static class Association {
  131. private List<ProjectQgateAssociation> projects;
  132. private boolean hasMoreResults;
  133. private Association(List<ProjectQgateAssociation> projects, boolean hasMoreResults) {
  134. this.projects = projects;
  135. this.hasMoreResults = hasMoreResults;
  136. }
  137. public List<ProjectQgateAssociation> projects() {
  138. return projects;
  139. }
  140. public boolean hasMoreResults() {
  141. return hasMoreResults;
  142. }
  143. }
  144. }