@@ -11,6 +11,7 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
1111import io.github.typesafegithub.workflows.shared.internal.getGithubToken
1212import io.ktor.http.ContentType
1313import io.ktor.http.HttpStatusCode
14+ import io.ktor.server.application.ApplicationCall
1415import io.ktor.server.application.call
1516import io.ktor.server.application.install
1617import 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