Skip to content

Commit a0c71b2

Browse files
committed
more generic post & pr create
1 parent 179ebc5 commit a0c71b2

File tree

8 files changed

+61
-40
lines changed

8 files changed

+61
-40
lines changed

src/main/scala/com/codacy/client/bitbucket/client/BitbucketClient.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
4747
/*
4848
* Does an API post
4949
*/
50-
def post[T](request: Request[T], values: Map[String, Seq[String]])(implicit reader: Reads[T]): RequestResponse[T] = {
50+
def post[T](request: Request[T], values: JsValue)(implicit reader: Reads[T]): RequestResponse[T] = {
5151
val client: WSClient = new NingWSClient(new AsyncHttpClient().getConfig)
5252

5353
val jpromise = client.url(request.url).sign(OAuthCalculator(KEY, TOKEN)).withFollowRedirects(follow = true).post(values)

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

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

33
import com.codacy.client.bitbucket.CommitComment
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
5+
import play.api.libs.json.Json
56

67
class CommitServices(client: BitbucketClient) {
78

89
def createComment(author: String, repo: String, commit: String, body: String): RequestResponse[CommitComment] = {
910
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/changesets/${commit.take(12)}/comments"
1011

11-
val values = Map(
12-
"content" -> Seq(body)
13-
)
12+
val values = Json.obj("content" -> body)
1413

1514
client.post(Request(url, classOf[CommitComment]), values)
1615
}

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@ package com.codacy.client.bitbucket.service
22

33
import com.codacy.client.bitbucket.Service
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
5+
import play.api.libs.json.Json
56

6-
class ServiceServices(client: BitbucketClient) {
7+
class HookServices(client: BitbucketClient) {
78

8-
def list(author: String, repo: String): RequestResponse[Seq[Service]] =
9-
AuthorRepoOps(author,repo).list()
10-
11-
def created(author: String, repo: String, payload: Map[String,Seq[String]]) =
12-
AuthorRepoOps(author,repo).created(payload)
13-
14-
def removed(author: String, repo: String, id:Long): RequestResponse[Boolean] =
15-
AuthorRepoOps(author,repo).removed(id)
9+
def list(author: String, repo: String): RequestResponse[Seq[Service]] = {
10+
val servicesUrl = getServicesUrl(author, repo)
11+
client.execute(Request(servicesUrl, classOf[Seq[Service]]))
12+
}
1613

17-
private[this] lazy val BASE_URL: String = "https://bitbucket.org/!api/1.0/repositories"
18-
private[this] def servicesUrl(author: String, repo: String) = s"$BASE_URL/$author/$repo/services"
14+
def create(author: String, repo: String, hookType: String, hookUrl: String): RequestResponse[Service] = {
15+
val servicesUrl = getServicesUrl(author, repo)
16+
val payload = Json.obj(
17+
"type" -> hookType,
18+
"URL" -> hookUrl
19+
)
20+
client.post(Request(servicesUrl, classOf[Service]), payload)
21+
}
1922

20-
private[this] case class AuthorRepoOps(author: String, repo: String){
21-
private[this] lazy val BASE_URL = servicesUrl(author,repo)
23+
def delete(author: String, repo: String, id: Long): RequestResponse[Boolean] = {
24+
val servicesUrl = getServicesUrl(author, repo)
25+
client.delete(s"$servicesUrl/$id")
26+
}
2227

23-
def created(payload: Map[String,Seq[String]]): RequestResponse[Service] =
24-
client.post(Request(BASE_URL,classOf[Service]),payload)
28+
private lazy val BASE_URL: String = "https://bitbucket.org/!api/1.0/repositories"
2529

26-
def removed(id:Long): RequestResponse[Boolean] = {
27-
client.delete(s"$BASE_URL/$id")
28-
}
30+
private def getServicesUrl(author: String, repo: String) = s"$BASE_URL/$author/$repo/services"
2931

30-
def list(): RequestResponse[Seq[Service]] =
31-
client.execute(Request(BASE_URL,classOf[Seq[Service]]))
32-
}
3332
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package com.codacy.client.bitbucket.service
22

33
import com.codacy.client.bitbucket.Issue
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
5+
import play.api.libs.json.Json
56

67
class IssueServices(client: BitbucketClient) {
78

89
def createIssue(author: String, repo: String, title: String, body: String): RequestResponse[Issue] = {
910
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/issues"
1011

11-
val values = Map(
12-
"title" -> Seq(title),
13-
"content" -> Seq(body)
12+
val values = Json.obj(
13+
"title" -> title,
14+
"content" -> body
1415
)
1516

1617
client.post(Request(url, classOf[Issue]), values)

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.codacy.client.bitbucket.service
22

33
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
44
import com.codacy.client.bitbucket.{Commit, PullRequest}
5-
import play.api.libs.json.JsObject
5+
import play.api.libs.json.{JsNull, JsObject, Json}
66

77
class PullRequestServices(client: BitbucketClient) {
88

@@ -28,9 +28,29 @@ class PullRequestServices(client: BitbucketClient) {
2828
client.executePaginated(Request(url, classOf[Seq[Commit]]))
2929
}
3030

31+
def create(owner: String, repository: String, title: String, sourceBranch: String, destinationBranch: String): RequestResponse[JsObject] = {
32+
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests"
33+
34+
val payload = Json.obj(
35+
"title" -> title,
36+
"source" -> Json.obj(
37+
"branch" -> Json.obj(
38+
"name" -> sourceBranch
39+
)
40+
),
41+
"destination" -> Json.obj(
42+
"branch" -> Json.obj(
43+
"name" -> destinationBranch
44+
)
45+
)
46+
)
47+
48+
client.post(Request(url, classOf[JsObject]), payload)
49+
}
50+
3151
def postApprove(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
3252
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/approve"
33-
client.post(Request(url, classOf[JsObject]), Map.empty)
53+
client.post(Request(url, classOf[JsObject]), JsNull)
3454
}
3555

3656
def deleteApprove(owner: String, repository: String, prId: Long): RequestResponse[Boolean] = {
@@ -40,12 +60,12 @@ class PullRequestServices(client: BitbucketClient) {
4060

4161
def merge(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
4262
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/merge"
43-
client.post(Request(url, classOf[JsObject]), Map.empty)
63+
client.post(Request(url, classOf[JsObject]), JsNull)
4464
}
4565

4666
def decline(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
4767
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/decline"
48-
client.post(Request(url, classOf[JsObject]), Map.empty)
68+
client.post(Request(url, classOf[JsObject]), JsNull)
4969
}
5070

5171
}

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

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

33
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
44
import com.codacy.client.bitbucket.{Repository, SimpleRepository, SshKey}
5+
import play.api.libs.json.Json
56

67
class RepositoryServices(client: BitbucketClient) {
78

@@ -27,9 +28,9 @@ class RepositoryServices(client: BitbucketClient) {
2728
def createKey(username: String, repo: String, key: String): RequestResponse[SshKey] = {
2829
val url = s"https://bitbucket.org/!api/1.0/repositories/$username/$repo/deploy-keys"
2930

30-
val values = Map(
31-
"key" -> Seq(key),
32-
"label" -> Seq("Codacy Key")
31+
val values = Json.obj(
32+
"key" -> key,
33+
"label" -> "Codacy Key"
3334
)
3435

3536
client.post(Request(url, classOf[SshKey]), values)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.codacy.client.bitbucket.service
22

33
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
4-
import play.api.libs.json.JsObject
4+
import play.api.libs.json.{JsNull, JsObject}
55

66
class UrlServices(client: BitbucketClient) {
77

88
/*
99
* Post to a api url
1010
*/
1111
def post(url: String): RequestResponse[JsObject] = {
12-
client.post(Request(url, classOf[JsObject]), Map.empty)
12+
client.post(Request(url, classOf[JsObject]), JsNull)
1313
}
1414

1515
}

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

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

33
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
44
import com.codacy.client.bitbucket.{SshKey, User}
5+
import play.api.libs.json.Json
56

67
class UserServices(client: BitbucketClient) {
78

@@ -25,9 +26,9 @@ class UserServices(client: BitbucketClient) {
2526
def createKey(username: String, key: String): RequestResponse[SshKey] = {
2627
val url = s"https://bitbucket.org/!api/1.0/users/$username/ssh-keys"
2728

28-
val values = Map(
29-
"key" -> Seq(key),
30-
"label" -> Seq("Codacy Key")
29+
val values = Json.obj(
30+
"key" -> key,
31+
"label" -> "Codacy Key"
3132
)
3233

3334
client.post(Request(url, classOf[SshKey]), values)

0 commit comments

Comments
 (0)