Skip to content

Commit 34e7d40

Browse files
authored
fix(abg): use the release date of the latest version for lastUpdated (#1612)
This makes the metadata file content more stable and allows to provide a checksum, as Kotlin for example is quite unhappy if it finds no checksum or cannot successfully verify it. Without this every request would produce a different file and thus also a different checksum file, so the checksum would never match the file contents.
1 parent 93bf081 commit 34e7d40

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
package io.github.typesafegithub.workflows.mavenbinding
22

33
import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions
4-
import java.time.Instant
5-
import java.time.ZoneId
64
import java.time.format.DateTimeFormatter
75

86
internal suspend fun buildMavenMetadataFile(
97
owner: String,
108
name: String,
119
githubToken: String,
1210
): String {
13-
val lastUpdated =
14-
DateTimeFormatter
15-
.ofPattern("yyyyMMddHHmmss")
16-
.withZone(ZoneId.systemDefault())
17-
.format(Instant.now())
1811
val availableMajorVersions =
1912
fetchAvailableVersions(owner = owner, name = name.substringBefore("__"), githubToken = githubToken)
2013
.filter { it.isMajorVersion() }
2114
val newest = availableMajorVersions.max()
15+
val lastUpdated =
16+
DateTimeFormatter
17+
.ofPattern("yyyyMMddHHmmss")
18+
.format(newest.getReleaseDate())
2219
return """
2320
<?xml version="1.0" encoding="UTF-8"?>
2421
<metadata>

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.ktor.client.request.get
99
import io.ktor.serialization.kotlinx.json.json
1010
import kotlinx.serialization.Serializable
1111
import kotlinx.serialization.json.Json
12+
import java.time.ZonedDateTime
1213

1314
suspend fun fetchAvailableVersions(
1415
owner: String,
@@ -19,12 +20,27 @@ suspend fun fetchAvailableVersions(
1920
apiTagsUrl(owner = owner, name = name),
2021
apiBranchesUrl(owner = owner, name = name),
2122
).flatMap { url -> fetchGithubRefs(url, githubToken) }
22-
.versions()
23+
.versions(githubToken)
2324

24-
private fun List<GithubRef>.versions(): List<Version> =
25+
private fun List<GithubRef>.versions(githubToken: String?): List<Version> =
2526
this.map { githubRef ->
2627
val version = githubRef.ref.substringAfterLast("/")
27-
Version(version)
28+
Version(version) {
29+
val response =
30+
httpClient
31+
.get(urlString = githubRef.`object`.url) {
32+
if (githubToken != null) {
33+
bearerAuth(githubToken)
34+
}
35+
}
36+
val releaseDate =
37+
when (githubRef.`object`.type) {
38+
"tag" -> response.body<Tag>().tagger
39+
"commit" -> response.body<Commit>().author
40+
else -> error("Unexpected target object type ${githubRef.`object`.type}")
41+
}.date
42+
ZonedDateTime.parse(releaseDate)
43+
}
2844
}
2945

3046
private suspend fun fetchGithubRefs(
@@ -51,6 +67,28 @@ private fun apiBranchesUrl(
5167
@Serializable
5268
private data class GithubRef(
5369
val ref: String,
70+
val `object`: Object,
71+
)
72+
73+
@Serializable
74+
private data class Object(
75+
val type: String,
76+
val url: String,
77+
)
78+
79+
@Serializable
80+
private data class Tag(
81+
val tagger: Person,
82+
)
83+
84+
@Serializable
85+
private data class Commit(
86+
val author: Person,
87+
)
88+
89+
@Serializable
90+
private data class Person(
91+
val date: String,
5492
)
5593

5694
private val httpClient by lazy {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package io.github.typesafegithub.workflows.shared.internal.model
22

3+
import java.time.ZonedDateTime
4+
35
data class Version(
46
val version: String,
7+
private val dateProvider: suspend () -> ZonedDateTime? = { null },
58
) : Comparable<Version> {
69
val input: String = version.removePrefix("v").removePrefix("V")
710
val major = input.substringBefore(".").toIntOrNull() ?: 0
@@ -23,4 +26,6 @@ data class Version(
2326
override fun toString(): String = version
2427

2528
fun isMajorVersion(): Boolean = version.contains(".").not()
29+
30+
suspend fun getReleaseDate() = dateProvider()
2631
}

0 commit comments

Comments
 (0)