Skip to content

Commit e228b0a

Browse files
authored
refactor(server): pass name and version from request to artifact generation (#2186)
This is a preliminary change making it easier to implement #1691. Even without the above project, it makes things more straightforward. In particular, the artifact names happen to match what comes in the request, but in general it doesn't have to be the case. We need to encode certain extra information in Maven artifact's names and owners, which leads to a mismatch where the GitHub action's name isn't equal to the requested Maven artifact's name. This change recognizes it by using different fields in these two circumstances.
1 parent cd58a94 commit e228b0a

16 files changed

Lines changed: 221 additions & 181 deletions

File tree

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.github.oshai.kotlinlogging.KotlinLogging.logger
55
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
66
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
77
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrint
8+
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
89
import io.github.typesafegithub.workflows.mavenbinding.JarArtifact
910
import io.github.typesafegithub.workflows.mavenbinding.TextArtifact
1011
import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
@@ -35,7 +36,7 @@ typealias CachedVersionArtifact = Optional<VersionArtifacts>
3536
private val prefetchScope = CoroutineScope(Dispatchers.IO)
3637

3738
fun Routing.artifactRoutes(
38-
bindingsCache: LoadingCache<ActionCoords, CachedVersionArtifact>,
39+
bindingsCache: LoadingCache<BindingsServerRequest, CachedVersionArtifact>,
3940
prometheusRegistry: PrometheusMeterRegistry? = null,
4041
) {
4142
prometheusRegistry?.let {
@@ -53,15 +54,15 @@ fun Routing.artifactRoutes(
5354

5455
private fun Route.artifact(
5556
prometheusRegistry: PrometheusMeterRegistry?,
56-
bindingsCache: LoadingCache<ActionCoords, CachedVersionArtifact>,
57+
bindingsCache: LoadingCache<BindingsServerRequest, CachedVersionArtifact>,
5758
refresh: Boolean = false,
5859
) {
5960
headArtifact(bindingsCache, prometheusRegistry, refresh)
6061
getArtifact(bindingsCache, prometheusRegistry, refresh)
6162
}
6263

6364
private fun Route.headArtifact(
64-
bindingsCache: LoadingCache<ActionCoords, CachedVersionArtifact>,
65+
bindingsCache: LoadingCache<BindingsServerRequest, CachedVersionArtifact>,
6566
prometheusRegistry: PrometheusMeterRegistry?,
6667
refresh: Boolean,
6768
) {
@@ -81,7 +82,7 @@ private fun Route.headArtifact(
8182
}
8283

8384
private fun Route.getArtifact(
84-
bindingsCache: LoadingCache<ActionCoords, CachedVersionArtifact>,
85+
bindingsCache: LoadingCache<BindingsServerRequest, CachedVersionArtifact>,
8586
prometheusRegistry: PrometheusMeterRegistry?,
8687
refresh: Boolean,
8788
) {
@@ -105,24 +106,32 @@ private fun Route.getArtifact(
105106

106107
internal fun prefetchBindingArtifacts(
107108
coords: Collection<ActionCoords>,
108-
bindingsCache: LoadingCache<ActionCoords, CachedVersionArtifact>,
109+
bindingsCache: LoadingCache<BindingsServerRequest, CachedVersionArtifact>,
109110
) {
110111
prefetchScope.launch {
111-
bindingsCache.getAll(coords)
112+
bindingsCache.getAll(
113+
coords.map {
114+
BindingsServerRequest(
115+
rawName = it.name,
116+
rawVersion = it.version,
117+
actionCoords = it,
118+
)
119+
},
120+
)
112121
}
113122
}
114123

115124
private suspend fun ApplicationCall.toBindingArtifacts(
116125
refresh: Boolean,
117-
bindingsCache: LoadingCache<ActionCoords, CachedVersionArtifact>,
126+
bindingsCache: LoadingCache<BindingsServerRequest, CachedVersionArtifact>,
118127
): VersionArtifacts? {
119-
val actionCoords = parameters.extractActionCoords(extractVersion = true)
128+
val parsedRequest = parameters.parseRequest(extractVersion = true)
120129

121-
logger.info { "➡️ Requesting ${actionCoords.prettyPrint}" }
130+
logger.info { "➡️ Requesting ${parsedRequest.actionCoords.prettyPrint}" }
122131
if (refresh) {
123-
bindingsCache.invalidate(actionCoords)
132+
bindingsCache.invalidate(parsedRequest)
124133
}
125-
return bindingsCache.get(actionCoords).getOrNull()
134+
return bindingsCache.get(parsedRequest).getOrNull()
126135
}
127136

128137
private fun PrometheusMeterRegistry.incrementArtifactCounter(

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.sksamuel.aedile.core.asLoadingCache
66
import com.sksamuel.aedile.core.refreshAfterWrite
77
import io.github.oshai.kotlinlogging.KotlinLogging.logger
88
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
9+
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
910
import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
1011
import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
1112
import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
@@ -67,9 +68,9 @@ fun main() {
6768
}
6869

6970
fun Application.appModule(
70-
buildVersionArtifacts: suspend (ActionCoords, HttpClient) -> VersionArtifacts?,
71+
buildVersionArtifacts: suspend (BindingsServerRequest, HttpClient) -> VersionArtifacts?,
7172
buildPackageArtifacts: suspend (
72-
ActionCoords,
73+
BindingsServerRequest,
7374
String,
7475
(Collection<ActionCoords>) -> Unit,
7576
MeterRegistry,
@@ -103,9 +104,9 @@ fun Application.appModule(
103104
}
104105

105106
private fun buildBindingsCache(
106-
buildVersionArtifacts: suspend (ActionCoords, HttpClient) -> VersionArtifacts?,
107+
buildVersionArtifacts: suspend (BindingsServerRequest, HttpClient) -> VersionArtifacts?,
107108
httpClient: HttpClient,
108-
): LoadingCache<ActionCoords, CachedVersionArtifact> =
109+
): LoadingCache<BindingsServerRequest, CachedVersionArtifact> =
109110
Caffeine
110111
.newBuilder()
111112
.refreshAfterWrite(1.hours)
@@ -114,20 +115,20 @@ private fun buildBindingsCache(
114115

115116
@Suppress("ktlint:standard:function-signature") // Conflict with detekt.
116117
private fun buildMetadataCache(
117-
bindingsCache: LoadingCache<ActionCoords, CachedVersionArtifact>,
118+
bindingsCache: LoadingCache<BindingsServerRequest, CachedVersionArtifact>,
118119
buildPackageArtifacts: suspend (
119-
ActionCoords,
120+
BindingsServerRequest,
120121
String,
121122
(Collection<ActionCoords>) -> Unit,
122123
MeterRegistry,
123124
) -> Map<String, String>,
124125
getGithubAuthToken: () -> String,
125-
): LoadingCache<ActionCoords, CachedMetadataArtifact> =
126+
): LoadingCache<BindingsServerRequest, CachedMetadataArtifact> =
126127
Caffeine
127128
.newBuilder()
128129
.refreshAfterWrite(1.hours)
129130
.recordStats()
130-
.asLoadingCache<ActionCoords, CachedMetadataArtifact> {
131+
.asLoadingCache<BindingsServerRequest, CachedMetadataArtifact> {
131132
buildPackageArtifacts(
132133
it,
133134
getGithubAuthToken(),

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package io.github.typesafegithub.workflows.jitbindingserver
22

33
import com.sksamuel.aedile.core.LoadingCache
44
import io.github.oshai.kotlinlogging.KotlinLogging.logger
5-
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
65
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrintWithoutVersion
6+
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
77
import io.ktor.http.HttpStatusCode
88
import io.ktor.server.response.respondText
99
import io.ktor.server.routing.Route
@@ -19,7 +19,7 @@ private val logger = logger { }
1919
typealias CachedMetadataArtifact = Map<String, String>
2020

2121
fun Routing.metadataRoutes(
22-
metadataCache: LoadingCache<ActionCoords, CachedMetadataArtifact>,
22+
metadataCache: LoadingCache<BindingsServerRequest, CachedMetadataArtifact>,
2323
prometheusRegistry: PrometheusMeterRegistry? = null,
2424
) {
2525
prometheusRegistry?.let {
@@ -38,16 +38,16 @@ fun Routing.metadataRoutes(
3838
}
3939

4040
private fun Route.headMetadata(
41-
metadataCache: LoadingCache<ActionCoords, CachedMetadataArtifact>,
41+
metadataCache: LoadingCache<BindingsServerRequest, CachedMetadataArtifact>,
4242
refresh: Boolean = false,
4343
) {
4444
head {
45-
val actionCoords = call.parameters.extractActionCoords(extractVersion = false)
45+
val request = call.parameters.parseRequest(extractVersion = false)
4646

4747
if (refresh) {
48-
metadataCache.invalidate(actionCoords)
48+
metadataCache.invalidate(request)
4949
}
50-
val metadataArtifacts = metadataCache.get(actionCoords)
50+
val metadataArtifacts = metadataCache.get(request)
5151

5252
if (refresh && !deliverOnRefreshRoute) return@head call.respondText(text = "OK")
5353

@@ -62,18 +62,18 @@ private fun Route.headMetadata(
6262
}
6363

6464
private fun Route.getMetadata(
65-
metadataCache: LoadingCache<ActionCoords, CachedMetadataArtifact>,
65+
metadataCache: LoadingCache<BindingsServerRequest, CachedMetadataArtifact>,
6666
refresh: Boolean = false,
6767
) {
6868
get {
69-
val actionCoords = call.parameters.extractActionCoords(extractVersion = false)
69+
val request = call.parameters.parseRequest(extractVersion = false)
7070

71-
logger.info { "➡️ Requesting metadata for ${actionCoords.prettyPrintWithoutVersion}" }
71+
logger.info { "➡️ Requesting metadata for ${request.actionCoords.prettyPrintWithoutVersion}" }
7272

7373
if (refresh) {
74-
metadataCache.invalidate(actionCoords)
74+
metadataCache.invalidate(request)
7575
}
76-
val metadataArtifacts = metadataCache.get(actionCoords)
76+
val metadataArtifacts = metadataCache.get(request)
7777

7878
if (refresh && !deliverOnRefreshRoute) return@get call.respondText(text = "OK")
7979

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ActionCoords.kt renamed to jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/RequestParsing.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package io.github.typesafegithub.workflows.jitbindingserver
33
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
44
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion
55
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.FULL
6+
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
67
import io.ktor.http.Parameters
78

8-
fun Parameters.extractActionCoords(extractVersion: Boolean): ActionCoords {
9+
fun Parameters.parseRequest(extractVersion: Boolean): BindingsServerRequest {
910
val owner = this["owner"]!!
1011
val nameAndPathAndSignificantVersionParts = this["name"]!!.split("___", limit = 2)
1112
val nameAndPath = nameAndPathAndSignificantVersionParts.first()
@@ -28,5 +29,9 @@ fun Parameters.extractActionCoords(extractVersion: Boolean): ActionCoords {
2829
.takeUnless { it.isBlank() }
2930
val version = if (extractVersion) this["version"]!! else "irrelevant"
3031

31-
return ActionCoords(owner, name, version, significantVersion, path)
32+
return BindingsServerRequest(
33+
rawName = this["name"]!!,
34+
rawVersion = this["version"],
35+
actionCoords = ActionCoords(owner, name, version, significantVersion, path),
36+
)
3237
}

jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ActionCoordsTest.kt

Lines changed: 0 additions & 95 deletions
This file was deleted.

jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoutesTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.typesafegithub.workflows.jitbindingserver
22

33
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
44
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
5+
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
56
import io.github.typesafegithub.workflows.mavenbinding.TextArtifact
67
import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
78
import io.kotest.core.spec.style.FunSpec
@@ -67,7 +68,7 @@ class ArtifactRoutesTest :
6768
test("when artifacts is not available, two requests in a row") {
6869
testApplication {
6970
// Given
70-
val mockBuildVersionArtifacts = mockk<(ActionCoords, HttpClient) -> VersionArtifacts?>()
71+
val mockBuildVersionArtifacts = mockk<(BindingsServerRequest, HttpClient) -> VersionArtifacts?>()
7172
every { mockBuildVersionArtifacts(any(), any()) } returns null
7273
application {
7374
appModule(
@@ -117,7 +118,7 @@ class ArtifactRoutesTest :
117118
test("when binding generation fails and then succeeds, and two requests are made") {
118119
testApplication {
119120
// Given
120-
val mockBuildVersionArtifacts = mockk<(ActionCoords, HttpClient) -> VersionArtifacts?>()
121+
val mockBuildVersionArtifacts = mockk<(BindingsServerRequest, HttpClient) -> VersionArtifacts?>()
121122
every { mockBuildVersionArtifacts(any(), any()) } throws
122123
Exception("An internal error occurred!") andThen
123124
VersionArtifacts(

jit-binding-server/src/test/kotlin/io/github/typesafegithub/workflows/jitbindingserver/MetadataRoutesTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.typesafegithub.workflows.jitbindingserver
22

33
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
4+
import io.github.typesafegithub.workflows.mavenbinding.BindingsServerRequest
45
import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
56
import io.kotest.core.spec.style.FunSpec
67
import io.kotest.matchers.shouldBe
@@ -104,7 +105,7 @@ class MetadataRoutesTest :
104105
val mockBuildPackageArtifacts =
105106
mockk<
106107
(
107-
ActionCoords,
108+
BindingsServerRequest,
108109
String,
109110
(Collection<ActionCoords>) -> Unit,
110111
MeterRegistry?,

0 commit comments

Comments
 (0)