Skip to content
Draft
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
Expand Up @@ -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 (
Expand All @@ -24,27 +25,45 @@ internal suspend fun ActionCoords.buildMavenMetadataFile(
prefetchBindingArtifacts: (Collection<ActionCoords>) -> 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 """
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>$owner</groupId>
<artifactId>$name</artifactId>
<groupId>${this.actionCoords.owner}</groupId>
<artifactId>${this.actionCoords.name}</artifactId>
<versioning>
<latest>$newest</latest>
<release>$newest</release>
<latest>$newestMaybeWithCommitHash</latest>
<release>$newestMaybeWithCommitHash</release>
<versions>
${availableVersions.joinToString(separator = "\n") {
${versionsWithCommitHashes.joinToString(separator = "\n") {
" <version>$it</version>"
}}
</versions>
Expand All @@ -53,3 +72,6 @@ ${availableVersions.joinToString(separator = "\n") {
</metadata>
""".trimIndent()
}

private suspend fun Version.withCommitHash(): String =
"${this.version}__${this.getCommitHash()}"
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ suspend fun buildPackageArtifacts(
meterRegistry: MeterRegistry,
): Map<String, String> {
val mavenMetadata =
bindingsServerRequest.actionCoords.buildMavenMetadataFile(
bindingsServerRequest.buildMavenMetadataFile(
githubAuthToken = githubAuthToken,
prefetchBindingArtifacts = prefetchBindingArtifacts,
meterRegistry = meterRegistry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private fun List<GithubRef>.versions(
either {
this@versions.map { githubRef ->
val version = githubRef.ref.substringAfterLast("/")
Version(version) {
Version(version, dateProvider = {
val response =
buildHttpClient(meterRegistry = meterRegistry).use { httpClient ->
httpClient
Expand All @@ -66,7 +66,22 @@ private fun List<GithubRef>.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<Commit>().sha
"tag" -> response.body<Tag>().`object`.sha
else -> error("Unexpected target object type ${githubRef.`object`.type}")
}
})
}
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Version> {
private val versionParts: List<String> = version.removePrefix("v").removePrefix("V").split('.')
private val versionIntParts: List<Int?> = versionParts.map { it.toIntOrNull() }
Expand Down Expand Up @@ -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()
}
Loading