Skip to content

Commit bb9468d

Browse files
authored
fix(abg): allow anchors and aliases in typings (#1628)
Fixes #1625
1 parent 91769d2 commit bb9468d

File tree

4 files changed

+137
-17
lines changed

4 files changed

+137
-17
lines changed

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/metadata/MetadataReading.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package io.github.typesafegithub.workflows.actionbindinggenerator.metadata
22

3+
import com.charleskorn.kaml.Yaml
34
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
45
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.CommitHash
56
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
67
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestForVersion
7-
import io.github.typesafegithub.workflows.actionbindinggenerator.utils.myYaml
88
import kotlinx.serialization.Serializable
99
import kotlinx.serialization.decodeFromString
1010
import java.io.IOException
@@ -66,7 +66,15 @@ public fun ActionCoords.fetchMetadata(
6666
} catch (e: IOException) {
6767
null
6868
}
69-
}?.let { myYaml.decodeFromString(it) }
69+
}?.let { yaml.decodeFromString(it) }
7070
}
7171

7272
internal fun fetchUri(uri: URI): String = uri.toURL().readText()
73+
74+
private val yaml =
75+
Yaml(
76+
configuration =
77+
Yaml.default.configuration.copy(
78+
strictMode = false,
79+
),
80+
)

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProviding.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.TypingAc
1111
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.repoName
1212
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.subName
1313
import io.github.typesafegithub.workflows.actionbindinggenerator.metadata.fetchUri
14-
import io.github.typesafegithub.workflows.actionbindinggenerator.utils.myYaml
1514
import io.github.typesafegithub.workflows.actionbindinggenerator.utils.toPascalCase
1615
import kotlinx.serialization.Serializable
1716
import kotlinx.serialization.decodeFromString
@@ -62,7 +61,7 @@ private fun ActionCoords.fetchTypingMetadata(
6261
}
6362
} ?: return null
6463

65-
return Pair(myYaml.decodeFromStringOrDefaultIfEmpty(typesMetadataYaml, ActionTypes()), ACTION)
64+
return Pair(yaml.decodeFromStringOrDefaultIfEmpty(typesMetadataYaml, ActionTypes()), ACTION)
6665
}
6766

6867
private fun ActionCoords.fetchFromTypingsFromCatalog(fetchUri: (URI) -> String = ::fetchUri): Pair<ActionTypes, TypingActualSource>? =
@@ -80,7 +79,7 @@ private fun ActionCoords.fetchTypingsForOlderVersionFromCatalog(fetchUri: (URI)
8079
} catch (e: IOException) {
8180
return null
8281
}
83-
val metadata = myYaml.decodeFromString<CatalogMetadata>(metadataYml)
82+
val metadata = yaml.decodeFromString<CatalogMetadata>(metadataYml)
8483
val fallbackVersion =
8584
metadata.versionsWithTypings
8685
.filter { it.versionToInt() < this.version.versionToInt() }
@@ -105,7 +104,7 @@ private fun fetchTypingsFromUrl(
105104
} catch (e: IOException) {
106105
null
107106
} ?: return null
108-
return myYaml.decodeFromStringOrDefaultIfEmpty(typesMetadataYml, ActionTypes())
107+
return yaml.decodeFromStringOrDefaultIfEmpty(typesMetadataYml, ActionTypes())
109108
}
110109

