Skip to content

Commit

Permalink
Merge pull request #50 from codacy/change-username-for-uuid
Browse files Browse the repository at this point in the history
Change username for UUID - FT-6455
  • Loading branch information
rtfpessoa authored Feb 22, 2019
2 parents c79b3ac + 3664f3f commit 14b422a
Show file tree
Hide file tree
Showing 12 changed files with 584 additions and 115 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/com/codacy/client/bitbucket/v2/Issue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Issue(
priority: String,
title: String,
content: String,
reporter: String,
reporter: User,
created_on: LocalDateTime,
kind: String
)
Expand All @@ -24,7 +24,7 @@ object Issue {
(__ \ "priority").read[String] and
(__ \ "title").read[String] and
(__ \ "content" \ "raw").read[String] and
(__ \ "reporter" \ "username").read[String] and
(__ \ "reporter").read[User] and
(__ \ "created_on").read[LocalDateTime] and
(__ \ "kind").read[String]
)(Issue.apply _)
Expand Down
31 changes: 16 additions & 15 deletions src/main/scala/com/codacy/client/bitbucket/v2/PullRequest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ case class PullRequest(
id: Long,
title: String,
description: String,
authorUsername: Option[String],
authorAvatar: Option[String],
author: User,
state: String,
created_on: LocalDateTime,
updated_on: LocalDateTime,
Expand All @@ -20,8 +19,7 @@ case class PullRequest(
destRepository: String,
destBranch: String,
destCommit: Option[String],
apiUrls: Seq[ApiUrl],
authorUUID: Option[String] = None
apiUrls: Seq[ApiUrl]
) {
val url = s"https://bitbucket.org/$destRepository/pull-request/$id"
}
Expand All @@ -43,7 +41,13 @@ object ApiUrlType extends Enumeration {
}
}

case class ApiUrl(urlType: ApiUrlType.Value, link: String)
case class ApiUrl(urlType: ApiUrlType.Value, link: Link)

case class Link(href: String)

object Link {
implicit val reader: Reads[Link] = Json.format[Link]
}

