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.

DeleteCommentAction.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.issue.IssueChangeDto;
  30. import org.sonar.db.issue.IssueDto;
  31. import org.sonar.server.exceptions.NotFoundException;
  32. import org.sonar.server.issue.IssueFinder;
  33. import org.sonar.server.user.UserSession;
  34. import static com.google.common.base.Preconditions.checkArgument;
  35. import static java.lang.String.format;
  36. import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
  37. import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_DELETE_COMMENT;
  38. import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_COMMENT;
  39. public class DeleteCommentAction implements IssuesWsAction {
  40. private final UserSession userSession;
  41. private final DbClient dbClient;
  42. private final IssueFinder issueFinder;
  43. private final OperationResponseWriter responseWriter;
  44. public DeleteCommentAction(UserSession userSession, DbClient dbClient, IssueFinder issueFinder, OperationResponseWriter responseWriter) {
  45. this.userSession = userSession;
  46. this.dbClient = dbClient;
  47. this.issueFinder = issueFinder;
  48. this.responseWriter = responseWriter;
  49. }
  50. @Override
  51. public void define(WebService.NewController context) {
  52. WebService.NewAction action = context.createAction(ACTION_DELETE_COMMENT)
  53. .setDescription("Delete a comment.<br/>" +
  54. "Requires authentication and the following permission: 'Browse' on the project of the specified issue.")
  55. .setSince("3.6")
  56. .setChangelog(
  57. new Change("10.2", "Add 'impacts', 'cleanCodeAttribute', 'cleanCodeAttributeCategory' fields to the response"),
  58. new Change("9.6", "Response field 'ruleDescriptionContextKey' added"),
  59. new Change("8.8", "The response field components.uuid is removed"),
  60. new Change("6.5", "the response field components.uuid is deprecated. Use components.key instead."),
  61. new Change("6.5", "the database ids of the components are removed from the response"),
  62. new Change("6.3", "the response returns the issue with all its details"),
  63. new Change("6.3", "the 'key' parameter is renamed 'comment'"))
  64. .setHandler(this)
  65. .setResponseExample(Resources.getResource(this.getClass(), "delete_comment-example.json"))
  66. .setPost(true);
  67. action.createParam(PARAM_COMMENT)
  68. .setDescription("Comment key")
  69. .setSince("6.3")
  70. .setRequired(true)
  71. .setExampleValue(UUID_EXAMPLE_01);
  72. }
  73. @Override
  74. public void handle(Request request, Response response) {
  75. userSession.checkLoggedIn();
  76. try (DbSession dbSession = dbClient.openSession(false)) {
  77. CommentData commentData = loadCommentData(dbSession, request);
  78. deleteComment(dbSession, commentData);
  79. IssueDto issueDto = commentData.getIssueDto();
  80. responseWriter.write(issueDto.getKey(), new SearchResponseData(issueDto), request, response);
  81. }
  82. }
  83. private CommentData loadCommentData(DbSession dbSession, Request request) {
  84. return new CommentData(dbSession, request.mandatoryParam(PARAM_COMMENT));
  85. }
  86. private void deleteComment(DbSession dbSession, CommentData commentData) {
  87. dbClient.issueChangeDao().deleteByKey(dbSession, commentData.getIssueChangeDto().getKey());
  88. dbSession.commit();
  89. }
  90. private class CommentData {
  91. private final IssueChangeDto issueChangeDto;
  92. private final IssueDto issueDto;
  93. CommentData(DbSession dbSession, String commentKey) {
  94. this.issueChangeDto = dbClient.issueChangeDao().selectCommentByKey(dbSession, commentKey)
  95. .orElseThrow(() -> new NotFoundException(format("Comment with key '%s' does not exist", commentKey)));
  96. // Load issue now to quickly fail if user hasn't permission to see it
  97. this.issueDto = issueFinder.getByKey(dbSession, issueChangeDto.getIssueKey());
  98. checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only delete your own comments");
  99. }
  100. IssueChangeDto getIssueChangeDto() {
  101. return issueChangeDto;
  102. }
  103. IssueDto getIssueDto() {
  104. return issueDto;
  105. }
  106. }
  107. }