Skip to content

Commit 0c4372e

Browse files
author
Johann Egger
committed
create list update for services
1 parent 400ee05 commit 0c4372e

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,36 @@ class BitbucketClient(key: String, secretKey: String, token: String, secretToken
7474
RequestResponse(None, result.statusText, hasError = true)
7575
}
7676
}
77+
/* copy paste from post ... */
78+
def delete[T](request: Request[T])(implicit reader: Reads[T]): RequestResponse[T] = {
79+
val client: WSClient = new NingWSClient(new AsyncHttpClient().getConfig)
80+
81+
val jpromise = client.url(request.url).sign(OAuthCalculator(KEY, TOKEN)).withFollowRedirects(follow = true).delete()
82+
val result = Await.result(jpromise, Duration(10, SECONDS))
83+
84+
if (Seq(HTTPStatusCodes.OK, HTTPStatusCodes.CREATED).contains(result.status)) {
85+
val body = result.body
86+
87+
/* TODO: remove this when not needed (only keep for debug purposes) */
88+
// println("\n\n")
89+
// println(s"STATUS: ${result.status}")
90+
// println("\n\n")
91+
// println(body)
92+
// println("\n\n")
93+
94+
val jsValue = parseJson(body)
95+
jsValue match {
96+
case Right(responseObj) =>
97+
RequestResponse(responseObj.asOpt[T])
98+
case Left(message) =>
99+
RequestResponse(None, message = message.detail, hasError = true)
100+
}
101+
} else {
102+
RequestResponse(None, result.statusText, hasError = true)
103+
}
104+
}
105+
106+
77107

78108
private def get(url: String): Either[ResponseError, JsValue] = {
79109
val client: WSClient = new NingWSClient(new AsyncHttpClient().getConfig)

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,36 @@ package com.codacy.client.bitbucket.service
33
import com.codacy.client.bitbucket.Service
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
55

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

8-
def createPostPushHook(author: String, repo: String, hookUrl:String): RequestResponse[Service] = {
9-
val url = s"https://bitbucket.org/!api/1.0/repositories/$author/$repo/services"
8+
def list(author: String, repo: String): RequestResponse[Seq[Service]] =
9+
AuthorRepoOps(author,repo).list()
1010

11-
val payload = Map(
12-
"type" -> "POST",
13-
"URL" -> hookUrl
14-
).mapValues(Seq(_))
11+
/*def byId(author: String, repo: String, id:Long) = {
12+
val resp = AuthorRepoOps(author,repo).list()
13+
val value = resp.value.map(_.find(_.id == id))
14+
resp.copy(value = value)
15+
}*/
1516

16-
client.post(Request(url, classOf[Service]), payload)
17+
def created(author: String, repo: String, payload: Map[String,Seq[String]]) =
18+
AuthorRepoOps(author,repo).created(payload)
19+
20+
def removed(author: String, repo: String, id:Long) =
21+
AuthorRepoOps(author,repo).removed(id)
22+
23+
private[this] lazy val BASE_URL: String = "https://bitbucket.org/!api/1.0/repositories"
24+
private[this] def servicesUrl(author: String, repo: String) = s"$BASE_URL/$author/$repo/services"
25+
26+
private[this] case class AuthorRepoOps(author: String, repo: String){
27+
private[this] lazy val BASE_URL = servicesUrl(author,repo)
28+
29+
def created(payload: Map[String,Seq[String]]): RequestResponse[Service] =
30+
client.post(Request(BASE_URL,classOf[Service]),payload)
31+
32+
def removed(id:Long): RequestResponse[Boolean] =
33+
client.delete(Request(s"%BASE_URL/$id",classOf[Boolean]))
34+
35+
def list(): RequestResponse[Seq[Service]] =
36+
client.execute(Request(BASE_URL,classOf[Seq[Service]]))
1737
}
1838
}

0 commit comments

Comments
 (0)