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

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