Skip to content

Commit 14b422a

Browse files
authored
Merge pull request #50 from codacy/change-username-for-uuid
Change username for UUID - FT-6455
2 parents c79b3ac + 3664f3f commit 14b422a

File tree

12 files changed

+584
-115
lines changed

12 files changed

+584
-115
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: 16 additions & 15 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
}
@@ -43,7 +41,13 @@ object ApiUrlType extends Enumeration {
4341
}
4442
}
4543

46-
case class ApiUrl(urlType: ApiUrlType.Value, link: String)
44+
case class ApiUrl(urlType: ApiUrlType.Value, link: Link)
45+
46+
case class Link(href: String)
47+
48+
object Link {
49+
implicit val reader: Reads[Link] = Json.format[Link]
50+
}
4751

4852
object PullRequest {
4953
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"
@@ -61,8 +65,7 @@ object PullRequest {
6165
(__ \ "id").read[Long] and
6266
(__ \ "title").read[String] and
6367
(__ \ "description").read[String] and
64-
(__ \ "author" \ "username").readNullable[String] and
65-
(__ \ "author" \ "links" \ "avatar" \ "href").readNullable[String].orElse((__ \ "author" \ "links").readNullable[String]) and
68+
(__ \ "author").read[User] and
6669
(__ \ "state").read[String] and
6770
(__ \ "created_on").read[LocalDateTime] and
6871
(__ \ "updated_on").read[LocalDateTime] and
@@ -73,16 +76,14 @@ object PullRequest {
7376
(__ \ "destination" \ "branch" \ "name").read[String] and
7477
(__ \ "destination" \ "commit" \ "hash").readNullable[String] and
7578
// TODO: (__ \ "destination" \ "commit" \ "hash").read[Option[String]] and
76-
(__ \ "links").read[Map[String, Map[String, String]]].map(parseLinks) and
77-
(__ \ "author" \ "uuid").readNullable[String]
79+
(__ \ "links").read[Map[String, Link]].map(parseLinks)
7880
) (PullRequest.apply _)
7981
// format: on
8082

81-
private def parseLinks(links: Map[String, Map[String, String]]): Seq[ApiUrl] = {
82-
(for {
83-
(linkName, linkMap) <- links
84-
urlType <- ApiUrlType.find(linkName)
85-
linkUrl <- linkMap.get("href")
86-
} yield ApiUrl(urlType, linkUrl)).toSeq
83+
private def parseLinks(links: Map[String, Link]): Seq[ApiUrl] = {
84+
links.flatMap {
85+
case (linkTypeStr, link) =>
86+
ApiUrlType.find(linkTypeStr).map(ApiUrl(_, link))
87+
}(collection.breakOut)
8788
}
8889
}

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

Lines changed: 18 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,12 @@ 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+
5058
private def parseLinks(links: Map[String, JsValue]): Seq[RepositoryUrl] = {
5159
links.flatMap {
5260
case (linkName, linkMap) =>
@@ -79,3 +87,11 @@ object RepositoryUrlType extends Enumeration {
7987
}
8088

8189
case class RepositoryUrl(urlType: RepositoryUrlType.Value, link: String)
90+
91+
sealed trait OwnerInfo {
92+
def value: String
93+
}
94+
95+
case class AccountId(value: String) extends OwnerInfo
96+
97+
case class TeamUsername(value: String) extends OwnerInfo

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

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

6-
case class Team(username: String, display_name: String)
6+
case class Team(uuid: String, username: String, display_name: String)
77

88
object Team {
99
// format: off
1010
implicit val reader: Reads[Team] = (
11-
(__ \ "team" \ "username").read[String] and
12-
(__ \ "team" \ "display_name").read[String]
11+
(__ \ "uuid").read[String] and
12+
(__ \ "username").read[String] and
13+
(__ \ "display_name").read[String]
1314
)(Team.apply _)
1415
// format: on
1516
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 TeamWithPermission(team: Team, permission: String)
7+
8+
object TeamWithPermission {
9+
// format: off
10+
implicit val reader: Reads[TeamWithPermission] = (
11+
(__ \ "team").read[Team] and
12+
(__ \ "permission").read[String]
13+
)(TeamWithPermission.apply _)
14+
// format: on
15+
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@ 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(
7+
account_id: String,
8+
uuid: String,
9+
display_name: String,
10+
nickname: Option[String],
11+
avatarUrl: Option[String]
12+
)
713

814
object User {
915
// format: off
1016
implicit val reader: Reads[User] = (
11-
(__ \ "username").read[String] and
12-
(__ \ "display_name").read[String]
17+
(__ \ "account_id").read[String] and
18+
(__ \ "uuid").read[String] and
19+
(__ \ "display_name").read[String] and
20+
(__ \ "nickname").readNullable[String] and
21+
(__ \ "links" \ "avatar" \ "href").readNullable[String]
1322
)(User.apply _)
1423
// format: on
1524
}

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

Lines changed: 5 additions & 3 deletions
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.{DeployKey, Repository}
3+
import com.codacy.client.bitbucket.v2.{DeployKey, OwnerInfo, Repository}
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
55
import play.api.libs.json.Json
66

@@ -10,8 +10,10 @@ class RepositoryServices(client: BitbucketClient) {
1010
* Gets the list of the user's repositories. Private repositories only appear on this list
1111
* if the caller is authenticated and is authorized to view the repository.
1212
*/
13-
def getRepositories(username: String): RequestResponse[Seq[Repository]] = {
14-
client.executePaginated(Request(s"https://bitbucket.org/api/2.0/repositories/$username", classOf[Seq[Repository]]))
13+
def getRepositories(ownerInfo: OwnerInfo): RequestResponse[Seq[Repository]] = {
14+
client.executePaginated(
15+
Request(s"https://bitbucket.org/api/2.0/repositories/${ownerInfo.value}", classOf[Seq[Repository]])
16+
)
1517
}
1618

1719
def createKey(

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

Lines changed: 9 additions & 7 deletions
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, Team, User}
3+
import com.codacy.client.bitbucket.v2._
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
55
import play.api.libs.json.Json
66

@@ -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
/*
@@ -30,15 +30,17 @@ class UserServices(client: BitbucketClient) {
3030
/*
3131
* Gets all the teams a user is a member of
3232
*/
33-
def getTeams: RequestResponse[Seq[Team]] = {
34-
client.executePaginated(Request(s"https://bitbucket.org/api/2.0/user/permissions/teams", classOf[Seq[Team]]))
33+
def getTeams: RequestResponse[Seq[TeamWithPermission]] = {
34+
client.executePaginated(
35+
Request(s"https://bitbucket.org/api/2.0/user/permissions/teams", classOf[Seq[TeamWithPermission]])
36+
)
3537
}
3638

3739
/*
3840
* Creates a ssh key
3941
*/
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"
42+
def createKey(userId: String, key: String, keyName: String): RequestResponse[SshKey] = {
43+
val url = s"https://bitbucket.org/api/2.0/users/$userId/ssh-keys"
4244

4345
val values = Json.obj("key" -> key, "label" -> keyName)
4446

0 commit comments

Comments
 (0)