Skip to content

Commit 19f07f6

Browse files
authored
Merge pull request #17 from codacy/OptionalCommitHash-FT-2452
Reading optional commit hash
2 parents 8e52501 + face543 commit 19f07f6

File tree

5 files changed

+281
-3
lines changed

5 files changed

+281
-3
lines changed

build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ resolvers += "Typesafe maven repository" at "http://repo.typesafe.com/typesafe/m
1414

1515
libraryDependencies ++= Seq(
1616
jodaTime,
17-
playWS
17+
playWS,
18+
scalaTest
1819
)
1920

2021
organization := "com.codacy"

project/Dependencies.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ object Dependencies {
88
// Play framework
99
lazy val playWS = "com.typesafe.play" %% "play-ws" % "2.4.3"
1010

11+
lazy val scalaTest = "org.scalatest" %% "scalatest" % "2.2.6" % "test"
1112
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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,
11+
destRepository: String, destBranch: String, destCommit: Option[String],
1212
apiUrls: Seq[ApiUrl]) {
1313
val url = s"https://bitbucket.org/$destRepository/pull-request/$id"
1414
}
@@ -57,7 +57,7 @@ object PullRequest {
5757
(__ \ "source" \ "commit" \ "hash").read[String] and
5858
(__ \ "destination" \ "repository" \ "full_name").read[String] and
5959
(__ \ "destination" \ "branch" \ "name").read[String] and
60-
(__ \ "destination" \ "commit" \ "hash").read[String] and
60+
(__ \ "destination" \ "commit" \ "hash").readNullable[String] and
6161
// TODO: (__ \ "destination" \ "commit" \ "hash").read[Option[String]] and
6262
(__ \ "links").read[Map[String, Map[String, String]]].map(parseLinks)
6363
) (PullRequest.apply _)

