From 490b3ce13b3dff3d65ca4c02595a461ea4d9bad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Wed, 22 Apr 2026 16:30:40 +0200 Subject: [PATCH] fix(server): Use the correct versions in maven-metadata.xml for commit_lenient --- .../mavenbinding/MavenMetadataBuilding.kt | 8 ++- .../mavenbinding/MavenMetadataBuildingTest.kt | 70 ++++++++++++++----- .../workflows/shared/internal/GithubApi.kt | 34 ++++++--- .../shared/internal/model/Version.kt | 3 + 4 files changed, 85 insertions(+), 30 deletions(-) diff --git a/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuilding.kt b/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuilding.kt index bb3a54fa54..1c10bf026d 100644 --- a/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuilding.kt +++ b/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuilding.kt @@ -4,6 +4,7 @@ import arrow.core.Either import arrow.core.getOrElse import io.github.oshai.kotlinlogging.KotlinLogging.logger import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords +import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.COMMIT_LENIENT import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.FULL import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions import io.github.typesafegithub.workflows.shared.internal.model.Version @@ -30,6 +31,7 @@ internal suspend fun ActionCoords.buildMavenMetadataFile( emptyList() }.filter { it.isMajorVersion() || (significantVersion < FULL) } prefetchBindingArtifacts(availableVersions.map { copy(version = "$it") }) + val commitLenient = significantVersion == COMMIT_LENIENT val newest = availableVersions.maxOrNull() ?: return null val lastUpdated = DateTimeFormatter @@ -41,10 +43,10 @@ internal suspend fun ActionCoords.buildMavenMetadataFile( $owner $name - $newest - $newest + $newest${if (commitLenient) "__${newest.getSha()}" else ""} + $newest${if (commitLenient) "__${newest.getSha()}" else ""} -${availableVersions.joinToString(separator = "\n") { +${availableVersions.map { "$it${if (commitLenient) "__${it.getSha()}" else ""}" }.joinToString(separator = "\n") { " $it" }} diff --git a/maven-binding-builder/src/test/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuildingTest.kt b/maven-binding-builder/src/test/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuildingTest.kt index ada7c1ce6f..fc368477f7 100644 --- a/maven-binding-builder/src/test/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuildingTest.kt +++ b/maven-binding-builder/src/test/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuildingTest.kt @@ -4,6 +4,7 @@ import arrow.core.Either import arrow.core.right import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion +import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.COMMIT_LENIENT import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.FULL import io.github.typesafegithub.workflows.shared.internal.model.Version import io.kotest.core.spec.style.FunSpec @@ -122,14 +123,46 @@ class MavenMetadataBuildingTest : MeterRegistry?, ) -> Either> = { owner, name, _, _ -> listOf( - Version(version = "v3-beta", dateProvider = { ZonedDateTime.parse("2024-07-01T00:00:00Z") }), - Version(version = "v2", dateProvider = { ZonedDateTime.parse("2024-05-01T00:00:00Z") }), - Version(version = "v1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }), - Version(version = "v1.1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }), - Version(version = "v1.1.0", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }), - Version(version = "v1.0.1", dateProvider = { ZonedDateTime.parse("2024-03-05T00:00:00Z") }), - Version(version = "v1.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }), - Version(version = "v1.0.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }), + Version( + version = "v3-beta", + shaProvider = { "1" }, + dateProvider = { ZonedDateTime.parse("2024-07-01T00:00:00Z") }, + ), + Version( + version = "v2", + shaProvider = { "2" }, + dateProvider = { ZonedDateTime.parse("2024-05-01T00:00:00Z") }, + ), + Version( + version = "v1", + shaProvider = { "3" }, + dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }, + ), + Version( + version = "v1.1", + shaProvider = { "4" }, + dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }, + ), + Version( + version = "v1.1.0", + shaProvider = { "5" }, + dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }, + ), + Version( + version = "v1.0.1", + shaProvider = { "6" }, + dateProvider = { ZonedDateTime.parse("2024-03-05T00:00:00Z") }, + ), + Version( + version = "v1.0", + shaProvider = { "7" }, + dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }, + ), + Version( + version = "v1.0.0", + shaProvider = { "8" }, + dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }, + ), ).right() } @@ -139,6 +172,7 @@ class MavenMetadataBuildingTest : fetchAvailableVersions = fetchAvailableVersions, ) + val commitLenient = significantVersion == COMMIT_LENIENT xml shouldBe """ @@ -146,17 +180,17 @@ class MavenMetadataBuildingTest : owner name - v2 - v2 + v2${if (commitLenient) "__2" else ""} + v2${if (commitLenient) "__2" else ""} - v3-beta - v2 - v1 - v1.1 - v1.1.0 - v1.0.1 - v1.0 - v1.0.0 + v3-beta${if (commitLenient) "__1" else ""} + v2${if (commitLenient) "__2" else ""} + v1${if (commitLenient) "__3" else ""} + v1.1${if (commitLenient) "__4" else ""} + v1.1.0${if (commitLenient) "__5" else ""} + v1.0.1${if (commitLenient) "__6" else ""} + v1.0${if (commitLenient) "__7" else ""} + v1.0.0${if (commitLenient) "__8" else ""} 20240501000000 diff --git a/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubApi.kt b/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubApi.kt index 0c6a00b53f..d6728e2aee 100644 --- a/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubApi.kt +++ b/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubApi.kt @@ -15,6 +15,7 @@ import io.ktor.client.plugins.logging.Logging import io.ktor.client.plugins.plugin import io.ktor.client.request.bearerAuth import io.ktor.client.request.get +import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.bodyAsText import io.ktor.http.isSuccess import io.ktor.serialization.kotlinx.json.json @@ -52,16 +53,21 @@ private fun List.versions( either { this@versions.map { githubRef -> val version = githubRef.ref.substringAfterLast("/") - Version(version) { - val response = - buildHttpClient(meterRegistry = meterRegistry).use { httpClient -> - httpClient - .get(urlString = githubRef.`object`.url) { - if (githubAuthToken != null) { - bearerAuth(githubAuthToken) - } + val objectProvider: suspend () -> HttpResponse = { + buildHttpClient(meterRegistry = meterRegistry).use { httpClient -> + httpClient + .get(urlString = githubRef.`object`.url) { + if (githubAuthToken != null) { + bearerAuth(githubAuthToken) } - } + } + } + } + Version( + version, + shaProvider = { githubRef.`object`.getCommitSha(objectProvider()) }, + ) { + val response = objectProvider() val releaseDate = when (githubRef.`object`.type) { "tag" -> response.body().tagger @@ -73,6 +79,13 @@ private fun List.versions( } } +private suspend fun Object.getCommitSha(httpResponse: HttpResponse) = + when (type) { + "tag" -> httpResponse.body().`object`.sha + "commit" -> httpResponse.body().sha + else -> error("Unexpected target object type $type") + } + private suspend fun fetchGithubRefs( url: String, githubAuthToken: String?, @@ -114,17 +127,20 @@ private data class GithubRef( @Serializable private data class Object( val type: String, + val sha: String, val url: String, ) @Serializable private data class Tag( val tagger: Person, + val `object`: Object, ) @Serializable private data class Commit( val author: Person, + val sha: String, ) @Serializable diff --git a/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/model/Version.kt b/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/model/Version.kt index e2f5cd5821..c5b1126d45 100644 --- a/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/model/Version.kt +++ b/shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/model/Version.kt @@ -4,6 +4,7 @@ import java.time.ZonedDateTime data class Version( val version: String, + private val shaProvider: suspend () -> String? = { null }, private val dateProvider: suspend () -> ZonedDateTime? = { null }, ) : Comparable { private val versionParts: List = version.removePrefix("v").removePrefix("V").split('.') @@ -41,5 +42,7 @@ data class Version( fun isMajorVersion(): Boolean = versionIntParts.singleOrNull() != null + suspend fun getSha() = shaProvider() + suspend fun getReleaseDate() = dateProvider() }