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.

CreateAction.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.measure.custom.ws;
  21. import org.sonar.api.resources.Scopes;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.api.utils.System2;
  26. import org.sonar.api.utils.text.JsonWriter;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.db.measure.custom.CustomMeasureDto;
  31. import org.sonar.db.metric.MetricDto;
  32. import org.sonar.db.user.UserDto;
  33. import org.sonar.server.component.ComponentFinder;
  34. import org.sonar.server.user.UserSession;
  35. import static com.google.common.base.Preconditions.checkArgument;
  36. import static com.google.common.base.Preconditions.checkState;
  37. import static java.util.Objects.requireNonNull;
  38. import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY;
  39. import static org.sonar.server.measure.custom.ws.CustomMeasureValidator.checkPermissions;
  40. import static org.sonar.server.measure.custom.ws.CustomMeasureValueDescription.measureValueDescription;
  41. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  42. import static org.sonar.server.ws.WsUtils.checkRequest;
  43. public class CreateAction implements CustomMeasuresWsAction {
  44. public static final String ACTION = "create";
  45. public static final String PARAM_PROJECT_ID = "projectId";
  46. public static final String PARAM_PROJECT_KEY = "projectKey";
  47. public static final String PARAM_METRIC_ID = "metricId";
  48. public static final String PARAM_METRIC_KEY = "metricKey";
  49. public static final String PARAM_VALUE = "value";
  50. public static final String PARAM_DESCRIPTION = "description";
  51. private final DbClient dbClient;
  52. private final UserSession userSession;
  53. private final System2 system;
  54. private final CustomMeasureValidator validator;
  55. private final CustomMeasureJsonWriter customMeasureJsonWriter;
  56. private final ComponentFinder componentFinder;
  57. public CreateAction(DbClient dbClient, UserSession userSession, System2 system, CustomMeasureValidator validator, CustomMeasureJsonWriter customMeasureJsonWriter,
  58. ComponentFinder componentFinder) {
  59. this.dbClient = dbClient;
  60. this.userSession = userSession;
  61. this.system = system;
  62. this.validator = validator;
  63. this.customMeasureJsonWriter = customMeasureJsonWriter;
  64. this.componentFinder = componentFinder;
  65. }
  66. @Override
  67. public void define(WebService.NewController context) {
  68. WebService.NewAction action = context.createAction(ACTION)
  69. .setDescription("Create a custom measure.<br /> " +
  70. "The project id or the project key must be provided (only project and module custom measures can be created). The metric id or the metric key must be provided.<br/>" +
  71. "Requires 'Administer' permission on the project.")
  72. .setSince("5.2")
  73. .setDeprecatedSince("7.4")
  74. .setPost(true)
  75. .setHandler(this);
  76. action.createParam(PARAM_PROJECT_ID)
  77. .setDescription("Project id")
  78. .setExampleValue("ce4c03d6-430f-40a9-b777-ad877c00aa4d");
  79. action.createParam(PARAM_PROJECT_KEY)
  80. .setDescription("Project key")
  81. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  82. action.createParam(PARAM_METRIC_ID)
  83. .setDescription("Metric id")
  84. .setExampleValue("16");
  85. action.createParam(PARAM_METRIC_KEY)
  86. .setDescription("Metric key")
  87. .setExampleValue("ncloc");
  88. action.createParam(PARAM_VALUE)
  89. .setRequired(true)
  90. .setDescription(measureValueDescription())
  91. .setExampleValue("47");
  92. action.createParam(PARAM_DESCRIPTION)
  93. .setDescription("Description")
  94. .setExampleValue("Team size growing.");
  95. }
  96. @Override
  97. public void handle(Request request, Response response) throws Exception {
  98. String valueAsString = request.mandatoryParam(PARAM_VALUE);
  99. String description = request.param(PARAM_DESCRIPTION);
  100. long now = system.now();
  101. try (DbSession dbSession = dbClient.openSession(false)) {
  102. ComponentDto component = componentFinder.getByUuidOrKey(dbSession, request.param(PARAM_PROJECT_ID), request.param(PARAM_PROJECT_KEY), PROJECT_ID_AND_KEY);
  103. MetricDto metric = searchMetric(dbSession, request);
  104. checkPermissions(userSession, component);
  105. checkIsProjectOrModule(component);
  106. checkMeasureDoesNotExistAlready(dbSession, component, metric);
  107. String userUuid = requireNonNull(userSession.getUuid(), "User uuid should not be null");
  108. UserDto user = dbClient.userDao().selectByUuid(dbSession, userUuid);
  109. checkState(user != null, "User with uuid '%s' does not exist", userUuid);
  110. CustomMeasureDto measure = new CustomMeasureDto()
  111. .setComponentUuid(component.uuid())
  112. .setMetricId(metric.getId())
  113. .setDescription(description)
  114. .setUserUuid(user.getUuid())
  115. .setCreatedAt(now)
  116. .setUpdatedAt(now);
  117. validator.setMeasureValue(measure, valueAsString, metric);
  118. dbClient.customMeasureDao().insert(dbSession, measure);
  119. dbSession.commit();
  120. JsonWriter json = response.newJsonWriter();
  121. customMeasureJsonWriter.write(json, measure, metric, component, user, true, CustomMeasureJsonWriter.OPTIONAL_FIELDS);
  122. json.close();
  123. }
  124. }
  125. private static void checkIsProjectOrModule(ComponentDto component) {
  126. checkRequest(Scopes.PROJECT.equals(component.scope()), "Component '%s' must be a project or a module.", component.getDbKey());
  127. }
  128. private void checkMeasureDoesNotExistAlready(DbSession dbSession, ComponentDto component, MetricDto metric) {
  129. int nbMeasuresOnSameMetricAndMeasure = dbClient.customMeasureDao().countByComponentIdAndMetricId(dbSession, component.uuid(), metric.getId());
  130. checkRequest(nbMeasuresOnSameMetricAndMeasure == 0,
  131. "A measure already exists for project '%s' and metric '%s'",
  132. component.getDbKey(), metric.getKey());
  133. }
  134. private MetricDto searchMetric(DbSession dbSession, Request request) {
  135. Integer metricId = request.paramAsInt(PARAM_METRIC_ID);
  136. String metricKey = request.param(PARAM_METRIC_KEY);
  137. checkArgument(metricId != null ^ metricKey != null, "Either the metric id or the metric key must be provided");
  138. if (metricId == null) {
  139. MetricDto metric = dbClient.metricDao().selectByKey(dbSession, metricKey);
  140. checkArgument(metric != null, "Metric with key '%s' does not exist", metricKey);
  141. return metric;
  142. }
  143. MetricDto metric = dbClient.metricDao().selectById(dbSession, metricId);
  144. checkArgument(metric != null, "Metric with id '%s' does not exist", metricId);
  145. return metric;
  146. }
  147. }