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..d389001058 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 @@ -9,10 +9,11 @@ import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions import io.github.typesafegithub.workflows.shared.internal.model.Version import io.micrometer.core.instrument.MeterRegistry import java.time.format.DateTimeFormatter +import kotlin.io.path.Path private val logger = logger { } -internal suspend fun ActionCoords.buildMavenMetadataFile( +internal suspend fun BindingsServerRequest.buildMavenMetadataFile( githubAuthToken: String, meterRegistry: MeterRegistry? = null, fetchAvailableVersions: suspend ( @@ -24,27 +25,45 @@ internal suspend fun ActionCoords.buildMavenMetadataFile( prefetchBindingArtifacts: (Collection) -> Unit = {}, ): String? { val availableVersions = - fetchAvailableVersions(owner, name, githubAuthToken, meterRegistry) + fetchAvailableVersions(this.actionCoords.owner, this.actionCoords.name, githubAuthToken, meterRegistry) .getOrElse { logger.error { it } emptyList() - }.filter { it.isMajorVersion() || (significantVersion < FULL) } - prefetchBindingArtifacts(availableVersions.map { copy(version = "$it") }) + }.filter { + if (this.rawName.endsWith("__commit_lenient")) { + it.isFullVersion() + } else { + it.isMajorVersion() || (this.actionCoords.significantVersion < FULL) + } + } + prefetchBindingArtifacts(availableVersions.map { this.actionCoords.copy(version = "$it") }) val newest = availableVersions.maxOrNull() ?: return null + val newestMaybeWithCommitHash = if (this.rawName.endsWith("__commit_lenient")) { + newest.withCommitHash() + } else { + newest + } val lastUpdated = DateTimeFormatter .ofPattern("yyyyMMddHHmmss") .format(newest.getReleaseDate()) + val versionsWithCommitHashes = availableVersions.map { + if (this.rawName.endsWith("__commit_lenient")) { + it.withCommitHash() + } else { + it.version + } + } return """ - $owner - $name + ${this.actionCoords.owner} + ${this.actionCoords.name} - $newest - $newest + $newestMaybeWithCommitHash + $newestMaybeWithCommitHash -${availableVersions.joinToString(separator = "\n") { +${versionsWithCommitHashes.joinToString(separator = "\n") { " $it" }} @@ -53,3 +72,6 @@ ${availableVersions.joinToString(separator = "\n") { """.trimIndent() } + +private suspend fun Version.withCommitHash(): String = + "${this.version}__${this.getCommitHash()}" diff --git a/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/PackageArtifactsBuilding.kt b/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/PackageArtifactsBuilding.kt index 2eb4de1a34..dd280280c0 100644 --- a/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/PackageArtifactsBuilding.kt +++ b/maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/PackageArtifactsBuilding.kt @@ -10,7 +10,7 @@ suspend fun buildPackageArtifacts( meterRegistry: MeterRegistry, ): Map { val mavenMetadata = - bindingsServerRequest.actionCoords.buildMavenMetadataFile( + bindingsServerRequest.buildMavenMetadataFile( githubAuthToken = githubAuthToken, prefetchBindingArtifacts = prefetchBindingArtifacts, meterRegistry = meterRegistry, 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 54a3eadc7a..61b5257fb3 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 @@ -49,7 +49,7 @@ private fun List.versions( either { this@versions.map { githubRef -> val version = githubRef.ref.substringAfterLast("/") - Version(version) { + Version(version, dateProvider = { val response = buildHttpClient(meterRegistry = meterRegistry).use { httpClient -> httpClient @@ -66,7 +66,22 @@ private fun List.versions( else -> error("Unexpected target object type ${githubRef.`object`.type}") }.date ZonedDateTime.parse(releaseDate) - } + }, commitHashProvider = { + val response = + buildHttpClient(meterRegistry = meterRegistry).use { httpClient -> + httpClient + .get(urlString = githubRef.`object`.url) { + if (githubAuthToken != null) { + bearerAuth(githubAuthToken) + } + } + } + when (githubRef.`object`.type) { + "commit" -> response.body().sha + "tag" -> response.body().`object`.sha + else -> error("Unexpected target object type ${githubRef.`object`.type}") + } + }) } } @@ -117,11 +132,18 @@ private data class Object( @Serializable private data class Tag( val tagger: Person, + val `object`: TagObject, +) + +@Serializable +private data class TagObject( + val sha: String, ) @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..92e1faf8a3 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 @@ -5,6 +5,7 @@ import java.time.ZonedDateTime data class Version( val version: String, private val dateProvider: suspend () -> ZonedDateTime? = { null }, + private val commitHashProvider: suspend () -> String? = { null }, ) : Comparable { private val versionParts: List = version.removePrefix("v").removePrefix("V").split('.') private val versionIntParts: List = versionParts.map { it.toIntOrNull() } @@ -41,5 +42,9 @@ data class Version( fun isMajorVersion(): Boolean = versionIntParts.singleOrNull() != null + fun isFullVersion(): Boolean = versionIntParts.size == 3 + suspend fun getReleaseDate() = dateProvider() + + suspend fun getCommitHash() = commitHashProvider() }