Skip to content

Commit b24d7d4

Browse files
committed
Store more info from user object on case class
1 parent c79b3ac commit b24d7d4

File tree

8 files changed

+417
-20
lines changed

8 files changed

+417
-20
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ case class Issue(
1111
priority: String,
1212
title: String,
1313
content: String,
14-
reporter: String,
14+
reporter: User,
1515
created_on: LocalDateTime,
1616
kind: String
1717
)
@@ -24,7 +24,7 @@ object Issue {
2424
(__ \ "priority").read[String] and
2525
(__ \ "title").read[String] and
2626
(__ \ "content" \ "raw").read[String] and
27-
(__ \ "reporter" \ "username").read[String] and
27+
(__ \ "reporter").read[User] and
2828
(__ \ "created_on").read[LocalDateTime] and
2929
(__ \ "kind").read[String]
3030
)(Issue.apply _)

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ case class PullRequest(
99
id: Long,
1010
title: String,
1111
description: String,
12-
authorUsername: Option[String],
13-
authorAvatar: Option[String],
12+
author: User,
1413
state: String,
1514
created_on: LocalDateTime,
1615
updated_on: LocalDateTime,
@@ -20,8 +19,7 @@ case class PullRequest(
2019
destRepository: String,
2120
destBranch: String,
2221
destCommit: Option[String],
23-
apiUrls: Seq[ApiUrl],
24-
authorUUID: Option[String] = None
22+
apiUrls: Seq[ApiUrl]
2523
) {
2624
val url = s"https://bitbucket.org/$destRepository/pull-request/$id"
2725
}
@@ -61,8 +59,7 @@ object PullRequest {
6159
(__ \ "id").read[Long] and
6260
(__ \ "title").read[String] and
6361
(__ \ "description").read[String] and
64-
(__ \ "author" \ "username").readNullable[String] and
65-
(__ \ "author" \ "links" \ "avatar" \ "href").readNullable[String].orElse((__ \ "author" \ "links").readNullable[String]) and
62+
(__ \ "author").read[User] and
6663
(__ \ "state").read[String] and
6764
(__ \ "created_on").read[LocalDateTime] and
6865
(__ \ "updated_on").read[LocalDateTime] and
@@ -73,8 +70,7 @@ object PullRequest {
7370
(__ \ "destination" \ "branch" \ "name").read[String] and
7471
(__ \ "destination" \ "commit" \ "hash").readNullable[String] and
7572
// TODO: (__ \ "destination" \ "commit" \ "hash").read[Option[String]] and
76-
(__ \ "links").read[Map[String, Map[String, String]]].map(parseLinks) and
77-
(__ \ "author" \ "uuid").readNullable[String]
73+
(__ \ "links").read[Map[String, Map[String, String]]].map(parseLinks)
7874
) (PullRequest.apply _)
7975
// format: on
8076

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package com.codacy.client.bitbucket.v2
22

33
import java.time.LocalDateTime
44

5+
import com.codacy.client.bitbucket.v2.Repository.Owner
56
import play.api.libs.functional.syntax._
67
import play.api.libs.json._
8+
import play.twirl.api.JavaScriptFormat
79

810
case class Repository(
911
name: String,
@@ -12,7 +14,7 @@ case class Repository(
1214
scm: String,
1315
created_on: LocalDateTime,
1416
updated_on: LocalDateTime,
15-
owner: String,
17+
owner: Owner,
1618
size: Long,
1719
has_issues: Boolean,
1820
is_private: Boolean,
@@ -37,7 +39,7 @@ object Repository {
3739
(__ \ "scm").read[String] and
3840
(__ \ "created_on").read[LocalDateTime] and
3941
(__ \ "updated_on").read[LocalDateTime] and
40-
(__ \ "owner" \ "username").read[String] and
42+
(__ \ "owner").read[Owner] and
4143
(__ \ "size").read[Long] and
4244
(__ \ "has_issues").read[Boolean] and
4345
(__ \ "is_private").read[Boolean] and
@@ -47,6 +49,13 @@ object Repository {
4749
}
4850
// format: on
4951

52+
final case class Owner(nickname: Option[String], username: Option[String], display_name: String, `type`: String)
53+
54+
object Owner {
55+
implicit val reader: Reads[Owner] = Json.format[Owner]
56+
}
57+
58+
5059
private def parseLinks(links: Map[String, JsValue]): Seq[RepositoryUrl] = {
5160
links.flatMap {
5261
case (linkName, linkMap) =>

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package com.codacy.client.bitbucket.v2
33
import play.api.libs.functional.syntax._
44
import play.api.libs.json._
55

6-
case class User(username: String, display_name: String)
6+
case class User(account_id: String, uuid: String, display_name: String, nickname: Option[String], avatarUrl: Option[String])
77

88
object User {
99
// format: off
1010
implicit val reader: Reads[User] = (
11-
(__ \ "username").read[String] and
12-
(__ \ "display_name").read[String]
11+
(__ \ "account_id").read[String] and
12+
(__ \ "uuid").read[String] and
13+
(__ \ "display_name").read[String] and
14+
(__ \ "nickname").readNullable[String] and
15+
(__ \ "links" \ "avatar" \ "href").readNullable[String]
1316
)(User.apply _)
1417
// format: on
1518
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class UserServices(client: BitbucketClient) {
1616
/*
1717
* Gets the basic information associated with an account.
1818
*/
19-
def getUser(username: String): RequestResponse[User] = {
20-
client.execute(Request(s"https://api.bitbucket.org/2.0/users/$username", classOf[User]))
19+
def getUser(userId: String): RequestResponse[User] = {
20+
client.execute(Request(s"https://api.bitbucket.org/2.0/users/$userId", classOf[User]))
2121
}
2222

2323
/*
@@ -37,8 +37,8 @@ class UserServices(client: BitbucketClient) {
3737
/*
3838
* Creates a ssh key
3939
*/
40-
def createKey(username: String, key: String, keyName: String): RequestResponse[SshKey] = {
41-
val url = s"https://bitbucket.org/api/2.0/users/$username/ssh-keys"
40+
def createKey(userId: String, key: String, keyName: String): RequestResponse[SshKey] = {
41+
val url = s"https://bitbucket.org/api/2.0/users/$userId/ssh-keys"
4242

4343
val values = Json.obj("key" -> key, "label" -> keyName)
4444

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
import play.api.libs.json.Json
5+
6+
class PullRequestSpecs extends FlatSpec with Matchers {
7+
"PullRequestSpecs" should "successfully parse PullRequest" in {
8+
val input =
9+
"""
10+
|{
11+
| "description":"README.md edited online with Bitbucket",
12+
| "links":
13+
| {
14+
| "decline":
15+
| {
16+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3\/decline"
17+
| },
18+
| "commits":
19+
| {
20+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3\/commits"
21+
| },
22+
| "self":
23+
| {
24+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3"
25+
| },
26+
| "comments":
27+
| {
28+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3\/comments"
29+
| },
30+
| "merge":
31+
| {
32+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3\/merge"
33+
| },
34+
| "html":
35+
| {
36+
| "href":"https:\/\/bitbucket.org\/jllopes\/test2\/pull-requests\/3"
37+
| },
38+
| "activity":
39+
| {
40+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3\/activity"
41+
| },
42+
| "diff":
43+
| {
44+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3\/diff"
45+
| },
46+
| "approve":
47+
| {
48+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3\/approve"
49+
| },
50+
| "statuses":
51+
| {
52+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/pullrequests\/3\/statuses"
53+
| }
54+
| },
55+
| "title":"README.md edited online with Bitbucket",
56+
| "close_source_branch":true,
57+
| "merge_commit":null,
58+
| "destination":
59+
| {
60+
| "commit":
61+
| {
62+
| "type":"commit",
63+
| "hash":"a1dbc5a0d643",
64+
| "links":
65+
| {
66+
| "self":
67+
| {
68+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/commit\/a1dbc5a0d643"
69+
| },
70+
| "html":
71+
| {
72+
| "href":"https:\/\/bitbucket.org\/jllopes\/test2\/commits\/a1dbc5a0d643"
73+
| }
74+
| }
75+
| },
76+
| "branch":
77+
| {
78+
| "name":"jllopes\/asdf2"
79+
| },
80+
| "repository":
81+
| {
82+
| "full_name":"jllopes\/test2",
83+
| "type":"repository",
84+
| "name":"test2",
85+
| "links":
86+
| {
87+
| "self":
88+
| {
89+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2"
90+
| },
91+
| "html":
92+
| {
93+
| "href":"https:\/\/bitbucket.org\/jllopes\/test2"
94+
| },
95+
| "avatar":
96+
| {
97+
| "href":"https:\/\/bytebucket.org\/ravatar\/%7B2eaf162b-90f7-4a63-b42f-a53381685ceb%7D?ts=default"
98+
| }
99+
| },
100+
| "uuid":"{2eaf162b-90f7-4a63-b42f-a53381685ceb}"
101+
| }
102+
| },
103+
| "comment_count":0,
104+
| "closed_by":null,
105+
| "source":
106+
| {
107+
| "commit":
108+
| {
109+
| "type":"commit",
110+
| "hash":"096bc35a6ba0",
111+
| "links":
112+
| {
113+
| "self":
114+
| {
115+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2\/commit\/096bc35a6ba0"
116+
| },
117+
| "html":
118+
| {
119+
| "href":"https:\/\/bitbucket.org\/jllopes\/test2\/commits\/096bc35a6ba0"
120+
| }
121+
| }
122+
| },
123+
| "branch":
124+
| {
125+
| "name":"asdf5"
126+
| },
127+
| "repository":
128+
| {
129+
| "full_name":"jllopes\/test2",
130+
| "type":"repository",
131+
| "name":"test2",
132+
| "links":
133+
| {
134+
| "self":
135+
| {
136+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes\/test2"
137+
| },
138+
| "html":
139+
| {
140+
| "href":"https:\/\/bitbucket.org\/jllopes\/test2"
141+
| },
142+
| "avatar":
143+
| {
144+
| "href":"https:\/\/bytebucket.org\/ravatar\/%7B2eaf162b-90f7-4a63-b42f-a53381685ceb%7D?ts=default"
145+
| }
146+
| },
147+
| "uuid":"{2eaf162b-90f7-4a63-b42f-a53381685ceb}"
148+
| }
149+
| },
150+
| "created_on":"2019-01-15T15:14:16.752685+00:00",
151+
| "state":"OPEN",
152+
| "task_count":0,
153+
| "reason":"",
154+
| "updated_on":"2019-01-31T11:28:53.675721+00:00",
155+
| "author":
156+
| {
157+
| "username":"jllopes",
158+
| "website":"",
159+
| "display_name":"João Lopes",
160+
| "account_id":"123abc456def789ghi101jkl",
161+
| "links":
162+
| {
163+
| "hooks":
164+
| {
165+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/users\/jllopes\/hooks"
166+
| },
167+
| "self":
168+
| {
169+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/users\/jllopes"
170+
| },
171+
| "repositories":
172+
| {
173+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/repositories\/jllopes"
174+
| },
175+
| "html":
176+
| {
177+
| "href":"https:\/\/bitbucket.org\/jllopes\/"
178+
| },
179+
| "followers":
180+
| {
181+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/users\/jllopes\/followers"
182+
| },
183+
| "avatar":
184+
| {
185+
| "href":"https:\/\/bitbucket.org\/account\/jllopes\/avatar\/"
186+
| },
187+
| "following":
188+
| {
189+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/users\/jllopes\/following"
190+
| },
191+
| "snippets":
192+
| {
193+
| "href":"https:\/\/bitbucket.org\/!api\/2.0\/snippets\/jllopes"
194+
| }
195+
| },
196+
| "type":"user",
197+
| "created_on":"2018-07-02T10:41:55.342788+00:00",
198+
| "is_staff":false,
199+
| "location":null,
200+
| "account_status":"active",
201+
| "nickname":"jllopes",
202+
| "uuid":"{c19f822b-0e29-433a-87a5-ec8ace58aa67}"
203+
| },
204+
| "summary":
205+
| {
206+
| "raw":"README.md edited online with Bitbucket",
207+
| "markup":"markdown",
208+
| "html":"<p>README.md edited online with Bitbucket<\/p>",
209+
| "type":"rendered"
210+
| },
211+
| "type":"pullrequest",
212+
| "id":3
213+
|}""".stripMargin
214+
215+
val json = Json.parse(input)
216+
val value = json.validate[PullRequest]
217+
218+
value.fold(e => fail(s"$e"), pr => {
219+
pr.id shouldBe 3
220+
pr.author.account_id shouldBe "123abc456def789ghi101jkl"
221+
})
222+
}
223+
224+
}

0 commit comments

Comments
 (0)