Skip to content
Open
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 @@ -4,6 +4,7 @@ import arrow.core.Either
import arrow.core.getOrElse
import io.github.oshai.kotlinlogging.KotlinLogging.logger
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.COMMIT_LENIENT
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.FULL
import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions
import io.github.typesafegithub.workflows.shared.internal.model.Version
Expand All @@ -30,6 +31,7 @@ internal suspend fun ActionCoords.buildMavenMetadataFile(
emptyList()
}.filter { it.isMajorVersion() || (significantVersion < FULL) }
prefetchBindingArtifacts(availableVersions.map { copy(version = "$it") })
val commitLenient = significantVersion == COMMIT_LENIENT
val newest = availableVersions.maxOrNull() ?: return null
val lastUpdated =
DateTimeFormatter
Expand All @@ -41,10 +43,10 @@ internal suspend fun ActionCoords.buildMavenMetadataFile(
<groupId>$owner</groupId>
<artifactId>$name</artifactId>
<versioning>
<latest>$newest</latest>
<release>$newest</release>
<latest>$newest${if (commitLenient) "__${newest.getSha()}" else ""}</latest>
<release>$newest${if (commitLenient) "__${newest.getSha()}" else ""}</release>
<versions>
${availableVersions.joinToString(separator = "\n") {
${availableVersions.map { "$it${if (commitLenient) "__${it.getSha()}" else ""}" }.joinToString(separator = "\n") {
" <version>$it</version>"
}}
</versions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import arrow.core.Either
import arrow.core.right
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.COMMIT_LENIENT
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.FULL
import io.github.typesafegithub.workflows.shared.internal.model.Version
import io.kotest.core.spec.style.FunSpec
Expand Down Expand Up @@ -122,14 +123,46 @@ class MavenMetadataBuildingTest :
MeterRegistry?,
) -> Either<String, List<Version>> = { owner, name, _, _ ->
listOf(
Version(version = "v3-beta", dateProvider = { ZonedDateTime.parse("2024-07-01T00:00:00Z") }),
Version(version = "v2", dateProvider = { ZonedDateTime.parse("2024-05-01T00:00:00Z") }),
Version(version = "v1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
Version(version = "v1.1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
Version(version = "v1.1.0", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
Version(version = "v1.0.1", dateProvider = { ZonedDateTime.parse("2024-03-05T00:00:00Z") }),
Version(version = "v1.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }),
Version(version = "v1.0.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }),
Version(
version = "v3-beta",
shaProvider = { "1" },
dateProvider = { ZonedDateTime.parse("2024-07-01T00:00:00Z") },
),
Version(
version = "v2",
shaProvider = { "2" },
dateProvider = { ZonedDateTime.parse("2024-05-01T00:00:00Z") },
),
Version(
version = "v1",
shaProvider = { "3" },
dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") },
),
Version(
version = "v1.1",
shaProvider = { "4" },
dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") },
),
Version(
version = "v1.1.0",
shaProvider = { "5" },
dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") },
),
Version(
version = "v1.0.1",
shaProvider = { "6" },
dateProvider = { ZonedDateTime.parse("2024-03-05T00:00:00Z") },
),
Version(
version = "v1.0",
shaProvider = { "7" },
dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") },
),
Version(
version = "v1.0.0",
shaProvider = { "8" },
dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") },
),
).right()
}

Expand All @@ -139,24 +172,25 @@ class MavenMetadataBuildingTest :
fetchAvailableVersions = fetchAvailableVersions,
)

val commitLenient = significantVersion == COMMIT_LENIENT
xml shouldBe
"""
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>owner</groupId>
<artifactId>name</artifactId>
<versioning>
<latest>v2</latest>
<release>v2</release>
<latest>v2${if (commitLenient) "__2" else ""}</latest>
<release>v2${if (commitLenient) "__2" else ""}</release>
<versions>
<version>v3-beta</version>
<version>v2</version>
<version>v1</version>
<version>v1.1</version>
<version>v1.1.0</version>
<version>v1.0.1</version>
<version>v1.0</version>
<version>v1.0.0</version>
<version>v3-beta${if (commitLenient) "__1" else ""}</version>
<version>v2${if (commitLenient) "__2" else ""}</version>
<version>v1${if (commitLenient) "__3" else ""}</version>
<version>v1.1${if (commitLenient) "__4" else ""}</version>
<version>v1.1.0${if (commitLenient) "__5" else ""}</version>
<version>v1.0.1${if (commitLenient) "__6" else ""}</version>
<version>v1.0${if (commitLenient) "__7" else ""}</version>
<version>v1.0.0${if (commitLenient) "__8" else ""}</version>
</versions>
<lastUpdated>20240501000000</lastUpdated>
</versioning>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.ktor.client.plugins.logging.Logging
import io.ktor.client.plugins.plugin
import io.ktor.client.request.bearerAuth
import io.ktor.client.request.get
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.http.isSuccess
import io.ktor.serialization.kotlinx.json.json
Expand Down Expand Up @@ -52,16 +53,21 @@ private fun List<GithubRef>.versions(
either {
this@versions.map { githubRef ->
val version = githubRef.ref.substringAfterLast("/")
Version(version) {
val response =
buildHttpClient(meterRegistry = meterRegistry).use { httpClient ->
httpClient
.get(urlString = githubRef.`object`.url) {
if (githubAuthToken != null) {
bearerAuth(githubAuthToken)
}
val objectProvider: suspend () -> HttpResponse = {
buildHttpClient(meterRegistry = meterRegistry).use { httpClient ->
httpClient
.get(urlString = githubRef.`object`.url) {
if (githubAuthToken != null) {
bearerAuth(githubAuthToken)
}
}
}
}
}
Version(
version,
shaProvider = { githubRef.`object`.getCommitSha(objectProvider()) },
) {
val response = objectProvider()
val releaseDate =
when (githubRef.`object`.type) {
"tag" -> response.body<Tag>().tagger
Expand All @@ -73,6 +79,13 @@ private fun List<GithubRef>.versions(
}
}

private suspend fun Object.getCommitSha(httpResponse: HttpResponse) =
when (type) {
"tag" -> httpResponse.body<Tag>().`object`.sha
"commit" -> httpResponse.body<Commit>().sha
else -> error("Unexpected target object type $type")
}

private suspend fun fetchGithubRefs(
url: String,
githubAuthToken: String?,
Expand Down Expand Up @@ -114,17 +127,20 @@ private data class GithubRef(
@Serializable
private data class Object(
val type: String,
val sha: String,
val url: String,
)

@Serializable
private data class Tag(
val tagger: Person,
val `object`: Object,
)

@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 @@ -4,6 +4,7 @@ import java.time.ZonedDateTime

data class Version(
val version: String,
private val shaProvider: suspend () -> String? = { null },
private val dateProvider: suspend () -> ZonedDateTime? = { null },
) : Comparable<Version> {
private val versionParts: List<String> = version.removePrefix("v").removePrefix("V").split('.')
Expand Down Expand Up @@ -41,5 +42,7 @@ data class Version(

fun isMajorVersion(): Boolean = versionIntParts.singleOrNull() != null

suspend fun getSha() = shaProvider()

suspend fun getReleaseDate() = dateProvider()
}
Loading