Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
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(
owner: String,
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 """
<?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()
}
Loading