Skip to content

Commit 6b00d12

Browse files
authored
fix(server): use correct versions in maven-metadata.xml for commit_lenient (#2299)
1 parent 02f6ba4 commit 6b00d12

4 files changed

Lines changed: 85 additions & 30 deletions

File tree

maven-binding-builder/src/main/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuilding.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import arrow.core.Either
44
import arrow.core.getOrElse
55
import io.github.oshai.kotlinlogging.KotlinLogging.logger
66
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
7+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.COMMIT_LENIENT
78
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.FULL
89
import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions
910
import io.github.typesafegithub.workflows.shared.internal.model.Version
@@ -30,6 +31,7 @@ internal suspend fun ActionCoords.buildMavenMetadataFile(
3031
emptyList()
3132
}.filter { it.isMajorVersion() || (significantVersion < FULL) }
3233
prefetchBindingArtifacts(availableVersions.map { copy(version = "$it") })
34+
val commitLenient = significantVersion == COMMIT_LENIENT
3335
val newest = availableVersions.maxOrNull() ?: return null
3436
val lastUpdated =
3537
DateTimeFormatter
@@ -41,10 +43,10 @@ internal suspend fun ActionCoords.buildMavenMetadataFile(
4143
<groupId>$owner</groupId>
4244
<artifactId>$name</artifactId>
4345
<versioning>
44-
<latest>$newest</latest>
45-
<release>$newest</release>
46+
<latest>$newest${if (commitLenient) "__${newest.getSha()}" else ""}</latest>
47+
<release>$newest${if (commitLenient) "__${newest.getSha()}" else ""}</release>
4648
<versions>
47-
${availableVersions.joinToString(separator = "\n") {
49+
${availableVersions.map { "$it${if (commitLenient) "__${it.getSha()}" else ""}" }.joinToString(separator = "\n") {
4850
" <version>$it</version>"
4951
}}
5052
</versions>

maven-binding-builder/src/test/kotlin/io/github/typesafegithub/workflows/mavenbinding/MavenMetadataBuildingTest.kt

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import arrow.core.Either
44
import arrow.core.right
55
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
66
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion
7+
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.COMMIT_LENIENT
78
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.SignificantVersion.FULL
89
import io.github.typesafegithub.workflows.shared.internal.model.Version
910
import io.kotest.core.spec.style.FunSpec
@@ -122,14 +123,46 @@ class MavenMetadataBuildingTest :
122123
MeterRegistry?,
123124
) -> Either<String, List<Version>> = { owner, name, _, _ ->
124125
listOf(
125-
Version(version = "v3-beta", dateProvider = { ZonedDateTime.parse("2024-07-01T00:00:00Z") }),
126-
Version(version = "v2", dateProvider = { ZonedDateTime.parse("2024-05-01T00:00:00Z") }),
127-
Version(version = "v1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
128-
Version(version = "v1.1", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
129-
Version(version = "v1.1.0", dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") }),
130-
Version(version = "v1.0.1", dateProvider = { ZonedDateTime.parse("2024-03-05T00:00:00Z") }),
131-
Version(version = "v1.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }),
132-
Version(version = "v1.0.0", dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") }),
126+
Version(
127+
version = "v3-beta",
128+
shaProvider = { "1" },
129+
dateProvider = { ZonedDateTime.parse("2024-07-01T00:00:00Z") },
130+
),
131+
Version(
132+
version = "v2",
133+
shaProvider = { "2" },
134+
dateProvider = { ZonedDateTime.parse("2024-05-01T00:00:00Z") },
135+
),
136+
Version(
137+
version = "v1",
138+
shaProvider = { "3" },
139+
dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") },
140+
),
141+
Version(
142+
version = "v1.1",
143+
shaProvider = { "4" },
144+
dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") },
145+
),
146+
Version(
147+
version = "v1.1.0",
148+
shaProvider = { "5" },
149+
dateProvider = { ZonedDateTime.parse("2024-03-07T00:00:00Z") },
150+
),
151+
Version(
152+
version = "v1.0.1",
153+
shaProvider = { "6" },
154+
dateProvider = { ZonedDateTime.parse("2024-03-05T00:00:00Z") },
155+
),
156+
Version(
157+
version = "v1.0",
158+
shaProvider = { "7" },
159+
dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") },
160+
),
161+
Version(
162+
version = "v1.0.0",
163+
shaProvider = { "8" },
164+
dateProvider = { ZonedDateTime.parse("2024-03-01T00:00:00Z") },
165+
),
133166
).right()
134167
}
135168

@@ -139,24 +172,25 @@ class MavenMetadataBuildingTest :
139172
fetchAvailableVersions = fetchAvailableVersions,
140173
)
141174

175+
val commitLenient = significantVersion == COMMIT_LENIENT
142176
xml shouldBe
143177
"""
144178
<?xml version="1.0" encoding="UTF-8"?>
145179
<metadata>
146180
<groupId>owner</groupId>
147181
<artifactId>name</artifactId>
148182
<versioning>
149-
<latest>v2</latest>
150-
<release>v2</release>
183+
<latest>v2${if (commitLenient) "__2" else ""}</latest>
184+
<release>v2${if (commitLenient) "__2" else ""}</release>
151185
<versions>
152-
<version>v3-beta</version>
153-
<version>v2</version>
154-
<version>v1</version>
155-
<version>v1.1</version>
156-
<version>v1.1.0</version>
157-
<version>v1.0.1</version>
158-
<version>v1.0</version>
159-
<version>v1.0.0</version>
186+
<version>v3-beta${if (commitLenient) "__1" else ""}</version>
187+
<version>v2${if (commitLenient) "__2" else ""}</version>
188+
<version>v1${if (commitLenient) "__3" else ""}</version>
189+
<version>v1.1${if (commitLenient) "__4" else ""}</version>
190+
<version>v1.1.0${if (commitLenient) "__5" else ""}</version>
191+
<version>v1.0.1${if (commitLenient) "__6" else ""}</version>
192+
<version>v1.0${if (commitLenient) "__7" else ""}</version>
193+
<version>v1.0.0${if (commitLenient) "__8" else ""}</version>
160194
</versions>
161195
<lastUpdated>20240501000000</lastUpdated>
162196
</versioning>

shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/GithubApi.kt

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.ktor.client.plugins.logging.Logging
1515
import io.ktor.client.plugins.plugin
1616
import io.ktor.client.request.bearerAuth
1717
import io.ktor.client.request.get
18+
import io.ktor.client.statement.HttpResponse
1819
import io.ktor.client.statement.bodyAsText
1920
import io.ktor.http.isSuccess
2021
import io.ktor.serialization.kotlinx.json.json
@@ -52,16 +53,21 @@ private fun List<GithubRef>.versions(
5253
either {
5354
this@versions.map { githubRef ->
5455
val version = githubRef.ref.substringAfterLast("/")
55-
Version(version) {
56-
val response =
57-
buildHttpClient(meterRegistry = meterRegistry).use { httpClient ->
58-
httpClient
59-
.get(urlString = githubRef.`object`.url) {
60-
if (githubAuthToken != null) {
61-
bearerAuth(githubAuthToken)
62-
}
56+
val objectProvider: suspend () -> HttpResponse = {
57+
buildHttpClient(meterRegistry = meterRegistry).use { httpClient ->
58+
httpClient
59+
.get(urlString = githubRef.`object`.url) {
60+
if (githubAuthToken != null) {
61+
bearerAuth(githubAuthToken)
6362
}
64-
}
63+
}
64+
}
65+
}
66+
Version(
67+
version,
68+
shaProvider = { githubRef.`object`.getCommitSha(objectProvider()) },
69+
) {
70+
val response = objectProvider()
6571
val releaseDate =
6672
when (githubRef.`object`.type) {
6773
"tag" -> response.body<Tag>().tagger
@@ -73,6 +79,13 @@ private fun List<GithubRef>.versions(
7379
}
7480
}
7581

82+
private suspend fun Object.getCommitSha(httpResponse: HttpResponse) =
83+
when (type) {
84+
"tag" -> httpResponse.body<Tag>().`object`.sha
85+
"commit" -> httpResponse.body<Commit>().sha
86+
else -> error("Unexpected target object type $type")
87+
}
88+
7689
private suspend fun fetchGithubRefs(
7790
url: String,
7891
githubAuthToken: String?,
@@ -114,17 +127,20 @@ private data class GithubRef(
114127
@Serializable
115128
private data class Object(
116129
val type: String,
130+
val sha: String,
117131
val url: String,
118132
)
119133

120134
@Serializable
121135
private data class Tag(
122136
val tagger: Person,
137+
val `object`: Object,
123138
)
124139

125140
@Serializable
126141
private data class Commit(
127142
val author: Person,
143+
val sha: String,
128144
)
129145

130146
@Serializable

shared-internal/src/main/kotlin/io/github/typesafegithub/workflows/shared/internal/model/Version.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java.time.ZonedDateTime
44

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

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

45+
suspend fun getSha() = shaProvider()
46+
4447
suspend fun getReleaseDate() = dateProvider()
4548
}

0 commit comments

Comments
 (0)