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.

DoTransitionAction.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 org.sonar.api.issue.DefaultTransitions;
  24. import org.sonar.api.server.ws.Change;
  25. import org.sonar.api.server.ws.Request;
  26. import org.sonar.api.server.ws.Response;
  27. import org.sonar.api.server.ws.WebService;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.core.issue.DefaultIssue;
  30. import org.sonar.core.issue.IssueChangeContext;
  31. import org.sonar.core.util.Uuids;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.component.BranchDto;
  35. import org.sonar.db.issue.IssueDto;
  36. import org.sonar.server.issue.IssueFinder;
  37. import org.sonar.server.issue.TransitionService;
  38. import org.sonar.server.pushapi.issues.IssueChangeEventService;
  39. import org.sonar.server.user.UserSession;
  40. import static java.lang.String.format;
  41. import static org.sonar.api.issue.DefaultTransitions.OPEN_AS_VULNERABILITY;
  42. import static org.sonar.api.issue.DefaultTransitions.RESET_AS_TO_REVIEW;
  43. import static org.sonar.api.issue.DefaultTransitions.RESOLVE_AS_REVIEWED;
  44. import static org.sonar.api.issue.DefaultTransitions.SET_AS_IN_REVIEW;
  45. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  46. import static org.sonar.db.component.BranchType.BRANCH;
  47. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_DO_TRANSITION;
  48. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUE;
  49. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TRANSITION;
  50. public class DoTransitionAction implements IssuesWsAction {
  51. private final DbClient dbClient;
  52. private final UserSession userSession;
  53. private final IssueChangeEventService issueChangeEventService;
  54. private final IssueFinder issueFinder;
  55. private final IssueUpdater issueUpdater;
  56. private final TransitionService transitionService;
  57. private final OperationResponseWriter responseWriter;
  58. private final System2 system2;
  59. public DoTransitionAction(DbClient dbClient, UserSession userSession, IssueChangeEventService issueChangeEventService,
  60. IssueFinder issueFinder, IssueUpdater issueUpdater, TransitionService transitionService,
  61. OperationResponseWriter responseWriter, System2 system2) {
  62. this.dbClient = dbClient;
  63. this.userSession = userSession;
  64. this.issueChangeEventService = issueChangeEventService;
  65. this.issueFinder = issueFinder;
  66. this.issueUpdater = issueUpdater;
  67. this.transitionService = transitionService;
  68. this.responseWriter = responseWriter;
  69. this.system2 = system2;
  70. }
  71. @Override
  72. public void define(WebService.NewController controller) {
  73. WebService.NewAction action = controller.createAction(ACTION_DO_TRANSITION)
  74. .setDescription("Do workflow transition on an issue. Requires authentication and Browse permission on project.<br/>" +
  75. "The transitions '" + DefaultTransitions.WONT_FIX + "' and '" + DefaultTransitions.FALSE_POSITIVE + "' require the permission 'Administer Issues'.<br/>" +
  76. "The transitions involving security hotspots require the permission 'Administer Security Hotspot'.")
  77. .setSince("3.6")
  78. .setChangelog(
  79. new Change("10.4", "The response fields 'status' and 'resolution' are deprecated. Please use 'issueStatus' instead."),
  80. new Change("10.4", "Add 'issueStatus' field to the response."),
  81. new Change("10.2", "Add 'impacts', 'cleanCodeAttribute', 'cleanCodeAttributeCategory' fields to the response"),
  82. new Change("9.6", "Response field 'ruleDescriptionContextKey' added"),
  83. new Change("8.8", "The response field components.uuid is removed"),
  84. new Change("8.1", format("transitions '%s' and '%s' are no more supported", SET_AS_IN_REVIEW, OPEN_AS_VULNERABILITY)),
  85. new Change("7.8", format("added '%s', %s, %s and %s transitions for security hotspots ", SET_AS_IN_REVIEW, RESOLVE_AS_REVIEWED, OPEN_AS_VULNERABILITY, RESET_AS_TO_REVIEW)),
  86. new Change("7.3", "added transitions for security hotspots"),
  87. new Change("6.5", "the database ids of the components are removed from the response"),
  88. new Change("6.5", "the response field components.uuid is deprecated. Use components.key instead."))
  89. .setHandler(this)
  90. .setResponseExample(Resources.getResource(this.getClass(), "do_transition-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_TRANSITION)
  97. .setDescription("Transition")
  98. .setRequired(true)
  99. .setPossibleValues(DefaultTransitions.ALL);
  100. }
  101. @Override
  102. public void handle(Request request, Response response) {
  103. userSession.checkLoggedIn();
  104. String issue = request.mandatoryParam(PARAM_ISSUE);
  105. try (DbSession dbSession = dbClient.openSession(false)) {
  106. IssueDto issueDto = issueFinder.getByKey(dbSession, issue);
  107. SearchResponseData preloadedSearchResponseData = doTransition(dbSession, issueDto, request.mandatoryParam(PARAM_TRANSITION));
  108. responseWriter.write(issue, preloadedSearchResponseData, request, response);
  109. }
  110. }
  111. private SearchResponseData doTransition(DbSession session, IssueDto issueDto, String transitionKey) {
  112. DefaultIssue defaultIssue = issueDto.toDefaultIssue();
  113. IssueChangeContext context = issueChangeContextByUserBuilder(new Date(system2.now()), userSession.getUuid()).withRefreshMeasures().build();
  114. transitionService.checkTransitionPermission(transitionKey, defaultIssue);
  115. if (transitionService.doTransition(defaultIssue, context, transitionKey)) {
  116. BranchDto branch = issueUpdater.getBranch(session, defaultIssue);
  117. SearchResponseData response = issueUpdater.saveIssueAndPreloadSearchResponseData(session, defaultIssue, context, branch);
  118. if (branch.getBranchType().equals(BRANCH) && response.getComponentByUuid(defaultIssue.projectUuid()) != null) {
  119. issueChangeEventService.distributeIssueChangeEvent(defaultIssue, null, null, transitionKey, branch,
  120. response.getComponentByUuid(defaultIssue.projectUuid()).getKey());
  121. }
  122. return response;
  123. }
  124. return new SearchResponseData(issueDto);
  125. }
  126. }