diff --git a/src/main/scala/gitbucket/core/api/ApiComment.scala b/src/main/scala/gitbucket/core/api/ApiComment.scala index 47244f2..ba558f1 100644 --- a/src/main/scala/gitbucket/core/api/ApiComment.scala +++ b/src/main/scala/gitbucket/core/api/ApiComment.scala @@ -14,16 +14,16 @@ user: ApiUser, body: String, created_at: Date, - updated_at: Date)(repositoryName: RepositoryName, issueId: Int){ - val html_url = ApiPath(s"/${repositoryName.fullName}/issues/${issueId}#comment-${id}") + updated_at: Date)(repositoryName: RepositoryName, issueId: Int, issueOrPullRequest: IssueOrPullRequest){ + val html_url = ApiPath(s"/${repositoryName.fullName}/${issueOrPullRequest.html}/${issueId}#comment-${id}") } object ApiComment{ - def apply(comment: IssueComment, repositoryName: RepositoryName, issueId: Int, user: ApiUser): ApiComment = + def apply(comment: IssueComment, repositoryName: RepositoryName, issueId: Int, user: ApiUser, isPullRequest: Boolean): ApiComment = ApiComment( id = comment.commentId, user = user, body = comment.content, created_at = comment.registeredDate, - updated_at = comment.updatedDate)(repositoryName, issueId) + updated_at = comment.updatedDate)(repositoryName, issueId, IssueOrPullRequest(isPullRequest)) } diff --git a/src/main/scala/gitbucket/core/api/ApiIssue.scala b/src/main/scala/gitbucket/core/api/ApiIssue.scala index 45b5d62..629e0a9 100644 --- a/src/main/scala/gitbucket/core/api/ApiIssue.scala +++ b/src/main/scala/gitbucket/core/api/ApiIssue.scala @@ -17,9 +17,9 @@ state: String, created_at: Date, updated_at: Date, - body: String)(repositoryName: RepositoryName){ - val comments_url = ApiPath(s"/api/v3/repos/${repositoryName.fullName}/issues/${number}/comments") - val html_url = ApiPath(s"/${repositoryName.fullName}/issues/${number}") + body: String)(repositoryName: RepositoryName, issueOrPullRequest: IssueOrPullRequest){ + val comments_url = ApiPath(s"/api/v3/repos/${repositoryName.fullName}/${issueOrPullRequest.api}/${number}/comments") + val html_url = ApiPath(s"/${repositoryName.fullName}/${issueOrPullRequest.html}/${number}") } object ApiIssue{ @@ -31,5 +31,5 @@ state = if(issue.closed){ "closed" }else{ "open" }, body = issue.content.getOrElse(""), created_at = issue.registeredDate, - updated_at = issue.updatedDate)(repositoryName) + updated_at = issue.updatedDate)(repositoryName, IssueOrPullRequest(issue.isPullRequest)) } diff --git a/src/main/scala/gitbucket/core/api/IssueOrPullRequest.scala b/src/main/scala/gitbucket/core/api/IssueOrPullRequest.scala new file mode 100644 index 0000000..7f05f53 --- /dev/null +++ b/src/main/scala/gitbucket/core/api/IssueOrPullRequest.scala @@ -0,0 +1,5 @@ +package gitbucket.core.api + +case class IssueOrPullRequest(isPullRequest:Boolean){ + val (html, api) = if(isPullRequest){ ("pull","pulls") }else{ ("issues","issues") } +} \ No newline at end of file diff --git a/src/main/scala/gitbucket/core/controller/IssuesController.scala b/src/main/scala/gitbucket/core/controller/IssuesController.scala index 1bee1bf..ddd49fc 100644 --- a/src/main/scala/gitbucket/core/controller/IssuesController.scala +++ b/src/main/scala/gitbucket/core/controller/IssuesController.scala @@ -86,7 +86,7 @@ issueId <- params("id").toIntOpt comments = getCommentsForApi(repository.owner, repository.name, issueId.toInt) } yield { - JsonFormat(comments.map{ case (issueComment, user) => ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(user)) }) + JsonFormat(comments.map{ case (issueComment, user, issue) => ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(user), issue.isPullRequest) }) }).getOrElse(NotFound) }) @@ -190,7 +190,7 @@ (issue, id) <- handleComment(issueId, Some(body), repository)() issueComment <- getComment(repository.owner, repository.name, id.toString()) } yield { - JsonFormat(ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(context.loginAccount.get))) + JsonFormat(ApiComment(issueComment, RepositoryName(repository), issueId, ApiUser(context.loginAccount.get), issue.isPullRequest)) }) getOrElse NotFound }) diff --git a/src/main/scala/gitbucket/core/service/IssuesService.scala b/src/main/scala/gitbucket/core/service/IssuesService.scala index dca5931..67dde49 100644 --- a/src/main/scala/gitbucket/core/service/IssuesService.scala +++ b/src/main/scala/gitbucket/core/service/IssuesService.scala @@ -22,11 +22,13 @@ def getComments(owner: String, repository: String, issueId: Int)(implicit s: Session) = IssueComments filter (_.byIssue(owner, repository, issueId)) list - /** @return IssueComment and commentedUser */ - def getCommentsForApi(owner: String, repository: String, issueId: Int)(implicit s: Session): List[(IssueComment, Account)] = + /** @return IssueComment and commentedUser and Issue */ + def getCommentsForApi(owner: String, repository: String, issueId: Int)(implicit s: Session): List[(IssueComment, Account, Issue)] = IssueComments.filter(_.byIssue(owner, repository, issueId)) .filter(_.action inSetBind Set("comment" , "close_comment", "reopen_comment")) .innerJoin(Accounts).on( (t1, t2) => t1.commentedUserName === t2.userName ) + .innerJoin(Issues).on{ case ((t1, t2), t3) => t3.byIssue(t1.userName, t1.repositoryName, t1.issueId) } + .map{ case ((t1, t2), t3) => (t1, t2, t3) } .list def getComment(owner: String, repository: String, commentId: String)(implicit s: Session) = diff --git a/src/main/scala/gitbucket/core/service/WebHookService.scala b/src/main/scala/gitbucket/core/service/WebHookService.scala index 04d4d50..d3a1713 100644 --- a/src/main/scala/gitbucket/core/service/WebHookService.scala +++ b/src/main/scala/gitbucket/core/service/WebHookService.scala @@ -273,7 +273,7 @@ action = "created", repository = ApiRepository(repository, repositoryUser), issue = ApiIssue(issue, RepositoryName(repository), ApiUser(issueUser)), - comment = ApiComment(comment, RepositoryName(repository), issue.issueId, ApiUser(commentUser)), + comment = ApiComment(comment, RepositoryName(repository), issue.issueId, ApiUser(commentUser), issue.isPullRequest), sender = ApiUser(sender)) } } diff --git a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala index 6308d26..631da54 100644 --- a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala +++ b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala @@ -90,7 +90,7 @@ user = apiUser, body= "Me too", created_at= date1, - updated_at= date1)(RepositoryName("octocat","Hello-World"), 100) + updated_at= date1)(RepositoryName("octocat","Hello-World"), 100, IssueOrPullRequest(false)) val apiCommentJson = s"""{ "id": 1, "body": "Me too", @@ -100,6 +100,21 @@ "updated_at": "2011-04-14T16:00:49Z" }""" + val apiCommentPR = ApiComment( + id =1, + user = apiUser, + body= "Me too", + created_at= date1, + updated_at= date1)(RepositoryName("octocat","Hello-World"), 100, IssueOrPullRequest(true)) + val apiCommentPRJson = s"""{ + "id": 1, + "body": "Me too", + "user": $apiUserJson, + "html_url" : "${context.baseUrl}/octocat/Hello-World/pull/100#comment-1", + "created_at": "2011-04-14T16:00:49Z", + "updated_at": "2011-04-14T16:00:49Z" + }""" + val apiPersonIdent = ApiPersonIdent("Monalisa Octocat","support@example.com",date1) val apiPersonIdentJson = """ { "name": "Monalisa Octocat", @@ -158,7 +173,7 @@ state = "open", body = "I'm having a problem with this.", created_at = date1, - updated_at = date1)(RepositoryName("octocat","Hello-World")) + updated_at = date1)(RepositoryName("octocat","Hello-World"), IssueOrPullRequest(false)) val apiIssueJson = s"""{ "number": 1347, "state": "open", @@ -171,6 +186,26 @@ "updated_at": "2011-04-14T16:00:49Z" }""" + val apiIssuePR = ApiIssue( + number = 1347, + title = "Found a bug", + user = apiUser, + state = "open", + body = "I'm having a problem with this.", + created_at = date1, + updated_at = date1)(RepositoryName("octocat","Hello-World"), IssueOrPullRequest(true)) + val apiIssuePRJson = s"""{ + "number": 1347, + "state": "open", + "title": "Found a bug", + "body": "I'm having a problem with this.", + "user": $apiUserJson, + "comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/comments", + "html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347", + "created_at": "2011-04-14T16:00:49Z", + "updated_at": "2011-04-14T16:00:49Z" + }""" + val apiPullRequest = ApiPullRequest( number = 1347, updated_at = date1, @@ -264,6 +299,7 @@ } "apiComment" in { JsonFormat(apiComment) must beFormatted(apiCommentJson) + JsonFormat(apiCommentPR) must beFormatted(apiCommentPRJson) } "apiCommitListItem" in { JsonFormat(apiCommitListItem) must beFormatted(apiCommitListItemJson) @@ -276,6 +312,7 @@ } "apiIssue" in { JsonFormat(apiIssue) must beFormatted(apiIssueJson) + JsonFormat(apiIssuePR) must beFormatted(apiIssuePRJson) } "apiPullRequest" in { JsonFormat(apiPullRequest) must beFormatted(apiPullRequestJson)