src/test/scala/HelloSpec.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import org.scalatest._
2+
import org.scalatest.Matchers
3+
4+
class HelloSpec extends FlatSpec with Matchers {
5+
"Hello" should "have tests" in {
6+
true shouldBe true
7+
}
8+
}
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
import org.scalatest._
2+
import org.scalatest.Matchers
3+
import play.api.libs.json.Json
4+
import com.codacy.client.bitbucket.PullRequest
5+
import com.codacy.client.bitbucket.PullRequest._
6+
7+
class RemoteRepositorySpecs extends FlatSpec with Matchers {
8+
9+
"RemoteRepository" should "successfully parse JSON with a commit" in {
10+
val input =
11+
"""
12+
|{
13+
| "description": "",
14+
| "links": {
15+
| "decline": {
16+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/decline"
17+
| },
18+
| "commits": {
19+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/commits"
20+
| },
21+
| "self": {
22+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368"
23+
| },
24+
| "comments": {
25+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/comments"
26+
| },
27+
| "merge": {
28+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/merge"
29+
| },
30+
| "html": {
31+
| "href": "https://bitbucket.org//frontend./pull-requests/368"
32+
| },
33+
| "activity": {
34+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/activity"
35+
| },
36+
| "diff": {
37+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/diff"
38+
| },
39+
| "approve": {
40+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/approve"
41+
| },
42+
| "statuses": {
43+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/statuses"
44+
| }
45+
| },
46+
| "title": "title",
47+
| "close_source_branch": true,
48+
| "merge_commit": null,
49+
| "destination": {
50+
| "commit": {
51+
| "hash" : "random_hash"
52+
| },
53+
| "repository": {
54+
| "links": {
55+
| "self": {
56+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend."
57+
| },
58+
| "html": {
59+
| "href": "https://bitbucket.org//frontend."
60+
| },
61+
| "avatar": {
62+
| "href": "https://bitbucket.org//frontend./avatar/32/"
63+
| }
64+
| },
65+
| "type": "repository",
66+
| "name": "frontend.",
67+
| "full_name": "/frontend.",
68+
| "uuid": "{ d8e5fe0b-fa32-47f1-a98b-88973c00c1d8}"
69+
| },
70+
| "branch": {
71+
| "name": "FASDREI-2035"
72+
| }
73+
| },
74+
| "state": "OPEN",
75+
| "closed_by": null,
76+
| "source": {
77+
| "commit": {
78+
| "hash": "ae0ff1c530b7",
79+
| "links": {
80+
| "self": {
81+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./commit/ae0ff1c530b7"
82+
| }
83+
| }
84+
| },
85+
| "repository": {
86+
| "links": {
87+
| "self": {
88+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend."
89+
| },
90+
| "html": {
91+
| "href": "https://bitbucket.org//frontend."
92+
| },
93+
| "avatar": {
94+
| "href": "https://bitbucket.org//frontend./avatar/32/"
95+
| }
96+
| },
97+
| "type": "repository",
98+
| "name": "frontend.",
99+
| "full_name": "/frontend.",
100+
| "uuid": "{ d8e5fe0b-fa32-47f1-a98b-88973c00c1d8}"
101+
| },
102+
| "branch": {
103+
| "name": "FASDREI-2033"
104+
| }
105+
| },
106+
| "comment_count": 0,
107+
| "author": {
108+
| "username": "username",
109+
| "display_name": "user name",
110+
| "type": "user",
111+
| "uuid": "{ f6f653be-75ea-4dc9-84af-645a5ede93aa}",
112+
| "links": {
113+
| "self": {
114+
| "href": "https://bitbucket.org/!api/2.0/users/username"
115+
| },
116+
| "html": {
117+
| "href": "https://bitbucket.org/username/"
118+
| },
119+
| "avatar": {
120+
| "href": "https://bitbucket.org/account/username/avatar/32/"
121+
| }
122+
| }
123+
| },
124+
| "created_on": "2016-06-16T16:08:51.006182+00:00",
125+
| "reason": "",
126+
| "updated_on": "2016-09-08T17:27:22.050009+00:00",
127+
| "type": "pullrequest",
128+
| "id": 368,
129+
| "task_count": 0
130+
| }
131+
""".stripMargin
132+
val json = Json.parse(input)
133+
val value = json.validate[PullRequest]
134+
135+
val parsed = value.fold(e => false, r => r.destCommit.isDefined)
136+
137+
parsed shouldBe true
138+
}
139+
140+
it should "successfully parse JSON with null commit" in {
141+
val input =
142+
"""
143+
|{
144+
| "description": "",
145+
| "links": {
146+
| "decline": {
147+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/decline"
148+
| },
149+
| "commits": {
150+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/commits"
151+
| },
152+
| "self": {
153+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368"
154+
| },
155+
| "comments": {
156+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/comments"
157+
| },
158+
| "merge": {
159+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/merge"
160+
| },
161+
| "html": {
162+
| "href": "https://bitbucket.org//frontend./pull-requests/368"
163+
| },
164+
| "activity": {
165+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/activity"
166+
| },
167+
| "diff": {
168+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/diff"
169+
| },
170+
| "approve": {
171+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/approve"
172+
| },
173+
| "statuses": {
174+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./pullrequests/368/statuses"
175+
| }
176+
| },
177+
| "title": "title",
178+
| "close_source_branch": true,
179+
| "merge_commit": null,
180+
| "destination": {
181+
| "commit": null,
182+
| "repository": {
183+
| "links": {
184+
| "self": {
185+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend."
186+
| },
187+
| "html": {
188+
| "href": "https://bitbucket.org//frontend."
189+
| },
190+
| "avatar": {
191+
| "href": "https://bitbucket.org//frontend./avatar/32/"
192+
| }
193+
| },
194+
| "type": "repository",
195+
| "name": "frontend.",
196+
| "full_name": "/frontend.",
197+
| "uuid": "{ d8e5fe0b-fa32-47f1-a98b-88973c00c1d8}"
198+
| },
199+
| "branch": {
200+
| "name": "FASDREI-2035"
201+
| }
202+
| },
203+
| "state": "OPEN",
204+
| "closed_by": null,
205+
| "source": {
206+
| "commit": {
207+
| "hash": "ae0ff1c530b7",
208+
| "links": {
209+
| "self": {
210+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend./commit/ae0ff1c530b7"
211+
| }
212+
| }
213+
| },
214+
| "repository": {
215+
| "links": {
216+
| "self": {
217+
| "href": "https://bitbucket.org/!api/2.0/repositories//frontend."
218+
| },
219+
| "html": {
220+
| "href": "https://bitbucket.org//frontend."
221+
| },
222+
| "avatar": {
223+
| "href": "https://bitbucket.org//frontend./avatar/32/"
224+
| }
225+
| },
226+
| "type": "repository",
227+
| "name": "frontend.",
228+
| "full_name": "/frontend.",
229+
| "uuid": "{ d8e5fe0b-fa32-47f1-a98b-88973c00c1d8}"
230+
| },
231+
| "branch": {
232+
| "name": "FASDREI-2033"
233+
| }
234+
| },
235+
| "comment_count": 0,
236+
| "author": {
237+
| "username": "username",
238+
| "display_name": "user name",
239+
| "type": "user",
240+
| "uuid": "{ f6f653be-75ea-4dc9-84af-645a5ede93aa}",
241+
| "links": {
242+
| "self": {
243+
| "href": "https://bitbucket.org/!api/2.0/users/username"
244+
| },
245+
| "html": {
246+
| "href": "https://bitbucket.org/username/"
247+
| },
248+
| "avatar": {
249+
| "href": "https://bitbucket.org/account/username/avatar/32/"
250+
| }
251+
| }
252+
| },
253+
| "created_on": "2016-06-16T16:08:51.006182+00:00",
254+
| "reason": "",
255+
| "updated_on": "2016-09-08T17:27:22.050009+00:00",
256+
| "type": "pullrequest",
257+
| "id": 368,
258+
| "task_count": 0
259+
| }
260+
""".stripMargin
261+
val json = Json.parse(input)
262+
val value = json.validate[PullRequest]
263+
264+
val parsed = value.fold(e =>false, r => r.destCommit.isEmpty)
265+
266+
parsed shouldBe true
267+
}
268+
}

0 commit comments

Comments
 (0)