111110
internal fun ActionTypes.toTypesMap(): Map<String, Typing> =
@@ -154,6 +153,15 @@ private inline fun <reified T> Yaml.decodeFromStringOrDefaultIfEmpty(
154153

155154
private fun String.versionToInt() = lowercase().removePrefix("v").toInt()
156155

156+
private val yaml =
157+
Yaml(
158+
configuration =
159+
Yaml.default.configuration.copy(
160+
strictMode = false,
161+
allowAnchorsAndAliases = true,
162+
),
163+
)
164+
157165
@Serializable
158166
private data class CatalogMetadata(
159167
val versionsWithTypings: List<String>,

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/utils/YamlParsingConfig.kt

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

action-binding-generator/src/test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing/TypesProvidingTest.kt

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,119 @@ class TypesProvidingTest :
257257
types shouldBe Pair(emptyMap(), null)
258258
}
259259
}
260+
261+
context("YAML anchors and aliases") {
262+
val typingYml =
263+
"""
264+
inputs:
265+
granted-scopes: &scopes-list
266+
type: list
267+
separator: ','
268+
list-item:
269+
type: enum
270+
name: GrantedScopes
271+
allowed-values:
272+
- read
273+
- write
274+
granted-scopes2: *scopes-list
275+
granted-scopes3:
276+
<<: *scopes-list
277+
separator: '\n'
278+
""".trimIndent()
279+
val metadata =
280+
"""
281+
"versionsWithTypings": &versions
282+
- "v2"
283+
- "v3"
284+
- "v4"
285+
""".trimIndent()
286+
287+
test("hosted by the action") {
288+
// Given
289+
val fetchUri: (URI) -> String = {
290+
when (it) {
291+
URI("https://raw.githubusercontent.com/some-owner/some-name/some-hash//action-types.yml") -> typingYml
292+
else -> throw IOException()
293+
}
294+
}
295+
val actionCoord = ActionCoords("some-owner", "some-name", "v3")
296+
297+
// When
298+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
299+
300+
// Then
301+
types shouldBe
302+
Pair(
303+
mapOf(
304+
"granted-scopes" to ListOfTypings(",", EnumTyping("GrantedScopes", listOf("read", "write"))),
305+
"granted-scopes2" to ListOfTypings(",", EnumTyping("GrantedScopes", listOf("read", "write"))),
306+
"granted-scopes3" to ListOfTypings("""\n""", EnumTyping("GrantedScopes", listOf("read", "write"))),
307+
),
308+
TypingActualSource.ACTION,
309+
)
310+
}
311+
312+
test("hosted by typing catalog") {
313+
// Given
314+
val fetchUri: (URI) -> String = {
315+
when (it) {
316+
URI(
317+
"https://raw.githubusercontent.com/typesafegithub/github-actions-typing-catalog/" +
318+
"main/typings/some-owner/some-name/v3//action-types.yml",
319+
),
320+
-> typingYml
321+
else -> throw IOException()
322+
}
323+
}
324+
val actionCoord = ActionCoords("some-owner", "some-name", "v3")
325+
326+
// When
327+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
328+
329+
// Then
330+
types shouldBe
331+
Pair(
332+
mapOf(
333+
"granted-scopes" to ListOfTypings(",", EnumTyping("GrantedScopes", listOf("read", "write"))),
334+
"granted-scopes2" to ListOfTypings(",", EnumTyping("GrantedScopes", listOf("read", "write"))),
335+
"granted-scopes3" to ListOfTypings("""\n""", EnumTyping("GrantedScopes", listOf("read", "write"))),
336+
),
337+
TypingActualSource.TYPING_CATALOG,
338+
)
339+
}
340+
341+
test("only stored in typing catalog for older version") {
342+
// Given
343+
val fetchUri: (URI) -> String = {
344+
when (it) {
345+
URI(
346+
"https://raw.githubusercontent.com/typesafegithub/github-actions-typing-catalog/" +
347+
"main/typings/some-owner/some-name/metadata.yml",
348+
),
349+
-> metadata
350+
URI(
351+
"https://raw.githubusercontent.com/typesafegithub/github-actions-typing-catalog/" +
352+
"main/typings/some-owner/some-name/v4//action-types.yml",
353+
),
354+
-> typingYml
355+
else -> throw IOException()
356+
}
357+
}
358+
val actionCoord = ActionCoords("some-owner", "some-name", "v6")
359+
360+
// When
361+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
362+
363+
// Then
364+
types shouldBe
365+
Pair(
366+
mapOf(
367+
"granted-scopes" to ListOfTypings(",", EnumTyping("GrantedScopes", listOf("read", "write"))),
368+
"granted-scopes2" to ListOfTypings(",", EnumTyping("GrantedScopes", listOf("read", "write"))),
369+
"granted-scopes3" to ListOfTypings("""\n""", EnumTyping("GrantedScopes", listOf("read", "write"))),
370+
),
371+
TypingActualSource.TYPING_CATALOG,
372+
)
373+
}
374+
}
260375
})

0 commit comments

Comments
 (0)