Skip to content

Commit 1ea14ef

Browse files
committed
Add endpoint to get user teams
1 parent 0231cfe commit 1ea14ef

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed
Lines changed: 13 additions & 0 deletions
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/UserServices.scala

Lines changed: 8 additions & 1 deletion
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

Lines changed: 93 additions & 0 deletions
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)