From 7e772c46df10587ade8ddc044cf8f63c65ab8e6d 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 | 21 ++++- .github/workflows/bindings-server.yaml | 24 ++++-- ...t-bindings-refresh.main.do-not-compile.kts | 22 +++++ .../workflows/jitbindingserver/Main.kt | 80 ++++++++++--------- 4 files changed, 105 insertions(+), 42 deletions(-) create mode 100644 .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..610a61e0b2 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 @@ -101,6 +101,16 @@ workflow( cleanMavenLocal() + run( + name = "Execute the script using the bindings from the server - with /refresh", + command = """ + mv .github/workflows/test-script-consuming-jit-bindings-refresh.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-refresh.main.kts + .github/workflows/test-script-consuming-jit-bindings-refresh.main.kts + """.trimIndent(), + ) + + cleanMavenLocal() + run( name = "Execute the script using bindings but without dependency on library", command = """ @@ -126,6 +136,15 @@ workflow( name = "Fetch maven-metadata.xml for nested action", command = "curl --fail http://localhost:8080/actions/cache__save/maven-metadata.xml | grep 'v4'", ) + + run( + name = "Fetch maven-metadata.xml for top-level action - with /refresh", + command = "curl --fail http://localhost:8080/refresh/actions/checkout/maven-metadata.xml | grep 'v4'", + ) + run( + name = "Fetch maven-metadata.xml for nested action - with /refresh", + command = "curl --fail http://localhost:8080/refresh/actions/cache__save/maven-metadata.xml | grep 'v4'", + ) } job( diff --git a/.github/workflows/bindings-server.yaml b/.github/workflows/bindings-server.yaml index 767516a542..1a9d2221bd 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 @@ -65,22 +65,36 @@ jobs: name: 'Clean Maven Local to fetch required POMs again' run: 'rm -rf ~/.m2/repository/' - id: 'step-9' + name: 'Execute the script using the bindings from the server - with /refresh' + run: |- + mv .github/workflows/test-script-consuming-jit-bindings-refresh.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-refresh.main.kts + .github/workflows/test-script-consuming-jit-bindings-refresh.main.kts + - id: 'step-10' + name: 'Clean Maven Local to fetch required POMs again' + run: 'rm -rf ~/.m2/repository/' + - id: 'step-11' name: 'Execute the script using bindings but without dependency on library' run: |- mv .github/workflows/test-served-bindings-depend-on-library.main.do-not-compile.kts .github/workflows/test-served-bindings-depend-on-library.main.kts .github/workflows/test-served-bindings-depend-on-library.main.kts - - id: 'step-10' + - id: 'step-12' name: 'Fetch maven-metadata.xml for top-level action - with /binding' run: 'curl --fail http://localhost:8080/binding/actions/checkout/maven-metadata.xml | grep ''v4''' - - id: 'step-11' + - id: 'step-13' name: 'Fetch maven-metadata.xml for nested action - with /binding' run: 'curl --fail http://localhost:8080/binding/actions/cache__save/maven-metadata.xml | grep ''v4''' - - id: 'step-12' + - id: 'step-14' name: 'Fetch maven-metadata.xml for top-level action' run: 'curl --fail http://localhost:8080/actions/checkout/maven-metadata.xml | grep ''v4''' - - id: 'step-13' + - id: 'step-15' name: 'Fetch maven-metadata.xml for nested action' run: 'curl --fail http://localhost:8080/actions/cache__save/maven-metadata.xml | grep ''v4''' + - id: 'step-16' + name: 'Fetch maven-metadata.xml for top-level action - with /refresh' + run: 'curl --fail http://localhost:8080/refresh/actions/checkout/maven-metadata.xml | grep ''v4''' + - id: 'step-17' + name: 'Fetch maven-metadata.xml for nested action - with /refresh' + run: 'curl --fail http://localhost:8080/refresh/actions/cache__save/maven-metadata.xml | grep ''v4''' deploy: name: 'Deploy to DockerHub' runs-on: 'ubuntu-latest' 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 100644 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..bae2eb452d 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 @@ -54,6 +55,12 @@ fun main() { artifact(bindingsCache) } + route("/refresh") { + route("{owner}/{name}/{version}/{file}") { + artifact(bindingsCache, refresh = true) + } + } + route("{owner}/{name}/{file}") { metadata() } @@ -88,26 +95,12 @@ 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 @@ -131,24 +124,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()