From 060e80bd5ec02403f5ede2f031124b230cbc0fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Fri, 23 Aug 2024 20:26:47 +0200 Subject: [PATCH] feat(server): allow to refresh generated artifacts on demand (#1598) --- .github/workflows/bindings-server.main.kts | 2 +- .github/workflows/bindings-server.yaml | 2 +- .../workflows/jitbindingserver/Main.kt | 96 ++++++++++++------- 3 files changed, 61 insertions(+), 39 deletions(-) diff --git a/.github/workflows/bindings-server.main.kts b/.github/workflows/bindings-server.main.kts index 8184eb7cbe..f70bc20a9f 100755 --- a/.github/workflows/bindings-server.main.kts +++ b/.github/workflows/bindings-server.main.kts @@ -82,7 +82,7 @@ workflow( cleanMavenLocal() run( - name = "Execute the script using the bindings from the serve - with /binding", + name = "Execute the script using the bindings from the server - with /binding", command = """ mv .github/workflows/test-script-consuming-jit-bindings-old.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-old.main.kts .github/workflows/test-script-consuming-jit-bindings-old.main.kts diff --git a/.github/workflows/bindings-server.yaml b/.github/workflows/bindings-server.yaml index 767516a542..2a25c084a8 100644 --- a/.github/workflows/bindings-server.yaml +++ b/.github/workflows/bindings-server.yaml @@ -49,7 +49,7 @@ jobs: name: 'Clean Maven Local to fetch required POMs again' run: 'rm -rf ~/.m2/repository/' - id: 'step-5' - name: 'Execute the script using the bindings from the serve - with /binding' + name: 'Execute the script using the bindings from the server - with /binding' run: |- mv .github/workflows/test-script-consuming-jit-bindings-old.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-old.main.kts .github/workflows/test-script-consuming-jit-bindings-old.main.kts diff --git a/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt b/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt index 71fae4d3d8..2746370a3a 100644 --- a/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt +++ b/jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt @@ -11,6 +11,7 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts import io.github.typesafegithub.workflows.shared.internal.getGithubToken import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode +import io.ktor.server.application.ApplicationCall import io.ktor.server.application.call import io.ktor.server.application.install import io.ktor.server.engine.embeddedServer @@ -58,6 +59,16 @@ fun main() { metadata() } + route("/refresh") { + route("{owner}/{name}/{version}/{file}") { + artifact(bindingsCache, refresh = true) + } + + route("{owner}/{name}/{file}") { + metadata(refresh = true) + } + } + get("/status") { call.respondText("OK") } @@ -65,8 +76,13 @@ fun main() { }.start(wait = true) } -private fun Route.metadata() { +private fun Route.metadata(refresh: Boolean = false) { get { + if (refresh && !deliverOnRefreshRoute) { + call.respondText(text = "Not found", status = HttpStatusCode.NotFound) + return@get + } + val owner = call.parameters["owner"]!! val name = call.parameters["name"]!! val file = call.parameters["file"]!! @@ -88,29 +104,18 @@ private fun Route.metadata() { } } -private fun Route.artifact(bindingsCache: Cache>>) { +private fun Route.artifact( + bindingsCache: Cache>>, + refresh: Boolean = false, +) { get { - val owner = call.parameters["owner"]!! - val name = call.parameters["name"]!! - val version = call.parameters["version"]!! - val actionCoords = - ActionCoords( - owner = owner, - name = name, - version = version, - ) - println("➡️ Requesting ${actionCoords.prettyPrint}") - val bindingArtifacts = - bindingsCache - .get(actionCoords) { - actionCoords.buildVersionArtifacts()?.let { - Result.success(it) - } ?: Result.failure(object : Throwable() {}) - }.getOrNull() - + val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh) if (bindingArtifacts == null) { call.respondText("Not found", status = HttpStatusCode.NotFound) return@get + } else if (refresh && !deliverOnRefreshRoute) { + call.respondText(text = "OK") + return@get } val file = call.parameters["file"]!! @@ -131,24 +136,8 @@ private fun Route.artifact(bindingsCache: Cache>>, + refresh: Boolean, +): Map? { + val owner = parameters["owner"]!! + val name = parameters["name"]!! + val version = parameters["version"]!! + val actionCoords = + ActionCoords( + owner = owner, + name = name, + version = version, + ) + println("➡️ Requesting ${actionCoords.prettyPrint}") + val bindingArtifacts = + if (refresh) { + actionCoords.buildVersionArtifacts().also { + bindingsCache.put(actionCoords, Result.of(it)) + } + } else { + bindingsCache + .get(actionCoords) { Result.of(actionCoords.buildVersionArtifacts()) } + .getOrNull() + } + return bindingArtifacts +} + +private fun Result.Companion.failure(): Result = failure(object : Throwable() {}) + +private fun Result.Companion.of(value: T?): Result = value?.let { success(it) } ?: failure() + +private val deliverOnRefreshRoute = System.getenv("GWKT_DELIVER_ON_REFRESH").toBoolean()