Skip to content

Add endpoint to list comments #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.codacy.client.bitbucket

import play.api.libs.functional.syntax._
import play.api.libs.json._

case class SimplePullRequestComment(id: Long, anchor: Option[String])

object SimplePullRequestComment {

implicit val reader: Reads[SimplePullRequestComment] = (
(__ \ "comment_id").read[Long] and
(__ \ "anchor").readNullable[String]
)(SimplePullRequestComment.apply _)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.codacy.client.bitbucket.service

import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
import com.codacy.client.bitbucket.util.CommitHelper
import com.codacy.client.bitbucket.{CommitComment, PullRequestComment}
import play.api.libs.json.{JsNumber, JsObject, JsString}

class CommitServices(client: BitbucketClient) {

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

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

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

client.delete(url)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.codacy.client.bitbucket.service

import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
import com.codacy.client.bitbucket.{PullRequestComment, PullRequest, SimpleCommit}
import com.codacy.client.bitbucket.util.CommitHelper
import com.codacy.client.bitbucket.{PullRequest, PullRequestComment, SimpleCommit, SimplePullRequestComment}
import play.api.libs.json._

class PullRequestServices(client: BitbucketClient) {
Expand Down Expand Up @@ -78,11 +79,12 @@ class PullRequestServices(client: BitbucketClient) {
postNewComment(author, repo, prId, values)
}

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

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

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

def listComments(author: String, repo: String, pullRequestId: Int): RequestResponse[Seq[SimplePullRequestComment]] = {
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/pullrequests/$pullRequestId/comments"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?pagelen=100

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok thx!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pagination is for v2, and unfortunately v2 no longer has the "anchor" that I need.. Even bitbucket themselves use their v1 API for comments when showing a PR online 😭


client.execute(Request(url, classOf[Seq[SimplePullRequestComment]]))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codacy.client.bitbucket.util

object CommitHelper {
def anchor(commitUUID: String): String = {
commitUUID.take(12)
}
}