Skip to content

Commit 50eaaa7

Browse files
authored
Merge pull request codacy#44 from codacy/migrate-endpoints-to-api2.0
Add some missing endpoints
2 parents e749ec7 + b9548b5 commit 50eaaa7

File tree

8 files changed

+131
-70
lines changed

8 files changed

+131
-70
lines changed

src/main/scala/com/codacy/client/bitbucket/v1/Webhook.scala

-14
This file was deleted.

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

-46
This file was deleted.

src/main/scala/com/codacy/client/bitbucket/v2/Repository.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import play.api.libs.json._
88
case class Repository(name: String, full_name: String, description: String, scm: String,
99
created_on: LocalDateTime, updated_on: LocalDateTime, owner: String, size: Long,
1010
has_issues: Boolean, is_private: Boolean, language: String,
11-
url: Seq[RepositoryUrl])
11+
urls: Seq[RepositoryUrl])
1212

1313
object Repository {
1414
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"
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 Team(username: String, display_name: String)
7+
8+
object Team {
9+
implicit val reader: Reads[Team] = (
10+
(__ \ "team" \ "username").read[String] and
11+
(__ \ "team" \ "display_name").read[String]
12+
)(Team.apply _)
13+
}

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class PullRequestServices(client: BitbucketClient) {
2828
client.executePaginated(Request(url, classOf[Seq[SimpleCommit]]))
2929
}
3030

31+
private[this] def postNewComment(owner: String, repo: String, prId: Int, values: JsObject): RequestResponse[PullRequestComment] = {
32+
val url = s"https://bitbucket.org/api/2.0/repositories/$owner/$repo/pullrequests/$prId/comments"
33+
client.postJson(Request(url, classOf[PullRequestComment]), values)
34+
}
35+
3136
def create(owner: String, repository: String, title: String, sourceBranch: String, destinationBranch: String): RequestResponse[JsObject] = {
3237
val url = s"https://bitbucket.org/api/2.0/repositories/$owner/$repository/pullrequests"
3338

@@ -68,19 +73,22 @@ class PullRequestServices(client: BitbucketClient) {
6873
client.postJson(Request(url, classOf[JsObject]), JsNull)
6974
}
7075

71-
def createComment(author: String, repo: String, prId: Int, body: String,
76+
def createLineComment(author: String, repo: String, prId: Int, body: String,
7277
file: Option[String], line: Option[Int]): RequestResponse[PullRequestComment] = {
73-
val url = s"https://bitbucket.org/api/2.0/repositories/$author/$repo/pullrequests/$prId/comments"
74-
7578
val params = for {
7679
filename <- file
7780
lineTo <- line
7881
} yield {
7982
"inline" -> Json.obj("path" -> JsString(filename), "to" -> JsNumber(lineTo))
8083
}
8184

82-
val values = JsObject(params.toSeq :+ "content" -> Json.obj("raw" -> JsString(body)))//, "anchor" -> JsString(CommitHelper.anchor(commitUUID))))
83-
client.postJson(Request(url, classOf[PullRequestComment]), values)
85+
val values = JsObject(params.toSeq :+ "content" -> Json.obj("raw" -> JsString(body)))
86+
postNewComment(author, repo, prId, values)
87+
}
88+
89+
def createPullRequestComment(author: String, repo: String, prId: Int, content: String): RequestResponse[PullRequestComment] = {
90+
val values = Json.obj("content" -> JsString(content))
91+
postNewComment(author, repo, prId, values)
8492
}
8593

8694
def deleteComment(author: String, repo: String, pullRequestId: Int, commentId: Long): RequestResponse[Boolean] = {
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.codacy.client.bitbucket.v2.service
22

3-
import com.codacy.client.bitbucket.v2.User
3+
import com.codacy.client.bitbucket.v2.Team
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
55

66
class TeamServices(client: BitbucketClient) {
77

8-
def list: RequestResponse[Seq[User]] = {
9-
client.executePaginated(Request(s"https://bitbucket.org/api/2.0/teams", classOf[Seq[User]]))
8+
def list(role: String = "member"): RequestResponse[Seq[Team]] = {
9+
client.executePaginated(Request(s"https://bitbucket.org/api/2.0/teams?role=$role", classOf[Seq[Team]]))
1010
}
1111

1212
}

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.codacy.client.bitbucket.v2.service
22

3-
import com.codacy.client.bitbucket.v2.{Email, SshKey, User}
3+
import com.codacy.client.bitbucket.v2.{Email, SshKey, Team, User}
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
55
import play.api.libs.json.Json
66

@@ -27,6 +27,13 @@ class UserServices(client: BitbucketClient) {
2727
client.executePaginated(Request(s"https://bitbucket.org/api/2.0/user/emails", classOf[Seq[Email]]))
2828
}
2929

30+
/*
31+
* Gets all the teams a user is a member of
32+
*/
33+
def getTeams: RequestResponse[Seq[Team]] = {
34+
client.executePaginated(Request(s"https://bitbucket.org/api/2.0/user/permissions/teams", classOf[Seq[Team]]))
35+
}
36+
3037
/*
3138
* Creates a ssh key
3239
*/

src/test/scala/com/codacy/client/bitbucket/v2/UserSpecs.scala

+93
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,97 @@ class UserSpecs extends FlatSpec with Matchers {
5050
emails => emails.length shouldBe 3
5151
)
5252
}
53+
it should "successfully parse a JSON into an array of Team" in {
54+
val input = """
55+
|[
56+
|{
57+
| "permission": "collaborator",
58+
| "type": "team_permission",
59+
| "user": {
60+
| "username": "jllopes",
61+
| "display_name": "João Lopes",
62+
| "account_id": "123asdfas87afsd8f9asd7as",
63+
| "links": {
64+
| "self": {
65+
| "href": "https://bitbucket.org/!api/2.0/users/jllopes"
66+
| },
67+
| "html": {
68+
| "href": "https://bitbucket.org/jllopes/"
69+
| },
70+
| "avatar": {
71+
| "href": "https://bitbucket.org/account/jllopes/avatar/"
72+
| }
73+
| },
74+
| "nickname": "jllopes",
75+
| "type": "user",
76+
| "uuid": "{42417371-64fe-4cde-b528-b7454e8a0aaf}"
77+
|},
78+
| "team": {
79+
| "username": "testteam1",
80+
| "display_name": "TestTeam1",
81+
| "type": "team",
82+
| "uuid": "{85ea8027-2a7f-4094-828d-f20c935d373a}",
83+
| "links": {
84+
| "self": {
85+
| "href": "https://bitbucket.org/!api/2.0/teams/testteam1"
86+
| },
87+
| "html": {
88+
| "href": "https://bitbucket.org/testteam1/"
89+
| },
90+
| "avatar": {
91+
| "href": "https://bitbucket.org/account/testteam1/avatar/"
92+
| }
93+
| }
94+
| }
95+
|},
96+
|{
97+
| "permission": "collaborator",
98+
| "type": "team_permission",
99+
| "user": {
100+
| "username": "jllopes",
101+
| "display_name": "João Lopes",
102+
| "account_id": "123asdfas87afsd8f9asd7as",
103+
| "links": {
104+
| "self": {
105+
| "href": "https://bitbucket.org/!api/2.0/users/jllopes"
106+
| },
107+
| "html": {
108+
| "href": "https://bitbucket.org/jllopes/"
109+
| },
110+
| "avatar": {
111+
| "href": "https://bitbucket.org/account/jllopes/avatar/"
112+
| }
113+
| },
114+
| "nickname": "jllopes",
115+
| "type": "user",
116+
| "uuid": "{42417371-64fe-4cde-b528-b7454e8a0aaf}"
117+
|},
118+
| "team": {
119+
| "username": "testteam2",
120+
| "display_name": "TestTeam2",
121+
| "type": "team",
122+
| "uuid": "{7ec1171b-3df1-4c00-bfcf-2d4eda99e44a}",
123+
| "links": {
124+
| "self": {
125+
| "href": "https://bitbucket.org/!api/2.0/teams/testteam2"
126+
| },
127+
| "html": {
128+
| "href": "https://bitbucket.org/testteam2/"
129+
| },
130+
| "avatar": {
131+
| "href": "https://bitbucket.org/account/testteam2/avatar/"
132+
| }
133+
| }
134+
| }
135+
|}
136+
|]
137+
""".stripMargin
138+
val json = Json.parse(input)
139+
val value = json.validate[Seq[Team]]
140+
141+
value.fold(e =>
142+
fail(s"$e"),
143+
teams => teams.length shouldBe 2
144+
)
145+
}
53146
}

0 commit comments

Comments
 (0)