Skip to content

Commit 63117d2

Browse files
authored
Merge pull request codacy#30 from codacy/fix-datetime-parsing
Fix datetime parsing
2 parents 01e9175 + b492ef4 commit 63117d2

File tree

3 files changed

+135
-10
lines changed

3 files changed

+135
-10
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ case class Repository(name: String, full_name: String, description: String, scm:
1111

1212
object Repository {
1313
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZ"
14+
val dateFormatWithoutMillis = "yyyy-MM-dd'T'HH:mm:ssZZ"
15+
1416
implicit val jodaDateTimeReads = Reads.jodaDateReads(dateFormat)
17+
.orElse(Reads.jodaDateReads(dateFormatWithoutMillis))
1518

1619
implicit val reader: Reads[Repository] = {
1720
((__ \ "name").read[String] and

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ case class SimpleRepository(name: String, description: String, scm: String, crea
1414

1515
object SimpleRepository {
1616
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
17+
val dateFormatWithoutMillis = "yyyy-MM-dd'T'HH:mm:ss"
18+
1719
implicit val jodaDateTimeReads = Reads.jodaDateReads(dateFormat)
20+
.orElse(Reads.jodaDateReads(dateFormatWithoutMillis))
1821

1922
implicit val reader: Reads[SimpleRepository] =
2023
((__ \ "slug").read[String] and
@@ -27,5 +30,5 @@ object SimpleRepository {
2730
(__ \ "has_issues").read[Boolean] and
2831
(__ \ "is_private").read[Boolean] and
2932
(__ \ "language").read[String]
30-
)(SimpleRepository.apply _)
33+
) (SimpleRepository.apply _)
3134
}

src/test/scala/RemoteRepositorySpecs.scala

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import org.scalatest._
22
import org.scalatest.Matchers
33
import play.api.libs.json.Json
4-
import com.codacy.client.bitbucket.PullRequest
4+
import com.codacy.client.bitbucket.{PullRequest, SimpleRepository}
55
import com.codacy.client.bitbucket.PullRequest._
66

77
class RemoteRepositorySpecs extends FlatSpec with Matchers {
@@ -132,9 +132,10 @@ class RemoteRepositorySpecs extends FlatSpec with Matchers {
132132
val json = Json.parse(input)
133133
val value = json.validate[PullRequest]
134134

135-
val parsed = value.fold(e => false, r => r.destCommit.isDefined)
136-
137-
parsed shouldBe true
135+
value.fold(e =>
136+
fail(s"$e"),
137+
r => r.destCommit.isDefined shouldBe true
138+
)
138139
}
139140

140141
it should "successfully parse JSON with null commit" in {
@@ -261,9 +262,10 @@ class RemoteRepositorySpecs extends FlatSpec with Matchers {
261262
val json = Json.parse(input)
262263
val value = json.validate[PullRequest]
263264

264-
val parsed = value.fold(e =>false, r => r.destCommit.isEmpty)
265-
266-
parsed shouldBe true
265+
value.fold(e =>
266+
fail(s"$e"),
267+
r => r.destCommit.isEmpty shouldBe true
268+
)
267269
}
268270

269271
it should "successfully parse a JSON with null author while getting info for PRs" in {
@@ -382,9 +384,126 @@ class RemoteRepositorySpecs extends FlatSpec with Matchers {
382384
val json = Json.parse(input)
383385
val value = json.validate[PullRequest]
384386

385-
val parsed = value.fold(e => false, r => r.destCommit.isDefined)
387+
value.fold(e =>
388+
fail(s"$e"),
389+
r => r.destCommit.isDefined shouldBe true
390+
)
391+
}
392+
393+
it should "successfully parse a JSON into an array of SimpleRepository" in {
386394

387-
parsed shouldBe true
395+
val input =
396+
"""[
397+
|{
398+
| "scm": "git",
399+
| "has_wiki": false,
400+
| "last_updated": "2016-01-26T21:39:24.485",
401+
| "no_forks": false,
402+
| "created_on": "2015-09-04T20:33:22.640",
403+
| "owner": "carrots",
404+
| "logo": "https://bitbucket.org/carrots/potatos/avatar/32/?ts=1453840764",
405+
| "email_mailinglist": "",
406+
| "is_mq": false,
407+
| "size": 14544338,
408+
| "read_only": false,
409+
| "fork_of": null,
410+
| "mq_of": null,
411+
| "state": "available",
412+
| "utc_created_on": "2015-09-04 18:37:22+00:00",
413+
| "website": "",
414+
| "description": "",
415+
| "has_issues": false,
416+
| "is_fork": false,
417+
| "slug": "potatos",
418+
| "is_private": true,
419+
| "name": "Carrots and company",
420+
| "language": "",
421+
| "utc_last_updated": "2016-01-26 20:39:24+00:00",
422+
| "no_public_forks": true,
423+
| "creator": null,
424+
| "resource_uri": "/1.0/repositories/carrots/potatos"
425+
|},
426+
|{
427+
| "scm": "git",
428+
| "has_wiki": false,
429+
| "last_updated": "2017-02-03T20:29:18.224",
430+
| "no_forks": false,
431+
| "created_on": "2017-02-01T17:42:37.640",
432+
| "owner": "carrots",
433+
| "logo": "https://bitbucket.org/carrots/potatos/avatar/32/?ts=1486150158",
434+
| "email_mailinglist": "",
435+
| "is_mq": false,
436+
| "size": 13489122,
437+
| "read_only": false,
438+
| "fork_of": null,
439+
| "mq_of": null,
440+
| "state": "available",
441+
| "utc_created_on": "2017-02-01 16:41:37+00:00",
442+
| "website": "",
443+
| "description": "credentials.certainteed.com (Rackspace)",
444+
| "has_issues": false,
445+
| "is_fork": false,
446+
| "slug": "potatos",
447+
| "is_private": true,
448+
| "name": "Nice potatos",
449+
| "language": "php",
450+
| "utc_last_updated": "2017-02-03 19:29:18+00:00",
451+
| "no_public_forks": true,
452+
| "creator": null,
453+
| "resource_uri": "/1.0/repositories/carrots/potatos"
454+
|}
455+
|]
456+
""".stripMargin
457+
val json = Json.parse(input)
458+
val value = json.validate[Seq[SimpleRepository]]
459+
460+
value.fold(e =>
461+
fail(s"$e"),
462+
repo => repo.length shouldBe 2
463+
)
464+
}
465+
466+
it should "successfully parse a JSON into an array of SimpleRepository that has no milliseconds on the created date" in {
467+
468+
val input =
469+
"""[
470+
|{
471+
| "scm": "git",
472+
| "has_wiki": false,
473+
| "last_updated": "2016-01-26T21:39:24.485",
474+
| "no_forks": false,
475+
| "created_on": "2015-09-04T20:33:22",
476+
| "owner": "carrots",
477+
| "logo": "https://bitbucket.org/carrots/potatos/avatar/32/?ts=1453840764",
478+
| "email_mailinglist": "",
479+
| "is_mq": false,
480+
| "size": 14544338,
481+
| "read_only": false,
482+
| "fork_of": null,
483+
| "mq_of": null,
484+
| "state": "available",
485+
| "utc_created_on": "2015-09-04 18:37:22+00:00",
486+
| "website": "",
487+
| "description": "",
488+
| "has_issues": false,
489+
| "is_fork": false,
490+
| "slug": "potatos",
491+
| "is_private": true,
492+
| "name": "Carrots and company",
493+
| "language": "",
494+
| "utc_last_updated": "2016-01-26 20:39:24+00:00",
495+
| "no_public_forks": true,
496+
| "creator": null,
497+
| "resource_uri": "/1.0/repositories/carrots/potatos"
498+
|}
499+
|]
500+
""".stripMargin
501+
val json = Json.parse(input)
502+
val value = json.validate[Seq[SimpleRepository]]
388503

504+
value.fold(e =>
505+
fail(s"$e"),
506+
repo => repo.length shouldBe 1
507+
)
389508
}
390509
}

0 commit comments

Comments
 (0)