Skip to content

Commit 48108cb

Browse files
authored
Merge pull request #26 from codacy/bitbucket-comments-ft-3108
Add endpoint to list comments
2 parents 1751287 + dfad848 commit 48108cb

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codacy.client.bitbucket
2+
3+
import play.api.libs.functional.syntax._
4+
import play.api.libs.json._
5+
6+
case class SimplePullRequestComment(id: Long, anchor: Option[String])
7+
8+
object SimplePullRequestComment {
9+
10+
implicit val reader: Reads[SimplePullRequestComment] = (
11+
(__ \ "comment_id").read[Long] and
12+
(__ \ "anchor").readNullable[String]
13+
)(SimplePullRequestComment.apply _)
14+
}

src/main/scala/com/codacy/client/bitbucket/service/CommitServices.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.codacy.client.bitbucket.service
22

33
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
4+
import com.codacy.client.bitbucket.util.CommitHelper
45
import com.codacy.client.bitbucket.{CommitComment, PullRequestComment}
56
import play.api.libs.json.{JsNumber, JsObject, JsString}
67

78
class CommitServices(client: BitbucketClient) {
89

910
def createComment(author: String, repo: String, commit: String, body: String, file: Option[String] = None, line: Option[Int] = None): RequestResponse[CommitComment] = {
10-
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/changesets/${commit.take(12)}/comments"
11+
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/changesets/${CommitHelper.anchor(commit)}/comments"
1112

1213
val params = file.map(filename => "filename" -> JsString(filename)) ++
1314
line.map(lineTo => "line_to" -> JsNumber(lineTo))
@@ -18,7 +19,7 @@ class CommitServices(client: BitbucketClient) {
1819
}
1920

2021
def deleteComment(author: String, repo: String, commit: String, commentId: Long): Unit = {
21-
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/changesets/${commit.take(12)}/comments/$commentId"
22+
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/changesets/${CommitHelper.anchor(commit)}/comments/$commentId"
2223

2324
client.delete(url)
2425
}

src/main/scala/com/codacy/client/bitbucket/service/PullRequestServices.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.codacy.client.bitbucket.service
22

33
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
4-
import com.codacy.client.bitbucket.{PullRequestComment, PullRequest, SimpleCommit}
4+
import com.codacy.client.bitbucket.util.CommitHelper
5+
import com.codacy.client.bitbucket.{PullRequest, PullRequestComment, SimpleCommit, SimplePullRequestComment}
56
import play.api.libs.json._
67

78
class PullRequestServices(client: BitbucketClient) {
@@ -78,11 +79,12 @@ class PullRequestServices(client: BitbucketClient) {
7879
postNewComment(author, repo, prId, values)
7980
}
8081

81-
def createComment(author: String, repo: String, prId: Int, commitUUID: String, body: String, file: Option[String], line: Option[Int]): RequestResponse[PullRequestComment] = {
82+
def createLineComment(author: String, repo: String, prId: Int, commitUUID: String, body: String,
83+
file: Option[String], line: Option[Int]): RequestResponse[PullRequestComment] = {
8284
val params = file.map(filename => "filename" -> JsString(filename)) ++
8385
line.map(lineTo => "line_to" -> JsNumber(lineTo))
8486

85-
val values = JsObject(params.toSeq :+ "content" -> JsString(body) :+ "anchor" -> JsString(commitUUID.take(12)))
87+
val values = JsObject(params.toSeq :+ "content" -> JsString(body) :+ "anchor" -> JsString(CommitHelper.anchor(commitUUID)))
8688
postNewComment(author, repo, prId, values = values)
8789
}
8890

@@ -92,4 +94,10 @@ class PullRequestServices(client: BitbucketClient) {
9294
client.delete(url)
9395
}
9496

97+
def listComments(author: String, repo: String, pullRequestId: Int): RequestResponse[Seq[SimplePullRequestComment]] = {
98+
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/pullrequests/$pullRequestId/comments"
99+
100+
client.execute(Request(url, classOf[Seq[SimplePullRequestComment]]))
101+
}
102+
95103
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codacy.client.bitbucket.util
2+
3+
object CommitHelper {
4+
def anchor(commitUUID: String): String = {
5+
commitUUID.take(12)
6+
}
7+
}

0 commit comments

Comments
 (0)