Skip to content

Commit d9b3d85

Browse files
committed
Merge pull request #6 from codacy/feature/changesetComments
Add comments on changesets and PR endpoints
2 parents c70c060 + 9f492f4 commit d9b3d85

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.codacy.client.bitbucket
2+
3+
import org.joda.time.DateTime
4+
import play.api.libs.functional.syntax._
5+
import play.api.libs.json._
6+
7+
case class PullRequestComment(id: Long, username: String, display_name: String, content: String, created_on: DateTime)
8+
9+
object PullRequestComment {
10+
val dateFormat = "yyyy-MM-dd HH:mm:ssZZ"
11+
implicit val jodaDateTimeReads = Reads.jodaDateReads(dateFormat)
12+
13+
implicit val reader: Reads[PullRequestComment] = (
14+
(__ \ "comment_id").read[Long] and
15+
(__ \ "username").read[String] and
16+
(__ \ "display_name").read[String] and
17+
(__ \ "content").read[String] and
18+
(__ \ "utc_created_on").read[DateTime]
19+
)(PullRequestComment.apply _)
20+
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package com.codacy.client.bitbucket.service
22

3-
import com.codacy.client.bitbucket.CommitComment
43
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
5-
import play.api.libs.json.Json
4+
import com.codacy.client.bitbucket.{CommitComment, PullRequestComment}
5+
import play.api.libs.json.{JsNumber, JsObject, JsString}
66

77
class CommitServices(client: BitbucketClient) {
88

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

12-
val values = Json.obj("content" -> body)
12+
val params = file.map(filename => "filename" -> JsString(filename)) ++
13+
line.map(lineTo => "line_to" -> JsNumber(lineTo))
14+
15+
val values = JsObject(params.toSeq :+ "content" -> JsString(body))
1316

1417
client.post(Request(url, classOf[CommitComment]), values)
1518
}
1619

17-
}
20+
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+
23+
client.delete(url)
24+
}
25+
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +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.{PullRequest, SimpleCommit}
5-
import play.api.libs.json.{JsNull, JsObject, Json}
4+
import com.codacy.client.bitbucket.{PullRequestComment, PullRequest, SimpleCommit}
5+
import play.api.libs.json._
66

77
class PullRequestServices(client: BitbucketClient) {
88

@@ -68,4 +68,21 @@ class PullRequestServices(client: BitbucketClient) {
6868
client.post(Request(url, classOf[JsObject]), JsNull)
6969
}
7070

71+
def createComment(author: String, repo: String, prId: Int, commitUUID: String, body: String, file: Option[String], line: Option[Int]): RequestResponse[PullRequestComment] = {
72+
val url = s"https://bitbucket.org/api/1.0/repositories/$author/$repo/pullrequests/$prId/comments"
73+
74+
val params = file.map(filename => "filename" -> JsString(filename)) ++
75+
line.map(lineTo => "line_to" -> JsNumber(lineTo))
76+
77+
val values = JsObject(params.toSeq :+ "content" -> JsString(body) :+ "anchor" -> JsString(commitUUID.take(12)))
78+
79+
client.post(Request(url, classOf[PullRequestComment]), values)
80+
}
81+
82+
def deleteComment(author: String, repo: String, commitUUID: String, pullRequestId: Int, commentId: Long): Unit = {
83+
val url = s"https://bitbucket.org/api/1.0/repositories/$author/$repo/pullrequests/$pullRequestId/comments/$commentId"
84+
85+
client.delete(url)
86+
}
87+
7188
}

0 commit comments

Comments
 (0)