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 19, 2024
1 parent 3b6b47c commit 3634715
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 42 deletions.
21 changes: 20 additions & 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 All @@ -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 = """
Expand All @@ -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 '<version>v4</version>'",
)

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 '<version>v4</version>'",
)
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 '<version>v4</version>'",
)
}

job(
Expand Down
24 changes: 19 additions & 5 deletions .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 All @@ -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 ''<version>v4</version>'''
- 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 ''<version>v4</version>'''
- 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 ''<version>v4</version>'''
- 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 ''<version>v4</version>'''
- 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 ''<version>v4</version>'''
- 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 ''<version>v4</version>'''
deploy:
name: 'Deploy to DockerHub'
runs-on: 'ubuntu-latest'
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 @@ -54,6 +55,12 @@ fun main() {
artifact(bindingsCache)
}

route("/refresh") {
route("{owner}/{name}/{version}/{file}") {
artifact(bindingsCache, refresh = true)
}
}

route("{owner}/{name}/{file}") {
metadata()
}
Expand Down Expand Up @@ -88,26 +95,12 @@ 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
Expand All @@ -131,24 +124,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 +137,34 @@ 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()

0 comments on commit 3634715

Please sign in to comment.