Skip to content

Commit bce8663

Browse files
authored
refactor(server): allow injecting logic for building package artifacts (#1941)
Part of #1938. To be able to mock it in tests.
1 parent 737380d commit bce8663

File tree

4 files changed

+43
-30
lines changed

4 files changed

+43
-30
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import com.sksamuel.aedile.core.refreshAfterWrite
77
import io.github.oshai.kotlinlogging.KotlinLogging.logger
88
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
99
import io.github.typesafegithub.workflows.mavenbinding.Artifact
10+
import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
1011
import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
12+
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthToken
1113
import io.ktor.http.HttpStatusCode
1214
import io.ktor.server.application.Application
1315
import io.ktor.server.application.ApplicationCall
@@ -48,19 +50,26 @@ fun main() {
4850
logger.error(throwable) { "Uncaught exception in thread $thread" }
4951
}
5052
embeddedServer(Netty, port = 8080) {
51-
appModule(buildVersionArtifacts = ::buildVersionArtifacts)
53+
appModule(
54+
buildVersionArtifacts = ::buildVersionArtifacts,
55+
buildPackageArtifacts = ::buildPackageArtifacts,
56+
)
5257
}.start(wait = true)
5358
}
5459

55-
fun Application.appModule(buildVersionArtifacts: (ActionCoords) -> Map<String, Artifact>?) {
60+
fun Application.appModule(
61+
buildVersionArtifacts: (ActionCoords) -> Map<String, Artifact>?,
62+
buildPackageArtifacts: suspend (ActionCoords, String, (Collection<ActionCoords>) -> Unit) -> Map<String, String>,
63+
) {
5664
val bindingsCache = buildBindingsCache(buildVersionArtifacts)
65+
val metadataCache = buildMetadataCache(bindingsCache, buildPackageArtifacts)
5766
installPlugins(prometheusRegistry)
5867

5968
routing {
6069
internalRoutes(prometheusRegistry)
6170

6271
artifactRoutes(bindingsCache, prometheusRegistry)
63-
metadataRoutes(bindingsCache, prometheusRegistry)
72+
metadataRoutes(metadataCache, prometheusRegistry)
6473
}
6574
}
6675

@@ -73,6 +82,25 @@ private fun buildBindingsCache(
7382
.recordStats()
7483
.asLoadingCache<ActionCoords, ArtifactResult> { runCatching { buildVersionArtifacts(it) } }
7584

85+
@Suppress("ktlint:standard:function-signature") // Conflict with detekt.
86+
private fun buildMetadataCache(
87+
bindingsCache: LoadingCache<ActionCoords, ArtifactResult>,
88+
buildPackageArtifacts: suspend (ActionCoords, String, (Collection<ActionCoords>) -> Unit) -> Map<String, String>,
89+
): LoadingCache<ActionCoords, MetadataResult> =
90+
Caffeine
91+
.newBuilder()
92+
.refreshAfterWrite(1.hours)
93+
.recordStats()
94+
.asLoadingCache<ActionCoords, MetadataResult> {
95+
runCatching {
96+
buildPackageArtifacts(
97+
it,
98+
getGithubAuthToken(),
99+
{ coords -> prefetchBindingArtifacts(coords, bindingsCache) },
100+
)
101+
}
102+
}
103+
76104
val deliverOnRefreshRoute = System.getenv("GWKT_DELIVER_ON_REFRESH").toBoolean()
77105

78106
suspend fun ApplicationCall.respondNotFound() = respondText(text = "Not found", status = HttpStatusCode.NotFound)

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,25 @@
11
package io.github.typesafegithub.workflows.jitbindingserver
22

3-
import com.github.benmanes.caffeine.cache.Caffeine
43
import com.sksamuel.aedile.core.LoadingCache
5-
import com.sksamuel.aedile.core.asLoadingCache
6-
import com.sksamuel.aedile.core.refreshAfterWrite
74
import io.github.oshai.kotlinlogging.KotlinLogging.logger
85
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
96
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrintWithoutVersion
10-
import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
11-
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthToken
127
import io.ktor.server.response.respondText
138
import io.ktor.server.routing.Route
149
import io.ktor.server.routing.Routing
1510
import io.ktor.server.routing.get
1611
import io.ktor.server.routing.route
1712
import io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics
1813
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
19-
import kotlin.time.Duration.Companion.hours
2014

2115
private val logger = logger { }
2216

2317
typealias MetadataResult = Result<Map<String, String>>
2418

25-
@Suppress("ktlint:standard:function-signature") // Conflict with detekt.
26-
private fun buildMetadataCache(
27-
bindingsCache: LoadingCache<ActionCoords, ArtifactResult>,
28-
): LoadingCache<ActionCoords, MetadataResult> =
29-
Caffeine
30-
.newBuilder()
31-
.refreshAfterWrite(1.hours)
32-
.recordStats()
33-
.asLoadingCache<ActionCoords, MetadataResult> {
34-
runCatching {
35-
it.buildPackageArtifacts(
36-
githubAuthToken = getGithubAuthToken(),
37-
{ coords -> prefetchBindingArtifacts(coords, bindingsCache) },
38-
)
39-
}
40-
}
41-
4219
fun Routing.metadataRoutes(
43-
bindingsCache: LoadingCache<ActionCoords, ArtifactResult>,
20+
metadataCache: LoadingCache<ActionCoords, MetadataResult>,
4421
prometheusRegistry: PrometheusMeterRegistry? = null,
4522
) {
46-
val metadataCache = buildMetadataCache(bindingsCache)
4723
prometheusRegistry?.let {
4824
CaffeineCacheMetrics.monitor(it, metadataCache.underlying(), "metadata_cache")
4925
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class ArtifactRoutesTest :
2323
buildVersionArtifacts = {
2424
mapOf("some-action-v4.pom" to TextArtifact { "Some POM contents" })
2525
},
26+
// Irrelevant for these tests.
27+
buildPackageArtifacts = { _, _, _ -> emptyMap() },
2628
)
2729
}
2830

@@ -41,6 +43,8 @@ class ArtifactRoutesTest :
4143
application {
4244
appModule(
4345
buildVersionArtifacts = { null },
46+
// Irrelevant for these tests.
47+
buildPackageArtifacts = { _, _, _ -> emptyMap() },
4448
)
4549
}
4650

@@ -58,6 +62,8 @@ class ArtifactRoutesTest :
5862
application {
5963
appModule(
6064
buildVersionArtifacts = { error("An internal error occurred!") },
65+
// Irrelevant for these tests.
66+
buildPackageArtifacts = { _, _, _ -> emptyMap() },
6167
)
6268
}
6369

@@ -79,6 +85,8 @@ class ArtifactRoutesTest :
7985
application {
8086
appModule(
8187
buildVersionArtifacts = mockBuildVersionArtifacts,
88+
// Irrelevant for these tests.
89+
buildPackageArtifacts = { _, _, _ -> emptyMap() },
8290
)
8391
}
8492

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/PackageArtifactsBuilding.kt

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

33
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
44

5-
suspend fun ActionCoords.buildPackageArtifacts(
5+
suspend fun buildPackageArtifacts(
6+
actionCoords: ActionCoords,
67
githubAuthToken: String,
78
prefetchBindingArtifacts: (Collection<ActionCoords>) -> Unit,
89
): Map<String, String> {
910
val mavenMetadata =
10-
buildMavenMetadataFile(
11+
actionCoords.buildMavenMetadataFile(
1112
githubAuthToken = githubAuthToken,
1213
prefetchBindingArtifacts = prefetchBindingArtifacts,
1314
) ?: return emptyMap()

0 commit comments

Comments
 (0)