diff --git a/src/main/scala/gitbucket/core/controller/api/ApiIssueCommentControllerBase.scala b/src/main/scala/gitbucket/core/controller/api/ApiIssueCommentControllerBase.scala index 353f570..6f8c1ec 100644 --- a/src/main/scala/gitbucket/core/controller/api/ApiIssueCommentControllerBase.scala +++ b/src/main/scala/gitbucket/core/controller/api/ApiIssueCommentControllerBase.scala @@ -4,6 +4,7 @@ import gitbucket.core.service._ import gitbucket.core.util.Implicits._ import gitbucket.core.util.{ReadableUsersAuthenticator, ReferrerAuthenticator, RepositoryName} +import org.scalatra.{ActionResult, NoContent} trait ApiIssueCommentControllerBase extends ControllerBase { self: AccountService @@ -112,8 +113,30 @@ /* * vi. Delete a comment - * https://developer.github.com/v3/issues/comments/#delete-a-comment + * https://docs.github.com/en/rest/reference/issues#delete-an-issue-comment + * */ + delete("/api/v3/repos/{owner}/{repo}/issues/comments/:id")(readableUsersOnly { repository => + val maybeDeleteResponse: Option[Either[ActionResult, Option[Int]]] = + for { + commentId <- params("id").toIntOpt + comment <- getComment(repository.owner, repository.name, commentId.toString) + issue <- getIssue(repository.owner, repository.name, comment.issueId.toString) + } yield { + if (isEditable(repository.owner, repository.name, comment.commentedUserName)) { + val maybeDeletedComment = deleteCommentByApi(repository, comment, issue) + Right(maybeDeletedComment.map(_.commentId)) + } else { + Left(Unauthorized()) + } + } + maybeDeleteResponse + .map { + case Right(maybeDeletedCommentId) => maybeDeletedCommentId.getOrElse(NotFound()) + case Left(err) => err + } + .getOrElse(NotFound()) + }) private def isEditable(owner: String, repository: String, author: String)(implicit context: Context): Boolean = hasDeveloperRole(owner, repository, context.loginAccount) || author == context.loginAccount.get.userName diff --git a/src/main/scala/gitbucket/core/plugin/IssueHook.scala b/src/main/scala/gitbucket/core/plugin/IssueHook.scala index 4b026cc..8019665 100644 --- a/src/main/scala/gitbucket/core/plugin/IssueHook.scala +++ b/src/main/scala/gitbucket/core/plugin/IssueHook.scala @@ -13,6 +13,10 @@ implicit session: Session, context: Context ): Unit = () + def deletedComment(commentId: Int, issue: Issue, repository: RepositoryInfo)( + implicit session: Session, + context: Context + ): Unit = () def updatedComment(commentId: Int, content: String, issue: Issue, repository: RepositoryInfo)( implicit session: Session, context: Context diff --git a/src/main/scala/gitbucket/core/service/HandleCommentService.scala b/src/main/scala/gitbucket/core/service/HandleCommentService.scala index fc35896..512c843 100644 --- a/src/main/scala/gitbucket/core/service/HandleCommentService.scala +++ b/src/main/scala/gitbucket/core/service/HandleCommentService.scala @@ -1,7 +1,7 @@ package gitbucket.core.service import gitbucket.core.controller.Context -import gitbucket.core.model.Issue +import gitbucket.core.model.{Issue, IssueComment} import gitbucket.core.model.Profile.profile.blockingApi._ import gitbucket.core.model.activity.{ CloseIssueInfo, @@ -11,7 +11,8 @@ ReopenIssueInfo, ReopenPullRequestInfo } -import gitbucket.core.plugin.PluginRegistry +import gitbucket.core.plugin.{IssueHook, PluginRegistry} +import gitbucket.core.service.RepositoryService.RepositoryInfo import gitbucket.core.util.SyntaxSugars._ import gitbucket.core.util.Implicits._ @@ -135,6 +136,24 @@ } } + def deleteCommentByApi(repoInfo: RepositoryInfo, comment: IssueComment, issue: Issue)( + implicit context: Context, + s: Session + ): Option[IssueComment] = context.loginAccount.flatMap { _ => + comment.action match { + case "comment" => + val deleteResult = deleteComment(comment.issueId, comment.commentId) + val registry = PluginRegistry() + val hooks: Seq[IssueHook] = if (issue.isPullRequest) registry.getPullRequestHooks else registry.getIssueHooks + hooks.foreach(_.deletedComment(comment.commentId, issue, repoInfo)) + deleteResult match { + case n if n > 0 => Some(comment) + case _ => None + } + case _ => None + } + } + def updateCommentByApi( repository: RepositoryService.RepositoryInfo, issue: Issue,