From 43ce61450455548620aad72405a77df7fc87d6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Fri, 16 Aug 2024 12:04:49 +0200 Subject: [PATCH] feat(server): allow to refresh generated artifacts at will --- .github/workflows/bindings-server.main.kts | 2 +- .github/workflows/bindings-server.yaml | 2 +- ...t-bindings-refresh.main.do-not-compile.kts | 22 +++++ .../workflows/jitbindingserver/Main.kt | 96 ++++++++++++------- 4 files changed, 83 insertions(+), 39 deletions(-) create mode 100755 .github/workflows/test-script-consuming-jit-bindings-refresh.main.do-not-compile.kts 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/.github/workflows/test-script-consuming-jit-bindings-refresh.main.do-not-compile.kts b/.github/workflows/test-script-consuming-jit-bindings-refresh.main.do-not-compile.kts new file mode 100755 index 0000000000..4ca7cf46c4 --- /dev/null +++ b/.github/workflows/test-script-consuming-jit-bindings-refresh.main.do-not-compile.kts @@ -0,0 +1,22 @@ +#!/usr/bin/env kotlin +@file:Repository("https://repo.maven.apache.org/maven2/") +@file:DependsOn("io.github.typesafegithub:github-workflows-kt:1.13.0") + +@file:Repository("http://localhost:8080/refresh/") + +// Regular, top-level action. +@file:DependsOn("actions:checkout:v4") + +// Nested action. +@file:DependsOn("gradle:actions__setup-gradle:v3") + +// Using specific version. +@file:DependsOn("actions:cache:v3.3.3") + +import io.github.typesafegithub.workflows.actions.actions.Cache +import io.github.typesafegithub.workflows.actions.actions.Checkout +import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle + +println(Checkout()) +println(ActionsSetupGradle()) +println(Cache(path = listOf("some-path"), key = "some-key")) 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()