Skip to content

Commit

Permalink
feat(server): allow to refresh generated artifacts at will
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Aug 22, 2024
1 parent bc71f66 commit 0f3c050
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bindings-server.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/bindings-server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"))
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,15 +59,30 @@ 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")
}
}
}.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"]!!
Expand All @@ -88,29 +104,18 @@ private fun Route.metadata() {
}
}

private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>) {
private fun Route.artifact(
bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>,
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"]!!
Expand All @@ -131,24 +136,8 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
}

head {
val owner = call.parameters["owner"]!!
val name = call.parameters["name"]!!
val version = call.parameters["version"]!!
val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
val file = call.parameters["file"]!!
val actionCoords =
ActionCoords(
owner = owner,
name = name,
version = version,
)
val bindingArtifacts =
bindingsCache
.get(actionCoords) {
actionCoords.buildVersionArtifacts()?.let {
Result.success(it)
} ?: Result.failure(object : Throwable() {})
}.getOrNull()

if (bindingArtifacts == null) {
call.respondText("Not found", status = HttpStatusCode.NotFound)
return@head
Expand All @@ -160,3 +149,36 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
}
}
}

private suspend fun ApplicationCall.toBindingArtifacts(
bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>,
refresh: Boolean,
): Map<String, Artifact>? {
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<Nothing> = failure(object : Throwable() {})

private fun <T> Result.Companion.of(value: T?): Result<T> = value?.let { success(it) } ?: failure()

private val deliverOnRefreshRoute = System.getenv("GWKT_DELIVER_ON_REFRESH").toBoolean()

0 comments on commit 0f3c050

Please sign in to comment.