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.

ShowAction.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.io.Resources;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import org.sonar.api.rule.RuleKey;
  25. import org.sonar.api.server.ws.Change;
  26. import org.sonar.api.server.ws.Request;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.rule.RuleDto;
  32. import org.sonar.db.rule.RuleParamDto;
  33. import org.sonar.server.exceptions.NotFoundException;
  34. import org.sonarqube.ws.Rules.ShowResponse;
  35. import static java.lang.String.format;
  36. import static java.util.Collections.singletonList;
  37. import static java.util.Optional.ofNullable;
  38. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  39. /**
  40. * @since 4.4
  41. */
  42. public class ShowAction implements RulesWsAction {
  43. public static final String PARAM_KEY = "key";
  44. public static final String PARAM_ACTIVES = "actives";
  45. private final DbClient dbClient;
  46. private final RulesResponseFormatter rulesResponseFormatter;
  47. public ShowAction(DbClient dbClient, RulesResponseFormatter rulesResponseFormatter) {
  48. this.dbClient = dbClient;
  49. this.rulesResponseFormatter = rulesResponseFormatter;
  50. }
  51. @Override
  52. public void define(WebService.NewController controller) {
  53. WebService.NewAction action = controller
  54. .createAction("show")
  55. .setDescription("Get detailed information about a rule<br>")
  56. .setSince("4.2")
  57. .setResponseExample(Resources.getResource(getClass(), "show-example.json"))
  58. .setHandler(this)
  59. .setChangelog(
  60. new Change("5.5", "The field 'effortToFixDescription' in the response has been deprecated, it becomes 'gapDescription'."),
  61. new Change("5.5", "The field 'debtRemFnCoeff' in the response has been deprecated, it becomes 'remFnGapMultiplier'."),
  62. new Change("5.5", "The field 'defaultDebtRemFnCoeff' in the response has been deprecated, it becomes 'defaultRemFnGapMultiplier'."),
  63. new Change("5.5", "The field 'debtRemFnOffset' in the response has been deprecated, it becomes 'remFnBaseEffort'."),
  64. new Change("5.5", "The field 'defaultDebtRemFnOffset' in the response has been deprecated, it becomes 'defaultRemFnBaseEffort'."),
  65. new Change("5.5", "The field 'debtOverloaded' in the response has been deprecated, it becomes 'remFnOverloaded'."),
  66. new Change("7.1", "The field 'scope' has been added."),
  67. new Change("9.5", "The field 'htmlDesc' in the response has been deprecated, it becomes 'descriptionSections'."),
  68. new Change("9.5", "The field 'descriptionSections' has been added to the payload."),
  69. new Change("9.6", "'descriptionSections' can optionally embed a context field."),
  70. new Change("9.6", "'educationPrinciples' has been added."),
  71. new Change("10.0", "The deprecated field 'effortToFixDescription' has been removed, use 'gapDescription' instead."),
  72. new Change("10.0", "The deprecated field 'debtRemFnCoeff' has been removed, use 'remFnGapMultiplier' instead."),
  73. new Change("10.0", "The deprecated field 'defaultDebtRemFnCoeff' has been removed, use 'defaultRemFnGapMultiplier' instead."),
  74. new Change("10.0", "The deprecated field 'debtRemFnOffset' has been removed, use 'remFnBaseEffort' instead."),
  75. new Change("10.0", "The deprecated field 'defaultDebtRemFnOffset' has been removed, use 'defaultRemFnBaseEffort' instead."),
  76. new Change("10.0", "The deprecated field 'debtOverloaded' has been removed, use 'remFnOverloaded' instead."),
  77. new Change("10.0", "The field 'defaultDebtRemFnType' has been deprecated, use 'defaultRemFnType' instead"),
  78. new Change("10.0", "The field 'debtRemFnType' has been deprecated, use 'remFnType' instead"),
  79. new Change("10.2", "Add 'impacts', 'cleanCodeAttribute', 'cleanCodeAttributeCategory' fields to the response"),
  80. new Change("10.2", "The field 'severity' and 'type' in the response have been deprecated, use 'impacts' instead."));
  81. action
  82. .createParam(PARAM_KEY)
  83. .setDescription("Rule key")
  84. .setRequired(true)
  85. .setExampleValue("javascript:EmptyBlock");
  86. action
  87. .createParam(PARAM_ACTIVES)
  88. .setDescription("Show rule's activations for all profiles (\"active rules\")")
  89. .setBooleanPossibleValues()
  90. .setDefaultValue(false);
  91. }
  92. @Override
  93. public void handle(Request request, Response response) throws Exception {
  94. RuleKey key = RuleKey.parse(request.mandatoryParam(PARAM_KEY));
  95. try (DbSession dbSession = dbClient.openSession(false)) {
  96. RuleDto rule = dbClient.ruleDao().selectByKey(dbSession, key)
  97. .orElseThrow(() -> new NotFoundException(format("Rule not found: %s", key)));
  98. List<RuleDto> templateRules = ofNullable(rule.getTemplateUuid())
  99. .flatMap(templateUuid -> dbClient.ruleDao().selectByUuid(rule.getTemplateUuid(), dbSession))
  100. .map(Collections::singletonList).orElseGet(Collections::emptyList);
  101. List<RuleParamDto> ruleParameters = dbClient.ruleDao().selectRuleParamsByRuleUuids(dbSession, singletonList(rule.getUuid()));
  102. ShowResponse showResponse = buildResponse(dbSession, request,
  103. new RulesResponseFormatter.SearchResult()
  104. .setRules(singletonList(rule))
  105. .setTemplateRules(templateRules)
  106. .setRuleParameters(ruleParameters)
  107. .setTotal(1L));
  108. writeProtobuf(showResponse, request, response);
  109. }
  110. }
  111. private ShowResponse buildResponse(DbSession dbSession, Request request, RulesResponseFormatter.SearchResult searchResult) {
  112. ShowResponse.Builder responseBuilder = ShowResponse.newBuilder();
  113. RuleDto rule = searchResult.getRules().get(0);
  114. responseBuilder.setRule(rulesResponseFormatter.formatRule(dbSession, searchResult));
  115. if (request.mandatoryParamAsBoolean(PARAM_ACTIVES)) {
  116. responseBuilder.addAllActives(rulesResponseFormatter.formatActiveRule(dbSession, rule));
  117. }
  118. return responseBuilder.build();
  119. }
  120. }