Skip to content

Commit c15a34b

Browse files
committed
Add missing endpoints for Api v2
1 parent 53f97f4 commit c15a34b

13 files changed

+259
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import java.time.LocalDateTime
4+
5+
import play.api.libs.functional.syntax._
6+
import play.api.libs.json._
7+
8+
case class CommitComment(id: Long, commit: String, content: String, created_on: LocalDateTime, deleted: Boolean)
9+
10+
object CommitComment {
11+
implicit val reader: Reads[CommitComment] = (
12+
(__ \ "id").read[Long] and
13+
(__ \ "commit" \ "hash").read[String] and
14+
(__ \ "content" \ "raw").read[String] and
15+
(__ \ "created_on").read[LocalDateTime] and
16+
(__ \ "deleted").read[Boolean]
17+
)(CommitComment.apply _)
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import play.api.libs.functional.syntax._
4+
import play.api.libs.json._
5+
6+
case class Email(email: String, is_primary: Boolean, is_confirmed: Boolean)
7+
8+
object Email {
9+
implicit def emailReader: Reads[Email] =
10+
((__ \ "email").read[String] and
11+
(__ \ "is_primary").read[Boolean] and
12+
(__ \ "is_confirmed").read[Boolean]
13+
) (Email.apply _)
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import java.time.LocalDateTime
4+
5+
import play.api.libs.functional.syntax._
6+
import play.api.libs.json._
7+
8+
case class Issue(id: Long, state: String, priority: String, title: String, content: String, reporter: String,
9+
created_on: LocalDateTime, kind: String)
10+
11+
object Issue {
12+
implicit val reader: Reads[Issue] = (
13+
(__ \ "id").read[Long] and
14+
(__ \ "state").read[String] and
15+
(__ \ "priority").read[String] and
16+
(__ \ "title").read[String] and
17+
(__ \ "content" \ "raw").read[String] and
18+
(__ \ "reporter" \ "username").read[String] and
19+
(__ \ "created_on").read[LocalDateTime] and
20+
(__ \ "kind").read[String]
21+
)(Issue.apply _)
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import java.time.LocalDateTime
4+
5+
import play.api.libs.functional.syntax._
6+
import play.api.libs.json._
7+
8+
case class PullRequestComment(id: Long, content: String, created_on: LocalDateTime, deleted: Boolean)
9+
10+
object PullRequestComment {
11+
implicit val reader: Reads[PullRequestComment] = (
12+
(__ \ "id").read[Long] and
13+
(__ \ "content" \ "raw").read[String] and
14+
(__ \ "created_on").read[LocalDateTime] and
15+
(__ \ "deleted").read[Boolean]
16+
)(PullRequestComment.apply _)
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import play.api.libs.json._
4+
5+
case class PullRequestReviewers(reviewers: Seq[String])
6+
7+
object PullRequestReviewers {
8+
9+
implicit val reader: Reads[PullRequestReviewers] =
10+
(__ \ "reviewers" ).read(Reads.list((__ \ "uuid").read[String])).map(PullRequestReviewers.apply)
11+
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import java.time.LocalDateTime
4+
5+
import play.api.libs.json._
6+
7+
case class SimpleCommit(hash: String, authorName: Option[String], parents: Seq[String], date: LocalDateTime, message: String)
8+
9+
object SimpleCommit {
10+
val dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
11+
implicit val dateTimeReads: Reads[LocalDateTime] = Reads.localDateTimeReads(dateFormat, s=> {println(s);s})
12+
13+
implicit def commitReader: Reads[SimpleCommit] = Reads { (json: JsValue) =>
14+
(for {
15+
hash <- (json \ "hash").asOpt[String]
16+
username = (json \ "author" \ "user" \ "username").asOpt[String]
17+
parents = (json \ "parents" \\ "hash").flatMap(_.asOpt[String])
18+
date <- (json \ "date").asOpt[LocalDateTime]
19+
message <- (json \ "message").asOpt[String]
20+
} yield SimpleCommit(hash, username, parents, date, message))
21+
.map(JsSuccess(_))
22+
.getOrElse(JsError("could not read commit"))
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import play.api.libs.functional.syntax._
4+
import play.api.libs.json._
5+
6+
case class SshKey(uuid: String, key: String, label: String)
7+
8+
object SshKey {
9+
implicit val reader: Reads[SshKey] = (
10+
(__ \ "uuid").read[String] and
11+
(__ \ "key").read[String] and
12+
(__ \ "label").read[String]
13+
)(SshKey.apply _)
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import play.api.libs.functional.syntax._
4+
import play.api.libs.json._
5+
6+
case class User(username: String, display_name: String)
7+
8+
object User {
9+
implicit val reader: Reads[User] = (
10+
(__ \ "username").read[String] and
11+
(__ \ "display_name").read[String]
12+
)(User.apply _)
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import play.api.libs.json.{Reads, Json, JsObject}
4+
5+
case class Webhook(uuid:String,description:Option[String],url:String,subject:JsObject,events:Set[String],active:Boolean,created_at:String,links:JsObject)
6+
object Webhook{
7+
implicit val reads: Reads[Webhook] = Json.reads[Webhook].map{ case hook =>
8+
hook.uuid match{
9+
case uuid if uuid.startsWith("{") && uuid.endsWith("}") =>
10+
hook.copy(uuid = uuid.drop(1).dropRight(1))
11+
case _ => hook
12+
}
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.codacy.client.bitbucket.v2.service
2+
3+
import com.codacy.client.bitbucket.v2.CommitComment
4+
import com.codacy.client.client.{BitbucketClient, Request, RequestResponse}
5+
import play.api.libs.json.{JsNumber, JsObject, JsString, Json}
6+
7+
class CommitServices(client: BitbucketClient) {
8+
9+
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/2.0/repositories/$author/$repo/commit/$commit/comments"
11+
12+
val params = for {
13+
filename <- file
14+
lineTo <- line
15+
} yield {
16+
"inline" -> Json.obj("path" -> JsString(filename), "to" -> JsNumber(lineTo))
17+
}
18+
19+
val values = JsObject(params.toSeq :+ "content" -> Json.obj("raw" -> JsString(body)))
20+
21+
client.postJson(Request(url, classOf[CommitComment]), values)
22+
}
23+
24+
def listComments(author: String, repo: String, commit: String): RequestResponse[Seq[CommitComment]] = {
25+
val url = s"https://bitbucket.org/api/2.0/repositories/$author/$repo/commit/$commit/comments"
26+
27+
client.executePaginated(Request(url, classOf[Seq[CommitComment]]))
28+
.map(_.filterNot(_.deleted))
29+
}
30+
31+
def deleteComment(author: String, repo: String, commit: String, commentId: Long): RequestResponse[Boolean] = {
32+
val url = s"https://bitbucket.org/api/2.0/repositories/$author/$repo/commit/$commit/comments/$commentId"
33+
34+
client.delete(url)
35+
}
36+
}

0 commit comments

Comments
 (0)