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 46c4eeffb1..2fe3859f88 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 @@ -2,20 +2,17 @@ package io.github.typesafegithub.workflows.mavenbinding import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions -import java.time.Instant -import java.time.ZoneId import java.time.format.DateTimeFormatter internal suspend fun ActionCoords.buildMavenMetadataFile(githubToken: String): String { - val lastUpdated = - DateTimeFormatter - .ofPattern("yyyyMMddHHmmss") - .withZone(ZoneId.systemDefault()) - .format(Instant.now()) val availableVersions = fetchAvailableVersions(owner = owner, name = name.substringBefore("__"), githubToken = githubToken) .filter { it.isMajorVersion() || name.endsWith("___major") || name.endsWith("___minor") } val newest = availableVersions.max() + val lastUpdated = + DateTimeFormatter + .ofPattern("yyyyMMddHHmmss") + .format(newest.getReleaseDate()) return """ 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 d1500bb566..5bbac0fec9 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 @@ -9,6 +9,7 @@ import io.ktor.client.request.get import io.ktor.serialization.kotlinx.json.json import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json +import java.time.ZonedDateTime suspend fun fetchAvailableVersions( owner: String, @@ -19,12 +20,27 @@ suspend fun fetchAvailableVersions( apiTagsUrl(owner = owner, name = name), apiBranchesUrl(owner = owner, name = name), ).flatMap { url -> fetchGithubRefs(url, githubToken) } - .versions() + .versions(githubToken) -private fun List.versions(): List = +private fun List.versions(githubToken: String?): List = this.map { githubRef -> val version = githubRef.ref.substringAfterLast("/") - Version(version) + Version(version) { + val response = + httpClient + .get(urlString = githubRef.`object`.url) { + if (githubToken != null) { + bearerAuth(githubToken) + } + } + val releaseDate = + when (githubRef.`object`.type) { + "tag" -> response.body().tagger + "commit" -> response.body().author + else -> error("Unexpected target object type ${githubRef.`object`.type}") + }.date + ZonedDateTime.parse(releaseDate) + } } private suspend fun fetchGithubRefs( @@ -51,6 +67,28 @@ private fun apiBranchesUrl( @Serializable private data class GithubRef( val ref: String, + val `object`: Object, +) + +@Serializable +private data class Object( + val type: String, + val url: String, +) + +@Serializable +private data class Tag( + val tagger: Person, +) + +@Serializable +private data class Commit( + val author: Person, +) + +@Serializable +private data class Person( + val date: String, ) private val httpClient by lazy { 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 30481b98ec..d9f0ebc802 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 @@ -1,7 +1,10 @@ package io.github.typesafegithub.workflows.shared.internal.model +import java.time.ZonedDateTime + data class Version( val version: String, + private val dateProvider: suspend () -> ZonedDateTime? = { null }, ) : Comparable { val input: String = version.removePrefix("v").removePrefix("V") val major = input.substringBefore(".").toIntOrNull() ?: 0 @@ -23,4 +26,6 @@ data class Version( override fun toString(): String = version fun isMajorVersion(): Boolean = version.contains(".").not() + + suspend fun getReleaseDate() = dateProvider() }