@@ -11,6 +11,7 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
11
11
import io.github.typesafegithub.workflows.shared.internal.getGithubToken
12
12
import io.ktor.http.ContentType
13
13
import io.ktor.http.HttpStatusCode
14
+ import io.ktor.server.application.ApplicationCall
14
15
import io.ktor.server.application.call
15
16
import io.ktor.server.application.install
16
17
import io.ktor.server.engine.embeddedServer
@@ -54,6 +55,12 @@ fun main() {
54
55
artifact(bindingsCache)
55
56
}
56
57
58
+ route(" /refresh" ) {
59
+ route(" {owner}/{name}/{version}/{file}" ) {
60
+ artifact(bindingsCache, refresh = true )
61
+ }
62
+ }
63
+
57
64
route(" {owner}/{name}/{file}" ) {
58
65
metadata()
59
66
}
@@ -88,26 +95,12 @@ private fun Route.metadata() {
88
95
}
89
96
}
90
97
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
+ ) {
92
102
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)
111
104
if (bindingArtifacts == null ) {
112
105
call.respondText(" Not found" , status = HttpStatusCode .NotFound )
113
106
return @get
@@ -131,24 +124,8 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
131
124
}
132
125
133
126
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)
137
128
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
-
152
129
if (bindingArtifacts == null ) {
153
130
call.respondText(" Not found" , status = HttpStatusCode .NotFound )
154
131
return @head
@@ -160,3 +137,34 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
160
137
}
161
138
}
162
139
}
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