fix pull-request url on webhook payload

This commit is contained in:
nazoking
2015-10-02 00:23:13 +09:00
parent 75d085a2c4
commit 0283ec574d
7 changed files with 59 additions and 15 deletions

View File

@@ -14,16 +14,16 @@ case class ApiComment(
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))
}

View File

@@ -17,9 +17,9 @@ case class ApiIssue(
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 @@ object ApiIssue{
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))
}

View File

@@ -0,0 +1,5 @@
package gitbucket.core.api
case class IssueOrPullRequest(isPullRequest:Boolean){
val (html, api) = if(isPullRequest){ ("pull","pulls") }else{ ("issues","issues") }
}

View File

@@ -86,7 +86,7 @@ trait IssuesControllerBase extends ControllerBase {
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 @@ trait IssuesControllerBase extends ControllerBase {
(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
})

View File

@@ -22,11 +22,13 @@ trait IssuesService {
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) =

View File

@@ -273,7 +273,7 @@ object WebHookService {
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))
}
}

View File

@@ -90,7 +90,7 @@ class JsonFormatSpec extends Specification {
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 @@ class JsonFormatSpec extends Specification {
"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 @@ class JsonFormatSpec extends Specification {
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 @@ class JsonFormatSpec extends Specification {
"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 @@ class JsonFormatSpec extends Specification {
}
"apiComment" in {
JsonFormat(apiComment) must beFormatted(apiCommentJson)
JsonFormat(apiCommentPR) must beFormatted(apiCommentPRJson)
}
"apiCommitListItem" in {
JsonFormat(apiCommitListItem) must beFormatted(apiCommitListItemJson)
@@ -276,6 +312,7 @@ class JsonFormatSpec extends Specification {
}
"apiIssue" in {
JsonFormat(apiIssue) must beFormatted(apiIssueJson)
JsonFormat(apiIssuePR) must beFormatted(apiIssuePRJson)
}
"apiPullRequest" in {
JsonFormat(apiPullRequest) must beFormatted(apiPullRequestJson)