Skip to content

Commit 3634715

Browse files
committed
feat(server): allow to refresh generated artifacts at will
1 parent 3b6b47c commit 3634715

File tree

4 files changed

+105
-42
lines changed

4 files changed

+105
-42
lines changed

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

Lines changed: 20 additions & 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
@@ -101,6 +101,16 @@ workflow(
101101

102102
cleanMavenLocal()
103103

104+
run(
105+
name = "Execute the script using the bindings from the server - with /refresh",
106+
command = """
107+
mv .github/workflows/test-script-consuming-jit-bindings-refresh.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-refresh.main.kts
108+
.github/workflows/test-script-consuming-jit-bindings-refresh.main.kts
109+
""".trimIndent(),
110+
)
111+
112+
cleanMavenLocal()
113+
104114
run(
105115
name = "Execute the script using bindings but without dependency on library",
106116
command = """
@@ -126,6 +136,15 @@ workflow(
126136
name = "Fetch maven-metadata.xml for nested action",
127137
command = "curl --fail http://localhost:8080/actions/cache__save/maven-metadata.xml | grep '<version>v4</version>'",
128138
)
139+
140+
run(
141+
name = "Fetch maven-metadata.xml for top-level action - with /refresh",
142+
command = "curl --fail http://localhost:8080/refresh/actions/checkout/maven-metadata.xml | grep '<version>v4</version>'",
143+
)
144+
run(
145+
name = "Fetch maven-metadata.xml for nested action - with /refresh",
146+
command = "curl --fail http://localhost:8080/refresh/actions/cache__save/maven-metadata.xml | grep '<version>v4</version>'",
147+
)
129148
}
130149

131150
job(

.github/workflows/bindings-server.yaml

Lines changed: 19 additions & 5 deletions
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
@@ -65,22 +65,36 @@ jobs:
6565
name: 'Clean Maven Local to fetch required POMs again'
6666
run: 'rm -rf ~/.m2/repository/'
6767
- id: 'step-9'
68+
name: 'Execute the script using the bindings from the server - with /refresh'
69+
run: |-
70+
mv .github/workflows/test-script-consuming-jit-bindings-refresh.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings-refresh.main.kts
71+
.github/workflows/test-script-consuming-jit-bindings-refresh.main.kts
72+
- id: 'step-10'
73+
name: 'Clean Maven Local to fetch required POMs again'
74+
run: 'rm -rf ~/.m2/repository/'
75+
- id: 'step-11'
6876
name: 'Execute the script using bindings but without dependency on library'
6977
run: |-
7078
mv .github/workflows/test-served-bindings-depend-on-library.main.do-not-compile.kts .github/workflows/test-served-bindings-depend-on-library.main.kts
7179
.github/workflows/test-served-bindings-depend-on-library.main.kts
72-
- id: 'step-10'
80+
- id: 'step-12'
7381
name: 'Fetch maven-metadata.xml for top-level action - with /binding'
7482
run: 'curl --fail http://localhost:8080/binding/actions/checkout/maven-metadata.xml | grep ''<version>v4</version>'''
75-
- id: 'step-11'
83+
- id: 'step-13'
7684
name: 'Fetch maven-metadata.xml for nested action - with /binding'
7785
run: 'curl --fail http://localhost:8080/binding/actions/cache__save/maven-metadata.xml | grep ''<version>v4</version>'''
78-
- id: 'step-12'
86+
- id: 'step-14'
7987
name: 'Fetch maven-metadata.xml for top-level action'
8088
run: 'curl --fail http://localhost:8080/actions/checkout/maven-metadata.xml | grep ''<version>v4</version>'''
81-
- id: 'step-13'
89+
- id: 'step-15'
8290
name: 'Fetch maven-metadata.xml for nested action'
8391
run: 'curl --fail http://localhost:8080/actions/cache__save/maven-metadata.xml | grep ''<version>v4</version>'''
92+
- id: 'step-16'
93+
name: 'Fetch maven-metadata.xml for top-level action - with /refresh'
94+
run: 'curl --fail http://localhost:8080/refresh/actions/checkout/maven-metadata.xml | grep ''<version>v4</version>'''
95+
- id: 'step-17'
96+
name: 'Fetch maven-metadata.xml for nested action - with /refresh'
97+
run: 'curl --fail http://localhost:8080/refresh/actions/cache__save/maven-metadata.xml | grep ''<version>v4</version>'''
8498
deploy:
8599
name: 'Deploy to DockerHub'
86100
runs-on: 'ubuntu-latest'
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: 44 additions & 36 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
@@ -54,6 +55,12 @@ fun main() {
5455
artifact(bindingsCache)
5556
}
5657

58+
route("/refresh") {
59+
route("{owner}/{name}/{version}/{file}") {
60+
artifact(bindingsCache, refresh = true)
61+
}
62+
}
63+
5764
route("{owner}/{name}/{file}") {
5865
metadata()
5966
}
@@ -88,26 +95,12 @@ private fun Route.metadata() {
8895
}
8996
}
9097

91-
private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>) {
98+
private fun Route.artifact(
99+
bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>,
100+
refresh: Boolean = false,
101+
) {
92102
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-
103+
val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
111104
if (bindingArtifacts == null) {
112105
call.respondText("Not found", status = HttpStatusCode.NotFound)
113106
return@get
@@ -131,24 +124,8 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
131124
}
132125

133126
head {
134-
val owner = call.parameters["owner"]!!
135-
val name = call.parameters["name"]!!
136-
val version = call.parameters["version"]!!
127+
val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
137128
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-
152129
if (bindingArtifacts == null) {
153130
call.respondText("Not found", status = HttpStatusCode.NotFound)
154131
return@head
@@ -160,3 +137,34 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
160137
}
161138
}
162139
}
140+
141+
private suspend fun ApplicationCall.toBindingArtifacts(
142+
bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>,
143+
refresh: Boolean,
144+
): Map<String, Artifact>? {
145+
val owner = parameters["owner"]!!
146+
val name = parameters["name"]!!
147+
val version = parameters["version"]!!
148+
val actionCoords =
149+
ActionCoords(
150+
owner = owner,
151+
name = name,
152+
version = version,
153+
)
154+
println("➡️ Requesting ${actionCoords.prettyPrint}")
155+
val bindingArtifacts =
156+
if (refresh) {
157+
actionCoords.buildVersionArtifacts().also {
158+
bindingsCache.put(actionCoords, Result.of(it))
159+
}
160+
} else {
161+
bindingsCache
162+
.get(actionCoords) { Result.of(actionCoords.buildVersionArtifacts()) }
163+
.getOrNull()
164+
}
165+
return bindingArtifacts
166+
}
167+
168+
private fun Result.Companion.failure(): Result<Nothing> = failure(object : Throwable() {})
169+
170+
private fun <T> Result.Companion.of(value: T?): Result<T> = value?.let { success(it) } ?: failure()

0 commit comments

Comments
 (0)