diff --git a/src/main/scala/gitbucket/core/controller/api/ApiIssueCommentControllerBase.scala b/src/main/scala/gitbucket/core/controller/api/ApiIssueCommentControllerBase.scala index 574d16e..353f570 100644 --- a/src/main/scala/gitbucket/core/controller/api/ApiIssueCommentControllerBase.scala +++ b/src/main/scala/gitbucket/core/controller/api/ApiIssueCommentControllerBase.scala @@ -75,9 +75,40 @@ }) /* - * v. Edit a comment - * https://developer.github.com/v3/issues/comments/#edit-a-comment + * v. Update an issue comment + * https://docs.github.com/en/rest/reference/issues#update-an-issue-comment */ + patch("/api/v3/repos/:owner/:repository/issues/comments/:id")(readableUsersOnly { repository => + val commentId = params("id") + val result = for { + issueComment <- getComment(repository.owner, repository.name, commentId) + issue <- getIssue(repository.owner, repository.name, issueComment.issueId.toString) + } yield { + if (isEditable(repository.owner, repository.name, issueComment.commentedUserName)) { + val body = extractFromJsonBody[CreateAComment].map(_.body) + updateCommentByApi(repository, issue, issueComment.commentId.toString, body) + getComment(repository.owner, repository.name, commentId) match { + case Some(issueComment) => + JsonFormat( + ApiComment( + issueComment, + RepositoryName(repository), + issue.issueId, + ApiUser(context.loginAccount.get), + issue.isPullRequest + ) + ) + case _ => + } + } else { + Unauthorized() + } + } + result match { + case Some(response) => response + case None => NotFound() + } + }) /* * vi. Delete a comment diff --git a/src/main/scala/gitbucket/core/plugin/IssueHook.scala b/src/main/scala/gitbucket/core/plugin/IssueHook.scala index 1b22971..4b026cc 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 updatedComment(commentId: Int, content: String, issue: Issue, repository: RepositoryInfo)( + implicit session: Session, + context: Context + ): Unit = () def closed(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = () def reopened(issue: Issue, repository: RepositoryInfo)(implicit session: Session, context: Context): Unit = () def assigned( diff --git a/src/main/scala/gitbucket/core/service/HandleCommentService.scala b/src/main/scala/gitbucket/core/service/HandleCommentService.scala index 0417ca7..fc35896 100644 --- a/src/main/scala/gitbucket/core/service/HandleCommentService.scala +++ b/src/main/scala/gitbucket/core/service/HandleCommentService.scala @@ -134,4 +134,42 @@ } } } + + def updateCommentByApi( + repository: RepositoryService.RepositoryInfo, + issue: Issue, + commentId: String, + content: Option[String] + )(implicit context: Context, s: Session): Option[(Issue, Int)] = { + context.loginAccount.flatMap { loginAccount => + defining(repository.owner, repository.name) { + case (owner, name) => + val userName = loginAccount.userName + content match { + case Some(content) => + // Update comment + val _commentId = Some(updateComment(issue.issueId, commentId.toInt, content)) + // Record comment activity + val commentInfo = if (issue.isPullRequest) { + PullRequestCommentInfo(owner, name, userName, content, issue.issueId) + } else { + IssueCommentInfo(owner, name, userName, content, issue.issueId) + } + recordActivity(commentInfo) + // extract references and create refer comment + createReferComment(owner, name, issue, content, loginAccount) + // call web hooks + commentId foreach (callIssueCommentWebHook(repository, issue, _, loginAccount, context.settings)) + // call hooks + if (issue.isPullRequest) + PluginRegistry().getPullRequestHooks + .foreach(_.updatedComment(commentId.toInt, content, issue, repository)) + else + PluginRegistry().getIssueHooks.foreach(_.updatedComment(commentId.toInt, content, issue, repository)) + _commentId.map(issue -> _) + case _ => None + } + } + } + } }