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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.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.component.BranchDto;
  36. import org.sonar.db.issue.IssueDto;
  37. import org.sonar.server.issue.IssueFieldsSetter;
  38. import org.sonar.server.issue.IssueFinder;
  39. import org.sonar.server.pushapi.issues.IssueChangeEventService;
  40. import org.sonar.server.user.UserSession;
  41. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  42. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  43. import static org.sonar.db.component.BranchType.BRANCH;
  44. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_SET_TYPE;
  45. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUE;
  46. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TYPE;
  47. public class SetTypeAction implements IssuesWsAction {
  48. private static final EnumSet<RuleType> ALL_RULE_TYPES_EXCEPT_SECURITY_HOTSPOTS = EnumSet.complementOf(EnumSet.of(RuleType.SECURITY_HOTSPOT));
  49. private final UserSession userSession;
  50. private final DbClient dbClient;
  51. private final IssueChangeEventService issueChangeEventService;
  52. private final IssueFinder issueFinder;
  53. private final IssueFieldsSetter issueFieldsSetter;
  54. private final IssueUpdater issueUpdater;
  55. private final OperationResponseWriter responseWriter;
  56. private final System2 system2;
  57. public SetTypeAction(UserSession userSession, DbClient dbClient, IssueChangeEventService issueChangeEventService, IssueFinder issueFinder,
  58. IssueFieldsSetter issueFieldsSetter, IssueUpdater issueUpdater, OperationResponseWriter responseWriter, System2 system2) {
  59. this.userSession = userSession;
  60. this.dbClient = dbClient;
  61. this.issueChangeEventService = issueChangeEventService;
  62. this.issueFinder = issueFinder;
  63. this.issueFieldsSetter = issueFieldsSetter;
  64. this.issueUpdater = issueUpdater;
  65. this.responseWriter = responseWriter;
  66. this.system2 = system2;
  67. }
  68. @Override
  69. public void define(WebService.NewController controller) {
  70. WebService.NewAction action = controller.createAction(ACTION_SET_TYPE)
  71. .setDescription("Change type of issue, for instance from 'code smell' to 'bug'.<br/>" +
  72. "Requires the following permissions:" +
  73. "<ul>" +
  74. " <li>'Authentication'</li>" +
  75. " <li>'Browse' rights on project of the specified issue</li>" +
  76. " <li>'Administer Issues' rights on project of the specified issue</li>" +
  77. "</ul>")
  78. .setSince("5.5")
  79. .setChangelog(
  80. new Change("10.4", "The response fields 'status' and 'resolution' are deprecated. Please use 'simpleStatus' instead."),
  81. new Change("10.4", "Add 'simpleStatus' field to the response."),
  82. new Change("10.2", "Add 'impacts', 'cleanCodeAttribute', 'cleanCodeAttributeCategory' fields to the response"),
  83. new Change("10.2", "This endpoint is now deprecated."),
  84. new Change("9.6", "Response field 'ruleDescriptionContextKey' added"),
  85. new Change("8.8", "The response field components.uuid is removed"),
  86. new Change("6.5", "the database ids of the components are removed from the response"),
  87. new Change("6.5", "the response field components.uuid is deprecated. Use components.key instead."))
  88. .setHandler(this)
  89. .setDeprecatedSince("10.2")
  90. .setResponseExample(Resources.getResource(this.getClass(), "set_type-example.json"))
  91. .setPost(true);
  92. action.createParam(PARAM_ISSUE)
  93. .setDescription("Issue key")
  94. .setRequired(true)
  95. .setExampleValue(Uuids.UUID_EXAMPLE_01);
  96. action.createParam(PARAM_TYPE)
  97. .setDescription("New type")
  98. .setRequired(true)
  99. .setPossibleValues(ALL_RULE_TYPES_EXCEPT_SECURITY_HOTSPOTS);
  100. }
  101. @Override
  102. public void handle(Request request, Response response) throws Exception {
  103. userSession.checkLoggedIn();
  104. String issueKey = request.mandatoryParam(PARAM_ISSUE);
  105. RuleType ruleType = RuleType.valueOf(request.mandatoryParam(PARAM_TYPE));
  106. try (DbSession session = dbClient.openSession(false)) {
  107. SearchResponseData preloadedSearchResponseData = setType(session, issueKey, ruleType);
  108. responseWriter.write(issueKey, preloadedSearchResponseData, request, response);
  109. }
  110. }
  111. private SearchResponseData setType(DbSession session, String issueKey, RuleType ruleType) {
  112. IssueDto issueDto = issueFinder.getByKey(session, issueKey);
  113. DefaultIssue issue = issueDto.toDefaultIssue();
  114. userSession.checkComponentUuidPermission(ISSUE_ADMIN, issue.projectUuid());
  115. IssueChangeContext context = issueChangeContextByUserBuilder(new Date(system2.now()), userSession.getUuid()).withRefreshMeasures().build();
  116. if (issueFieldsSetter.setType(issue, ruleType, context)) {
  117. BranchDto branch = issueUpdater.getBranch(session, issue);
  118. SearchResponseData response = issueUpdater.saveIssueAndPreloadSearchResponseData(session, issue, context, branch);
  119. if (branch.getBranchType().equals(BRANCH) && response.getComponentByUuid(issue.projectUuid()) != null) {
  120. issueChangeEventService.distributeIssueChangeEvent(issue, null, ruleType.name(), null, branch,
  121. response.getComponentByUuid(issue.projectUuid()).getKey());
  122. }
  123. return response;
  124. }
  125. return new SearchResponseData(issueDto);
  126. }
  127. }