Skip to content

Commit ca3de23

Browse files
committed
add getEmails for user
1 parent 72e1e21 commit ca3de23

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codacy.client.bitbucket
2+
3+
import play.api.libs.functional.syntax._
4+
import play.api.libs.json._
5+
6+
case class Email(email: String, primary: Boolean, active: Boolean)
7+
8+
object Email {
9+
implicit def emailReader: Reads[Email] =
10+
((__ \ "email").read[String] and
11+
(__ \ "primary").read[Boolean] and
12+
(__ \ "active").read[Boolean]
13+
) (Email.apply _)
14+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codacy.client.bitbucket.service
22

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

77
class UserServices(client: BitbucketClient) {
@@ -20,6 +20,13 @@ class UserServices(client: BitbucketClient) {
2020
client.execute(Request(s"https://bitbucket.org/api/1.0/users/$username", classOf[User]))
2121
}
2222

23+
/*
24+
* Gets all the emails of an account
25+
*/
26+
def getEmails(username: String): RequestResponse[Seq[Email]] = {
27+
client.execute(Request(s"https://bitbucket.org/api/1.0/users/$username/emails", classOf[Seq[Email]]))
28+
}
29+
2330
/*
2431
* Creates a ssh key
2532
*/

src/test/scala/RemoteRepositorySpecs.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.scalatest._
22
import org.scalatest.Matchers
33
import play.api.libs.json.Json
4-
import com.codacy.client.bitbucket.{PullRequest, SimpleRepository}
4+
import com.codacy.client.bitbucket.{Email, PullRequest, SimpleRepository}
55
import com.codacy.client.bitbucket.PullRequest._
66

77
class RemoteRepositorySpecs extends FlatSpec with Matchers {
@@ -506,4 +506,28 @@ class RemoteRepositorySpecs extends FlatSpec with Matchers {
506506
repo => repo.length shouldBe 1
507507
)
508508
}
509+
510+
it should "successfully parse a JSON into an array of Email" in {
511+
val input =
512+
"""[
513+
|{
514+
| "active": true,
515+
| "email": "[email protected]",
516+
| "primary": true
517+
|},
518+
|{
519+
| "active": false,
520+
| "email": "[email protected]",
521+
| "primary": false
522+
|}
523+
|]
524+
""".stripMargin
525+
val json = Json.parse(input)
526+
val value = json.validate[Seq[Email]]
527+
528+
value.fold(e =>
529+
fail(s"$e"),
530+
emails => emails.length shouldBe 2
531+
)
532+
}
509533
}

0 commit comments

Comments
 (0)