Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

EditCommentAction.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.Objects;
  23. import org.sonar.api.server.ws.Change;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.issue.IssueChangeDto;
  31. import org.sonar.db.issue.IssueDto;
  32. import org.sonar.server.exceptions.NotFoundException;
  33. import org.sonar.server.issue.IssueFinder;
  34. import org.sonar.server.user.UserSession;
  35. import static com.google.common.base.Preconditions.checkArgument;
  36. import static com.google.common.base.Strings.isNullOrEmpty;
  37. import static java.lang.String.format;
  38. import static java.util.Objects.requireNonNull;
  39. import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
  40. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_EDIT_COMMENT;
  41. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMMENT;
  42. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_TEXT;
  43. public class EditCommentAction implements IssuesWsAction {
  44. private final System2 system2;
  45. private final UserSession userSession;
  46. private final DbClient dbClient;
  47. private final IssueFinder issueFinder;
  48. private final OperationResponseWriter responseWriter;
  49. public EditCommentAction(System2 system2, UserSession userSession, DbClient dbClient, IssueFinder issueFinder, OperationResponseWriter responseWriter) {
  50. this.system2 = system2;
  51. this.userSession = userSession;
  52. this.dbClient = dbClient;
  53. this.issueFinder = issueFinder;
  54. this.responseWriter = responseWriter;
  55. }
  56. @Override
  57. public void define(WebService.NewController context) {
  58. WebService.NewAction action = context.createAction(ACTION_EDIT_COMMENT)
  59. .setDescription("Edit a comment.<br/>" +
  60. "Requires authentication and the following permission: 'Browse' on the project of the specified issue.")
  61. .setSince("3.6")
  62. .setChangelog(
  63. new Change("10.4", "The response fields 'status' and 'resolution' are deprecated. Please use 'simpleStatus' instead."),
  64. new Change("10.4", "Add 'simpleStatus' field to the response."),
  65. new Change("10.2", "Add 'impacts', 'cleanCodeAttribute', 'cleanCodeAttributeCategory' fields to the response"),
  66. new Change("9.6", "Response field 'ruleDescriptionContextKey' added"),
  67. new Change("8.8", "The response field components.uuid is removed"),
  68. new Change("6.3", "the response returns the issue with all its details"),
  69. new Change("6.3", format("the 'key' parameter has been renamed %s", PARAM_COMMENT)),
  70. new Change("6.5", "the database ids of the components are removed from the response"),
  71. new Change("6.5", "the response field components.uuid is deprecated. Use components.key instead."))
  72. .setHandler(this)
  73. .setResponseExample(Resources.getResource(this.getClass(), "edit_comment-example.json"))
  74. .setPost(true);
  75. action.createParam(PARAM_COMMENT)
  76. .setDescription("Comment key")
  77. .setSince("6.3")
  78. .setRequired(true)
  79. .setExampleValue(UUID_EXAMPLE_01);
  80. action.createParam(PARAM_TEXT)
  81. .setDescription("Comment text")
  82. .setRequired(true)
  83. .setExampleValue("Won't fix because it doesn't apply to the context");
  84. }
  85. @Override
  86. public void handle(Request request, Response response) {
  87. userSession.checkLoggedIn();
  88. try (DbSession dbSession = dbClient.openSession(false)) {
  89. CommentData commentData = loadCommentData(dbSession, toWsRequest(request));
  90. updateComment(dbSession, commentData);
  91. IssueDto issueDto = commentData.getIssueDto();
  92. responseWriter.write(issueDto.getKey(), new SearchResponseData(issueDto), request, response);
  93. }
  94. }
  95. private CommentData loadCommentData(DbSession dbSession, EditCommentRequest request) {
  96. return new CommentData(dbSession, request);
  97. }
  98. private void updateComment(DbSession dbSession, CommentData commentData) {
  99. commentData.getIssueChangeDto().setUpdatedAt(system2.now());
  100. commentData.getIssueChangeDto().setChangeData(commentData.getRequest().getText());
  101. dbClient.issueChangeDao().update(dbSession, commentData.getIssueChangeDto());
  102. dbSession.commit();
  103. }
  104. private static EditCommentRequest toWsRequest(Request request) {
  105. EditCommentRequest wsRequest = new EditCommentRequest(request.mandatoryParam(PARAM_COMMENT), request.mandatoryParam(PARAM_TEXT));
  106. checkArgument(!isNullOrEmpty(wsRequest.getText()), "Cannot set empty comment to an issue");
  107. return wsRequest;
  108. }
  109. private class CommentData {
  110. private final IssueChangeDto issueChangeDto;
  111. private final IssueDto issueDto;
  112. private final EditCommentRequest request;
  113. CommentData(DbSession dbSession, EditCommentRequest request) {
  114. this.request = request;
  115. this.issueChangeDto = dbClient.issueChangeDao().selectCommentByKey(dbSession, request.getComment())
  116. .orElseThrow(() -> new NotFoundException(format("Comment with key '%s' does not exist", request.getComment())));
  117. // Load issue now to quickly fail if user hasn't permission to see it
  118. this.issueDto = issueFinder.getByKey(dbSession, issueChangeDto.getIssueKey());
  119. checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only edit your own comments");
  120. }
  121. IssueChangeDto getIssueChangeDto() {
  122. return issueChangeDto;
  123. }
  124. IssueDto getIssueDto() {
  125. return issueDto;
  126. }
  127. EditCommentRequest getRequest() {
  128. return request;
  129. }
  130. }
  131. public static class EditCommentRequest {
  132. private final String comment;
  133. private final String text;
  134. public EditCommentRequest(String comment, String text) {
  135. this.comment = requireNonNull(comment, "Comment key cannot be null");
  136. this.text = requireNonNull(text, "Text cannot be null");
  137. }
  138. public String getComment() {
  139. return comment;
  140. }
  141. public String getText() {
  142. return text;
  143. }
  144. }
  145. }