Skip to content

Commit 80584d5

Browse files
authored
Merge branch 'master' into update-readme-version
2 parents fefaa8b + bdd7406 commit 80584d5

File tree

9 files changed

+82
-12
lines changed

9 files changed

+82
-12
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Dependencies._
22

33
name := """bitbucket-scala-client"""
44

5-
version := "1.8.0-SNAPSHOT"
5+
version := "1.9.0-SNAPSHOT"
66

77
scalaVersion := "2.10.5"
88

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/PullRequest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import play.api.libs.functional.syntax._
55
import play.api.libs.json._
66

77
case class PullRequest(id: Long, title: String, description: String,
8-
authorUsername: Option[String], authorAvatar: Option[String],
9-
state: String, created_on: DateTime, updated_on: DateTime,
8+
authorUsername: Option[String], authorAvatar: Option[String], state: String, created_on: DateTime, updated_on: DateTime,
109
sourceRepository: String, sourceBranch: String, sourceCommit: String,
1110
destRepository: String, destBranch: String, destCommit: Option[String],
12-
apiUrls: Seq[ApiUrl]) {
11+
apiUrls: Seq[ApiUrl], authorUUID: Option[String] = None) {
1312
val url = s"https://bitbucket.org/$destRepository/pull-request/$id"
1413
}
1514

@@ -59,7 +58,8 @@ object PullRequest {
5958
(__ \ "destination" \ "branch" \ "name").read[String] and
6059
(__ \ "destination" \ "commit" \ "hash").readNullable[String] and
6160
// TODO: (__ \ "destination" \ "commit" \ "hash").read[Option[String]] and
62-
(__ \ "links").read[Map[String, Map[String, String]]].map(parseLinks)
61+
(__ \ "links").read[Map[String, Map[String, String]]].map(parseLinks) and
62+
(__ \ "author" \ "uuid").readNullable[String]
6363
) (PullRequest.apply _)
6464

6565
private def parseLinks(links: Map[String, Map[String, String]]): Seq[ApiUrl] = {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.codacy.client.bitbucket
2+
3+
import play.api.libs.json._
4+
5+
case class PullRequestReviewers(reviewers: Seq[String])
6+
7+
object PullRequestReviewers {
8+
9+
implicit val reader: Reads[PullRequestReviewers] =
10+
(__ \ "reviewers" ).read(Reads.list((__ \ "uuid").read[String])).map(PullRequestReviewers.apply)
11+
12+
}

src/main/scala/com/codacy/client/bitbucket/client/Authentication.scala

Lines changed: 7 additions & 0 deletions
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/PullRequestServices.scala

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

33
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
44
import com.codacy.client.bitbucket.util.CommitHelper
5-
import com.codacy.client.bitbucket.{PullRequest, PullRequestComment, SimpleCommit, SimplePullRequestComment}
5+
import com.codacy.client.bitbucket.{PullRequest, PullRequestComment, SimpleCommit, SimplePullRequestComment, PullRequestReviewers}
66
import play.api.libs.json._
77

88
class PullRequestServices(client: BitbucketClient) {
@@ -100,4 +100,10 @@ class PullRequestServices(client: BitbucketClient) {
100100
client.execute(Request(url, classOf[Seq[SimplePullRequestComment]]))
101101
}
102102

103+
def getPullRequestsReviewers(owner: String, repository: String, prId: Long): RequestResponse[PullRequestReviewers] = {
104+
val url = s"https://bitbucket.org/api/2.0/repositories/$owner/$repository/pullrequests/$prId"
105+
106+
client.execute(Request(url, classOf[PullRequestReviewers]))
107+
}
108+
103109
}

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 10 additions & 3 deletions
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,15 +20,22 @@ 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
*/
26-
def createKey(username: String, key: String): RequestResponse[SshKey] = {
33+
def createKey(username: String, key: String, keyName: String = "Codacy Key"): RequestResponse[SshKey] = {
2734
val url = s"https://bitbucket.org/api/1.0/users/$username/ssh-keys"
2835

2936
val values = Json.obj(
3037
"key" -> key,
31-
"label" -> "Codacy Key"
38+
"label" -> keyName
3239
)
3340

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

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)