Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts
import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
import io.github.typesafegithub.workflows.shared.internal.getGithubAuthToken
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.application.ApplicationCall
Expand Down Expand Up @@ -60,11 +62,12 @@ fun main() {
}

fun Application.appModule(
buildVersionArtifacts: suspend (ActionCoords) -> VersionArtifacts?,
buildVersionArtifacts: suspend (ActionCoords, HttpClient) -> VersionArtifacts?,
buildPackageArtifacts: suspend (ActionCoords, String, (Collection<ActionCoords>) -> Unit) -> Map<String, String>,
getGithubAuthToken: () -> String,
) {
val bindingsCache = buildBindingsCache(buildVersionArtifacts)
val httpClient = HttpClient(CIO)
val bindingsCache = buildBindingsCache(buildVersionArtifacts, httpClient)
val metadataCache = buildMetadataCache(bindingsCache, buildPackageArtifacts, getGithubAuthToken)
installPlugins(prometheusRegistry)

Expand All @@ -77,13 +80,14 @@ fun Application.appModule(
}

private fun buildBindingsCache(
buildVersionArtifacts: suspend (ActionCoords) -> VersionArtifacts?,
buildVersionArtifacts: suspend (ActionCoords, HttpClient) -> VersionArtifacts?,
httpClient: HttpClient,
): LoadingCache<ActionCoords, CachedVersionArtifact> =
Caffeine
.newBuilder()
.refreshAfterWrite(1.hours)
.recordStats()
.asLoadingCache<ActionCoords, CachedVersionArtifact> { buildVersionArtifacts(it) }
.asLoadingCache<ActionCoords, CachedVersionArtifact> { buildVersionArtifacts(it, httpClient) }

@Suppress("ktlint:standard:function-signature") // Conflict with detekt.
private fun buildMetadataCache(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.typesafegithub.workflows.mavenbinding.TextArtifact
import io.github.typesafegithub.workflows.mavenbinding.VersionArtifacts
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
Expand All @@ -22,7 +23,7 @@ class ArtifactRoutesTest :
// Given
application {
appModule(
buildVersionArtifacts = {
buildVersionArtifacts = { _, _ ->
VersionArtifacts(
files = mapOf("some-action-v4.pom" to TextArtifact { "Some POM contents" }),
typingActualSource = TypingActualSource.TYPING_CATALOG,
Expand All @@ -48,7 +49,7 @@ class ArtifactRoutesTest :
// Given
application {
appModule(
buildVersionArtifacts = { null },
buildVersionArtifacts = { _, _ -> null },
// Irrelevant for these tests.
buildPackageArtifacts = { _, _, _ -> emptyMap() },
getGithubAuthToken = { "" },
Expand All @@ -68,7 +69,7 @@ class ArtifactRoutesTest :
// Given
application {
appModule(
buildVersionArtifacts = { error("An internal error occurred!") },
buildVersionArtifacts = { _, _ -> error("An internal error occurred!") },
// Irrelevant for these tests.
buildPackageArtifacts = { _, _, _ -> emptyMap() },
getGithubAuthToken = { "" },
Expand All @@ -86,8 +87,8 @@ class ArtifactRoutesTest :
test("when binding generation fails and then succeeds, and two requests are made") {
testApplication {
// Given
val mockBuildVersionArtifacts = mockk<(ActionCoords) -> VersionArtifacts?>()
every { mockBuildVersionArtifacts(any()) } throws
val mockBuildVersionArtifacts = mockk<(ActionCoords, HttpClient) -> VersionArtifacts?>()
every { mockBuildVersionArtifacts(any(), any()) } throws
Exception("An internal error occurred!") andThen
VersionArtifacts(
files = mapOf("some-action-v4.pom" to TextArtifact { "Some POM contents" }),
Expand All @@ -112,7 +113,7 @@ class ArtifactRoutesTest :
// Then
response2.status shouldBe HttpStatusCode.OK

verify(exactly = 2) { mockBuildVersionArtifacts(any()) }
verify(exactly = 2) { mockBuildVersionArtifacts(any(), any()) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MetadataRoutesTest :
},
getGithubAuthToken = { "some-token" },
// Irrelevant for these tests.
buildVersionArtifacts = {
buildVersionArtifacts = { _, _ ->
VersionArtifacts(
files = emptyMap(),
typingActualSource = null,
Expand Down Expand Up @@ -53,7 +53,7 @@ class MetadataRoutesTest :
},
getGithubAuthToken = { "some-token" },
// Irrelevant for these tests.
buildVersionArtifacts = {
buildVersionArtifacts = { _, _ ->
VersionArtifacts(
files = emptyMap(),
typingActualSource = null,
Expand All @@ -80,7 +80,7 @@ class MetadataRoutesTest :
},
getGithubAuthToken = { "some-token" },
// Irrelevant for these tests.
buildVersionArtifacts = {
buildVersionArtifacts = { _, _ ->
VersionArtifacts(
files = emptyMap(),
typingActualSource = null,
Expand Down Expand Up @@ -116,7 +116,7 @@ class MetadataRoutesTest :
buildPackageArtifacts = mockBuildPackageArtifacts,
getGithubAuthToken = { "some-token" },
// Irrelevant for these tests.
buildVersionArtifacts = {
buildVersionArtifacts = { _, _ ->
VersionArtifacts(
files = emptyMap(),
typingActualSource = null,
Expand Down
2 changes: 2 additions & 0 deletions maven-binding-builder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ plugins {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-compiler")
api("io.arrow-kt:arrow-core:2.2.0")
api("io.ktor:ktor-client-core:3.3.3")
api(projects.actionBindingGenerator)
implementation(projects.sharedInternal)
implementation("io.github.oshai:kotlin-logging:7.0.13")

runtimeOnly(projects.githubWorkflowsKt)

testImplementation("io.ktor:ktor-client-cio:3.3.3")
testImplementation(projects.testUtils)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestFo
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.ActionBinding
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.generateBinding
import io.ktor.client.HttpClient
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
Expand All @@ -31,9 +32,9 @@ internal data class Jars(
val typingActualSource: TypingActualSource?,
)

internal suspend fun ActionCoords.buildJars(): Jars? {
internal suspend fun ActionCoords.buildJars(httpClient: HttpClient): Jars? {
val binding =
generateBinding(metadataRevision = NewestForVersion).also {
generateBinding(metadataRevision = NewestForVersion, httpClient = httpClient).also {
if (it.isEmpty()) return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.typesafegithub.workflows.mavenbinding

import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingActualSource
import io.ktor.client.HttpClient

sealed interface Artifact

Expand All @@ -18,9 +19,12 @@ data class VersionArtifacts(
val typingActualSource: TypingActualSource?,
)

suspend fun buildVersionArtifacts(actionCoords: ActionCoords): VersionArtifacts? {
suspend fun buildVersionArtifacts(
actionCoords: ActionCoords,
httpClient: HttpClient,
): VersionArtifacts? {
with(actionCoords) {
val jars = buildJars() ?: return null
val jars = buildJars(httpClient = httpClient) ?: return null
val pom = buildPomFile()
val mainJarSize by lazy { jars.mainJar().size }
val mainJarMd5Checksum by lazy { jars.mainJar().md5Checksum() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCo
import io.github.typesafegithub.workflows.testutils.JdkVersionToBytecodeVersion.JDK_8
import io.github.typesafegithub.workflows.testutils.shouldHaveBytecodeVersion
import io.kotest.core.spec.style.FunSpec
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO

class JavaBytecodeVersionTest :
FunSpec(
{
test("bindings are built with desired Java bytecode version") {
val actionCoords = ActionCoords("actions", "setup-java", "v3")
val jars = actionCoords.buildJars()!!
val jars = actionCoords.buildJars(httpClient = HttpClient(CIO))!!
jars.randomClassFile() shouldHaveBytecodeVersion JDK_8
}
},
Expand Down
Loading