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.

AssignAction.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.base.Strings;
  22. import com.google.common.io.Resources;
  23. import java.util.Date;
  24. import javax.annotation.CheckForNull;
  25. import javax.annotation.Nullable;
  26. import org.sonar.api.server.ws.Change;
  27. import org.sonar.api.server.ws.Request;
  28. import org.sonar.api.server.ws.Response;
  29. import org.sonar.api.server.ws.WebService;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.core.issue.DefaultIssue;
  32. import org.sonar.core.issue.IssueChangeContext;
  33. import org.sonar.core.util.Uuids;
  34. import org.sonar.db.DbClient;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.issue.IssueDto;
  37. import org.sonar.db.user.UserDto;
  38. import org.sonar.server.issue.IssueFieldsSetter;
  39. import org.sonar.server.issue.IssueFinder;
  40. import org.sonar.server.user.UserSession;
  41. import static com.google.common.base.Strings.emptyToNull;
  42. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  43. import static org.sonar.server.exceptions.NotFoundException.checkFound;
  44. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_ASSIGN;
  45. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ASSIGNEE;
  46. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ISSUE;
  47. public class AssignAction implements IssuesWsAction {
  48. private static final String ASSIGN_TO_ME_VALUE = "_me";
  49. private final System2 system2;
  50. private final UserSession userSession;
  51. private final DbClient dbClient;
  52. private final IssueFinder issueFinder;
  53. private final IssueFieldsSetter issueFieldsSetter;
  54. private final IssueUpdater issueUpdater;
  55. private final OperationResponseWriter responseWriter;
  56. public AssignAction(System2 system2, UserSession userSession, DbClient dbClient, IssueFinder issueFinder, IssueFieldsSetter issueFieldsSetter, IssueUpdater issueUpdater,
  57. OperationResponseWriter responseWriter) {
  58. this.system2 = system2;
  59. this.userSession = userSession;
  60. this.dbClient = dbClient;
  61. this.issueFinder = issueFinder;
  62. this.issueFieldsSetter = issueFieldsSetter;
  63. this.issueUpdater = issueUpdater;
  64. this.responseWriter = responseWriter;
  65. }
  66. @Override
  67. public void define(WebService.NewController controller) {
  68. WebService.NewAction action = controller.createAction(ACTION_ASSIGN)
  69. .setDescription("Assign/Unassign an issue. Requires authentication and Browse permission on project")
  70. .setSince("3.6")
  71. .setChangelog(
  72. new Change("10.2", "Add 'impacts', 'cleanCodeAttribute', 'cleanCodeAttributeCategory' fields to the response"),
  73. new Change("9.6", "Response field 'ruleDescriptionContextKey' added"),
  74. new Change("8.8", "The response field components.uuid is removed"),
  75. new Change("6.5", "the database ids of the components are removed from the response"),
  76. new Change("6.5", "the response field components.uuid is deprecated. Use components.key instead."))
  77. .setHandler(this)
  78. .setResponseExample(Resources.getResource(this.getClass(), "assign-example.json"))
  79. .setPost(true);
  80. action.createParam(PARAM_ISSUE)
  81. .setDescription("Issue key")
  82. .setRequired(true)
  83. .setExampleValue(Uuids.UUID_EXAMPLE_01);
  84. action.createParam(PARAM_ASSIGNEE)
  85. .setDescription("Login of the assignee. When not set, it will unassign the issue. Use '%s' to assign to current user", ASSIGN_TO_ME_VALUE)
  86. .setExampleValue("admin");
  87. }
  88. @Override
  89. public void handle(Request request, Response response) throws Exception {
  90. userSession.checkLoggedIn();
  91. String assignee = getAssignee(request);
  92. String key = request.mandatoryParam(PARAM_ISSUE);
  93. SearchResponseData preloadedResponseData = assign(key, assignee);
  94. responseWriter.write(key, preloadedResponseData, request, response);
  95. }
  96. private SearchResponseData assign(String issueKey, @Nullable String login) {
  97. try (DbSession dbSession = dbClient.openSession(false)) {
  98. IssueDto issueDto = issueFinder.getByKey(dbSession, issueKey);
  99. DefaultIssue issue = issueDto.toDefaultIssue();
  100. UserDto user = getUser(dbSession, login);
  101. IssueChangeContext context = issueChangeContextByUserBuilder(new Date(system2.now()), userSession.getUuid()).build();
  102. if (issueFieldsSetter.assign(issue, user, context)) {
  103. return issueUpdater.saveIssueAndPreloadSearchResponseData(dbSession, issue, context);
  104. }
  105. return new SearchResponseData(issueDto);
  106. }
  107. }
  108. @CheckForNull
  109. private String getAssignee(Request request) {
  110. String assignee = emptyToNull(request.param(PARAM_ASSIGNEE));
  111. return ASSIGN_TO_ME_VALUE.equals(assignee) ? userSession.getLogin() : assignee;
  112. }
  113. @CheckForNull
  114. private UserDto getUser(DbSession dbSession, @Nullable String assignee) {
  115. if (Strings.isNullOrEmpty(assignee)) {
  116. return null;
  117. }
  118. return checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, assignee), "Unknown user: %s", assignee);
  119. }
  120. }