Skip to content

Commit cad153f

Browse files
committed
parse links & direct api calls
1 parent 31e611c commit cad153f

File tree

3 files changed

+83
-22
lines changed

3 files changed

+83
-22
lines changed

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,30 @@ case class PullRequest(id: Long, title: String, description: String,
88
authorUsername: String, authorAvatar: Option[String],
99
state: String, created_on: DateTime, updated_on: DateTime,
1010
sourceRepository: String, sourceBranch: String, sourceCommit: String,
11-
destRepository: String, destBranch: String, destCommit: String) {
12-
val url: String = s"https://bitbucket.org/$destRepository/pull-request/$id"
11+
destRepository: String, destBranch: String, destCommit: String,
12+
apiUrls: Seq[ApiUrl]) {
13+
val url = s"https://bitbucket.org/$destRepository/pull-request/$id"
1314
}
1415

16+
object ApiUrlType extends Enumeration {
17+
val Commits = Value("commits")
18+
val Decline = Value("decline")
19+
val Self = Value("self")
20+
val Comments = Value("comments")
21+
val Patch = Value("patch")
22+
val Merge = Value("merge")
23+
val Html = Value("html")
24+
val Activity = Value("activity")
25+
val Diff = Value("diff")
26+
val Approve = Value("approve")
27+
28+
def find(urlType: String): Option[Value] = {
29+
values.find(_.toString == urlType)
30+
}
31+
}
32+
33+
case class ApiUrl(urlType: ApiUrlType.Value, link: String)
34+
1535
object PullRequest {
1636
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZ"
1737
implicit val jodaDateTimeReads = Reads.jodaDateReads(dateFormat)
@@ -30,6 +50,15 @@ object PullRequest {
3050
(__ \ "source" \ "commit" \ "hash").read[String] and
3151
(__ \ "destination" \ "repository" \ "full_name").read[String] and
3252
(__ \ "destination" \ "branch" \ "name").read[String] and
33-
(__ \ "destination" \ "commit" \ "hash").read[String]
53+
(__ \ "destination" \ "commit" \ "hash").read[String] and
54+
(__ \ "links").read[Map[String, Map[String, String]]].map(parseLinks)
3455
)(PullRequest.apply _)
56+
57+
private def parseLinks(links: Map[String, Map[String, String]]): Seq[ApiUrl] = {
58+
(for {
59+
(linkName, linkMap) <- links
60+
urlType <- ApiUrlType.find(linkName)
61+
linkUrl <- linkMap.get("href")
62+
} yield ApiUrl(urlType, linkUrl)).toSeq
63+
}
3564
}

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,61 @@ import org.joda.time.DateTime
44
import play.api.libs.functional.syntax._
55
import play.api.libs.json._
66

7-
case class Repository(name: String, full_name: String, description: String,
8-
scm: String, created_on: DateTime, updated_on: DateTime,
9-
httpsUrl: String, sshUrl: String, owner: String, size: Long,
10-
has_issues: Boolean, is_private: Boolean, language: String)
7+
case class Repository(name: String, full_name: String, description: String, scm: String,
8+
created_on: DateTime, updated_on: DateTime, owner: String, size: Long,
9+
has_issues: Boolean, is_private: Boolean, language: String,
10+
url: Seq[RepositoryUrl])
1111

1212
object Repository {
1313
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZ"
1414
implicit val jodaDateTimeReads = Reads.jodaDateReads(dateFormat)
1515

1616
implicit val reader: Reads[Repository] = {
17-
val url1 = (__ \ "links" \ "clone")(0).read[Url]
18-
val url2 = (__ \ "links" \ "clone")(1).read[Url]
19-
20-
val httpUrl = url1.filter(_.name == "https").orElse(url2).map(_.href)
21-
val sshUrl = url2.filter(_.name == "ssh").orElse(url1).map(_.href)
22-
2317
((__ \ "name").read[String] and
2418
(__ \ "full_name").read[String] and
2519
(__ \ "description").read[String] and
2620
(__ \ "scm").read[String] and
2721
(__ \ "created_on").read[DateTime] and
2822
(__ \ "updated_on").read[DateTime] and
29-
httpUrl and
30-
sshUrl and
3123
(__ \ "owner" \ "username").read[String] and
3224
(__ \ "size").read[Long] and
3325
(__ \ "has_issues").read[Boolean] and
3426
(__ \ "is_private").read[Boolean] and
35-
(__ \ "language").read[String]
27+
(__ \ "language").read[String] and
28+
(__ \ "links").read[Map[String, JsValue]].map(parseLinks)
3629
)(Repository.apply _)
3730
}
31+
32+
private def parseLinks(links: Map[String, JsValue]): Seq[RepositoryUrl] = {
33+
links.flatMap {
34+
case (linkName, linkMap) =>
35+
36+
val simpleLinks = for {
37+
ref <- linkMap.asOpt[Map[String, String]]
38+
urlType <- RepositoryUrlType.find(linkName)
39+
linkUrl <- ref.get("href")
40+
} yield RepositoryUrl(urlType, linkUrl)
41+
42+
val complexLinks = for {
43+
refs <- linkMap.asOpt[Seq[Map[String, String]]].toSeq
44+
ref <- refs
45+
linkName <- ref.get("name")
46+
urlType <- RepositoryUrlType.find(linkName)
47+
linkUrl <- ref.get("href")
48+
} yield RepositoryUrl(urlType, linkUrl)
49+
50+
simpleLinks ++ complexLinks
51+
}.toSeq
52+
}
3853
}
3954

40-
case class Url(name: String, href: String)
55+
object RepositoryUrlType extends Enumeration {
56+
val Https = Value("https")
57+
val Ssh = Value("ssh")
4158

42-
object Url {
43-
implicit val reader: Reads[Url] = (
44-
(__ \ "name").read[String] and
45-
(__ \ "href").read[String]
46-
)(Url.apply _)
59+
def find(urlType: String): Option[Value] = {
60+
values.find(_.toString == urlType)
61+
}
4762
}
63+
64+
case class RepositoryUrl(urlType: RepositoryUrlType.Value, link: String)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codacy.client.bitbucket.service
2+
3+
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
4+
import play.api.libs.json.JsValue
5+
6+
class UrlServices(client: BitbucketClient) {
7+
8+
/*
9+
* Post to a api url
10+
*/
11+
def post(url: String): RequestResponse[JsValue] = {
12+
client.post(Request(url, classOf[JsValue]), Map.empty)
13+
}
14+
15+
}

0 commit comments

Comments
 (0)