Skip to content

Commit bdd7406

Browse files
authored
Merge pull request #37 from mw-ding/master
Add an OAuth2Credentials and getEmails
2 parents c6e0e20 + bc7b547 commit bdd7406

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed
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/client/Authentication.scala

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ object Authentication {
1515

1616
case class OAuthCredentials(key: String, secretKey: String, token: String, secretToken: String) extends Credentials
1717

18+
case class OAuth2Credentials(accessToken: String) extends Credentials
19+
1820
/**
1921
* Your username and password | app password.
2022
*/
@@ -29,6 +31,7 @@ object Authentication {
2931
def fromCredentials(credentials: Credentials): Authenticator = {
3032
credentials match {
3133
case c: OAuthCredentials => new OAuthAuthenticator(c)
34+
case c: OAuth2Credentials => new OAuth2Authenticator(c)
3235
case c: BasicAuthCredentials => new BasicAuthAuthenticator(c)
3336
}
3437
}
@@ -43,6 +46,10 @@ object Authentication {
4346
def authenticate(req: WSRequest): WSRequest = req.sign(requestSigner)
4447
}
4548

49+
class OAuth2Authenticator(credentials: OAuth2Credentials) extends Authenticator {
50+
override def authenticate(req: WSRequest): WSRequest = req.withQueryString("access_token" -> credentials.accessToken)
51+
}
52+
4653
class BasicAuthAuthenticator(credentials: BasicAuthCredentials) extends Authenticator {
4754
def authenticate(req: WSRequest): WSRequest = req.withAuth(credentials.username, credentials.password, WSAuthScheme.BASIC)
4855
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class RepositoryServices(client: BitbucketClient) {
2525
/*
2626
* Creates a ssh key
2727
*/
28-
def createKey(username: String, repo: String, key: String): RequestResponse[SshKey] = {
28+
def createKey(username: String, repo: String, key: String, label: String = "Codacy Key"): RequestResponse[SshKey] = {
2929
val url = s"https://bitbucket.org/api/1.0/repositories/$username/$repo/deploy-keys"
3030

3131
val values = Json.obj(
3232
"key" -> key,
33-
"label" -> "Codacy Key"
33+
"label" -> label
3434
)
3535

3636
client.postJson(Request(url, classOf[SshKey]), values)

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

+8-1
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

+25-1
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)