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.

UpdateAction.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.rule.ws;
  21. import com.google.common.base.Splitter;
  22. import com.google.common.io.Resources;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.sonar.api.rule.RuleKey;
  28. import org.sonar.api.rule.RuleStatus;
  29. import org.sonar.api.rule.Severity;
  30. import org.sonar.api.server.debt.DebtRemediationFunction;
  31. import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
  32. import org.sonar.api.server.ws.Request;
  33. import org.sonar.api.server.ws.Response;
  34. import org.sonar.api.server.ws.WebService;
  35. import org.sonar.api.utils.KeyValueFormat;
  36. import org.sonar.db.DbClient;
  37. import org.sonar.db.DbSession;
  38. import org.sonar.db.organization.OrganizationDto;
  39. import org.sonar.db.rule.RuleDefinitionDto;
  40. import org.sonar.db.rule.RuleDto;
  41. import org.sonar.db.rule.RuleParamDto;
  42. import org.sonar.server.exceptions.NotFoundException;
  43. import org.sonar.server.rule.RuleUpdate;
  44. import org.sonar.server.rule.RuleUpdater;
  45. import org.sonar.server.user.UserSession;
  46. import org.sonarqube.ws.Rules.UpdateResponse;
  47. import static com.google.common.collect.Sets.newHashSet;
  48. import static java.lang.String.format;
  49. import static java.util.Collections.singletonList;
  50. import static java.util.Optional.ofNullable;
  51. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
  52. import static org.sonar.server.rule.ws.CreateAction.KEY_MAXIMUM_LENGTH;
  53. import static org.sonar.server.rule.ws.CreateAction.NAME_MAXIMUM_LENGTH;
  54. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  55. public class UpdateAction implements RulesWsAction {
  56. public static final String PARAM_KEY = "key";
  57. public static final String PARAM_TAGS = "tags";
  58. public static final String PARAM_MARKDOWN_NOTE = "markdown_note";
  59. public static final String PARAM_REMEDIATION_FN_TYPE = "remediation_fn_type";
  60. public static final String PARAM_REMEDIATION_FN_BASE_EFFORT = "remediation_fn_base_effort";
  61. public static final String PARAM_REMEDIATION_FN_GAP_MULTIPLIER = "remediation_fy_gap_multiplier";
  62. public static final String PARAM_NAME = "name";
  63. public static final String PARAM_DESCRIPTION = "markdown_description";
  64. public static final String PARAM_SEVERITY = "severity";
  65. public static final String PARAM_STATUS = "status";
  66. public static final String PARAM_ORGANIZATION = "organization";
  67. public static final String PARAMS = "params";
  68. private final DbClient dbClient;
  69. private final RuleUpdater ruleUpdater;
  70. private final RuleMapper mapper;
  71. private final UserSession userSession;
  72. private final RuleWsSupport ruleWsSupport;
  73. public UpdateAction(DbClient dbClient, RuleUpdater ruleUpdater, RuleMapper mapper, UserSession userSession, RuleWsSupport ruleWsSupport) {
  74. this.dbClient = dbClient;
  75. this.ruleUpdater = ruleUpdater;
  76. this.mapper = mapper;
  77. this.userSession = userSession;
  78. this.ruleWsSupport = ruleWsSupport;
  79. }
  80. @Override
  81. public void define(WebService.NewController controller) {
  82. WebService.NewAction action = controller
  83. .createAction("update")
  84. .setPost(true)
  85. .setResponseExample(Resources.getResource(getClass(), "update-example.json"))
  86. .setDescription("Update an existing rule.<br>" +
  87. "Requires the 'Administer Quality Profiles' permission")
  88. .setSince("4.4")
  89. .setHandler(this);
  90. action.createParam(PARAM_KEY)
  91. .setRequired(true)
  92. .setMaximumLength(KEY_MAXIMUM_LENGTH)
  93. .setDescription("Key of the rule to update")
  94. .setExampleValue("javascript:NullCheck");
  95. action.createParam(PARAM_TAGS)
  96. .setDescription("Optional comma-separated list of tags to set. Use blank value to remove current tags. Tags " +
  97. "are not changed if the parameter is not set.")
  98. .setExampleValue("java8,security");
  99. action.createParam(PARAM_MARKDOWN_NOTE)
  100. .setDescription("Optional note in markdown format. Use empty value to remove current note. Note is not changed " +
  101. "if the parameter is not set.")
  102. .setExampleValue("my *note*");
  103. action.createParam("debt_sub_characteristic")
  104. .setDescription("Debt characteristics are no more supported. This parameter is ignored.")
  105. .setDeprecatedSince("5.5");
  106. action.createParam(PARAM_REMEDIATION_FN_TYPE)
  107. .setDescription("Type of the remediation function of the rule")
  108. .setPossibleValues(DebtRemediationFunction.Type.values())
  109. .setSince("5.5");
  110. action.createParam(PARAM_REMEDIATION_FN_BASE_EFFORT)
  111. .setDescription("Base effort of the remediation function of the rule")
  112. .setExampleValue("1d")
  113. .setSince("5.5");
  114. action.createParam(PARAM_REMEDIATION_FN_GAP_MULTIPLIER)
  115. .setDescription("Gap multiplier of the remediation function of the rule")
  116. .setExampleValue("3min")
  117. .setSince("5.5");
  118. action
  119. .createParam(PARAM_NAME)
  120. .setMaximumLength(NAME_MAXIMUM_LENGTH)
  121. .setDescription("Rule name (mandatory for custom rule)")
  122. .setExampleValue("My custom rule");
  123. action
  124. .createParam(PARAM_DESCRIPTION)
  125. .setDescription("Rule description (mandatory for custom rule and manual rule)")
  126. .setExampleValue("Description of my custom rule");
  127. action
  128. .createParam(PARAM_SEVERITY)
  129. .setDescription("Rule severity (Only when updating a custom rule)")
  130. .setPossibleValues(Severity.ALL);
  131. action
  132. .createParam(PARAM_STATUS)
  133. .setPossibleValues(RuleStatus.values())
  134. .setDescription("Rule status (Only when updating a custom rule)");
  135. action.createParam(PARAM_ORGANIZATION)
  136. .setRequired(false)
  137. .setInternal(true)
  138. .setDescription("Organization key")
  139. .setExampleValue("my-org")
  140. .setSince("6.4");
  141. action.createParam(PARAMS)
  142. .setDescription("Parameters as semi-colon list of <key>=<value>, for example 'params=key1=v1;key2=v2' (Only when updating a custom rule)");
  143. }
  144. @Override
  145. public void handle(Request request, Response response) throws Exception {
  146. userSession.checkLoggedIn();
  147. try (DbSession dbSession = dbClient.openSession(false)) {
  148. OrganizationDto organization = ruleWsSupport.getOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION));
  149. userSession.checkPermission(ADMINISTER_QUALITY_PROFILES, organization);
  150. RuleUpdate update = readRequest(dbSession, request, organization);
  151. ruleUpdater.update(dbSession, update, organization, userSession);
  152. UpdateResponse updateResponse = buildResponse(dbSession, update.getRuleKey(), organization);
  153. writeProtobuf(updateResponse, request, response);
  154. }
  155. }
  156. private RuleUpdate readRequest(DbSession dbSession, Request request, OrganizationDto organization) {
  157. RuleKey key = RuleKey.parse(request.mandatoryParam(PARAM_KEY));
  158. RuleUpdate update = createRuleUpdate(dbSession, key, organization);
  159. readTags(request, update);
  160. readMarkdownNote(request, update);
  161. readDebt(request, update);
  162. String name = request.param(PARAM_NAME);
  163. if (name != null) {
  164. update.setName(name);
  165. }
  166. String description = request.param(PARAM_DESCRIPTION);
  167. if (description != null) {
  168. update.setMarkdownDescription(description);
  169. }
  170. String severity = request.param(PARAM_SEVERITY);
  171. if (severity != null) {
  172. update.setSeverity(severity);
  173. }
  174. String status = request.param(PARAM_STATUS);
  175. if (status != null) {
  176. update.setStatus(RuleStatus.valueOf(status));
  177. }
  178. String params = request.param(PARAMS);
  179. if (params != null) {
  180. update.setParameters(KeyValueFormat.parse(params));
  181. }
  182. return update;
  183. }
  184. private RuleUpdate createRuleUpdate(DbSession dbSession, RuleKey key, OrganizationDto organization) {
  185. RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key)
  186. .orElseThrow(() -> new NotFoundException(format("This rule does not exist: %s", key)));
  187. RuleUpdate ruleUpdate = ofNullable(rule.getTemplateId())
  188. .map(x -> RuleUpdate.createForCustomRule(key))
  189. .orElseGet(() -> RuleUpdate.createForPluginRule(key));
  190. ruleUpdate.setOrganization(organization);
  191. return ruleUpdate;
  192. }
  193. private static void readTags(Request request, RuleUpdate update) {
  194. String value = request.param(PARAM_TAGS);
  195. if (value != null) {
  196. if (StringUtils.isBlank(value)) {
  197. update.setTags(null);
  198. } else {
  199. update.setTags(newHashSet(Splitter.on(',').omitEmptyStrings().trimResults().split(value)));
  200. }
  201. }
  202. // else do not touch this field
  203. }
  204. private static void readMarkdownNote(Request request, RuleUpdate update) {
  205. String value = request.param(PARAM_MARKDOWN_NOTE);
  206. if (value != null) {
  207. update.setMarkdownNote(value);
  208. }
  209. // else do not touch this field
  210. }
  211. private static void readDebt(Request request, RuleUpdate update) {
  212. String value = request.param(PARAM_REMEDIATION_FN_TYPE);
  213. if (value != null) {
  214. if (StringUtils.isBlank(value)) {
  215. update.setDebtRemediationFunction(null);
  216. } else {
  217. DebtRemediationFunction fn = new DefaultDebtRemediationFunction(
  218. DebtRemediationFunction.Type.valueOf(value),
  219. request.param(PARAM_REMEDIATION_FN_GAP_MULTIPLIER),
  220. request.param(PARAM_REMEDIATION_FN_BASE_EFFORT));
  221. update.setDebtRemediationFunction(fn);
  222. }
  223. }
  224. }
  225. private UpdateResponse buildResponse(DbSession dbSession, RuleKey key, OrganizationDto organization) {
  226. RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, organization, key)
  227. .orElseThrow(() -> new NotFoundException(format("Rule not found: %s", key)));
  228. List<RuleDefinitionDto> templateRules = new ArrayList<>(1);
  229. if (rule.getDefinition().isCustomRule()) {
  230. dbClient.ruleDao().selectDefinitionById(rule.getTemplateId(), dbSession)
  231. .ifPresent(templateRules::add);
  232. }
  233. List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleIds(dbSession, singletonList(rule.getId()));
  234. UpdateResponse.Builder responseBuilder = UpdateResponse.newBuilder();
  235. SearchAction.SearchResult searchResult = new SearchAction.SearchResult()
  236. .setRules(singletonList(rule))
  237. .setTemplateRules(templateRules)
  238. .setRuleParameters(ruleParameters)
  239. .setTotal(1L);
  240. responseBuilder
  241. .setRule(mapper.toWsRule(rule.getDefinition(), searchResult, Collections.emptySet(), rule.getMetadata(), ruleWsSupport.getUsersByUuid(dbSession, singletonList(rule))));
  242. return responseBuilder.build();
  243. }
  244. }