Skip to content

Commit

Permalink
fix: update flank-scripts serialization (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotradamczyk5 authored Sep 23, 2020
1 parent 2823944 commit 4550fb9
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ data class GitHubRelease(
)

object GithubReleaseDeserializable : ResponseDeserializable<GitHubRelease> {
override fun deserialize(content: String) = content.toObject(GitHubRelease.serializer())
override fun deserialize(content: String): GitHubRelease = content.toObject()
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package flank.scripts.ci.releasenotes

import com.github.kittinunf.fuel.core.ResponseDeserializable
import flank.scripts.utils.toObject
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

@Serializable
data class GithubPullRequest(
Expand All @@ -21,7 +20,5 @@ data class GithubUser(
)

object GithubPullRequestDeserializer : ResponseDeserializable<List<GithubPullRequest>> {
override fun deserialize(content: String): List<GithubPullRequest> {
return Json.decodeFromString(content)
}
override fun deserialize(content: String): List<GithubPullRequest> = content.toObject()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package flank.scripts.exceptions
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.isClientError
import com.github.kittinunf.result.Result
import flank.scripts.github.GitHubErrorResponse
import flank.scripts.release.updatebugsnag.BugSnagResponse
import flank.scripts.utils.toObject

fun <V : Any, E : FuelError, E2 : Exception> Result<V, E>.mapClientError(transform: (E) -> E2) = when (this) {
is Result.Success -> this
is Result.Failure -> if (error.response.isClientError) Result.Failure(transform(error)) else this
}
fun FuelError.toGithubException() =
GitHubException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject(GitHubErrorResponse.serializer()))

fun FuelError.toGithubException() = GitHubException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject())

fun FuelError.toBugsnagException() =
BugsnagException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject(BugSnagResponse.serializer()))
BugsnagException(response.body().asString(APPLICATION_JSON_CONTENT_TYPE).toObject())

private const val APPLICATION_JSON_CONTENT_TYPE = "application/json"
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ data class BugSnagResponse(
)

object BugSnagResponseDeserializer : ResponseDeserializable<BugSnagResponse> {
override fun deserialize(content: String) = content.toObject(BugSnagResponse.serializer())
override fun deserialize(content: String): BugSnagResponse = content.toObject()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private fun createRequestBody(bugsnagApiKey: String, appVersion: String, githubW
builderName = "github-actions",
sourceControl = githubActionsSourceControl(appVersion),
metadata = mapOf("github_actions_build_url" to githubWorkflowUrl)
).toJson(BugSnagRequest.serializer())
).toJson()

private fun githubActionsSourceControl(appVersion: String) = SourceControl(
"github",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package flank.scripts.utils

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

private val json by lazy {
Expand All @@ -10,8 +10,6 @@ private val json by lazy {
}
}

fun <T> T.toJson(serializationStrategy: SerializationStrategy<T>) =
json.encodeToString(serializationStrategy, this)
internal inline fun <reified T> T.toJson() = json.encodeToString(this)

fun <T> String.toObject(deserializationStrategy: DeserializationStrategy<T>) =
json.decodeFromString(deserializationStrategy, this)
internal inline fun <reified T> String.toObject() = json.decodeFromString<T>(this)
11 changes: 6 additions & 5 deletions flank-scripts/src/test/kotlin/flank/scripts/FuelTestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FuelTestRunner(klass: Class<*>) : BlockJUnit4ClassRunner(klass) {
val url = request.url.toString()
return when {
url == "https://api.github.com/repos/Flank/flank/git/refs/tags/success" -> request.buildResponse("", 200)
url == "https://api.github.com/repos/flank/flank/releases/latest" && request.headers["Authorization"].contains("token success") -> request.buildResponse(GitHubRelease("v20.08.0").toJson(GitHubRelease.serializer()), 200)
url == "https://api.github.com/repos/flank/flank/releases/latest" && request.headers["Authorization"].contains("token success") -> request.buildResponse(GitHubRelease("v20.08.0").toJson(), 200)
url == "https://api.github.com/repos/flank/flank/commits/success/pulls" -> request.buildResponse(
Json.encodeToString(githubPullRequestTest), 200)
request.isFailedGithubRequest() -> request.buildResponse(githubErrorBody, 422)
Expand All @@ -52,17 +52,18 @@ class FuelTestRunner(klass: Class<*>) : BlockJUnit4ClassRunner(klass) {
))

private fun Request.handleBugsnagResponse() =
if (body.asString("application/json").toObject(BugSnagRequest.serializer()).apiKey == "success") {
if (body.asString("application/json").toObject<BugSnagRequest>().apiKey == "success") {
buildResponse(
body = BugSnagResponse("success")
.toJson(BugSnagResponse.serializer()), statusCode = 200
body = BugSnagResponse("success").toJson(),
statusCode = 200
)
} else {
buildResponse(
body = BugSnagResponse(
status = "failure",
errors = listOf("errors")
).toJson(BugSnagResponse.serializer()), statusCode = 422
).toJson(),
statusCode = 422
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SerializationTest {
)

// when
val actual = testJson.toObject(GitHubErrorResponse.serializer())
val actual = testJson.toObject<GitHubErrorResponse>()

// then
assertThat(actual).isEqualTo(expected)
Expand All @@ -35,7 +35,7 @@ class SerializationTest {
val expected = "{\"provider\":\"a\",\"repository\":\"b\",\"revision\":\"c\"}"

// when
val actual = testObject.toJson(SourceControl.serializer())
val actual = testObject.toJson()

// then
assertThat(actual).isEqualTo(expected)
Expand Down

0 comments on commit 4550fb9

Please sign in to comment.