Skip to content

Commit 85d6510

Browse files
committed
Fix create issue
Bug due to endpoint not accepting json values. Problem reported in: * https://bitbucket.org/site/master/issues/12099/error-creating-issue-through-api Also reported, and solutioned in: * https://bitbucket.org/site/master/issues/8620/the-repository-issues-components-not
1 parent f220324 commit 85d6510

File tree

8 files changed

+28
-22
lines changed

8 files changed

+28
-22
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.codacy.client.bitbucket.client
22

3-
import java.util.concurrent.{SynchronousQueue, ThreadPoolExecutor, TimeUnit}
4-
53
import com.codacy.client.bitbucket.util.HTTPStatusCodes
64
import com.ning.http.client.AsyncHttpClientConfig
5+
import java.util.concurrent.{SynchronousQueue, ThreadPoolExecutor, TimeUnit}
6+
import play.api.http.{ContentTypeOf, Writeable}
77
import play.api.libs.json.{JsValue, Json, Reads}
88
import play.api.libs.oauth._
99
import play.api.libs.ws.DefaultWSClientConfig
1010
import play.api.libs.ws.ning.{NingAsyncHttpClientConfigBuilder, NingWSClient}
11-
1211
import scala.concurrent.Await
1312
import scala.concurrent.duration.{Duration, SECONDS}
1413
import scala.util.{Failure, Properties, Success, Try}
@@ -54,7 +53,8 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
5453
/*
5554
* Does an API post
5655
*/
57-
def post[T](request: Request[T], values: JsValue)(implicit reader: Reads[T]): RequestResponse[T] = withClientRequest { client =>
56+
private def post[D,T](request: Request[T], values: D)(implicit reader: Reads[T], writer: Writeable[D], contentType: ContentTypeOf[D]): RequestResponse[T] = withClientRequest { client =>
57+
5858
val jpromise = client.url(request.url)
5959
.sign(requestSigner)
6060
.withFollowRedirects(follow = true)
@@ -78,6 +78,14 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
7878
value
7979
}
8080

81+
def postForm[T](request: Request[T], values: Map[String, Seq[String]])(implicit reader: Reads[T]): RequestResponse[T] = {
82+
post(request, values)
83+
}
84+
85+
def postJson[T](request: Request[T], values: JsValue)(implicit reader: Reads[T]): RequestResponse[T] = {
86+
post(request, values)
87+
}
88+
8189
/* copy paste from post ... */
8290
def delete[T](url: String): RequestResponse[Boolean] = withClientRequest { client =>
8391
val jpromise = client.url(url)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CommitServices(client: BitbucketClient) {
1414

1515
val values = JsObject(params.toSeq :+ "content" -> JsString(body))
1616

17-
client.post(Request(url, classOf[CommitComment]), values)
17+
client.postJson(Request(url, classOf[CommitComment]), values)
1818
}
1919

2020
def deleteComment(author: String, repo: String, commit: String, commentId: Long): Unit = {

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

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

3-
import com.codacy.client.bitbucket.{Webhook, Service}
3+
import com.codacy.client.bitbucket.Webhook
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
55
import play.api.libs.json.Json
66

@@ -19,7 +19,8 @@ class HookServices(client: BitbucketClient) {
1919
"url" -> hookUrl,
2020
"events" -> events
2121
)
22-
client.post(Request(servicesUrl, classOf[Webhook]), payload)
22+
23+
client.postJson(Request(servicesUrl, classOf[Webhook]), payload)
2324
}
2425

2526
def delete(author: String, repo: String, uuid: String): RequestResponse[Boolean] = {

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +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
65

76
class IssueServices(client: BitbucketClient) {
87

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

12-
val values = Json.obj(
13-
"title" -> title,
14-
"content" -> body
15-
)
10+
val url = s"https://api.bitbucket.org/1.0/repositories/$author/$repo/issues"
1611

17-
client.post(Request(url, classOf[Issue]), values)
12+
val values = Map("title" -> Seq(title), "content" -> Seq(body))
13+
14+
client.postForm(Request(url, classOf[Issue]), values)
1815
}
1916

2017
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ class PullRequestServices(client: BitbucketClient) {
4545
)
4646
)
4747

48-
client.post(Request(url, classOf[JsObject]), payload)
48+
client.postJson(Request(url, classOf[JsObject]), payload)
4949
}
5050

5151
def postApprove(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
5252
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/approve"
53-
client.post(Request(url, classOf[JsObject]), JsNull)
53+
client.postJson(Request(url, classOf[JsObject]), JsNull)
5454
}
5555

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

6161
def merge(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
6262
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/merge"
63-
client.post(Request(url, classOf[JsObject]), JsNull)
63+
client.postJson(Request(url, classOf[JsObject]), JsNull)
6464
}
6565

6666
def decline(owner: String, repository: String, prId: Long): RequestResponse[JsObject] = {
6767
val url = s"https://bitbucket.org/!api/2.0/repositories/$owner/$repository/pullrequests/$prId/decline"
68-
client.post(Request(url, classOf[JsObject]), JsNull)
68+
client.postJson(Request(url, classOf[JsObject]), JsNull)
6969
}
7070

7171
def createComment(author: String, repo: String, prId: Int, commitUUID: String, body: String, file: Option[String], line: Option[Int]): RequestResponse[PullRequestComment] = {
@@ -76,7 +76,7 @@ class PullRequestServices(client: BitbucketClient) {
7676

7777
val values = JsObject(params.toSeq :+ "content" -> JsString(body) :+ "anchor" -> JsString(commitUUID.take(12)))
7878

79-
client.post(Request(url, classOf[PullRequestComment]), values)
79+
client.postJson(Request(url, classOf[PullRequestComment]), values)
8080
}
8181

8282
def deleteComment(author: String, repo: String, commitUUID: String, pullRequestId: Int, commentId: Long): Unit = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class RepositoryServices(client: BitbucketClient) {
3333
"label" -> "Codacy Key"
3434
)
3535

36-
client.post(Request(url, classOf[SshKey]), values)
36+
client.postJson(Request(url, classOf[SshKey]), values)
3737
}
3838

3939
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class UrlServices(client: BitbucketClient) {
99
* Post to a api url
1010
*/
1111
def post(url: String): RequestResponse[JsObject] = {
12-
client.post(Request(url, classOf[JsObject]), JsNull)
12+
client.postJson(Request(url, classOf[JsObject]), JsNull)
1313
}
1414

1515
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class UserServices(client: BitbucketClient) {
3131
"label" -> "Codacy Key"
3232
)
3333

34-
client.post(Request(url, classOf[SshKey]), values)
34+
client.postJson(Request(url, classOf[SshKey]), values)
3535
}
3636

3737
}

0 commit comments

Comments
 (0)