Skip to content

Commit d8a12d2

Browse files
authored
refactor: move fetchAvailableVersions to shared-internal (#1399)
Removing duplication, so that it can be reused in multiple places.
1 parent 1b1da04 commit d8a12d2

File tree

8 files changed

+126
-147
lines changed

8 files changed

+126
-147
lines changed

automation/code-generator/src/main/kotlin/io/github/typesafegithub/workflows/codegenerator/updating/CreateActionUpdatePRs.kt

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.generation.Acti
88
import io.github.typesafegithub.workflows.actionbindinggenerator.generation.generateBinding
99
import io.github.typesafegithub.workflows.codegenerator.bindingsToGenerate
1010
import io.github.typesafegithub.workflows.codegenerator.model.ActionBindingRequest
11-
import io.github.typesafegithub.workflows.codegenerator.versions.GithubRef
12-
import io.github.typesafegithub.workflows.codegenerator.versions.GithubTag
13-
import io.github.typesafegithub.workflows.codegenerator.versions.httpClient
14-
import io.github.typesafegithub.workflows.codegenerator.versions.json
1511
import io.github.typesafegithub.workflows.shared.internal.getGithubToken
12+
import io.ktor.client.HttpClient
13+
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
1614
import io.ktor.client.request.bearerAuth
1715
import io.ktor.client.request.get
1816
import io.ktor.client.statement.bodyAsText
1917
import io.ktor.http.HttpStatusCode
18+
import io.ktor.serialization.kotlinx.json.json
19+
import kotlinx.serialization.SerialName
20+
import kotlinx.serialization.Serializable
21+
import kotlinx.serialization.json.Json
2022
import java.nio.file.Path
2123
import java.time.Instant
2224
import kotlin.io.path.Path
@@ -151,5 +153,46 @@ private suspend fun fetchGithubUrl(
151153
return response.bodyAsText()
152154
}
153155

156+
@Serializable
157+
private data class GithubRef(
158+
val ref: String,
159+
@SerialName("object")
160+
val obj: GithubRefObject?,
161+
)
162+
163+
@Serializable
164+
private data class GithubRefObject(
165+
val sha: String,
166+
val type: String,
167+
val url: String,
168+
)
169+
170+
@Serializable
171+
private data class GithubTag(
172+
@SerialName("object")
173+
val obj: GithubTagObject?,
174+
)
175+
176+
@Serializable
177+
private data class GithubTagObject(
178+
val sha: String,
179+
val type: String,
180+
val url: String,
181+
)
182+
154183
private fun ActionCoords.buildCommitHashFilePath(): Path =
155184
Path.of("actions").resolve(owner).resolve(name).resolve(version).resolve("commit-hash.txt")
185+
186+
internal val httpClient by lazy {
187+
HttpClient {
188+
install(ContentNegotiation) {
189+
json(
190+
Json {
191+
ignoreUnknownKeys = true
192+
},
193+
)
194+
}
195+
}
196+
}
197+
198+
private val json = Json { ignoreUnknownKeys = true }

automation/code-generator/src/main/kotlin/io/github/typesafegithub/workflows/codegenerator/updating/GitHubPullRequestCreation.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.typesafegithub.workflows.codegenerator.updating
22

3-
import io.github.typesafegithub.workflows.codegenerator.versions.httpClient
43
import io.ktor.client.HttpClient
54
import io.ktor.client.call.body
65
import io.ktor.client.request.HttpRequestBuilder

automation/code-generator/src/main/kotlin/io/github/typesafegithub/workflows/codegenerator/versions/Http.kt

Lines changed: 0 additions & 73 deletions
This file was deleted.

automation/code-generator/src/main/kotlin/io/github/typesafegithub/workflows/codegenerator/versions/SuggestVersions.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPr
99
import io.github.typesafegithub.workflows.actionbindinggenerator.metadata.Metadata
1010
import io.github.typesafegithub.workflows.actionbindinggenerator.metadata.fetchMetadata
1111
import io.github.typesafegithub.workflows.codegenerator.bindingsToGenerate
12+
import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions
1213
import io.github.typesafegithub.workflows.shared.internal.getGithubToken
1314
import io.github.typesafegithub.workflows.shared.internal.model.Version
1415
import java.io.File
@@ -41,7 +42,7 @@ suspend fun main() {
4142
.mapValues { (_, value) -> value.map { Version(it.version) } }
4243

4344
for ((coords, existingVersions) in actionsMap) {
44-
val available = coords.fetchAvailableVersions(githubAuthorization)
45+
val available = fetchAvailableVersions(owner = coords.owner, name = coords.name, githubAuthorization)
4546
coords.suggestNewerVersion(existingVersions, available)?.let { message ->
4647
val outputForAction = "# ${coords.prettyPrint}\n$message"
4748
println(outputForAction)
@@ -60,12 +61,6 @@ suspend fun main() {
6061
}
6162
}
6263

63-
fun List<GithubRef>.versions(): List<Version> =
64-
this.map { githubRef ->
65-
val version = githubRef.ref.substringAfterLast("/")
66-
Version(version)
67-
}
68-
6964
fun ActionCoords.suggestNewerVersion(
7065
existingVersions: List<Version>,
7166
availableVersions: List<Version>,
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
plugins {
22
buildsrc.convention.`kotlin-jvm`
3-
kotlin("plugin.serialization")
43
}
54

65
dependencies {
76
implementation("org.jetbrains.kotlin:kotlin-compiler")
87
api(projects.actionBindingGenerator)
9-
implementation(platform("io.ktor:ktor-bom:2.3.10"))
10-
implementation("io.ktor:ktor-client-core")
11-
implementation("io.ktor:ktor-client-cio")
12-
implementation("io.ktor:ktor-client-content-negotiation")
13-
implementation("io.ktor:ktor-serialization-kotlinx-json")
8+
implementation(projects.sharedInternal)
149

1510
runtimeOnly(projects.githubWorkflowsKt)
1611
}
Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package io.github.typesafegithub.workflows.mavenbinding
22

3-
import io.ktor.client.HttpClient
4-
import io.ktor.client.call.body
5-
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
6-
import io.ktor.client.request.bearerAuth
7-
import io.ktor.client.request.get
8-
import io.ktor.serialization.kotlinx.json.json
9-
import kotlinx.serialization.Serializable
10-
import kotlinx.serialization.json.Json
3+
import io.github.typesafegithub.workflows.shared.internal.fetchAvailableVersions
114
import java.time.Instant
125
import java.time.ZoneId
136
import java.time.format.DateTimeFormatter
@@ -23,8 +16,8 @@ internal suspend fun buildMavenMetadataFile(
2316
.format(Instant.now())
2417
val availableMajorVersions =
2518
fetchAvailableVersions(owner = owner, name = name.substringBefore("__"), githubToken = githubToken)
26-
.filter { it.removePrefix("v").toIntOrNull() != null }
27-
val newest = availableMajorVersions.maxBy { it.removePrefix("v") }
19+
.filter { it.isMajorVersion() }
20+
val newest = availableMajorVersions.max()
2821
return """
2922
<?xml version="1.0" encoding="UTF-8"?>
3023
<metadata>
@@ -43,49 +36,3 @@ ${availableMajorVersions.joinToString(separator = "\n") {
4336
</metadata>
4437
""".trimIndent()
4538
}
46-
47-
private suspend fun fetchAvailableVersions(
48-
owner: String,
49-
name: String,
50-
githubToken: String,
51-
): List<String> =
52-
listOf(
53-
apiTagsUrl(owner = owner, name = name),
54-
apiBranchesUrl(owner = owner, name = name),
55-
).flatMap { url -> fetchGithubRefs(url, githubToken) }
56-
.map { it.ref.substringAfterLast("/") }
57-
58-
private suspend fun fetchGithubRefs(
59-
url: String,
60-
githubToken: String,
61-
): List<GithubRef> =
62-
httpClient.get(urlString = url) {
63-
bearerAuth(githubToken)
64-
}.body()
65-
66-
private val httpClient by lazy {
67-
HttpClient {
68-
install(ContentNegotiation) {
69-
json(
70-
Json {
71-
ignoreUnknownKeys = true
72-
},
73-
)
74-
}
75-
}
76-
}
77-
78-
private fun apiTagsUrl(
79-
owner: String,
80-
name: String,
81-
): String = "https://api.github.com/repos/$owner/$name/git/matching-refs/tags/v"
82-
83-
private fun apiBranchesUrl(
84-
owner: String,
85-
name: String,
86-
): String = "https://api.github.com/repos/$owner/$name/git/matching-refs/heads/v"
87-
88-
@Serializable
89-
data class GithubRef(
90-
val ref: String,
91-
)

shared-internal/build.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
plugins {
22
buildsrc.convention.`kotlin-jvm`
33
buildsrc.convention.publishing
4+
5+
kotlin("plugin.serialization")
46
}
57

68
group = rootProject.group
79
version = rootProject.version
10+
11+
dependencies {
12+
implementation(platform("io.ktor:ktor-bom:2.3.10"))
13+
implementation("io.ktor:ktor-client-core")
14+
implementation("io.ktor:ktor-client-cio")
15+
implementation("io.ktor:ktor-client-content-negotiation")
16+
implementation("io.ktor:ktor-serialization-kotlinx-json")
17+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.github.typesafegithub.workflows.shared.internal
2+
3+
import io.github.typesafegithub.workflows.shared.internal.model.Version
4+
import io.ktor.client.HttpClient
5+
import io.ktor.client.call.body
6+
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
7+
import io.ktor.client.request.bearerAuth
8+
import io.ktor.client.request.get
9+
import io.ktor.serialization.kotlinx.json.json
10+
import kotlinx.serialization.Serializable
11+
import kotlinx.serialization.json.Json
12+
13+
suspend fun fetchAvailableVersions(
14+
owner: String,
15+
name: String,
16+
githubToken: String,
17+
): List<Version> =
18+
listOf(
19+
apiTagsUrl(owner = owner, name = name),
20+
apiBranchesUrl(owner = owner, name = name),
21+
).flatMap { url -> fetchGithubRefs(url, githubToken) }
22+
.versions()
23+
24+
private fun List<GithubRef>.versions(): List<Version> =
25+
this.map { githubRef ->
26+
val version = githubRef.ref.substringAfterLast("/")
27+
Version(version)
28+
}
29+
30+
private suspend fun fetchGithubRefs(
31+
url: String,
32+
githubToken: String,
33+
): List<GithubRef> =
34+
httpClient.get(urlString = url) {
35+
bearerAuth(githubToken)
36+
}.body()
37+
38+
private fun apiTagsUrl(
39+
owner: String,
40+
name: String,
41+
): String = "https://api.github.com/repos/$owner/$name/git/matching-refs/tags/v"
42+
43+
private fun apiBranchesUrl(
44+
owner: String,
45+
name: String,
46+
): String = "https://api.github.com/repos/$owner/$name/git/matching-refs/heads/v"
47+
48+
@Serializable
49+
private data class GithubRef(
50+
val ref: String,
51+
)
52+
53+
private val httpClient by lazy {
54+
HttpClient {
55+
install(ContentNegotiation) {
56+
json(
57+
Json {
58+
ignoreUnknownKeys = true
59+
},
60+
)
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)