From c741439420ea20a23902c3b1c271b01e4ce2633e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Mon, 19 Aug 2024 17:05:59 +0200 Subject: [PATCH] fix(abg): use the release date of the latest version for lastUpdated --- .../mavenbinding/MavenMetadataBuilding.kt | 11 ++--- .../workflows/shared/internal/GithubApi.kt | 44 +++++++++++++++++-- .../shared/internal/model/Version.kt | 5 +++ 3 files changed, 50 insertions(+), 10 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 bf0e7e08aa..2e0b9aee6a 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 @@ -1,8 +1,6 @@ package io.github.typesafegithub.workflows.mavenbinding import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions -import java.time.Instant -import java.time.ZoneId import java.time.format.DateTimeFormatter internal suspend fun buildMavenMetadataFile( @@ -10,15 +8,14 @@ internal suspend fun buildMavenMetadataFile( name: String, githubToken: String, ): String { - val lastUpdated = - DateTimeFormatter - .ofPattern("yyyyMMddHHmmss") - .withZone(ZoneId.systemDefault()) - .format(Instant.now()) val availableMajorVersions = fetchAvailableVersions(owner = owner, name = name.substringBefore("__"), githubToken = githubToken) .filter { it.isMajorVersion() } val newest = availableMajorVersions.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() }