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.

ComponentTreeAction.java 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.measure.ws;
  21. import com.google.common.collect.FluentIterable;
  22. import com.google.common.collect.HashBasedTable;
  23. import com.google.common.collect.ImmutableSortedSet;
  24. import com.google.common.collect.Lists;
  25. import com.google.common.collect.Maps;
  26. import com.google.common.collect.Sets;
  27. import com.google.common.collect.Table;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.Collections;
  31. import java.util.HashSet;
  32. import java.util.LinkedHashSet;
  33. import java.util.List;
  34. import java.util.Map;
  35. import java.util.Objects;
  36. import java.util.Optional;
  37. import java.util.Set;
  38. import java.util.function.Function;
  39. import java.util.function.Predicate;
  40. import java.util.stream.Collectors;
  41. import java.util.stream.Stream;
  42. import javax.annotation.CheckForNull;
  43. import javax.annotation.Nonnull;
  44. import javax.annotation.Nullable;
  45. import org.sonar.api.resources.Qualifiers;
  46. import org.sonar.api.resources.ResourceTypes;
  47. import org.sonar.api.resources.Scopes;
  48. import org.sonar.api.server.ws.Change;
  49. import org.sonar.api.server.ws.Request;
  50. import org.sonar.api.server.ws.Response;
  51. import org.sonar.api.server.ws.WebService;
  52. import org.sonar.api.server.ws.WebService.Param;
  53. import org.sonar.api.utils.Paging;
  54. import org.sonar.api.web.UserRole;
  55. import org.sonar.core.i18n.I18n;
  56. import org.sonar.db.DbClient;
  57. import org.sonar.db.DbSession;
  58. import org.sonar.db.component.BranchDto;
  59. import org.sonar.db.component.ComponentDto;
  60. import org.sonar.db.component.ComponentTreeQuery;
  61. import org.sonar.db.component.ComponentTreeQuery.Strategy;
  62. import org.sonar.db.component.SnapshotDto;
  63. import org.sonar.db.measure.LiveMeasureDto;
  64. import org.sonar.db.measure.MeasureTreeQuery;
  65. import org.sonar.db.metric.MetricDto;
  66. import org.sonar.db.metric.MetricDtoFunctions;
  67. import org.sonar.server.component.ComponentFinder;
  68. import org.sonar.server.exceptions.NotFoundException;
  69. import org.sonar.server.user.UserSession;
  70. import org.sonarqube.ws.Measures;
  71. import org.sonarqube.ws.Measures.ComponentTreeWsResponse;
  72. import org.sonarqube.ws.client.component.ComponentsWsParameters;
  73. import static com.google.common.base.Preconditions.checkArgument;
  74. import static com.google.common.base.Preconditions.checkState;
  75. import static java.lang.String.format;
  76. import static java.util.Collections.emptyList;
  77. import static java.util.Collections.emptyMap;
  78. import static java.util.Optional.ofNullable;
  79. import static org.sonar.api.measures.Metric.ValueType.DATA;
  80. import static org.sonar.api.measures.Metric.ValueType.DISTRIB;
  81. import static org.sonar.api.utils.Paging.offset;
  82. import static org.sonar.db.component.ComponentTreeQuery.Strategy.CHILDREN;
  83. import static org.sonar.db.component.ComponentTreeQuery.Strategy.LEAVES;
  84. import static org.sonar.db.metric.RemovedMetricConverter.includeRenamedMetrics;
  85. import static org.sonar.db.metric.RemovedMetricConverter.withRemovedMetricAlias;
  86. import static org.sonar.server.component.ws.MeasuresWsParameters.ACTION_COMPONENT_TREE;
  87. import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_METRICS;
  88. import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIOD;
  89. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS;
  90. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH;
  91. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT;
  92. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS;
  93. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_PERIOD_SORT;
  94. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT;
  95. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT_FILTER;
  96. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST;
  97. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_QUALIFIERS;
  98. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_STRATEGY;
  99. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  100. import static org.sonar.server.measure.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
  101. import static org.sonar.server.measure.ws.ComponentResponseCommon.addMeasureIncludingRenamedMetric;
  102. import static org.sonar.server.measure.ws.ComponentResponseCommon.addMetricToResponseIncludingRenamedMetric;
  103. import static org.sonar.server.measure.ws.MeasureDtoToWsMeasure.updateMeasureBuilder;
  104. import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createAdditionalFieldsParameter;
  105. import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createMetricKeysParameter;
  106. import static org.sonar.server.measure.ws.SnapshotDtoToWsPeriod.snapshotToWsPeriods;
  107. import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
  108. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  109. import static org.sonar.server.ws.KeyExamples.KEY_PULL_REQUEST_EXAMPLE_001;
  110. import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
  111. import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
  112. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  113. /**
  114. * <p>Navigate through components based on different strategy with specified measures.
  115. * To limit the number of rows in database, a best value algorithm exists in database.</p>
  116. * A measure is not stored in database if:
  117. * <ul>
  118. * <li>the component is a file (production or test)</li>
  119. * <li>optimization algorithm is enabled on the metric</li>
  120. * <li>the measure computed equals the metric best value</li>
  121. * <li>the period values are all equal to 0</li>
  122. * </ul>
  123. * To recreate a best value 2 different cases:
  124. * <ul>
  125. * <li>Metric starts with 'new_' (ex: new_violations): the best value measure doesn't have a value and period values are all equal to 0</li>
  126. * <li>Other metrics: the best value measure has a value of 0 and no period value</li>
  127. * </ul>
  128. */
  129. public class ComponentTreeAction implements MeasuresWsAction {
  130. private static final int MAX_SIZE = 500;
  131. private static final int QUERY_MINIMUM_LENGTH = 3;
  132. // tree exploration strategies
  133. static final String ALL_STRATEGY = "all";
  134. static final String CHILDREN_STRATEGY = "children";
  135. static final String LEAVES_STRATEGY = "leaves";
  136. static final Map<String, Strategy> STRATEGIES = Map.of(
  137. ALL_STRATEGY, LEAVES,
  138. CHILDREN_STRATEGY, CHILDREN,
  139. LEAVES_STRATEGY, LEAVES);
  140. // sort
  141. static final String NAME_SORT = "name";
  142. static final String PATH_SORT = "path";
  143. static final String QUALIFIER_SORT = "qualifier";
  144. static final String METRIC_SORT = "metric";
  145. static final String METRIC_PERIOD_SORT = "metricPeriod";
  146. static final Set<String> SORTS = ImmutableSortedSet.of(NAME_SORT, PATH_SORT, QUALIFIER_SORT, METRIC_SORT, METRIC_PERIOD_SORT);
  147. static final String ALL_METRIC_SORT_FILTER = "all";
  148. static final String WITH_MEASURES_ONLY_METRIC_SORT_FILTER = "withMeasuresOnly";
  149. static final Set<String> METRIC_SORT_FILTERS = ImmutableSortedSet.of(ALL_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER);
  150. private static final int MAX_METRIC_KEYS = 15;
  151. private static final String COMMA_JOIN_SEPARATOR = ", ";
  152. private static final Set<String> QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE = Set.of(Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE);
  153. private final DbClient dbClient;
  154. private final ComponentFinder componentFinder;
  155. private final UserSession userSession;
  156. private final I18n i18n;
  157. private final ResourceTypes resourceTypes;
  158. public ComponentTreeAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, I18n i18n,
  159. ResourceTypes resourceTypes) {
  160. this.dbClient = dbClient;
  161. this.componentFinder = componentFinder;
  162. this.userSession = userSession;
  163. this.i18n = i18n;
  164. this.resourceTypes = resourceTypes;
  165. }
  166. @Override
  167. public void define(WebService.NewController context) {
  168. WebService.NewAction action = context.createAction(ACTION_COMPONENT_TREE)
  169. .setDescription(format("Navigate through components based on the chosen strategy with specified measures.<br>" +
  170. "Requires the following permission: 'Browse' on the specified project.<br>" +
  171. "For applications, it also requires 'Browse' permission on its child projects. <br>" +
  172. "When limiting search with the %s parameter, directories are not returned.", Param.TEXT_QUERY))
  173. .setResponseExample(getClass().getResource("component_tree-example.json"))
  174. .setSince("5.4")
  175. .setHandler(this)
  176. .addPagingParams(100, MAX_SIZE)
  177. .setChangelog(
  178. new Change("10.5", "Added new accepted values for the 'metricKeys' param: 'new_maintainability_issues', 'new_reliability_issues', 'new_security_issues'"),
  179. new Change("10.5", String.format("The metrics %s are now deprecated " +
  180. "without exact replacement. Use 'maintainability_issues', 'reliability_issues' and 'security_issues' instead.",
  181. MeasuresWsModule.getDeprecatedMetricsInSonarQube105())),
  182. new Change("10.5", "Added new accepted values for the 'metricKeys' param: 'maintainability_issues', 'reliability_issues', 'security_issues'"),
  183. new Change("10.4", String.format("The metrics %s are now deprecated " +
  184. "without exact replacement. Use 'maintainability_issues', 'reliability_issues' and 'security_issues' instead.",
  185. MeasuresWsModule.getDeprecatedMetricsInSonarQube104())),
  186. new Change("10.4", "The metrics 'open_issues', 'reopened_issues' and 'confirmed_issues' are now deprecated in the response. Consume 'violations' instead."),
  187. new Change("10.4", "The use of 'open_issues', 'reopened_issues' and 'confirmed_issues' values in 'metricKeys' param are now deprecated. Use 'violations' instead."),
  188. new Change("10.4", "The metric 'wont_fix_issues' is now deprecated in the response. Consume 'accepted_issues' instead."),
  189. new Change("10.4", "The use of 'wont_fix_issues' value in 'metricKeys' and 'metricSort' params is now deprecated. Use 'accepted_issues' instead."),
  190. new Change("10.4", "Added new accepted value for the 'metricKeys' and 'metricSort' param: 'accepted_issues'."),
  191. new Change("10.1", String.format("The use of 'BRC' as value for parameter '%s' is removed", ComponentsWsParameters.PARAM_QUALIFIERS)),
  192. new Change("10.0", format("The use of the following metrics in 'metricKeys' parameter is not deprecated anymore: %s",
  193. MeasuresWsModule.getDeprecatedMetricsInSonarQube93())),
  194. new Change("10.0", "the response field periods under measures field is removed."),
  195. new Change("10.0", "the option `periods` of 'additionalFields' request field is removed."),
  196. new Change("9.3", format("The use of the following metrics in 'metricKeys' parameter is deprecated: %s",
  197. MeasuresWsModule.getDeprecatedMetricsInSonarQube93())),
  198. new Change("8.8", "parameter 'component' is now required"),
  199. new Change("8.8", "deprecated parameter 'baseComponentId' has been removed"),
  200. new Change("8.8", "deprecated parameter 'baseComponentKey' has been removed."),
  201. new Change("8.8", "deprecated response field 'id' has been removed"),
  202. new Change("8.8", "deprecated response field 'refId' has been removed."),
  203. new Change("8.1", "the response field periods under measures field is deprecated. Use period instead."),
  204. new Change("8.1", "the response field periods is deprecated. Use period instead."),
  205. new Change("7.6", format("The use of module keys in parameter '%s' is deprecated", PARAM_COMPONENT)),
  206. new Change("7.2", "field 'bestValue' is added to the response"),
  207. new Change("6.3", format("Number of metric keys is limited to %s", MAX_METRIC_KEYS)),
  208. new Change("6.6", "the response field 'id' is deprecated. Use 'key' instead."),
  209. new Change("6.6", "the response field 'refId' is deprecated. Use 'refKey' instead."));
  210. action.createSortParams(SORTS, NAME_SORT, true)
  211. .setDescription("Comma-separated list of sort fields")
  212. .setExampleValue(NAME_SORT + "," + PATH_SORT);
  213. action.createParam(Param.TEXT_QUERY)
  214. .setDescription("Limit search to: <ul>" +
  215. "<li>component names that contain the supplied string</li>" +
  216. "<li>component keys that are exactly the same as the supplied string</li>" +
  217. "</ul>")
  218. .setMinimumLength(QUERY_MINIMUM_LENGTH)
  219. .setExampleValue("FILE_NAM");
  220. action.createParam(PARAM_COMPONENT)
  221. .setRequired(true)
  222. .setDescription("Component key. The search is based on this component.")
  223. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  224. action.createParam(PARAM_BRANCH)
  225. .setDescription("Branch key. Not available in the community edition.")
  226. .setExampleValue(KEY_BRANCH_EXAMPLE_001)
  227. .setSince("6.6");
  228. action.createParam(PARAM_PULL_REQUEST)
  229. .setDescription("Pull request id. Not available in the community edition.")
  230. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001)
  231. .setSince("7.1");
  232. action.createParam(PARAM_METRIC_SORT)
  233. .setDescription(
  234. format("Metric key to sort by. The '%s' parameter must contain the '%s' or '%s' value. It must be part of the '%s' parameter", Param.SORT, METRIC_SORT, METRIC_PERIOD_SORT,
  235. PARAM_METRIC_KEYS))
  236. .setExampleValue("ncloc");
  237. action.createParam(PARAM_METRIC_PERIOD_SORT)
  238. .setDescription(format("Sort measures by leak period or not ?. The '%s' parameter must contain the '%s' value.", Param.SORT, METRIC_PERIOD_SORT))
  239. .setSince("5.5")
  240. .setPossibleValues(1);
  241. action.createParam(PARAM_METRIC_SORT_FILTER)
  242. .setDescription(format("Filter components. Sort must be on a metric. Possible values are: " +
  243. "<ul>" +
  244. "<li>%s: return all components</li>" +
  245. "<li>%s: filter out components that do not have a measure on the sorted metric</li>" +
  246. "</ul>", ALL_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER))
  247. .setDefaultValue(ALL_METRIC_SORT_FILTER)
  248. .setPossibleValues(METRIC_SORT_FILTERS);
  249. createMetricKeysParameter(action)
  250. .setDescription("Comma-separated list of metric keys. Types %s are not allowed. For type %s only %s metrics are supported",
  251. String.join(COMMA_JOIN_SEPARATOR, UnsupportedMetrics.FORBIDDEN_METRIC_TYPES),
  252. DATA.name(),
  253. String.join(COMMA_JOIN_SEPARATOR, UnsupportedMetrics.PARTIALLY_SUPPORTED_METRICS.get(DATA.name())))
  254. .setMaxValuesAllowed(MAX_METRIC_KEYS);
  255. createAdditionalFieldsParameter(action);
  256. createQualifiersParameter(action, newQualifierParameterContext(i18n, resourceTypes));
  257. action.createParam(PARAM_STRATEGY)
  258. .setDescription("Strategy to search for base component descendants:" +
  259. "<ul>" +
  260. "<li>children: return the children components of the base component. Grandchildren components are not returned</li>" +
  261. "<li>all: return all the descendants components of the base component. Grandchildren are returned.</li>" +
  262. "<li>leaves: return all the descendant components (files, in general) which don't have other children. They are the leaves of the component tree.</li>" +
  263. "</ul>")
  264. .setPossibleValues(STRATEGIES.keySet())
  265. .setDefaultValue(ALL_STRATEGY);
  266. }
  267. @Override
  268. public void handle(Request request, Response response) throws Exception {
  269. ComponentTreeWsResponse componentTreeWsResponse = doHandle(toComponentTreeWsRequest(request));
  270. writeProtobuf(componentTreeWsResponse, request, response);
  271. }
  272. private ComponentTreeWsResponse doHandle(ComponentTreeRequest request) {
  273. ComponentTreeData data = load(request);
  274. if (data.getComponents() == null) {
  275. return emptyResponse(data.getBaseComponent(), data.getBranch(), request);
  276. }
  277. return buildResponse(
  278. request,
  279. data,
  280. Paging.forPageIndex(
  281. request.getPage())
  282. .withPageSize(request.getPageSize())
  283. .andTotal(data.getComponentCount()),
  284. request.getMetricKeys());
  285. }
  286. private static ComponentTreeWsResponse buildResponse(ComponentTreeRequest request, ComponentTreeData data, Paging paging,
  287. List<String> requestedMetrics) {
  288. ComponentTreeWsResponse.Builder response = ComponentTreeWsResponse.newBuilder();
  289. response.getPagingBuilder()
  290. .setPageIndex(paging.pageIndex())
  291. .setPageSize(paging.pageSize())
  292. .setTotal(paging.total())
  293. .build();
  294. boolean isMainBranch = data.getBranch() == null || data.getBranch().isMain();
  295. response.setBaseComponent(
  296. toWsComponent(
  297. data.getBaseComponent(),
  298. data.getMeasuresByComponentUuidAndMetric().row(data.getBaseComponent().uuid()),
  299. data.getReferenceComponentsByUuid(), isMainBranch ? null : request.getBranch(), request.getPullRequest(), requestedMetrics));
  300. for (ComponentDto componentDto : data.getComponents()) {
  301. if (componentDto.getCopyComponentUuid() != null) {
  302. String refBranch = data.getBranchByReferenceUuid().get(componentDto.getCopyComponentUuid());
  303. response.addComponents(toWsComponent(
  304. componentDto,
  305. data.getMeasuresByComponentUuidAndMetric().row(componentDto.uuid()),
  306. data.getReferenceComponentsByUuid(), refBranch, null, requestedMetrics));
  307. } else {
  308. response.addComponents(toWsComponent(
  309. componentDto,
  310. data.getMeasuresByComponentUuidAndMetric().row(componentDto.uuid()),
  311. data.getReferenceComponentsByUuid(), isMainBranch ? null : request.getBranch(), request.getPullRequest(), requestedMetrics));
  312. }
  313. }
  314. if (areMetricsInResponse(request)) {
  315. for (MetricDto metricDto : data.getMetrics()) {
  316. addMetricToResponseIncludingRenamedMetric(metric -> response.getMetricsBuilder().addMetrics(metric), requestedMetrics, metricDto);
  317. }
  318. }
  319. List<String> additionalFields = ofNullable(request.getAdditionalFields()).orElse(Collections.emptyList());
  320. if (additionalFields.contains(ADDITIONAL_PERIOD) && data.getPeriod() != null) {
  321. response.setPeriod(data.getPeriod());
  322. }
  323. return response.build();
  324. }
  325. private static boolean areMetricsInResponse(ComponentTreeRequest request) {
  326. List<String> additionalFields = request.getAdditionalFields();
  327. return additionalFields != null && additionalFields.contains(ADDITIONAL_METRICS);
  328. }
  329. private static ComponentTreeWsResponse emptyResponse(@Nullable ComponentDto baseComponent, @Nullable BranchDto branch, ComponentTreeRequest request) {
  330. ComponentTreeWsResponse.Builder response = ComponentTreeWsResponse.newBuilder();
  331. response.getPagingBuilder()
  332. .setPageIndex(request.getPage())
  333. .setPageSize(request.getPageSize())
  334. .setTotal(0);
  335. if (baseComponent != null) {
  336. boolean isMainBranch = branch == null || branch.isMain();
  337. response.setBaseComponent(componentDtoToWsComponent(baseComponent, isMainBranch ? null : request.getBranch(), request.getPullRequest()));
  338. }
  339. return response.build();
  340. }
  341. private static ComponentTreeRequest toComponentTreeWsRequest(Request request) {
  342. List<String> metricKeys = request.mandatoryParamAsStrings(PARAM_METRIC_KEYS);
  343. checkArgument(metricKeys.size() <= MAX_METRIC_KEYS, "Number of metrics keys is limited to %s, got %s", MAX_METRIC_KEYS, metricKeys.size());
  344. ComponentTreeRequest componentTreeRequest = new ComponentTreeRequest()
  345. .setComponent(request.mandatoryParam(PARAM_COMPONENT))
  346. .setBranch(request.param(PARAM_BRANCH))
  347. .setPullRequest(request.param(PARAM_PULL_REQUEST))
  348. .setMetricKeys(metricKeys)
  349. .setStrategy(request.mandatoryParam(PARAM_STRATEGY))
  350. .setQualifiers(request.paramAsStrings(PARAM_QUALIFIERS))
  351. .setAdditionalFields(request.paramAsStrings(PARAM_ADDITIONAL_FIELDS))
  352. .setSort(request.paramAsStrings(Param.SORT))
  353. .setAsc(request.paramAsBoolean(Param.ASCENDING))
  354. .setMetricSort(includeRenamedMetrics(request.param(PARAM_METRIC_SORT)))
  355. .setMetricSortFilter(request.mandatoryParam(PARAM_METRIC_SORT_FILTER))
  356. .setMetricPeriodSort(request.paramAsInt(PARAM_METRIC_PERIOD_SORT))
  357. .setPage(request.mandatoryParamAsInt(Param.PAGE))
  358. .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
  359. .setQuery(request.param(Param.TEXT_QUERY));
  360. String metricSortValue = componentTreeRequest.getMetricSort();
  361. checkRequest(!componentTreeRequest.getMetricKeys().isEmpty(), "The '%s' parameter must contain at least one metric key", PARAM_METRIC_KEYS);
  362. List<String> sorts = ofNullable(componentTreeRequest.getSort()).orElse(emptyList());
  363. checkRequest(metricSortValue == null ^ sorts.contains(METRIC_SORT) ^ sorts.contains(METRIC_PERIOD_SORT),
  364. "To sort by a metric, the '%s' parameter must contain '%s' or '%s', and a metric key must be provided in the '%s' parameter",
  365. Param.SORT, METRIC_SORT, METRIC_PERIOD_SORT, PARAM_METRIC_SORT);
  366. checkRequest(metricSortValue == null ^ componentTreeRequest.getMetricKeys().contains(metricSortValue),
  367. "To sort by the '%s' metric, it must be in the list of metric keys in the '%s' parameter", metricSortValue, PARAM_METRIC_KEYS);
  368. checkRequest(componentTreeRequest.getMetricPeriodSort() == null ^ sorts.contains(METRIC_PERIOD_SORT),
  369. "To sort by a metric period, the '%s' parameter must contain '%s' and the '%s' must be provided.", Param.SORT, METRIC_PERIOD_SORT, PARAM_METRIC_PERIOD_SORT);
  370. checkRequest(ALL_METRIC_SORT_FILTER.equals(componentTreeRequest.getMetricSortFilter()) || metricSortValue != null,
  371. "To filter components based on the sort metric, the '%s' parameter must contain '%s' or '%s' and the '%s' parameter must be provided",
  372. Param.SORT, METRIC_SORT, METRIC_PERIOD_SORT, PARAM_METRIC_SORT);
  373. return componentTreeRequest;
  374. }
  375. private static Measures.Component.Builder toWsComponent(ComponentDto component, Map<MetricDto, ComponentTreeData.Measure> measures,
  376. Map<String, ComponentDto> referenceComponentsByUuid, @Nullable String branch, @Nullable String pullRequest, List<String> requestedMetrics) {
  377. Measures.Component.Builder wsComponent = componentDtoToWsComponent(component, branch, pullRequest);
  378. ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyComponentUuid());
  379. if (referenceComponent != null) {
  380. wsComponent.setRefKey(referenceComponent.getKey());
  381. String displayQualifier = getDisplayQualifier(component, referenceComponent);
  382. wsComponent.setQualifier(displayQualifier);
  383. }
  384. Measures.Measure.Builder measureBuilder = Measures.Measure.newBuilder();
  385. for (Map.Entry<MetricDto, ComponentTreeData.Measure> entry : measures.entrySet()) {
  386. ComponentTreeData.Measure measure = entry.getValue();
  387. boolean onNewCode = entry.getKey().getKey().startsWith("new_");
  388. updateMeasureBuilder(measureBuilder, entry.getKey(), measure.getValue(), measure.getData(), onNewCode);
  389. addMeasureIncludingRenamedMetric(requestedMetrics, wsComponent, measureBuilder);
  390. measureBuilder.clear();
  391. }
  392. return wsComponent;
  393. }
  394. // https://jira.sonarsource.com/browse/SONAR-13703 - for apps that were added as a local reference to a portfolio, we want to
  395. // show them as apps, not sub-portfolios
  396. private static String getDisplayQualifier(ComponentDto component, ComponentDto referenceComponent) {
  397. String qualifier = component.qualifier();
  398. if (qualifier.equals(Qualifiers.SUBVIEW) && referenceComponent.qualifier().equals(Qualifiers.APP)) {
  399. return Qualifiers.APP;
  400. }
  401. return qualifier;
  402. }
  403. private ComponentTreeData load(ComponentTreeRequest wsRequest) {
  404. try (DbSession dbSession = dbClient.openSession(false)) {
  405. ComponentDto baseComponent = loadComponent(dbSession, wsRequest);
  406. checkPermissions(baseComponent);
  407. // portfolios don't have branches
  408. BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, baseComponent.branchUuid()).orElse(null);
  409. Optional<SnapshotDto> baseSnapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, baseComponent.branchUuid());
  410. if (baseSnapshot.isEmpty()) {
  411. return ComponentTreeData.builder()
  412. .setBranch(branchDto)
  413. .setBaseComponent(baseComponent)
  414. .build();
  415. }
  416. ComponentTreeQuery componentTreeQuery = toComponentTreeQuery(wsRequest, baseComponent);
  417. List<ComponentDto> components = searchComponents(dbSession, componentTreeQuery);
  418. List<MetricDto> metrics = searchMetrics(dbSession, new HashSet<>(withRemovedMetricAlias(ofNullable(wsRequest.getMetricKeys()).orElse(List.of()))));
  419. Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric = searchMeasuresByComponentUuidAndMetric(dbSession, baseComponent, componentTreeQuery,
  420. components, metrics);
  421. components = filterComponents(components, measuresByComponentUuidAndMetric, metrics, wsRequest);
  422. components = filterAuthorizedComponents(components);
  423. components = sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric);
  424. int componentCount = components.size();
  425. components = paginateComponents(components, wsRequest);
  426. Map<String, ComponentDto> referencesByUuid = searchReferenceComponentsById(dbSession, components);
  427. Map<String, String> branchByReferenceUuid = searchReferenceBranchKeys(dbSession, referencesByUuid.keySet());
  428. return ComponentTreeData.builder()
  429. .setBaseComponent(baseComponent)
  430. .setBranch(branchDto)
  431. .setComponentsFromDb(components)
  432. .setComponentCount(componentCount)
  433. .setBranchByReferenceUuid(branchByReferenceUuid)
  434. .setMeasuresByComponentUuidAndMetric(measuresByComponentUuidAndMetric)
  435. .setMetrics(metrics)
  436. .setPeriod(snapshotToWsPeriods(baseSnapshot.get()).orElse(null))
  437. .setReferenceComponentsByUuid(referencesByUuid)
  438. .build();
  439. }
  440. }
  441. private Map<String, String> searchReferenceBranchKeys(DbSession dbSession, Set<String> referenceUuids) {
  442. return dbClient.branchDao().selectByUuids(dbSession, referenceUuids).stream()
  443. .filter(b -> !b.isMain())
  444. .collect(Collectors.toMap(BranchDto::getUuid, BranchDto::getBranchKey));
  445. }
  446. private ComponentDto loadComponent(DbSession dbSession, ComponentTreeRequest request) {
  447. String componentKey = request.getComponent();
  448. String branch = request.getBranch();
  449. String pullRequest = request.getPullRequest();
  450. return componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, componentKey, branch, pullRequest);
  451. }
  452. private Map<String, ComponentDto> searchReferenceComponentsById(DbSession dbSession, List<ComponentDto> components) {
  453. List<String> referenceComponentUUids = components.stream()
  454. .map(ComponentDto::getCopyComponentUuid)
  455. .filter(Objects::nonNull)
  456. .toList();
  457. if (referenceComponentUUids.isEmpty()) {
  458. return emptyMap();
  459. }
  460. return FluentIterable.from(dbClient.componentDao().selectByUuids(dbSession, referenceComponentUUids))
  461. .uniqueIndex(ComponentDto::uuid);
  462. }
  463. private List<ComponentDto> searchComponents(DbSession dbSession, ComponentTreeQuery componentTreeQuery) {
  464. Collection<String> qualifiers = componentTreeQuery.getQualifiers();
  465. if (qualifiers != null && qualifiers.isEmpty()) {
  466. return Collections.emptyList();
  467. }
  468. return dbClient.componentDao().selectDescendants(dbSession, componentTreeQuery);
  469. }
  470. private List<MetricDto> searchMetrics(DbSession dbSession, Set<String> metricKeys) {
  471. List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
  472. if (metrics.size() < metricKeys.size()) {
  473. List<String> foundMetricKeys = Lists.transform(metrics, MetricDto::getKey);
  474. Set<String> missingMetricKeys = Sets.difference(
  475. new LinkedHashSet<>(metricKeys),
  476. new LinkedHashSet<>(foundMetricKeys));
  477. throw new NotFoundException(format("The following metric keys are not found: %s", String.join(COMMA_JOIN_SEPARATOR, missingMetricKeys)));
  478. }
  479. String forbiddenMetrics = metrics.stream()
  480. .filter(UnsupportedMetrics.INSTANCE)
  481. .map(MetricDto::getKey)
  482. .sorted()
  483. .collect(Collectors.joining(COMMA_JOIN_SEPARATOR));
  484. checkArgument(forbiddenMetrics.isEmpty(), "Metrics %s can't be requested in this web service. Please use api/measures/component", forbiddenMetrics);
  485. return metrics;
  486. }
  487. private Table<String, MetricDto, ComponentTreeData.Measure> searchMeasuresByComponentUuidAndMetric(DbSession dbSession, ComponentDto baseComponent,
  488. ComponentTreeQuery componentTreeQuery, List<ComponentDto> components, List<MetricDto> metrics) {
  489. Map<String, MetricDto> metricsByUuid = Maps.uniqueIndex(metrics, MetricDto::getUuid);
  490. MeasureTreeQuery measureQuery = MeasureTreeQuery.builder()
  491. .setStrategy(MeasureTreeQuery.Strategy.valueOf(componentTreeQuery.getStrategy().name()))
  492. .setNameOrKeyQuery(componentTreeQuery.getNameOrKeyQuery())
  493. .setQualifiers(componentTreeQuery.getQualifiers())
  494. .setMetricUuids(new ArrayList<>(metricsByUuid.keySet()))
  495. .build();
  496. Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric = HashBasedTable.create(components.size(), metrics.size());
  497. dbClient.liveMeasureDao().selectTreeByQuery(dbSession, baseComponent, measureQuery, result -> {
  498. LiveMeasureDto measureDto = result.getResultObject();
  499. measuresByComponentUuidAndMetric.put(
  500. measureDto.getComponentUuid(),
  501. metricsByUuid.get(measureDto.getMetricUuid()),
  502. ComponentTreeData.Measure.createFromMeasureDto(measureDto));
  503. });
  504. addBestValuesToMeasures(measuresByComponentUuidAndMetric, components, metrics);
  505. return measuresByComponentUuidAndMetric;
  506. }
  507. /**
  508. * Conditions for best value measure:
  509. * <ul>
  510. * <li>component is a production file or test file</li>
  511. * <li>metric is optimized for best value</li>
  512. * </ul>
  513. */
  514. private static void addBestValuesToMeasures(Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric, List<ComponentDto> components,
  515. List<MetricDto> metrics) {
  516. List<MetricDtoWithBestValue> metricDtosWithBestValueMeasure = metrics.stream()
  517. .filter(MetricDtoFunctions.isOptimizedForBestValue())
  518. .map(new MetricDtoToMetricDtoWithBestValue())
  519. .toList();
  520. if (metricDtosWithBestValueMeasure.isEmpty()) {
  521. return;
  522. }
  523. Stream<ComponentDto> componentsEligibleForBestValue = components.stream().filter(ComponentTreeAction::isFileComponent);
  524. componentsEligibleForBestValue.forEach(component -> {
  525. for (MetricDtoWithBestValue metricWithBestValue : metricDtosWithBestValueMeasure) {
  526. if (measuresByComponentUuidAndMetric.get(component.uuid(), metricWithBestValue.getMetric()) == null) {
  527. measuresByComponentUuidAndMetric.put(component.uuid(), metricWithBestValue.getMetric(),
  528. ComponentTreeData.Measure.createFromMeasureDto(metricWithBestValue.getBestValue()));
  529. }
  530. }
  531. });
  532. }
  533. private static List<ComponentDto> filterComponents(List<ComponentDto> components,
  534. Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric, List<MetricDto> metrics, ComponentTreeRequest wsRequest) {
  535. if (!componentWithMeasuresOnly(wsRequest)) {
  536. return components;
  537. }
  538. String metricKeyToSort = wsRequest.getMetricSort();
  539. Optional<MetricDto> metricToSort = metrics.stream().filter(m -> metricKeyToSort.equals(m.getKey())).findFirst();
  540. checkState(metricToSort.isPresent(), "Metric '%s' not found", metricKeyToSort, wsRequest.getMetricKeys());
  541. return components
  542. .stream()
  543. .filter(new HasMeasure(measuresByComponentUuidAndMetric, metricToSort.get()))
  544. .toList();
  545. }
  546. private List<ComponentDto> filterAuthorizedComponents(List<ComponentDto> components) {
  547. return userSession.keepAuthorizedComponents(UserRole.USER, components);
  548. }
  549. private static boolean componentWithMeasuresOnly(ComponentTreeRequest wsRequest) {
  550. return WITH_MEASURES_ONLY_METRIC_SORT_FILTER.equals(wsRequest.getMetricSortFilter());
  551. }
  552. private static List<ComponentDto> sortComponents(List<ComponentDto> components, ComponentTreeRequest wsRequest, List<MetricDto> metrics,
  553. Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric) {
  554. return ComponentTreeSort.sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric);
  555. }
  556. private static List<ComponentDto> paginateComponents(List<ComponentDto> components, ComponentTreeRequest wsRequest) {
  557. return components.stream()
  558. .skip(offset(wsRequest.getPage(), wsRequest.getPageSize()))
  559. .limit(wsRequest.getPageSize())
  560. .toList();
  561. }
  562. @CheckForNull
  563. private List<String> childrenQualifiers(ComponentTreeRequest request, String baseQualifier) {
  564. List<String> requestQualifiers = request.getQualifiers();
  565. List<String> childrenQualifiers = null;
  566. if (LEAVES_STRATEGY.equals(request.getStrategy())) {
  567. childrenQualifiers = resourceTypes.getLeavesQualifiers(baseQualifier);
  568. }
  569. if (requestQualifiers == null) {
  570. return childrenQualifiers;
  571. }
  572. if (childrenQualifiers == null) {
  573. return requestQualifiers;
  574. }
  575. Sets.SetView<String> qualifiersIntersection = Sets.intersection(new HashSet<>(childrenQualifiers), new HashSet<Object>(requestQualifiers));
  576. return new ArrayList<>(qualifiersIntersection);
  577. }
  578. private ComponentTreeQuery toComponentTreeQuery(ComponentTreeRequest wsRequest, ComponentDto baseComponent) {
  579. List<String> childrenQualifiers = childrenQualifiers(wsRequest, baseComponent.qualifier());
  580. ComponentTreeQuery.Builder componentTreeQueryBuilder = ComponentTreeQuery.builder()
  581. .setBaseUuid(baseComponent.uuid())
  582. .setStrategy(STRATEGIES.get(wsRequest.getStrategy()));
  583. if (wsRequest.getQuery() != null) {
  584. componentTreeQueryBuilder.setNameOrKeyQuery(wsRequest.getQuery());
  585. }
  586. if (childrenQualifiers != null) {
  587. componentTreeQueryBuilder.setQualifiers(childrenQualifiers);
  588. }
  589. return componentTreeQueryBuilder.build();
  590. }
  591. private void checkPermissions(ComponentDto baseComponent) {
  592. userSession.checkComponentPermission(UserRole.USER, baseComponent);
  593. if (Scopes.PROJECT.equals(baseComponent.scope()) && Qualifiers.APP.equals(baseComponent.qualifier())) {
  594. userSession.checkChildProjectsPermission(UserRole.USER, baseComponent);
  595. }
  596. }
  597. public static boolean isFileComponent(@Nonnull ComponentDto input) {
  598. return QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE.contains(input.qualifier());
  599. }
  600. private static class MetricDtoToMetricDtoWithBestValue implements Function<MetricDto, MetricDtoWithBestValue> {
  601. @Override
  602. public MetricDtoWithBestValue apply(@Nonnull MetricDto input) {
  603. return new MetricDtoWithBestValue(input);
  604. }
  605. }
  606. private enum UnsupportedMetrics implements Predicate<MetricDto> {
  607. INSTANCE;
  608. static final Set<String> FORBIDDEN_METRIC_TYPES = Set.of(DISTRIB.name());
  609. static final Map<String, Set<String>> PARTIALLY_SUPPORTED_METRICS = Map.of(
  610. DATA.name(),
  611. DataSupportedMetrics.IMPACTS_SUPPORTED_METRICS);
  612. @Override
  613. public boolean test(@Nonnull MetricDto input) {
  614. if (FORBIDDEN_METRIC_TYPES.contains(input.getValueType())) {
  615. return true;
  616. }
  617. Set<String> partialSupport = PARTIALLY_SUPPORTED_METRICS.get(input.getValueType());
  618. if (partialSupport == null) {
  619. return false;
  620. } else {
  621. return !partialSupport.contains(input.getKey());
  622. }
  623. }
  624. }
  625. }