
com.codacy.client.bitbucket.service.PullRequestServices.scala Maven / Gradle / Ivy
package com.codacy.client.bitbucket.service
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
import com.codacy.client.bitbucket.{PullRequestComment, PullRequest, SimpleCommit}
import play.api.libs.json._
class PullRequestServices(client: BitbucketClient) {
/*
* Gets the list of a repository pull requests
*
* States: OPEN | MERGED | DECLINED
*
*/
def getPullRequests(owner: String, repository: String, states: Seq[String] = Seq("OPEN")): RequestResponse[Seq[PullRequest]] = {
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests?state=${states.mkString(",")}"
client.executePaginated(Request(url, classOf[Seq[PullRequest]]))
}
/*
* Gets the list of commits of a pull request
*
*/
def getPullRequestCommits(owner: String, repository: String, prId: Long): RequestResponse[Seq[SimpleCommit]] = {
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/commits"
client.executePaginated(Request(url, classOf[Seq[SimpleCommit]]))
}
def create(owner: String, repository: String, title: String, sourceBranch: String, destinationBranch: String): RequestResponse[JsObject] = {
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests"
val payload = Json.obj(
"title" -> title,
"source" -> Json.obj(
"branch" -> Json.obj(
"name" -> sourceBranch
)
),
"destination" -> Json.obj(
"branch" -> Json.obj(
"name" -> destinationBranch
)
)
)
client.post(Request(url, classOf[JsObject]), payload)
}
def postApprove(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/approve"
client.post(Request(url, classOf[JsObject]), JsNull)
}
def deleteApprove(owner: String, repository: String, prId: Long): RequestResponse[Boolean] = {
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/approve"
client.delete(url)
}
def merge(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/merge"
client.post(Request(url, classOf[JsObject]), JsNull)
}
def decline(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/decline"
client.post(Request(url, classOf[JsObject]), JsNull)
}
def createComment(author: String, repo: String, prId: Int, commitUUID: String, body: String, file: Option[String], line: Option[Int]): RequestResponse[PullRequestComment] = {
val url = s"https://bitbucket.org/api/1.0/repositories/$author/$repo/pullrequests/$prId/comments"
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)))
client.post(Request(url, classOf[PullRequestComment]), values)
}
def deleteComment(author: String, repo: String, commitUUID: String, pullRequestId: Int, commentId: Long): Unit = {
val url = s"https://bitbucket.org/api/1.0/repositories/$author/$repo/pullrequests/$pullRequestId/comments/$commentId"
client.delete(url)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy