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.

SetTypeAction.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.issue.ws;
  21. import com.google.common.io.Resources;
  22. import java.util.Date;
  23. import java.util.EnumSet;
  24. import org.sonar.api.rules.RuleType;
  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.api.utils.System2;
  30. import org.sonar.core.issue.DefaultIssue;
  31. import org.sonar.core.issue.IssueChangeContext;
  32. import org.sonar.core.util.Uuids;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbSession;
  35. import org.sonar.db.issue.IssueDto;
  36. import org.sonar.server.issue.IssueFieldsSetter;
  37. import org.sonar.server.issue.IssueFinder;
  38. import org.sonar.server.user.UserSession;
  39. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  40. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_SET_TYPE;
  41. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUE;
  42. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TYPE;
  43. public class SetTypeAction implements IssuesWsAction {
  44. private static final EnumSet<RuleType> ALL_RULE_TYPES_EXCEPT_SECURITY_HOTSPOTS = EnumSet.complementOf(EnumSet.of(RuleType.SECURITY_HOTSPOT));
  45. private final UserSession userSession;
  46. private final DbClient dbClient;
  47. private final IssueFinder issueFinder;
  48. private final IssueFieldsSetter issueFieldsSetter;
  49. private final IssueUpdater issueUpdater;
  50. private final OperationResponseWriter responseWriter;
  51. private final System2 system2;
  52. public SetTypeAction(UserSession userSession, DbClient dbClient, IssueFinder issueFinder, IssueFieldsSetter issueFieldsSetter, IssueUpdater issueUpdater,
  53. OperationResponseWriter responseWriter, System2 system2) {
  54. this.userSession = userSession;
  55. this.dbClient = dbClient;
  56. this.issueFinder = issueFinder;
  57. this.issueFieldsSetter = issueFieldsSetter;
  58. this.issueUpdater = issueUpdater;
  59. this.responseWriter = responseWriter;
  60. this.system2 = system2;
  61. }
  62. @Override
  63. public void define(WebService.NewController controller) {
  64. WebService.NewAction action = controller.createAction(ACTION_SET_TYPE)
  65. .setDescription("Change type of issue, for instance from 'code smell' to 'bug'.<br/>" +
  66. "Requires the following permissions:" +
  67. "<ul>" +
  68. " <li>'Authentication'</li>" +
  69. " <li>'Browse' rights on project of the specified issue</li>" +
  70. " <li>'Administer Issues' rights on project of the specified issue</li>" +
  71. "</ul>")
  72. .setSince("5.5")
  73. .setChangelog(
  74. new Change("6.5", "the database ids of the components are removed from the response"),
  75. new Change("6.5", "the response field components.uuid is deprecated. Use components.key instead."))
  76. .setHandler(this)
  77. .setResponseExample(Resources.getResource(this.getClass(), "set_type-example.json"))
  78. .setPost(true);
  79. action.createParam(PARAM_ISSUE)
  80. .setDescription("Issue key")
  81. .setRequired(true)
  82. .setExampleValue(Uuids.UUID_EXAMPLE_01);
  83. action.createParam(PARAM_TYPE)
  84. .setDescription("New type")
  85. .setRequired(true)
  86. .setPossibleValues(ALL_RULE_TYPES_EXCEPT_SECURITY_HOTSPOTS);
  87. }
  88. @Override
  89. public void handle(Request request, Response response) throws Exception {
  90. userSession.checkLoggedIn();
  91. String issueKey = request.mandatoryParam(PARAM_ISSUE);
  92. RuleType ruleType = RuleType.valueOf(request.mandatoryParam(PARAM_TYPE));
  93. try (DbSession session = dbClient.openSession(false)) {
  94. SearchResponseData preloadedSearchResponseData = setType(session, issueKey, ruleType);
  95. responseWriter.write(issueKey, preloadedSearchResponseData, request, response);
  96. }
  97. }
  98. private SearchResponseData setType(DbSession session, String issueKey, RuleType ruleType) {
  99. IssueDto issueDto = issueFinder.getByKey(session, issueKey);
  100. DefaultIssue issue = issueDto.toDefaultIssue();
  101. userSession.checkComponentUuidPermission(ISSUE_ADMIN, issue.projectUuid());
  102. IssueChangeContext context = IssueChangeContext.createUser(new Date(system2.now()), userSession.getUuid());
  103. if (issueFieldsSetter.setType(issue, ruleType, context)) {
  104. return issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, true);
  105. }
  106. return new SearchResponseData(issueDto);
  107. }
  108. }