Skip to content

Commit c659f55

Browse files
committed
feat(server): allow to refresh generated artifacts at will
1 parent abfd5e0 commit c659f55

File tree

4 files changed

+83
-39
lines changed

4 files changed

+83
-39
lines changed

.github/workflows/bindings-server.main.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ workflow(
8282
cleanMavenLocal()
8383

8484
run(
85-
name = "Execute the script using the bindings from the serve - with /binding",
85+
name = "Execute the script using the bindings from the server - with /binding",
8686
command = """
8787
mv .github/workflows/test-script-consuming-jit-bindings-old.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-old.main.kts
8888
.github/workflows/test-script-consuming-jit-bindings-old.main.kts

.github/workflows/bindings-server.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
name: 'Clean Maven Local to fetch required POMs again'
5050
run: 'rm -rf ~/.m2/repository/'
5151
- id: 'step-5'
52-
name: 'Execute the script using the bindings from the serve - with /binding'
52+
name: 'Execute the script using the bindings from the server - with /binding'
5353
run: |-
5454
mv .github/workflows/test-script-consuming-jit-bindings-old.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-old.main.kts
5555
.github/workflows/test-script-consuming-jit-bindings-old.main.kts
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env kotlin
2+
@file:Repository("https://repo.maven.apache.org/maven2/")
3+
@file:DependsOn("io.github.typesafegithub:github-workflows-kt:1.13.0")
4+
5+
@file:Repository("http://localhost:8080/refresh/")
6+
7+
// Regular, top-level action.
8+
@file:DependsOn("actions:checkout:v4")
9+
10+
// Nested action.
11+
@file:DependsOn("gradle:actions__setup-gradle:v3")
12+
13+
// Using specific version.
14+
@file:DependsOn("actions:cache:v3.3.3")
15+
16+
import io.github.typesafegithub.workflows.actions.actions.Cache
17+
import io.github.typesafegithub.workflows.actions.actions.Checkout
18+
import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradle
19+
20+
println(Checkout())
21+
println(ActionsSetupGradle())
22+
println(Cache(path = listOf("some-path"), key = "some-key"))

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
1111
import io.github.typesafegithub.workflows.shared.internal.getGithubToken
1212
import io.ktor.http.ContentType
1313
import io.ktor.http.HttpStatusCode
14+
import io.ktor.server.application.ApplicationCall
1415
import io.ktor.server.application.call
1516
import io.ktor.server.application.install
1617
import io.ktor.server.engine.embeddedServer
@@ -58,15 +59,30 @@ fun main() {
5859
metadata()
5960
}
6061

62+
route("/refresh") {
63+
route("{owner}/{name}/{version}/{file}") {
64+
artifact(bindingsCache, refresh = true)
65+
}
66+
67+
route("{owner}/{name}/{file}") {
68+
metadata(refresh = true)
69+
}
70+
}
71+
6172
get("/status") {
6273
call.respondText("OK")
6374
}
6475
}
6576
}.start(wait = true)
6677
}
6778

68-
private fun Route.metadata() {
79+
private fun Route.metadata(refresh: Boolean = false) {
6980
get {
81+
if (refresh && !deliverOnRefreshRoute) {
82+
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
83+
return@get
84+
}
85+
7086
val owner = call.parameters["owner"]!!
7187
val name = call.parameters["name"]!!
7288
val file = call.parameters["file"]!!
@@ -88,29 +104,18 @@ private fun Route.metadata() {
88104
}
89105
}
90106

91-
private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>) {
107+
private fun Route.artifact(
108+
bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>,
109+
refresh: Boolean = false,
110+
) {
92111
get {
93-
val owner = call.parameters["owner"]!!
94-
val name = call.parameters["name"]!!
95-
val version = call.parameters["version"]!!
96-
val actionCoords =
97-
ActionCoords(
98-
owner = owner,
99-
name = name,
100-
version = version,
101-
)
102-
println("➡️ Requesting ${actionCoords.prettyPrint}")
103-
val bindingArtifacts =
104-
bindingsCache
105-
.get(actionCoords) {
106-
actionCoords.buildVersionArtifacts()?.let {
107-
Result.success(it)
108-
} ?: Result.failure(object : Throwable() {})
109-
}.getOrNull()
110-
112+
val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
111113
if (bindingArtifacts == null) {
112114
call.respondText("Not found", status = HttpStatusCode.NotFound)
113115
return@get
116+
} else if (refresh && !deliverOnRefreshRoute) {
117+
call.respondText(text = "OK")
118+
return@get
114119
}
115120

116121
val file = call.parameters["file"]!!
@@ -131,24 +136,8 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
131136
}
132137

133138
head {
134-
val owner = call.parameters["owner"]!!
135-
val name = call.parameters["name"]!!
136-
val version = call.parameters["version"]!!
139+
val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
137140
val file = call.parameters["file"]!!
138-
val actionCoords =
139-
ActionCoords(
140-
owner = owner,
141-
name = name,
142-
version = version,
143-
)
144-
val bindingArtifacts =
145-
bindingsCache
146-
.get(actionCoords) {
147-
actionCoords.buildVersionArtifacts()?.let {
148-
Result.success(it)
149-
} ?: Result.failure(object : Throwable() {})
150-
}.getOrNull()
151-
152141
if (bindingArtifacts == null) {
153142
call.respondText("Not found", status = HttpStatusCode.NotFound)
154143
return@head
@@ -160,3 +149,36 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
160149
}
161150
}
162151
}
152+
153+
private suspend fun ApplicationCall.toBindingArtifacts(
154+
bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>,
155+
refresh: Boolean,
156+
): Map<String, Artifact>? {
157+
val owner = parameters["owner"]!!
158+
val name = parameters["name"]!!
159+
val version = parameters["version"]!!
160+
val actionCoords =
161+
ActionCoords(
162+
owner = owner,
163+
name = name,
164+
version = version,
165+
)
166+
println("➡️ Requesting ${actionCoords.prettyPrint}")
167+
val bindingArtifacts =
168+
if (refresh) {
169+
actionCoords.buildVersionArtifacts().also {
170+
bindingsCache.put(actionCoords, Result.of(it))
171+
}
172+
} else {
173+
bindingsCache
174+
.get(actionCoords) { Result.of(actionCoords.buildVersionArtifacts()) }
175+
.getOrNull()
176+
}
177+
return bindingArtifacts
178+
}
179+
180+
private fun Result.Companion.failure(): Result<Nothing> = failure(object : Throwable() {})
181+
182+
private fun <T> Result.Companion.of(value: T?): Result<T> = value?.let { success(it) } ?: failure()
183+
184+
private val deliverOnRefreshRoute = System.getenv("GWKT_DELIVER_ON_REFRESH").toBoolean()

0 commit comments

Comments
 (0)