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.

ListAction.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.issue.ws;
  21. import com.google.common.base.Preconditions;
  22. import java.util.EnumSet;
  23. import java.util.List;
  24. import javax.annotation.Nullable;
  25. import org.sonar.api.rules.RuleType;
  26. import org.sonar.api.server.ws.Request;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.api.utils.Paging;
  30. import org.sonar.api.web.UserRole;
  31. import org.sonar.db.DbClient;
  32. import org.sonar.db.DbSession;
  33. import org.sonar.db.Pagination;
  34. import org.sonar.db.component.BranchDto;
  35. import org.sonar.db.component.ComponentDto;
  36. import org.sonar.db.issue.IssueDto;
  37. import org.sonar.db.issue.IssueListQuery;
  38. import org.sonar.db.newcodeperiod.NewCodePeriodType;
  39. import org.sonar.db.project.ProjectDto;
  40. import org.sonar.server.component.ComponentFinder;
  41. import org.sonar.server.component.ComponentFinder.ProjectAndBranch;
  42. import org.sonar.server.issue.NewCodePeriodResolver;
  43. import org.sonar.server.issue.NewCodePeriodResolver.ResolvedNewCodePeriod;
  44. import org.sonar.server.user.UserSession;
  45. import org.sonarqube.ws.Common;
  46. import org.sonarqube.ws.Issues;
  47. import static com.google.common.base.Strings.isNullOrEmpty;
  48. import static java.util.Collections.singletonList;
  49. import static org.sonar.api.server.ws.WebService.Param.PAGE;
  50. import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
  51. import static org.sonar.api.utils.Paging.forPageIndex;
  52. import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE;
  53. import static org.sonar.server.issue.index.IssueQueryFactory.ISSUE_STATUSES;
  54. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  55. import static org.sonarqube.ws.WsUtils.checkArgument;
  56. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_LIST;
  57. public class ListAction implements IssuesWsAction {
  58. private static final String PARAM_PROJECT = "project";
  59. private static final String PARAM_BRANCH = "branch";
  60. private static final String PARAM_PULL_REQUEST = "pullRequest";
  61. private static final String PARAM_COMPONENT = "component";
  62. private static final String PARAM_TYPES = "types";
  63. private static final String PARAM_RESOLVED = "resolved";
  64. private static final String PARAM_IN_NEW_CODE_PERIOD = "inNewCodePeriod";
  65. private final UserSession userSession;
  66. private final DbClient dbClient;
  67. private final NewCodePeriodResolver newCodePeriodResolver;
  68. private final SearchResponseLoader searchResponseLoader;
  69. private final SearchResponseFormat searchResponseFormat;
  70. private final ComponentFinder componentFinder;
  71. public ListAction(UserSession userSession, DbClient dbClient, NewCodePeriodResolver newCodePeriodResolver, SearchResponseLoader searchResponseLoader,
  72. SearchResponseFormat searchResponseFormat, ComponentFinder componentFinder) {
  73. this.userSession = userSession;
  74. this.dbClient = dbClient;
  75. this.newCodePeriodResolver = newCodePeriodResolver;
  76. this.searchResponseLoader = searchResponseLoader;
  77. this.searchResponseFormat = searchResponseFormat;
  78. this.componentFinder = componentFinder;
  79. }
  80. @Override
  81. public void define(WebService.NewController controller) {
  82. WebService.NewAction action = controller
  83. .createAction(ACTION_LIST)
  84. .setHandler(this)
  85. .setInternal(true)
  86. .setDescription("List issues. This endpoint is used in degraded mode, when issue indexation is running." +
  87. "<br>Either 'project' or 'component' parameter is required." +
  88. "<br>Total number of issues will be always equal to a page size, as this counting all issues is not supported. " +
  89. "<br>Requires the 'Browse' permission on the specified project. ")
  90. .setSince("10.2")
  91. .setResponseExample(getClass().getResource("list-example.json"));
  92. action.addPagingParams(100, MAX_PAGE_SIZE);
  93. action.createParam(PARAM_PROJECT)
  94. .setDescription("Project key")
  95. .setExampleValue("my-project-key");
  96. action.createParam(PARAM_BRANCH)
  97. .setDescription("Branch key. Not available in the community edition.")
  98. .setExampleValue("feature/my-new-feature");
  99. action.createParam(PARAM_PULL_REQUEST)
  100. .setDescription("Filter issues that belong to the specified pull request. Not available in the community edition.")
  101. .setExampleValue("42");
  102. action.createParam(PARAM_COMPONENT)
  103. .setDescription("Component key")
  104. .setExampleValue("my_project:my_file.js");
  105. action.createParam(PARAM_TYPES)
  106. .setDescription("Comma-separated list of issue types")
  107. .setExampleValue("BUG, VULNERABILITY")
  108. .setPossibleValues(RuleType.BUG.name(), RuleType.VULNERABILITY.name(), RuleType.CODE_SMELL.name());
  109. action.createParam(PARAM_IN_NEW_CODE_PERIOD)
  110. .setDescription("Filter issues created in the new code period of the project")
  111. .setExampleValue("true")
  112. .setDefaultValue(false)
  113. .setBooleanPossibleValues();
  114. action.createParam(PARAM_RESOLVED)
  115. .setDescription("Filter issues that are resolved or not, if not provided all issues will be returned")
  116. .setExampleValue("true")
  117. .setBooleanPossibleValues();
  118. }
  119. @Override
  120. public final void handle(Request request, Response response) {
  121. WsRequest wsRequest = toWsRequest(request);
  122. ProjectAndBranch projectAndBranch = validateRequest(wsRequest);
  123. List<IssueDto> issues = getIssueKeys(wsRequest, projectAndBranch);
  124. Issues.ListWsResponse wsResponse = formatResponse(wsRequest, issues);
  125. writeProtobuf(wsResponse, request, response);
  126. }
  127. private static WsRequest toWsRequest(Request request) {
  128. WsRequest wsRequest = new WsRequest();
  129. wsRequest.project(request.param(PARAM_PROJECT));
  130. wsRequest.component(request.param(PARAM_COMPONENT));
  131. wsRequest.branch(request.param(PARAM_BRANCH));
  132. wsRequest.pullRequest(request.param(PARAM_PULL_REQUEST));
  133. List<String> types = request.paramAsStrings(PARAM_TYPES);
  134. wsRequest.types(types == null ? List.of(RuleType.BUG.getDbConstant(), RuleType.VULNERABILITY.getDbConstant(), RuleType.CODE_SMELL.getDbConstant())
  135. : types.stream().map(RuleType::valueOf).map(RuleType::getDbConstant).toList());
  136. wsRequest.newCodePeriod(request.mandatoryParamAsBoolean(PARAM_IN_NEW_CODE_PERIOD));
  137. wsRequest.resolved(request.paramAsBoolean(PARAM_RESOLVED));
  138. wsRequest.page(request.mandatoryParamAsInt(PAGE));
  139. wsRequest.pageSize(request.mandatoryParamAsInt(PAGE_SIZE));
  140. return wsRequest;
  141. }
  142. private ProjectAndBranch validateRequest(WsRequest wsRequest) {
  143. checkArgument(!isNullOrEmpty(wsRequest.project) || !isNullOrEmpty(wsRequest.component),
  144. "Either '%s' or '%s' parameter must be provided", PARAM_PROJECT, PARAM_COMPONENT);
  145. Preconditions.checkArgument(isNullOrEmpty(wsRequest.branch) || isNullOrEmpty(wsRequest.pullRequest),
  146. "Only one of parameters '%s' and '%s' can be provided", PARAM_BRANCH, PARAM_PULL_REQUEST);
  147. ProjectAndBranch projectAndBranch;
  148. try (DbSession dbSession = dbClient.openSession(false)) {
  149. if (!isNullOrEmpty(wsRequest.component)) {
  150. projectAndBranch = checkComponentPermission(wsRequest, dbSession);
  151. } else {
  152. projectAndBranch = checkProjectAndBranchPermission(wsRequest, dbSession);
  153. }
  154. }
  155. return projectAndBranch;
  156. }
  157. private ProjectAndBranch checkComponentPermission(WsRequest wsRequest, DbSession dbSession) {
  158. ComponentDto componentDto = componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, wsRequest.component, wsRequest.branch, wsRequest.pullRequest);
  159. BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, componentDto.branchUuid())
  160. .orElseThrow(() -> new IllegalStateException("Branch does not exist: " + componentDto.branchUuid()));
  161. ProjectDto projectDto = dbClient.projectDao().selectByUuid(dbSession, branchDto.getProjectUuid())
  162. .orElseThrow(() -> new IllegalArgumentException("Project does not exist: " + wsRequest.project));
  163. userSession.checkEntityPermission(UserRole.USER, projectDto);
  164. return new ProjectAndBranch(projectDto, branchDto);
  165. }
  166. private ProjectAndBranch checkProjectAndBranchPermission(WsRequest wsRequest, DbSession dbSession) {
  167. ProjectAndBranch projectAndBranch = componentFinder.getProjectAndBranch(dbSession, wsRequest.project, wsRequest.branch, wsRequest.pullRequest);
  168. userSession.checkEntityPermission(UserRole.USER, projectAndBranch.getProject());
  169. return projectAndBranch;
  170. }
  171. private List<IssueDto> getIssueKeys(WsRequest wsRequest, ProjectAndBranch projectAndBranch) {
  172. try (DbSession dbSession = dbClient.openSession(false)) {
  173. BranchDto branch = projectAndBranch.getBranch();
  174. IssueListQuery.IssueListQueryBuilder queryBuilder = IssueListQuery.IssueListQueryBuilder.newIssueListQueryBuilder()
  175. .project(wsRequest.project)
  176. .component(wsRequest.component)
  177. .branch(branch.getBranchKey())
  178. .pullRequest(branch.getPullRequestKey())
  179. .resolved(wsRequest.resolved)
  180. .statuses(ISSUE_STATUSES)
  181. .types(wsRequest.types);
  182. String branchKey = branch.getBranchKey();
  183. if (wsRequest.inNewCodePeriod && wsRequest.pullRequest == null && branchKey != null) {
  184. ResolvedNewCodePeriod newCodePeriod = newCodePeriodResolver.resolveForProjectAndBranch(dbSession, wsRequest.project, branchKey);
  185. if (NewCodePeriodType.REFERENCE_BRANCH == newCodePeriod.type()) {
  186. queryBuilder.newCodeOnReference(true);
  187. } else {
  188. queryBuilder.createdAfter(newCodePeriod.periodDate());
  189. }
  190. }
  191. Pagination pagination = Pagination.forPage(wsRequest.page).andSize(wsRequest.pageSize);
  192. return dbClient.issueDao().selectByQuery(dbSession, queryBuilder.build(), pagination);
  193. }
  194. }
  195. private Issues.ListWsResponse formatResponse(WsRequest request, List<IssueDto> issues) {
  196. Issues.ListWsResponse.Builder response = Issues.ListWsResponse.newBuilder();
  197. response.setPaging(Common.Paging.newBuilder()
  198. .setPageIndex(request.page)
  199. .setPageSize(issues.size())
  200. .build());
  201. List<String> issueKeys = issues.stream().map(IssueDto::getKey).toList();
  202. SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(issueKeys);
  203. collectLoggedInUser(collector);
  204. SearchResponseData preloadedData = new SearchResponseData(issues);
  205. EnumSet<SearchAdditionalField> additionalFields = EnumSet.of(SearchAdditionalField.ACTIONS, SearchAdditionalField.COMMENTS, SearchAdditionalField.TRANSITIONS);
  206. SearchResponseData data = searchResponseLoader.load(preloadedData, collector, additionalFields, null);
  207. Paging paging = forPageIndex(request.page)
  208. .withPageSize(request.pageSize)
  209. .andTotal(request.pageSize);
  210. return searchResponseFormat.formatList(additionalFields, data, paging);
  211. }
  212. private void collectLoggedInUser(SearchResponseLoader.Collector collector) {
  213. if (userSession.isLoggedIn()) {
  214. collector.addUserUuids(singletonList(userSession.getUuid()));
  215. }
  216. }
  217. private static class WsRequest {
  218. private String project = null;
  219. private String component = null;
  220. private String branch = null;
  221. private String pullRequest = null;
  222. private List<Integer> types = null;
  223. private boolean inNewCodePeriod = false;
  224. private Boolean resolved = null;
  225. private int page = 1;
  226. private int pageSize = 100;
  227. public WsRequest project(@Nullable String project) {
  228. this.project = project;
  229. return this;
  230. }
  231. public WsRequest component(@Nullable String component) {
  232. this.component = component;
  233. return this;
  234. }
  235. public WsRequest branch(@Nullable String branch) {
  236. this.branch = branch;
  237. return this;
  238. }
  239. public WsRequest pullRequest(@Nullable String pullRequest) {
  240. this.pullRequest = pullRequest;
  241. return this;
  242. }
  243. public WsRequest types(@Nullable List<Integer> types) {
  244. this.types = types;
  245. return this;
  246. }
  247. public WsRequest newCodePeriod(boolean newCodePeriod) {
  248. inNewCodePeriod = newCodePeriod;
  249. return this;
  250. }
  251. public WsRequest resolved(@Nullable Boolean resolved) {
  252. this.resolved = resolved;
  253. return this;
  254. }
  255. public WsRequest page(int page) {
  256. this.page = page;
  257. return this;
  258. }
  259. public WsRequest pageSize(int pageSize) {
  260. this.pageSize = pageSize;
  261. return this;
  262. }
  263. }
  264. }