object PullRequest {
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"
Expand All @@ -61,8 +65,7 @@ object PullRequest {
(__ \ "id").read[Long] and
(__ \ "title").read[String] and
(__ \ "description").read[String] and
(__ \ "author" \ "username").readNullable[String] and
(__ \ "author" \ "links" \ "avatar" \ "href").readNullable[String].orElse((__ \ "author" \ "links").readNullable[String]) and
(__ \ "author").read[User] and
(__ \ "state").read[String] and
(__ \ "created_on").read[LocalDateTime] and
(__ \ "updated_on").read[LocalDateTime] and
Expand All @@ -73,16 +76,14 @@ object PullRequest {
(__ \ "destination" \ "branch" \ "name").read[String] and
(__ \ "destination" \ "commit" \ "hash").readNullable[String] and
// TODO: (__ \ "destination" \ "commit" \ "hash").read[Option[String]] and
(__ \ "links").read[Map[String, Map[String, String]]].map(parseLinks) and
(__ \ "author" \ "uuid").readNullable[String]
(__ \ "links").read[Map[String, Link]].map(parseLinks)
) (PullRequest.apply _)
// format: on

private def parseLinks(links: Map[String, Map[String, String]]): Seq[ApiUrl] = {
(for {
(linkName, linkMap) <- links
urlType <- ApiUrlType.find(linkName)
linkUrl <- linkMap.get("href")
} yield ApiUrl(urlType, linkUrl)).toSeq
private def parseLinks(links: Map[String, Link]): Seq[ApiUrl] = {
links.flatMap {
case (linkTypeStr, link) =>
ApiUrlType.find(linkTypeStr).map(ApiUrl(_, link))
}(collection.breakOut)
}
}
20 changes: 18 additions & 2 deletions src/main/scala/com/codacy/client/bitbucket/v2/Repository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.codacy.client.bitbucket.v2

import java.time.LocalDateTime

import com.codacy.client.bitbucket.v2.Repository.Owner
import play.api.libs.functional.syntax._
import play.api.libs.json._
import play.twirl.api.JavaScriptFormat

case class Repository(
name: String,
Expand All @@ -12,7 +14,7 @@ case class Repository(
scm: String,
created_on: LocalDateTime,
updated_on: LocalDateTime,
owner: String,
owner: Owner,
size: Long,
has_issues: Boolean,
is_private: Boolean,
Expand All @@ -37,7 +39,7 @@ object Repository {
(__ \ "scm").read[String] and
(__ \ "created_on").read[LocalDateTime] and
(__ \ "updated_on").read[LocalDateTime] and
(__ \ "owner" \ "username").read[String] and
(__ \ "owner").read[Owner] and
(__ \ "size").read[Long] and
(__ \ "has_issues").read[Boolean] and
(__ \ "is_private").read[Boolean] and
Expand All @@ -47,6 +49,12 @@ object Repository {
}
// format: on

final case class Owner(nickname: Option[String], username: Option[String], display_name: String, `type`: String)

object Owner {
implicit val reader: Reads[Owner] = Json.format[Owner]
}

private def parseLinks(links: Map[String, JsValue]): Seq[RepositoryUrl] = {
links.flatMap {
case (linkName, linkMap) =>
Expand Down Expand Up @@ -79,3 +87,11 @@ object RepositoryUrlType extends Enumeration {
}

case class RepositoryUrl(urlType: RepositoryUrlType.Value, link: String)

sealed trait OwnerInfo {
def value: String
}

case class AccountId(value: String) extends OwnerInfo

case class TeamUsername(value: String) extends OwnerInfo
7 changes: 4 additions & 3 deletions src/main/scala/com/codacy/client/bitbucket/v2/Team.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package com.codacy.client.bitbucket.v2
import play.api.libs.functional.syntax._
import play.api.libs.json._

case class Team(username: String, display_name: String)
case class Team(uuid: String, username: String, display_name: String)

object Team {
// format: off
implicit val reader: Reads[Team] = (
(__ \ "team" \ "username").read[String] and
(__ \ "team" \ "display_name").read[String]
(__ \ "uuid").read[String] and
(__ \ "username").read[String] and
(__ \ "display_name").read[String]
)(Team.apply _)
// format: on
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.codacy.client.bitbucket.v2

import play.api.libs.functional.syntax._
import play.api.libs.json._

case class TeamWithPermission(team: Team, permission: String)

object TeamWithPermission {
// format: off
implicit val reader: Reads[TeamWithPermission] = (
(__ \ "team").read[Team] and
(__ \ "permission").read[String]
)(TeamWithPermission.apply _)
// format: on
}
15 changes: 12 additions & 3 deletions src/main/scala/com/codacy/client/bitbucket/v2/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ package com.codacy.client.bitbucket.v2
import play.api.libs.functional.syntax._
import play.api.libs.json._

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

object User {
// format: off
implicit val reader: Reads[User] = (
(__ \ "username").read[String] and
(__ \ "display_name").read[String]
(__ \ "account_id").read[String] and
(__ \ "uuid").read[String] and
(__ \ "display_name").read[String] and
(__ \ "nickname").readNullable[String] and
(__ \ "links" \ "avatar" \ "href").readNullable[String]
)(User.apply _)
// format: on
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.codacy.client.bitbucket.v2.service

import com.codacy.client.bitbucket.v2.{DeployKey, Repository}
import com.codacy.client.bitbucket.v2.{DeployKey, OwnerInfo, Repository}
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
import play.api.libs.json.Json

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

def createKey(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.codacy.client.bitbucket.v2.service

import com.codacy.client.bitbucket.v2.{Email, SshKey, Team, User}
import com.codacy.client.bitbucket.v2._
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
import play.api.libs.json.Json

Expand All @@ -16,8 +16,8 @@ class UserServices(client: BitbucketClient) {
/*
* Gets the basic information associated with an account.
*/
def getUser(username: String): RequestResponse[User] = {
client.execute(Request(s"https://api.bitbucket.org/2.0/users/$username", classOf[User]))
def getUser(userId: String): RequestResponse[User] = {
client.execute(Request(s"https://api.bitbucket.org/2.0/users/$userId", classOf[User]))
}

/*
Expand All @@ -30,15 +30,17 @@ class UserServices(client: BitbucketClient) {
/*
* Gets all the teams a user is a member of
*/
def getTeams: RequestResponse[Seq[Team]] = {
client.executePaginated(Request(s"https://bitbucket.org/api/2.0/user/permissions/teams", classOf[Seq[Team]]))
def getTeams: RequestResponse[Seq[TeamWithPermission]] = {
client.executePaginated(
Request(s"https://bitbucket.org/api/2.0/user/permissions/teams", classOf[Seq[TeamWithPermission]])
)
}

/*
* Creates a ssh key
*/
def createKey(username: String, key: String, keyName: String): RequestResponse[SshKey] = {
val url = s"https://bitbucket.org/api/2.0/users/$username/ssh-keys"
def createKey(userId: String, key: String, keyName: String): RequestResponse[SshKey] = {
val url = s"https://bitbucket.org/api/2.0/users/$userId/ssh-keys"

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

Expand Down
Loading

0 comments on commit 14b422a

Please sign in to comment.