Skip to content

Commit b47c234

Browse files
committed
feat: use BaseCommit for getCommit (ala-797) :breaking:
1 parent 8db81ff commit b47c234

File tree

7 files changed

+193
-6
lines changed

7 files changed

+193
-6
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import play.api.libs.functional.syntax._
4+
import play.api.libs.json._
5+
6+
import java.time.LocalDateTime
7+
8+
case class Account(
9+
uuid: String,
10+
username: String,
11+
displayName: Option[String],
12+
avatarLink: Option[String],
13+
createdOn: LocalDateTime
14+
)
15+
16+
object Account {
17+
18+
implicit val accountReads: Reads[Account] = (
19+
(__ \ "uuid").read[String] and
20+
(__ \ "username").read[String] and
21+
(__ \ "display_name").readNullable[String] and
22+
(__ \ "links" \ "avatar" \ "href").readNullable[String] and
23+
(__ \ "created_on").read[LocalDateTime]
24+
)(Account.apply _)
25+
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 Author(raw: String, user: Option[Account])
7+
8+
object Author {
9+
10+
implicit val authorReads: Reads[Author] = (
11+
(__ \ "raw").read[String] and
12+
(__ \ "user").readNullable[Account]
13+
)(Author.apply _)
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import play.api.libs.json._
4+
5+
import java.time.LocalDateTime
6+
7+
case class BaseCommit(hash: String, parents: Seq[String], date: LocalDateTime, message: String, author: Author)
8+
9+
object BaseCommit {
10+
11+
implicit val dateTimeReads: Reads[LocalDateTime] = Reads.localDateTimeReads("yyyy-MM-dd'T'HH:mm:ssXXX", identity)
12+
13+
implicit def commitReader: Reads[BaseCommit] = Reads { (json: JsValue) =>
14+
(for {
15+
hash <- (json \ "hash").asOpt[String]
16+
parents = (json \ "parents" \\ "hash").flatMap(_.asOpt[String])
17+
date <- (json \ "date").asOpt[LocalDateTime]
18+
message <- (json \ "message").asOpt[String]
19+
author <- (json \ "author").asOpt[Author]
20+
} yield BaseCommit(hash, parents.toSeq /*cross-compile*/, date, message, author))
21+
.map(JsSuccess(_))
22+
.getOrElse(JsError("could not read commit"))
23+
}
24+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java.time.LocalDateTime
44

55
import play.api.libs.json._
66

7+
@deprecated("Use BaseCommit instead", "18.7.1")
78
case class SimpleCommit(
89
hash: String,
910
authorName: Option[String],
@@ -13,8 +14,8 @@ case class SimpleCommit(
1314
)
1415

1516
object SimpleCommit {
16-
val dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
17-
implicit val dateTimeReads: Reads[LocalDateTime] = Reads.localDateTimeReads(dateFormat, s => { println(s); s })
17+
18+
implicit val dateTimeReads: Reads[LocalDateTime] = Reads.localDateTimeReads("yyyy-MM-dd'T'HH:mm:ssXXX", identity)
1819

1920
implicit def commitReader: Reads[SimpleCommit] = Reads { (json: JsValue) =>
2021
(for {
@@ -23,7 +24,7 @@ object SimpleCommit {
2324
parents = (json \ "parents" \\ "hash").flatMap(_.asOpt[String])
2425
date <- (json \ "date").asOpt[LocalDateTime]
2526
message <- (json \ "message").asOpt[String]
26-
} yield SimpleCommit(hash, username, parents.toSeq, date, message))
27+
} yield SimpleCommit(hash, username, parents.toSeq /*cross-compile*/, date, message))
2728
.map(JsSuccess(_))
2829
.getOrElse(JsError("could not read commit"))
2930
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package com.codacy.client.bitbucket.v2.service
22

33
import java.net.URLEncoder
44
import com.codacy.client.bitbucket.client.{BitbucketClient, RequestResponse}
5-
import com.codacy.client.bitbucket.v2.{CommitComment, SimpleCommit, SimpleCommitSha}
5+
import com.codacy.client.bitbucket.v2.{BaseCommit, CommitComment, SimpleCommitSha}
66
import play.api.libs.json.{JsNumber, JsObject, JsString, Json}
77

88
class CommitServices(client: BitbucketClient) {
99

10-
def getCommit(author: String, repository: String, commit: String): RequestResponse[SimpleCommit] =
11-
client.execute[SimpleCommit](generateCommitUrl(author, repository, commit))
10+
def getCommit(author: String, repository: String, commit: String): RequestResponse[BaseCommit] =
11+
client.execute[BaseCommit](generateCommitUrl(author, repository, commit))
1212

1313
def createComment(
1414
author: String,
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"rendered": {
3+
"message": {
4+
"raw": "Add a GEORDI_OUTPUT_DIR setting",
5+
"markup": "markdown",
6+
"html": "<p>Add a GEORDI_OUTPUT_DIR setting</p>",
7+
"type": "rendered"
8+
}
9+
},
10+
"hash": "f7591a13eda445d9a9167f98eb870319f4b6c2d8",
11+
"repository": {
12+
"name": "geordi",
13+
"type": "repository",
14+
"full_name": "bitbucket/geordi",
15+
"links": {
16+
"self": {
17+
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/geordi"
18+
},
19+
"html": {
20+
"href": "https://bitbucket.org/bitbucket/geordi"
21+
},
22+
"avatar": {
23+
"href": "https://bytebucket.org/ravatar/%7B85d08b4e-571d-44e9-a507-fa476535aa98%7D?ts=1730260"
24+
}
25+
},
26+
"uuid": "{85d08b4e-571d-44e9-a507-fa476535aa98}"
27+
},
28+
"links": {
29+
"self": {
30+
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/geordi/commit/f7591a13eda445d9a9167f98eb870319f4b6c2d8"
31+
},
32+
"comments": {
33+
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/geordi/commit/f7591a13eda445d9a9167f98eb870319f4b6c2d8/comments"
34+
},
35+
"patch": {
36+
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/geordi/patch/f7591a13eda445d9a9167f98eb870319f4b6c2d8"
37+
},
38+
"html": {
39+
"href": "https://bitbucket.org/bitbucket/geordi/commits/f7591a13eda445d9a9167f98eb870319f4b6c2d8"
40+
},
41+
"diff": {
42+
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/geordi/diff/f7591a13eda445d9a9167f98eb870319f4b6c2d8"
43+
},
44+
"approve": {
45+
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/geordi/commit/f7591a13eda445d9a9167f98eb870319f4b6c2d8/approve"
46+
},
47+
"statuses": {
48+
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/geordi/commit/f7591a13eda445d9a9167f98eb870319f4b6c2d8/statuses"
49+
}
50+
},
51+
"author": {
52+
"raw": "Brodie Rao <[email protected]>",
53+
"type": "author",
54+
"user": {
55+
"display_name": "Brodie Rao",
56+
"username": "brodie",
57+
"uuid": "{9484702e-c663-4afd-aefb-c93a8cd31c28}",
58+
"links": {
59+
"self": {
60+
"href": "https://api.bitbucket.org/2.0/users/%7B9484702e-c663-4afd-aefb-c93a8cd31c28%7D"
61+
},
62+
"html": {
63+
"href": "https://bitbucket.org/%7B9484702e-c663-4afd-aefb-c93a8cd31c28%7D/"
64+
},
65+
"avatar": {
66+
"href": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/557058:3aae1e05-702a-41e5-81c8-f36f29afb6ca/613070db-28b0-421f-8dba-ae8a87e2a5c7/128"
67+
}
68+
},
69+
"type": "user",
70+
"nickname": "brodie",
71+
"account_id": "557058:3aae1e05-702a-41e5-81c8-f36f29afb6ca",
72+
"created_on": "2012-07-16T19:37:54+00:00"
73+
}
74+
},
75+
"summary": {
76+
"raw": "Add a GEORDI_OUTPUT_DIR setting",
77+
"markup": "markdown",
78+
"html": "<p>Add a GEORDI_OUTPUT_DIR setting</p>",
79+
"type": "rendered"
80+
},
81+
"participants": [],
82+
"parents": [
83+
{
84+
"type": "commit",
85+
"hash": "f06941fec4ef6bcb0c2456927a0cf258fa4f899b",
86+
"links": {
87+
"self": {
88+
"href": "https://api.bitbucket.org/2.0/repositories/bitbucket/geordi/commit/f06941fec4ef6bcb0c2456927a0cf258fa4f899b"
89+
},
90+
"html": {
91+
"href": "https://bitbucket.org/bitbucket/geordi/commits/f06941fec4ef6bcb0c2456927a0cf258fa4f899b"
92+
}
93+
}
94+
}
95+
],
96+
"date": "2012-07-16T19:37:54+00:00",
97+
"message": "Add a GEORDI_OUTPUT_DIR setting",
98+
"type": "commit"
99+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import com.codacy.client.bitbucket.TestUtils
4+
import org.scalatest._
5+
import play.api.libs.json.{JsError, JsSuccess, Json}
6+
7+
class CommitSpecs extends FlatSpec with Matchers {
8+
9+
"Reads[BaseCommit]" should "successfully parse BaseCommit" in {
10+
val rawJson = TestUtils.getTestContent("/test-files/get_a_commit.json")
11+
val json = Json.parse(rawJson)
12+
val value = json.validate[BaseCommit]
13+
14+
val expectedAvatarLink =
15+
"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/557058:3aae1e05-702a-41e5-81c8-f36f29afb6ca/613070db-28b0-421f-8dba-ae8a87e2a5c7/128"
16+
17+
value match {
18+
case JsSuccess(value, _) => assert(value.author.user.flatMap(_.avatarLink).contains(expectedAvatarLink))
19+
case JsError(errors) => fail(s"Failed with$errors")
20+
}
21+
}
22+
23+
}

0 commit comments

Comments
 (0)