Skip to content

Commit 87b5ebc

Browse files
authored
chore: instantiate HttpClient for calling GitHub in server (#2165)
Part of #2160. To be able to control the client from the server, and e.g. be able to log certain requests it makes.
1 parent 5fde7a9 commit 87b5ebc

File tree

7 files changed

+33
-19
lines changed

7 files changed

+33
-19
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
1010
import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
1111
import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
1212
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthToken
13+
import io.ktor.client.HttpClient
14+
import io.ktor.client.engine.cio.CIO
1315
import io.ktor.http.HttpStatusCode
1416
import io.ktor.server.application.Application
1517
import io.ktor.server.application.ApplicationCall
@@ -60,11 +62,12 @@ fun main() {
6062
}
6163

6264
fun Application.appModule(
63-
buildVersionArtifacts: suspend (ActionCoords) -> VersionArtifacts?,
65+
buildVersionArtifacts: suspend (ActionCoords, HttpClient) -> VersionArtifacts?,
6466
buildPackageArtifacts: suspend (ActionCoords, String, (Collection<ActionCoords>) -> Unit) -> Map<String, String>,
6567
getGithubAuthToken: () -> String,
6668
) {
67-
val bindingsCache = buildBindingsCache(buildVersionArtifacts)
69+
val httpClient = HttpClient(CIO)
70+
val bindingsCache = buildBindingsCache(buildVersionArtifacts, httpClient)
6871
val metadataCache = buildMetadataCache(bindingsCache, buildPackageArtifacts, getGithubAuthToken)
6972
installPlugins(prometheusRegistry)
7073

@@ -77,13 +80,14 @@ fun Application.appModule(
7780
}
7881

7982
private fun buildBindingsCache(
80-
buildVersionArtifacts: suspend (ActionCoords) -> VersionArtifacts?,
83+
buildVersionArtifacts: suspend (ActionCoords, HttpClient) -> VersionArtifacts?,
84+
httpClient: HttpClient,
8185
): LoadingCache<ActionCoords, CachedVersionArtifact> =
8286
Caffeine
8387
.newBuilder()
8488
.refreshAfterWrite(1.hours)
8589
.recordStats()
86-
.asLoadingCache<ActionCoords, CachedVersionArtifact> { buildVersionArtifacts(it) }
90+
.asLoadingCache<ActionCoords, CachedVersionArtifact> { buildVersionArtifacts(it, httpClient) }
8791

8892
@Suppress("ktlint:standard:function-signature") // Conflict with detekt.
8993
private fun buildMetadataCache(

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.github.typesafegithub.workflows.mavenbinding.TextArtifact
66
import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
77
import io.kotest.core.spec.style.FunSpec
88
import io.kotest.matchers.shouldBe
9+
import io.ktor.client.HttpClient
910
import io.ktor.client.request.get
1011
import io.ktor.client.statement.bodyAsText
1112
import io.ktor.http.HttpStatusCode
@@ -22,7 +23,7 @@ class ArtifactRoutesTest :
2223
// Given
2324
application {
2425
appModule(
25-
buildVersionArtifacts = {
26+
buildVersionArtifacts = { _, _ ->
2627
VersionArtifacts(
2728
files = mapOf("some-action-v4.pom" to TextArtifact { "Some POM contents" }),
2829
typingActualSource = TypingActualSource.TYPING_CATALOG,
@@ -48,7 +49,7 @@ class ArtifactRoutesTest :
4849
// Given
4950
application {
5051
appModule(
51-
buildVersionArtifacts = { null },
52+
buildVersionArtifacts = { _, _ -> null },
5253
// Irrelevant for these tests.
5354
buildPackageArtifacts = { _, _, _ -> emptyMap() },
5455
getGithubAuthToken = { "" },
@@ -68,7 +69,7 @@ class ArtifactRoutesTest :
6869
// Given
6970
application {
7071
appModule(
71-
buildVersionArtifacts = { error("An internal error occurred!") },
72+
buildVersionArtifacts = { _, _ -> error("An internal error occurred!") },
7273
// Irrelevant for these tests.
7374
buildPackageArtifacts = { _, _, _ -> emptyMap() },
7475
getGithubAuthToken = { "" },
@@ -86,8 +87,8 @@ class ArtifactRoutesTest :
8687
test("when binding generation fails and then succeeds, and two requests are made") {
8788
testApplication {
8889
// Given
89-
val mockBuildVersionArtifacts = mockk<(ActionCoords) -> VersionArtifacts?>()
90-
every { mockBuildVersionArtifacts(any()) } throws
90+
val mockBuildVersionArtifacts = mockk<(ActionCoords, HttpClient) -> VersionArtifacts?>()
91+
every { mockBuildVersionArtifacts(any(), any()) } throws
9192
Exception("An internal error occurred!") andThen
9293
VersionArtifacts(
9394
files = mapOf("some-action-v4.pom" to TextArtifact { "Some POM contents" }),
@@ -112,7 +113,7 @@ class ArtifactRoutesTest :
112113
// Then
113114
response2.status shouldBe HttpStatusCode.OK
114115

115-
verify(exactly = 2) { mockBuildVersionArtifacts(any()) }
116+
verify(exactly = 2) { mockBuildVersionArtifacts(any(), any()) }
116117
}
117118
}
118119
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MetadataRoutesTest :
2525
},
2626
getGithubAuthToken = { "some-token" },
2727
// Irrelevant for these tests.
28-
buildVersionArtifacts = {
28+
buildVersionArtifacts = { _, _ ->
2929
VersionArtifacts(
3030
files = emptyMap(),
3131
typingActualSource = null,
@@ -53,7 +53,7 @@ class MetadataRoutesTest :
5353
},
5454
getGithubAuthToken = { "some-token" },
5555
// Irrelevant for these tests.
56-
buildVersionArtifacts = {
56+
buildVersionArtifacts = { _, _ ->
5757
VersionArtifacts(
5858
files = emptyMap(),
5959
typingActualSource = null,
@@ -80,7 +80,7 @@ class MetadataRoutesTest :
8080
},
8181
getGithubAuthToken = { "some-token" },
8282
// Irrelevant for these tests.
83-
buildVersionArtifacts = {
83+
buildVersionArtifacts = { _, _ ->
8484
VersionArtifacts(
8585
files = emptyMap(),
8686
typingActualSource = null,
@@ -116,7 +116,7 @@ class MetadataRoutesTest :
116116
buildPackageArtifacts = mockBuildPackageArtifacts,
117117
getGithubAuthToken = { "some-token" },
118118
// Irrelevant for these tests.
119-
buildVersionArtifacts = {
119+
buildVersionArtifacts = { _, _ ->
120120
VersionArtifacts(
121121
files = emptyMap(),
122122
typingActualSource = null,

maven-binding-builder/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ plugins {
55
dependencies {
66
implementation("org.jetbrains.kotlin:kotlin-compiler")
77
api("io.arrow-kt:arrow-core:2.2.0")
8+
api("io.ktor:ktor-client-core:3.3.3")
89
api(projects.actionBindingGenerator)
910
implementation(projects.sharedInternal)
1011
implementation("io.github.oshai:kotlin-logging:7.0.13")
1112

1213
runtimeOnly(projects.githubWorkflowsKt)
1314

15+
testImplementation("io.ktor:ktor-client-cio:3.3.3")
1416
testImplementation(projects.testUtils)
1517
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestFo
77
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
88
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.ActionBinding
99
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.generateBinding
10+
import io.ktor.client.HttpClient
1011
import org.jetbrains.kotlin.cli.common.ExitCode
1112
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
1213
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
@@ -31,9 +32,9 @@ internal data class Jars(
3132
val typingActualSource: TypingActualSource?,
3233
)
3334

34-
internal suspend fun ActionCoords.buildJars(): Jars? {
35+
internal suspend fun ActionCoords.buildJars(httpClient: HttpClient): Jars? {
3536
val binding =
36-
generateBinding(metadataRevision = NewestForVersion).also {
37+
generateBinding(metadataRevision = NewestForVersion, httpClient = httpClient).also {
3738
if (it.isEmpty()) return null
3839
}
3940

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

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

33
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
44
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
5+
import io.ktor.client.HttpClient
56

67
sealed interface Artifact
78

@@ -18,9 +19,12 @@ data class VersionArtifacts(
1819
val typingActualSource: TypingActualSource?,
1920
)
2021

21-
suspend fun buildVersionArtifacts(actionCoords: ActionCoords): VersionArtifacts? {
22+
suspend fun buildVersionArtifacts(
23+
actionCoords: ActionCoords,
24+
httpClient: HttpClient,
25+
): VersionArtifacts? {
2226
with(actionCoords) {
23-
val jars = buildJars() ?: return null
27+
val jars = buildJars(httpClient = httpClient) ?: return null
2428
val pom = buildPomFile()
2529
val mainJarSize by lazy { jars.mainJar().size }
2630
val mainJarMd5Checksum by lazy { jars.mainJar().md5Checksum() }

maven-binding-builder/src/test/kotlin/io/github/typesafegithub/workflows/mavenbinding/JavaBytecodeVersionTest.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCo
44
import io.github.typesafegithub.workflows.testutils.JdkVersionToBytecodeVersion.JDK_8
55
import io.github.typesafegithub.workflows.testutils.shouldHaveBytecodeVersion
66
import io.kotest.core.spec.style.FunSpec
7+
import io.ktor.client.HttpClient
8+
import io.ktor.client.engine.cio.CIO
79

810
class JavaBytecodeVersionTest :
911
FunSpec(
1012
{
1113
test("bindings are built with desired Java bytecode version") {
1214
val actionCoords = ActionCoords("actions", "setup-java", "v3")
13-
val jars = actionCoords.buildJars()!!
15+
val jars = actionCoords.buildJars(httpClient = HttpClient(CIO))!!
1416
jars.randomClassFile() shouldHaveBytecodeVersion JDK_8
1517
}
1618
},

0 commit comments

Comments
 (0)