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.

ComponentAction.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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.ImmutableSortedSet;
  22. import com.google.common.collect.Maps;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.HashSet;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Optional;
  29. import java.util.Set;
  30. import java.util.stream.Collectors;
  31. import javax.annotation.CheckForNull;
  32. import javax.annotation.Nullable;
  33. import org.sonar.api.resources.Qualifiers;
  34. import org.sonar.api.server.ws.Change;
  35. import org.sonar.api.server.ws.Request;
  36. import org.sonar.api.server.ws.Response;
  37. import org.sonar.api.server.ws.WebService;
  38. import org.sonar.api.web.UserRole;
  39. import org.sonar.db.DbClient;
  40. import org.sonar.db.DbSession;
  41. import org.sonar.db.component.BranchDto;
  42. import org.sonar.db.component.ComponentDto;
  43. import org.sonar.db.component.SnapshotDto;
  44. import org.sonar.db.measure.LiveMeasureDto;
  45. import org.sonar.db.metric.MetricDto;
  46. import org.sonar.db.metric.MetricDtoFunctions;
  47. import org.sonar.server.component.ComponentFinder;
  48. import org.sonar.server.exceptions.NotFoundException;
  49. import org.sonar.server.user.UserSession;
  50. import org.sonarqube.ws.Measures;
  51. import org.sonarqube.ws.Measures.ComponentWsResponse;
  52. import static java.lang.String.format;
  53. import static java.util.Collections.emptyMap;
  54. import static java.util.Collections.singletonList;
  55. import static java.util.Collections.singletonMap;
  56. import static org.sonar.db.metric.RemovedMetricConverter.withRemovedMetricAlias;
  57. import static org.sonar.server.component.ws.MeasuresWsParameters.ACTION_COMPONENT;
  58. import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_METRICS;
  59. import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIOD;
  60. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS;
  61. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH;
  62. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT;
  63. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS;
  64. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST;
  65. import static org.sonar.server.exceptions.BadRequestException.checkRequest;
  66. import static org.sonar.server.measure.ws.ComponentDtoToWsComponent.componentDtoToWsComponent;
  67. import static org.sonar.server.measure.ws.ComponentResponseCommon.addMetricToResponseIncludingRenamedMetric;
  68. import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createAdditionalFieldsParameter;
  69. import static org.sonar.server.measure.ws.MeasuresWsParametersBuilder.createMetricKeysParameter;
  70. import static org.sonar.server.measure.ws.SnapshotDtoToWsPeriod.snapshotToWsPeriods;
  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. public class ComponentAction implements MeasuresWsAction {
  76. private static final Set<String> QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE = ImmutableSortedSet.of(Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE);
  77. private final DbClient dbClient;
  78. private final ComponentFinder componentFinder;
  79. private final UserSession userSession;
  80. public ComponentAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession) {
  81. this.dbClient = dbClient;
  82. this.componentFinder = componentFinder;
  83. this.userSession = userSession;
  84. }
  85. @Override
  86. public void define(WebService.NewController context) {
  87. WebService.NewAction action = context.createAction(ACTION_COMPONENT)
  88. .setDescription("Return component with specified measures.<br>" +
  89. "Requires the following permission: 'Browse' on the project of specified component.")
  90. .setResponseExample(getClass().getResource("component-example.json"))
  91. .setSince("5.4")
  92. .setChangelog(
  93. new Change("10.5", String.format("The metrics %s are now deprecated " +
  94. "without exact replacement. Use 'maintainability_issues', 'reliability_issues' and 'security_issues' instead.",
  95. MeasuresWsModule.getDeprecatedMetricsInSonarQube105())),
  96. new Change("10.5", "Added new accepted values for the 'metricKeys' param: 'new_maintainability_issues', 'new_reliability_issues', 'new_security_issues'"),
  97. new Change("10.4", String.format("The metrics %s are now deprecated " +
  98. "without exact replacement. Use 'maintainability_issues', 'reliability_issues' and 'security_issues' instead.",
  99. MeasuresWsModule.getDeprecatedMetricsInSonarQube104())),
  100. new Change("10.4", "Added new accepted values for the 'metricKeys' param: 'maintainability_issues', 'reliability_issues', 'security_issues'"),
  101. new Change("10.4", "The metrics 'open_issues', 'reopened_issues' and 'confirmed_issues' are now deprecated in the response. Consume 'violations' instead."),
  102. new Change("10.4", "The use of 'open_issues', 'reopened_issues' and 'confirmed_issues' values in 'metricKeys' param are now deprecated. Use 'violations' instead."),
  103. new Change("10.4", "The metric 'wont_fix_issues' is now deprecated in the response. Consume 'accepted_issues' instead."),
  104. new Change("10.4", "The use of 'wont_fix_issues' value in 'metricKeys' param is now deprecated. Use 'accepted_issues' instead."),
  105. new Change("10.4", "Added new accepted value for the 'metricKeys' param: 'accepted_issues'."),
  106. new Change("10.1", String.format("The use of module keys in parameter '%s' is removed", PARAM_COMPONENT)),
  107. new Change("10.0", format("The use of the following metrics in 'metricKeys' parameter is not deprecated anymore: %s",
  108. MeasuresWsModule.getDeprecatedMetricsInSonarQube93())),
  109. new Change("10.0", "the response field periods under measures field is removed."),
  110. new Change("10.0", "the option `periods` of 'additionalFields' request field is removed."),
  111. new Change("9.3", "When the new code period is set to 'reference branch', the response field 'date' under the 'period' field has been removed"),
  112. new Change("9.3", format("The use of the following metrics in 'metricKeys' parameter is deprecated: %s",
  113. MeasuresWsModule.getDeprecatedMetricsInSonarQube93())),
  114. new Change("8.8", "deprecated response field 'id' has been removed"),
  115. new Change("8.8", "deprecated response field 'refId' has been removed."),
  116. new Change("8.1", "the response field periods under measures field is deprecated. Use period instead."),
  117. new Change("8.1", "the response field periods is deprecated. Use period instead."),
  118. new Change("7.6", format("The use of module keys in parameter '%s' is deprecated", PARAM_COMPONENT)),
  119. new Change("6.6", "the response field 'id' is deprecated. Use 'key' instead."),
  120. new Change("6.6", "the response field 'refId' is deprecated. Use 'refKey' instead."))
  121. .setHandler(this);
  122. action.createParam(PARAM_COMPONENT)
  123. .setDescription("Component key")
  124. .setRequired(true)
  125. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  126. action.createParam(PARAM_BRANCH)
  127. .setDescription("Branch key. Not available in the community edition.")
  128. .setExampleValue(KEY_BRANCH_EXAMPLE_001)
  129. .setSince("6.6");
  130. action.createParam(PARAM_PULL_REQUEST)
  131. .setDescription("Pull request id. Not available in the community edition.")
  132. .setExampleValue(KEY_PULL_REQUEST_EXAMPLE_001)
  133. .setSince("7.1");
  134. createMetricKeysParameter(action);
  135. createAdditionalFieldsParameter(action);
  136. }
  137. @Override
  138. public void handle(Request request, Response response) throws Exception {
  139. ComponentWsResponse componentWsResponse = doHandle(toComponentWsRequest(request));
  140. writeProtobuf(componentWsResponse, request, response);
  141. }
  142. private ComponentWsResponse doHandle(ComponentRequest request) {
  143. try (DbSession dbSession = dbClient.openSession(false)) {
  144. String branch = request.getBranch();
  145. String pullRequest = request.getPullRequest();
  146. ComponentDto component = loadComponent(dbSession, request, branch, pullRequest);
  147. checkPermissions(component);
  148. SnapshotDto analysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, component.branchUuid()).orElse(null);
  149. List<MetricDto> metrics = searchMetrics(dbSession, new HashSet<>(withRemovedMetricAlias(request.getMetricKeys())));
  150. List<LiveMeasureDto> measures = searchMeasures(dbSession, component, metrics);
  151. Map<MetricDto, LiveMeasureDto> measuresByMetric = getMeasuresByMetric(measures, metrics);
  152. Measures.Period period = snapshotToWsPeriods(analysis).orElse(null);
  153. return buildResponse(dbSession, request, component, measuresByMetric, metrics, period, request.getMetricKeys());
  154. }
  155. }
  156. public List<MetricDto> searchMetrics(DbSession dbSession, Set<String> metricKeys) {
  157. List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
  158. if (metrics.size() < metricKeys.size()) {
  159. Set<String> foundMetricKeys = metrics.stream().map(MetricDto::getKey).collect(Collectors.toSet());
  160. Set<String> missingMetricKeys = metricKeys.stream().filter(m -> !foundMetricKeys.contains(m)).collect(Collectors.toSet());
  161. throw new NotFoundException(format("The following metric keys are not found: %s", String.join(", ", missingMetricKeys)));
  162. }
  163. return metrics;
  164. }
  165. private List<LiveMeasureDto> searchMeasures(DbSession dbSession, ComponentDto component, Collection<MetricDto> metrics) {
  166. Set<String> metricUuids = metrics.stream().map(MetricDto::getUuid).collect(Collectors.toSet());
  167. List<LiveMeasureDto> measures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricUuids(dbSession, singletonList(component.uuid()), metricUuids);
  168. addBestValuesToMeasures(measures, component, metrics);
  169. return measures;
  170. }
  171. private static Map<MetricDto, LiveMeasureDto> getMeasuresByMetric(List<LiveMeasureDto> measures, Collection<MetricDto> metrics) {
  172. Map<String, MetricDto> metricsByUuid = Maps.uniqueIndex(metrics, MetricDto::getUuid);
  173. Map<MetricDto, LiveMeasureDto> measuresByMetric = new HashMap<>();
  174. for (LiveMeasureDto measure : measures) {
  175. MetricDto metric = metricsByUuid.get(measure.getMetricUuid());
  176. measuresByMetric.put(metric, measure);
  177. }
  178. return measuresByMetric;
  179. }
  180. /**
  181. * Conditions for best value measure:
  182. * <ul>
  183. * <li>component is a production file or test file</li>
  184. * <li>metric is optimized for best value</li>
  185. * </ul>
  186. */
  187. private static void addBestValuesToMeasures(List<LiveMeasureDto> measures, ComponentDto component, Collection<MetricDto> metrics) {
  188. if (!QUALIFIERS_ELIGIBLE_FOR_BEST_VALUE.contains(component.qualifier())) {
  189. return;
  190. }
  191. List<MetricDtoWithBestValue> metricWithBestValueList = metrics.stream()
  192. .filter(MetricDtoFunctions.isOptimizedForBestValue())
  193. .map(MetricDtoWithBestValue::new)
  194. .toList();
  195. Map<String, LiveMeasureDto> measuresByMetricUuid = Maps.uniqueIndex(measures, LiveMeasureDto::getMetricUuid);
  196. for (MetricDtoWithBestValue metricWithBestValue : metricWithBestValueList) {
  197. if (measuresByMetricUuid.get(metricWithBestValue.getMetric().getUuid()) == null) {
  198. measures.add(metricWithBestValue.getBestValue());
  199. }
  200. }
  201. }
  202. private ComponentDto loadComponent(DbSession dbSession, ComponentRequest request, @Nullable String branch, @Nullable String pullRequest) {
  203. String componentKey = request.getComponent();
  204. checkRequest(componentKey != null, "The '%s' parameter is missing", PARAM_COMPONENT);
  205. return componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, componentKey, branch, pullRequest);
  206. }
  207. private Optional<RefComponent> getReference(DbSession dbSession, ComponentDto component) {
  208. String copyComponentUuid = component.getCopyComponentUuid();
  209. if (copyComponentUuid == null) {
  210. return Optional.empty();
  211. }
  212. Optional<ComponentDto> refComponent = dbClient.componentDao().selectByUuid(dbSession, copyComponentUuid);
  213. if (refComponent.isEmpty()) {
  214. return Optional.empty();
  215. }
  216. Optional<BranchDto> refBranch = dbClient.branchDao().selectByUuid(dbSession, refComponent.get().branchUuid());
  217. return refBranch.map(rb -> new RefComponent(rb, refComponent.get()));
  218. }
  219. private ComponentWsResponse buildResponse(DbSession dbSession, ComponentRequest request, ComponentDto component,
  220. Map<MetricDto, LiveMeasureDto> measuresByMetric, Collection<MetricDto> metrics, @Nullable Measures.Period period,
  221. Collection<String> requestedMetrics) {
  222. ComponentWsResponse.Builder response = ComponentWsResponse.newBuilder();
  223. RefComponent reference = getReference(dbSession, component).orElse(null);
  224. if (reference != null) {
  225. BranchDto refBranch = reference.getRefBranch();
  226. ComponentDto refComponent = reference.getComponent();
  227. response.setComponent(componentDtoToWsComponent(component, measuresByMetric, singletonMap(refComponent.uuid(), refComponent),
  228. refBranch.isMain() ? null : refBranch.getBranchKey(), null, requestedMetrics));
  229. } else {
  230. boolean isMainBranch = dbClient.branchDao().selectByUuid(dbSession, component.branchUuid()).map(BranchDto::isMain).orElse(true);
  231. response.setComponent(componentDtoToWsComponent(component, measuresByMetric, emptyMap(), isMainBranch ? null : request.getBranch(),
  232. request.getPullRequest(), requestedMetrics));
  233. }
  234. setAdditionalFields(request, metrics, period, response, requestedMetrics);
  235. return response.build();
  236. }
  237. private static void setAdditionalFields(ComponentRequest request, Collection<MetricDto> metrics, @Nullable Measures.Period period,
  238. ComponentWsResponse.Builder response, Collection<String> requestedMetrics) {
  239. List<String> additionalFields = request.getAdditionalFields();
  240. if (additionalFields != null) {
  241. if (additionalFields.contains(ADDITIONAL_METRICS)) {
  242. for (MetricDto metricDto : metrics) {
  243. addMetricToResponseIncludingRenamedMetric(metric -> response.getMetricsBuilder().addMetrics(metric), requestedMetrics, metricDto);
  244. }
  245. }
  246. if (additionalFields.contains(ADDITIONAL_PERIOD) && period != null) {
  247. response.setPeriod(period);
  248. }
  249. }
  250. }
  251. private static ComponentRequest toComponentWsRequest(Request request) {
  252. ComponentRequest componentRequest = new ComponentRequest()
  253. .setComponent(request.mandatoryParam(PARAM_COMPONENT))
  254. .setBranch(request.param(PARAM_BRANCH))
  255. .setPullRequest(request.param(PARAM_PULL_REQUEST))
  256. .setAdditionalFields(request.paramAsStrings(PARAM_ADDITIONAL_FIELDS))
  257. .setMetricKeys(request.mandatoryParamAsStrings(PARAM_METRIC_KEYS));
  258. checkRequest(!componentRequest.getMetricKeys().isEmpty(), "At least one metric key must be provided");
  259. return componentRequest;
  260. }
  261. private void checkPermissions(ComponentDto baseComponent) {
  262. userSession.checkComponentPermission(UserRole.USER, baseComponent);
  263. }
  264. private static class ComponentRequest {
  265. private String component = null;
  266. private String branch = null;
  267. private String pullRequest = null;
  268. private List<String> metricKeys = null;
  269. private List<String> additionalFields = null;
  270. private String getComponent() {
  271. return component;
  272. }
  273. private ComponentRequest setComponent(@Nullable String component) {
  274. this.component = component;
  275. return this;
  276. }
  277. @CheckForNull
  278. private String getBranch() {
  279. return branch;
  280. }
  281. private ComponentRequest setBranch(@Nullable String branch) {
  282. this.branch = branch;
  283. return this;
  284. }
  285. @CheckForNull
  286. public String getPullRequest() {
  287. return pullRequest;
  288. }
  289. public ComponentRequest setPullRequest(@Nullable String pullRequest) {
  290. this.pullRequest = pullRequest;
  291. return this;
  292. }
  293. private List<String> getMetricKeys() {
  294. return metricKeys;
  295. }
  296. private ComponentRequest setMetricKeys(@Nullable List<String> metricKeys) {
  297. this.metricKeys = metricKeys;
  298. return this;
  299. }
  300. @CheckForNull
  301. private List<String> getAdditionalFields() {
  302. return additionalFields;
  303. }
  304. private ComponentRequest setAdditionalFields(@Nullable List<String> additionalFields) {
  305. this.additionalFields = additionalFields;
  306. return this;
  307. }
  308. }
  309. private static class RefComponent {
  310. private final BranchDto refBranch;
  311. private final ComponentDto component;
  312. public RefComponent(BranchDto refBranch, ComponentDto component) {
  313. this.refBranch = refBranch;
  314. this.component = component;
  315. }
  316. public BranchDto getRefBranch() {
  317. return refBranch;
  318. }
  319. public ComponentDto getComponent() {
  320. return component;
  321. }
  322. }
  323. }