mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-15 09:55:49 +01:00
adding support for GitHub v3 delete comment (#2540)
This commit is contained in:
@@ -4,6 +4,7 @@ import gitbucket.core.controller.{Context, ControllerBase}
|
||||
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 @@ trait ApiIssueCommentControllerBase extends ControllerBase {
|
||||
|
||||
/*
|
||||
* 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
|
||||
|
||||
@@ -13,6 +13,10 @@ trait IssueHook {
|
||||
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
|
||||
|
||||
@@ -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 @@ import gitbucket.core.model.activity.{
|
||||
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 @@ trait HandleCommentService {
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user