Skip to content

Commit

Permalink
fix(abg): Use the release date of the latest version for lastUpdated
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Aug 22, 2024
1 parent 14dc378 commit 4efb3cf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<GithubRef>.versions(): List<Version> =
private fun List<GithubRef>.versions(githubToken: String?): List<Version> =
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<Tag>().tagger
"commit" -> response.body<Commit>().author
else -> error("Unexpected target object type ${githubRef.`object`.type}")
}.date
ZonedDateTime.parse(releaseDate)
}
}

private suspend fun fetchGithubRefs(
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Version> {
val input: String = version.removePrefix("v").removePrefix("V")
val major = input.substringBefore(".").toIntOrNull() ?: 0
Expand All @@ -23,4 +26,6 @@ data class Version(
override fun toString(): String = version

fun isMajorVersion(): Boolean = version.contains(".").not()

suspend fun getReleaseDate() = dateProvider()
}

0 comments on commit 4efb3cf

Please sign in to comment.