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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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.issue.ws;
  21. import com.google.common.base.Joiner;
  22. import com.google.common.collect.ImmutableList;
  23. import com.google.common.collect.Lists;
  24. import java.util.Arrays;
  25. import java.util.Collection;
  26. import java.util.EnumSet;
  27. import java.util.HashSet;
  28. import java.util.LinkedHashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import java.util.stream.Collectors;
  33. import java.util.stream.Stream;
  34. import javax.annotation.Nullable;
  35. import org.elasticsearch.action.search.SearchResponse;
  36. import org.elasticsearch.search.SearchHit;
  37. import org.sonar.api.issue.Issue;
  38. import org.sonar.api.rule.RuleKey;
  39. import org.sonar.api.rule.Severity;
  40. import org.sonar.api.rules.RuleType;
  41. import org.sonar.api.server.ws.Change;
  42. import org.sonar.api.server.ws.Request;
  43. import org.sonar.api.server.ws.Response;
  44. import org.sonar.api.server.ws.WebService;
  45. import org.sonar.api.server.ws.WebService.Param;
  46. import org.sonar.api.utils.Paging;
  47. import org.sonar.api.utils.System2;
  48. import org.sonar.api.utils.log.Logger;
  49. import org.sonar.api.utils.log.Loggers;
  50. import org.sonar.core.util.stream.MoreCollectors;
  51. import org.sonar.db.DbClient;
  52. import org.sonar.db.DbSession;
  53. import org.sonar.db.rule.RuleDefinitionDto;
  54. import org.sonar.server.es.Facets;
  55. import org.sonar.server.es.SearchOptions;
  56. import org.sonar.server.issue.IssueQuery;
  57. import org.sonar.server.issue.IssueQueryFactory;
  58. import org.sonar.server.issue.SearchRequest;
  59. import org.sonar.server.issue.index.IssueIndex;
  60. import org.sonar.server.user.UserSession;
  61. import org.sonarqube.ws.Issues.SearchWsResponse;
  62. import static com.google.common.base.Preconditions.checkArgument;
  63. import static com.google.common.collect.Iterables.concat;
  64. import static com.google.common.collect.Sets.newHashSet;
  65. import static java.lang.String.format;
  66. import static java.util.Collections.emptyList;
  67. import static java.util.Collections.singletonList;
  68. import static org.sonar.api.utils.Paging.forPageIndex;
  69. import static org.sonar.core.util.stream.MoreCollectors.toSet;
  70. import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
  71. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  72. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  73. import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
  74. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  75. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_SEARCH;
  76. import static org.sonarqube.ws.client.issue.IssuesWsParameters.DEPRECATED_FACET_MODE_DEBT;
  77. import static org.sonarqube.ws.client.issue.IssuesWsParameters.DEPRECATED_PARAM_ACTION_PLANS;
  78. import static org.sonarqube.ws.client.issue.IssuesWsParameters.FACET_ASSIGNED_TO_ME;
  79. import static org.sonarqube.ws.client.issue.IssuesWsParameters.FACET_MODE;
  80. import static org.sonarqube.ws.client.issue.IssuesWsParameters.FACET_MODE_COUNT;
  81. import static org.sonarqube.ws.client.issue.IssuesWsParameters.FACET_MODE_EFFORT;
  82. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ADDITIONAL_FIELDS;
  83. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ASC;
  84. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ASSIGNED;
  85. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ASSIGNEES;
  86. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_AUTHORS;
  87. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_BRANCH;
  88. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENTS;
  89. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_KEYS;
  90. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_ROOTS;
  91. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_ROOT_UUIDS;
  92. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMPONENT_UUIDS;
  93. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_CREATED_AFTER;
  94. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_CREATED_AT;
  95. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_CREATED_BEFORE;
  96. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_CREATED_IN_LAST;
  97. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_DIRECTORIES;
  98. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_FILE_UUIDS;
  99. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUES;
  100. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_LANGUAGES;
  101. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_MODULE_UUIDS;
  102. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ON_COMPONENT_ONLY;
  103. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ORGANIZATION;
  104. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_PLANNED;
  105. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_PROJECTS;
  106. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_PROJECT_KEYS;
  107. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_PROJECT_UUIDS;
  108. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_PULL_REQUEST;
  109. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_REPORTERS;
  110. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_RESOLUTIONS;
  111. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_RESOLVED;
  112. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_RULES;
  113. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SEVERITIES;
  114. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_SINCE_LEAK_PERIOD;
  115. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_STATUSES;
  116. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TAGS;
  117. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TYPES;
  118. public class SearchAction implements IssuesWsAction {
  119. private static final String INTERNAL_PARAMETER_DISCLAIMER = "This parameter is mostly used by the Issues page, please prefer usage of the componentKeys parameter. ";
  120. private static final Set<String> IGNORED_FACETS = newHashSet(PARAM_PLANNED, DEPRECATED_PARAM_ACTION_PLANS, PARAM_REPORTERS);
  121. private static final Set<String> FACETS_REQUIRING_PROJECT_OR_ORGANIZATION = newHashSet(PARAM_FILE_UUIDS, PARAM_DIRECTORIES, PARAM_MODULE_UUIDS);
  122. private static final Joiner COMA_JOINER = Joiner.on(",");
  123. private static final Logger LOGGER = Loggers.get(SearchAction.class);
  124. private final UserSession userSession;
  125. private final IssueIndex issueIndex;
  126. private final IssueQueryFactory issueQueryFactory;
  127. private final SearchResponseLoader searchResponseLoader;
  128. private final SearchResponseFormat searchResponseFormat;
  129. private final System2 system2;
  130. private final DbClient dbClient;
  131. public SearchAction(UserSession userSession, IssueIndex issueIndex, IssueQueryFactory issueQueryFactory,
  132. SearchResponseLoader searchResponseLoader, SearchResponseFormat searchResponseFormat, System2 system2,
  133. DbClient dbClient) {
  134. this.userSession = userSession;
  135. this.issueIndex = issueIndex;
  136. this.issueQueryFactory = issueQueryFactory;
  137. this.searchResponseLoader = searchResponseLoader;
  138. this.searchResponseFormat = searchResponseFormat;
  139. this.system2 = system2;
  140. this.dbClient = dbClient;
  141. }
  142. @Override
  143. public void define(WebService.NewController controller) {
  144. WebService.NewAction action = controller
  145. .createAction(ACTION_SEARCH)
  146. .setHandler(this)
  147. .setDescription(
  148. "Search for issues.<br>" +
  149. "At most one of the following parameters can be provided at the same time: %s, %s, %s, %s, %s.<br>" +
  150. "Requires the 'Browse' permission on the specified project(s).",
  151. PARAM_COMPONENT_KEYS, PARAM_COMPONENT_UUIDS, PARAM_COMPONENTS, PARAM_COMPONENT_ROOT_UUIDS, PARAM_COMPONENT_ROOTS)
  152. .setSince("3.6")
  153. .setChangelog(
  154. new Change("6.5", "parameters 'projects', 'projectUuids', 'moduleUuids', 'directories', 'fileUuids' are marked as internal"),
  155. new Change("6.3", "response field 'email' is renamed 'avatar'"),
  156. new Change("5.5", "response fields 'reporter' and 'actionPlan' are removed (drop of action plan and manual issue features)"),
  157. new Change("5.5", "parameters 'reporters', 'actionPlans' and 'planned' are dropped and therefore ignored (drop of action plan and manual issue features)"),
  158. new Change("5.5", "response field 'debt' is renamed 'effort'"))
  159. .setResponseExample(getClass().getResource("search-example.json"));
  160. action.addPagingParams(100, MAX_LIMIT);
  161. action.createParam(Param.FACETS)
  162. .setDescription("Comma-separated list of the facets to be computed. No facet is computed by default.<br/>" +
  163. "Since 5.5, facet 'actionPlans' is deprecated.<br/>" +
  164. "Since 5.5, facet 'reporters' is deprecated.")
  165. .setPossibleValues(IssueIndex.SUPPORTED_FACETS);
  166. action.createParam(FACET_MODE)
  167. .setDefaultValue(FACET_MODE_COUNT)
  168. .setDescription("Choose the returned value for facet items, either count of issues or sum of debt.<br/>" +
  169. "Since 5.5, 'debt' mode is deprecated and replaced by 'effort'")
  170. .setPossibleValues(FACET_MODE_COUNT, FACET_MODE_EFFORT, DEPRECATED_FACET_MODE_DEBT);
  171. action.addSortParams(IssueQuery.SORTS, null, true);
  172. action.createParam(PARAM_ADDITIONAL_FIELDS)
  173. .setSince("5.2")
  174. .setDescription("Comma-separated list of the optional fields to be returned in response. Action plans are dropped in 5.5, it is not returned in the response.")
  175. .setPossibleValues(SearchAdditionalField.possibleValues());
  176. addComponentRelatedParams(action);
  177. action.createParam(PARAM_ISSUES)
  178. .setDescription("Comma-separated list of issue keys")
  179. .setExampleValue("5bccd6e8-f525-43a2-8d76-fcb13dde79ef");
  180. action.createParam(PARAM_SEVERITIES)
  181. .setDescription("Comma-separated list of severities")
  182. .setExampleValue(Severity.BLOCKER + "," + Severity.CRITICAL)
  183. .setPossibleValues(Severity.ALL);
  184. action.createParam(PARAM_STATUSES)
  185. .setDescription("Comma-separated list of statuses")
  186. .setExampleValue(Issue.STATUS_OPEN + "," + Issue.STATUS_REOPENED)
  187. .setPossibleValues(Issue.STATUSES);
  188. action.createParam(PARAM_RESOLUTIONS)
  189. .setDescription("Comma-separated list of resolutions")
  190. .setExampleValue(Issue.RESOLUTION_FIXED + "," + Issue.RESOLUTION_REMOVED)
  191. .setPossibleValues(Issue.RESOLUTIONS);
  192. action.createParam(PARAM_RESOLVED)
  193. .setDescription("To match resolved or unresolved issues")
  194. .setBooleanPossibleValues();
  195. action.createParam(PARAM_RULES)
  196. .setDescription("Comma-separated list of coding rule keys. Format is &lt;repository&gt;:&lt;rule&gt;")
  197. .setExampleValue("squid:AvoidCycles");
  198. action.createParam(PARAM_TAGS)
  199. .setDescription("Comma-separated list of tags.")
  200. .setExampleValue("security,convention");
  201. action.createParam(PARAM_TYPES)
  202. .setDescription("Comma-separated list of types.")
  203. .setSince("5.5")
  204. .setPossibleValues((Object[]) RuleType.values())
  205. .setExampleValue(format("%s,%s", RuleType.CODE_SMELL, RuleType.BUG));
  206. action.createParam(PARAM_AUTHORS)
  207. .setDescription("Comma-separated list of SCM accounts")
  208. .setExampleValue("torvalds@linux-foundation.org");
  209. action.createParam(PARAM_ASSIGNEES)
  210. .setDescription("Comma-separated list of assignee logins. The value '__me__' can be used as a placeholder for user who performs the request")
  211. .setExampleValue("admin,usera,__me__");
  212. action.createParam(PARAM_ASSIGNED)
  213. .setDescription("To retrieve assigned or unassigned issues")
  214. .setBooleanPossibleValues();
  215. action.createParam(PARAM_LANGUAGES)
  216. .setDescription("Comma-separated list of languages. Available since 4.4")
  217. .setExampleValue("java,js");
  218. action.createParam(PARAM_CREATED_AT)
  219. .setDescription("Datetime to retrieve issues created during a specific analysis")
  220. .setExampleValue("2017-10-19T13:00:00+0200");
  221. action.createParam(PARAM_CREATED_AFTER)
  222. .setDescription("To retrieve issues created after the given date (inclusive). <br>" +
  223. "Either a date (server timezone) or datetime can be provided. <br>" +
  224. "If this parameter is set, createdSince must not be set")
  225. .setExampleValue("2017-10-19 or 2017-10-19T13:00:00+0200");
  226. action.createParam(PARAM_CREATED_BEFORE)
  227. .setDescription("To retrieve issues created before the given date (inclusive). <br>" +
  228. "Either a date (server timezone) or datetime can be provided.")
  229. .setExampleValue("2017-10-19 or 2017-10-19T13:00:00+0200");
  230. action.createParam(PARAM_CREATED_IN_LAST)
  231. .setDescription("To retrieve issues created during a time span before the current time (exclusive). " +
  232. "Accepted units are 'y' for year, 'm' for month, 'w' for week and 'd' for day. " +
  233. "If this parameter is set, createdAfter must not be set")
  234. .setExampleValue("1m2w (1 month 2 weeks)");
  235. action.createParam(PARAM_SINCE_LEAK_PERIOD)
  236. .setDescription("To retrieve issues created since the leak period.<br>" +
  237. "If this parameter is set to a truthy value, createdAfter must not be set and one component id or key must be provided.")
  238. .setBooleanPossibleValues()
  239. .setDefaultValue("false");
  240. }
  241. private static void addComponentRelatedParams(WebService.NewAction action) {
  242. action.createParam(PARAM_ON_COMPONENT_ONLY)
  243. .setDescription("Return only issues at a component's level, not on its descendants (modules, directories, files, etc). " +
  244. "This parameter is only considered when componentKeys or componentUuids is set. " +
  245. "Using the deprecated componentRoots or componentRootUuids parameters will set this parameter to false. " +
  246. "Using the deprecated components parameter will set this parameter to true.")
  247. .setBooleanPossibleValues()
  248. .setDefaultValue("false");
  249. action.createParam(PARAM_COMPONENT_KEYS)
  250. .setDescription("Comma-separated list of component keys. Retrieve issues associated to a specific list of components (and all its descendants). " +
  251. "A component can be a portfolio, project, module, directory or file.")
  252. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  253. action.createParam(PARAM_COMPONENTS)
  254. .setDeprecatedSince("5.1")
  255. .setDescription("If used, will have the same meaning as componentKeys AND onComponentOnly=true.");
  256. action.createParam(PARAM_COMPONENT_UUIDS)
  257. .setDescription("To retrieve issues associated to a specific list of components their sub-components (comma-separated list of component IDs). " +
  258. INTERNAL_PARAMETER_DISCLAIMER +
  259. "A component can be a project, module, directory or file.")
  260. .setDeprecatedSince("6.5")
  261. .setExampleValue("584a89f2-8037-4f7b-b82c-8b45d2d63fb2");
  262. action.createParam(PARAM_COMPONENT_ROOTS)
  263. .setDeprecatedSince("5.1")
  264. .setDescription("If used, will have the same meaning as componentKeys AND onComponentOnly=false.");
  265. action.createParam(PARAM_COMPONENT_ROOT_UUIDS)
  266. .setDeprecatedSince("5.1")
  267. .setDescription("If used, will have the same meaning as componentUuids AND onComponentOnly=false.");
  268. action.createParam(PARAM_PROJECTS)
  269. .setDescription("To retrieve issues associated to a specific list of projects (comma-separated list of project keys). " +
  270. INTERNAL_PARAMETER_DISCLAIMER +
  271. "If this parameter is set, projectUuids must not be set.")
  272. .setDeprecatedKey(PARAM_PROJECT_KEYS, "6.5")
  273. .setInternal(true)
  274. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  275. action.createParam(PARAM_PROJECT_UUIDS)
  276. .setDescription("To retrieve issues associated to a specific list of projects (comma-separated list of project IDs). " +
  277. INTERNAL_PARAMETER_DISCLAIMER +
  278. "Portfolios are not supported. If this parameter is set, '%s' must not be set.", PARAM_PROJECTS)
  279. .setInternal(true)
  280. .setExampleValue("7d8749e8-3070-4903-9188-bdd82933bb92");
  281. action.createParam(PARAM_MODULE_UUIDS)
  282. .setDescription("To retrieve issues associated to a specific list of modules (comma-separated list of module IDs). " +
  283. INTERNAL_PARAMETER_DISCLAIMER)
  284. .setInternal(true)
  285. .setExampleValue("7d8749e8-3070-4903-9188-bdd82933bb92");
  286. action.createParam(PARAM_DIRECTORIES)
  287. .setDescription("To retrieve issues associated to a specific list of directories (comma-separated list of directory paths). " +
  288. "This parameter is only meaningful when a module is selected. " +
  289. INTERNAL_PARAMETER_DISCLAIMER)
  290. .setInternal(true)
  291. .setSince("5.1")
  292. .setExampleValue("src/main/java/org/sonar/server/");
  293. action.createParam(PARAM_FILE_UUIDS)
  294. .setDescription("To retrieve issues associated to a specific list of files (comma-separated list of file IDs). " +
  295. INTERNAL_PARAMETER_DISCLAIMER)
  296. .setInternal(true)
  297. .setExampleValue("bdd82933-3070-4903-9188-7d8749e8bb92");
  298. action.createParam(PARAM_BRANCH)
  299. .setDescription("Branch key")
  300. .setExampleValue(KEY_BRANCH_EXAMPLE_001)
  301. .setInternal(true)
  302. .setSince("6.6");
  303. action.createParam(PARAM_PULL_REQUEST)
  304. .setDescription("Pull request id")
  305. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001)
  306. .setInternal(true)
  307. .setSince("7.1");
  308. action.createParam(PARAM_ORGANIZATION)
  309. .setDescription("Organization key")
  310. .setRequired(false)
  311. .setInternal(true)
  312. .setExampleValue("my-org")
  313. .setSince("6.4");
  314. }
  315. @Override
  316. public final void handle(Request request, Response response) {
  317. SearchWsResponse searchWsResponse = doHandle(toSearchWsRequest(request), request);
  318. writeProtobuf(searchWsResponse, request, response);
  319. }
  320. private SearchWsResponse doHandle(SearchRequest request, Request wsRequest) {
  321. // prepare the Elasticsearch request
  322. SearchOptions options = createSearchOptionsFromRequest(request);
  323. EnumSet<SearchAdditionalField> additionalFields = SearchAdditionalField.getFromRequest(request);
  324. IssueQuery query = issueQueryFactory.create(request);
  325. // execute request
  326. SearchResponse result = issueIndex.search(query, options);
  327. List<String> issueKeys = Arrays.stream(result.getHits().getHits())
  328. .map(SearchHit::getId)
  329. .collect(MoreCollectors.toList(result.getHits().getHits().length));
  330. // load the additional information to be returned in response
  331. SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(additionalFields, issueKeys);
  332. collectLoggedInUser(collector);
  333. collectRequestParams(collector, request);
  334. Facets facets = null;
  335. if (!options.getFacets().isEmpty()) {
  336. facets = new Facets(result, system2.getDefaultTimeZone());
  337. // add missing values to facets. For example if assignee "john" and facet on "assignees" are requested, then
  338. // "john" should always be listed in the facet. If it is not present, then it is added with value zero.
  339. // This is a constraint from webapp UX.
  340. completeFacets(facets, request, wsRequest);
  341. collectFacets(collector, facets);
  342. Set<String> facetsRequiringProjectOrOrganizationParameter = facets.getNames().stream()
  343. .filter(FACETS_REQUIRING_PROJECT_OR_ORGANIZATION::contains)
  344. .collect(toSet());
  345. checkArgument(facetsRequiringProjectOrOrganizationParameter.isEmpty() ||
  346. (!query.projectUuids().isEmpty()) || query.organizationUuid() != null, "Facet(s) '%s' require to also filter by project or organization",
  347. COMA_JOINER.join(facetsRequiringProjectOrOrganizationParameter));
  348. }
  349. SearchResponseData preloadedData = new SearchResponseData(emptyList());
  350. preloadedData.setRules(ImmutableList.copyOf(query.rules()));
  351. SearchResponseData data = searchResponseLoader.load(preloadedData, collector, facets);
  352. // format response
  353. // Filter and reorder facets according to the requested ordered names.
  354. // Must be done after loading of data as the "hidden" facet "debt"
  355. // can be used to get total debt.
  356. facets = reorderFacets(facets, options.getFacets());
  357. replaceRuleIdsByRuleKeys(facets, data.getRules() == null ? emptyList() : data.getRules());
  358. // FIXME allow long in Paging
  359. Paging paging = forPageIndex(options.getPage()).withPageSize(options.getLimit()).andTotal((int) result.getHits().getTotalHits());
  360. return searchResponseFormat.formatSearch(additionalFields, data, paging, facets);
  361. }
  362. private void replaceRuleIdsByRuleKeys(@Nullable Facets facets, List<RuleDefinitionDto> alreadyLoadedRules) {
  363. if (facets == null) {
  364. return;
  365. }
  366. LinkedHashMap<String, Long> rulesFacet = facets.get(PARAM_RULES);
  367. if (rulesFacet == null) {
  368. return;
  369. }
  370. // The facet for PARAM_RULES contains the id of the rule as the key
  371. // We need to update the key to be a RuleKey
  372. try (DbSession dbSession = dbClient.openSession(false)) {
  373. Set<Integer> ruleIdsToLoad = new HashSet<>();
  374. rulesFacet.keySet().forEach(s -> {
  375. try {
  376. ruleIdsToLoad.add(Integer.parseInt(s));
  377. } catch (NumberFormatException e) {
  378. // ignore, this is already a key
  379. }
  380. });
  381. ruleIdsToLoad.removeAll(
  382. alreadyLoadedRules
  383. .stream()
  384. .map(RuleDefinitionDto::getId)
  385. .collect(Collectors.toList()));
  386. List<RuleDefinitionDto> ruleDefinitions = Stream.concat(
  387. alreadyLoadedRules.stream(),
  388. dbClient.ruleDao().selectDefinitionByIds(dbSession, ruleIdsToLoad).stream())
  389. .collect(MoreCollectors.toList());
  390. Map<Integer, RuleKey> ruleKeyById = ruleDefinitions.stream()
  391. .collect(Collectors.toMap(RuleDefinitionDto::getId, RuleDefinitionDto::getKey));
  392. Map<String, Integer> idByRuleKeyAsString = ruleDefinitions.stream()
  393. .collect(Collectors.toMap(s -> s.getKey().toString(), RuleDefinitionDto::getId));
  394. LinkedHashMap<String, Long> newRulesFacet = new LinkedHashMap<>();
  395. rulesFacet.forEach((k, v) -> {
  396. try {
  397. int ruleId = Integer.parseInt(k);
  398. RuleKey ruleKey = ruleKeyById.get(ruleId);
  399. if (ruleKey != null) {
  400. newRulesFacet.put(ruleKey.toString(), v);
  401. } else {
  402. // RuleKey not found ES/DB incorrect?
  403. LOGGER.error("Rule with id {} is not available in database", k);
  404. }
  405. } catch (NumberFormatException e) {
  406. // RuleKey are added into the facet from the HTTP request, there may be a result for this rule from the
  407. // ES search (with the ruleId as a key). If so, do not add this entry again (anyway, value for ruleKey is
  408. // always 0 since it is added SearchAction#completeFacets).
  409. String ruleId = String.valueOf(idByRuleKeyAsString.get(k));
  410. if (!rulesFacet.containsKey(ruleId)) {
  411. newRulesFacet.put(k, v);
  412. }
  413. }
  414. });
  415. rulesFacet.clear();
  416. rulesFacet.putAll(newRulesFacet);
  417. }
  418. }
  419. private static SearchOptions createSearchOptionsFromRequest(SearchRequest request) {
  420. SearchOptions options = new SearchOptions();
  421. options.setPage(request.getPage(), request.getPageSize());
  422. options.addFacets(request.getFacets());
  423. return options;
  424. }
  425. private Facets reorderFacets(@Nullable Facets facets, Collection<String> orderedNames) {
  426. if (facets == null) {
  427. return null;
  428. }
  429. LinkedHashMap<String, LinkedHashMap<String, Long>> orderedFacets = new LinkedHashMap<>();
  430. for (String facetName : orderedNames) {
  431. LinkedHashMap<String, Long> facet = facets.get(facetName);
  432. if (facet != null) {
  433. orderedFacets.put(facetName, facet);
  434. }
  435. }
  436. return new Facets(orderedFacets, system2.getDefaultTimeZone());
  437. }
  438. private void completeFacets(Facets facets, SearchRequest request, Request wsRequest) {
  439. addMandatoryValuesToFacet(facets, PARAM_SEVERITIES, Severity.ALL);
  440. addMandatoryValuesToFacet(facets, PARAM_STATUSES, Issue.STATUSES);
  441. addMandatoryValuesToFacet(facets, PARAM_RESOLUTIONS, concat(singletonList(""), Issue.RESOLUTIONS));
  442. addMandatoryValuesToFacet(facets, PARAM_PROJECT_UUIDS, request.getProjectUuids());
  443. List<String> assignees = Lists.newArrayList("");
  444. List<String> assigneesFromRequest = request.getAssignees();
  445. if (assigneesFromRequest != null) {
  446. assignees.addAll(assigneesFromRequest);
  447. assignees.remove(IssueQueryFactory.LOGIN_MYSELF);
  448. }
  449. addMandatoryValuesToFacet(facets, PARAM_ASSIGNEES, assignees);
  450. addMandatoryValuesToFacet(facets, FACET_ASSIGNED_TO_ME, singletonList(userSession.getLogin()));
  451. addMandatoryValuesToFacet(facets, PARAM_RULES, request.getRules());
  452. addMandatoryValuesToFacet(facets, PARAM_LANGUAGES, request.getLanguages());
  453. addMandatoryValuesToFacet(facets, PARAM_TAGS, request.getTags());
  454. addMandatoryValuesToFacet(facets, PARAM_TYPES, RuleType.names());
  455. addMandatoryValuesToFacet(facets, PARAM_COMPONENT_UUIDS, request.getComponentUuids());
  456. List<String> requestedFacets = request.getFacets();
  457. if (requestedFacets == null) {
  458. return;
  459. }
  460. requestedFacets.stream()
  461. .filter(facetName -> !FACET_ASSIGNED_TO_ME.equals(facetName))
  462. .filter(facetName -> !IGNORED_FACETS.contains(facetName))
  463. .forEach(facetName -> {
  464. LinkedHashMap<String, Long> buckets = facets.get(facetName);
  465. List<String> requestParams = wsRequest.paramAsStrings(facetName);
  466. if (buckets == null || requestParams == null) {
  467. return;
  468. }
  469. requestParams.stream()
  470. .filter(param -> !buckets.containsKey(param) && !IssueQueryFactory.LOGIN_MYSELF.equals(param))
  471. // Prevent appearance of a glitch value due to dedicated parameter for this facet
  472. .forEach(param -> buckets.put(param, 0L));
  473. });
  474. }
  475. private static void addMandatoryValuesToFacet(Facets facets, String facetName, @Nullable Iterable<String> mandatoryValues) {
  476. Map<String, Long> buckets = facets.get(facetName);
  477. if (buckets != null && mandatoryValues != null) {
  478. for (String mandatoryValue : mandatoryValues) {
  479. if (!buckets.containsKey(mandatoryValue)) {
  480. buckets.put(mandatoryValue, 0L);
  481. }
  482. }
  483. }
  484. }
  485. private void collectLoggedInUser(SearchResponseLoader.Collector collector) {
  486. if (userSession.isLoggedIn()) {
  487. collector.add(SearchAdditionalField.USERS, userSession.getLogin());
  488. }
  489. }
  490. private static void collectFacets(SearchResponseLoader.Collector collector, Facets facets) {
  491. Set<String> facetRules = facets.getBucketKeys(PARAM_RULES);
  492. if (facetRules != null) {
  493. collector.addAll(SearchAdditionalField.RULE_IDS_AND_KEYS, facetRules);
  494. }
  495. collector.addProjectUuids(facets.getBucketKeys(PARAM_PROJECT_UUIDS));
  496. collector.addComponentUuids(facets.getBucketKeys(PARAM_COMPONENT_UUIDS));
  497. collector.addComponentUuids(facets.getBucketKeys(PARAM_FILE_UUIDS));
  498. collector.addComponentUuids(facets.getBucketKeys(PARAM_MODULE_UUIDS));
  499. collector.addAll(SearchAdditionalField.USERS, facets.getBucketKeys(PARAM_ASSIGNEES));
  500. }
  501. private static void collectRequestParams(SearchResponseLoader.Collector collector, SearchRequest request) {
  502. collector.addProjectUuids(request.getProjectUuids());
  503. collector.addComponentUuids(request.getFileUuids());
  504. collector.addComponentUuids(request.getModuleUuids());
  505. collector.addComponentUuids(request.getComponentRootUuids());
  506. collector.addAll(SearchAdditionalField.USERS, request.getAssignees());
  507. }
  508. private static SearchRequest toSearchWsRequest(Request request) {
  509. return new SearchRequest()
  510. .setAdditionalFields(request.paramAsStrings(PARAM_ADDITIONAL_FIELDS))
  511. .setAsc(request.paramAsBoolean(PARAM_ASC))
  512. .setAssigned(request.paramAsBoolean(PARAM_ASSIGNED))
  513. .setAssignees(request.paramAsStrings(PARAM_ASSIGNEES))
  514. .setAuthors(request.paramAsStrings(PARAM_AUTHORS))
  515. .setComponentKeys(request.paramAsStrings(PARAM_COMPONENT_KEYS))
  516. .setComponentRootUuids(request.paramAsStrings(PARAM_COMPONENT_ROOT_UUIDS))
  517. .setComponentRoots(request.paramAsStrings(PARAM_COMPONENT_ROOTS))
  518. .setComponentUuids(request.paramAsStrings(PARAM_COMPONENT_UUIDS))
  519. .setComponents(request.paramAsStrings(PARAM_COMPONENTS))
  520. .setCreatedAfter(request.param(PARAM_CREATED_AFTER))
  521. .setCreatedAt(request.param(PARAM_CREATED_AT))
  522. .setCreatedBefore(request.param(PARAM_CREATED_BEFORE))
  523. .setCreatedInLast(request.param(PARAM_CREATED_IN_LAST))
  524. .setDirectories(request.paramAsStrings(PARAM_DIRECTORIES))
  525. .setFacetMode(request.mandatoryParam(FACET_MODE))
  526. .setFacets(request.paramAsStrings(Param.FACETS))
  527. .setFileUuids(request.paramAsStrings(PARAM_FILE_UUIDS))
  528. .setIssues(request.paramAsStrings(PARAM_ISSUES))
  529. .setLanguages(request.paramAsStrings(PARAM_LANGUAGES))
  530. .setModuleUuids(request.paramAsStrings(PARAM_MODULE_UUIDS))
  531. .setOnComponentOnly(request.paramAsBoolean(PARAM_ON_COMPONENT_ONLY))
  532. .setBranch(request.param(PARAM_BRANCH))
  533. .setPullRequest(request.param(PARAM_PULL_REQUEST))
  534. .setOrganization(request.param(PARAM_ORGANIZATION))
  535. .setPage(request.mandatoryParamAsInt(Param.PAGE))
  536. .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
  537. .setProjectKeys(request.paramAsStrings(PARAM_PROJECTS))
  538. .setProjectUuids(request.paramAsStrings(PARAM_PROJECT_UUIDS))
  539. .setProjects(request.paramAsStrings(PARAM_PROJECTS))
  540. .setResolutions(request.paramAsStrings(PARAM_RESOLUTIONS))
  541. .setResolved(request.paramAsBoolean(PARAM_RESOLVED))
  542. .setRules(request.paramAsStrings(PARAM_RULES))
  543. .setSinceLeakPeriod(request.mandatoryParamAsBoolean(PARAM_SINCE_LEAK_PERIOD))
  544. .setSort(request.param(Param.SORT))
  545. .setSeverities(request.paramAsStrings(PARAM_SEVERITIES))
  546. .setStatuses(request.paramAsStrings(PARAM_STATUSES))
  547. .setTags(request.paramAsStrings(PARAM_TAGS))
  548. .setTypes(request.paramAsStrings(PARAM_TYPES));
  549. }
  550. }