Skip to content

Commit 653a7a9

Browse files
authored
fix(abg): handle non-numeric versions gracefully (#1668)
Fixes #1667. In the logic, we were trying to parse version like `v2-beta` into an integer. We could generally try to do it, but I think there are too many possible variations to support them, so let's just support versions that after removing the leading `v`, gives us an integer.
1 parent ad735ce commit 653a7a9

File tree

2 files changed

+33
-2
lines changed
  • action-binding-generator/src
    • main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing
    • test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/typing

2 files changed

+33
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ private fun ActionCoords.fetchTypingsForOlderVersionFromCatalog(fetchUri: (URI)
8080
return null
8181
}
8282
val metadata = yaml.decodeFromString<CatalogMetadata>(metadataYml)
83+
val requestedVersionAsInt = this.version.versionToIntOrNull() ?: return null
8384
val fallbackVersion =
8485
metadata.versionsWithTypings
85-
.filter { it.versionToInt() < this.version.versionToInt() }
86+
.filter { it.versionToInt() < requestedVersionAsInt }
8687
.maxByOrNull { it.versionToInt() }
8788
?: run {
8889
println(" ... no fallback version found!")
@@ -151,7 +152,9 @@ private inline fun <reified T> Yaml.decodeFromStringOrDefaultIfEmpty(
151152
default
152153
}
153154

154-
private fun String.versionToInt() = lowercase().removePrefix("v").toInt()
155+
private fun String.versionToInt() = this.versionToIntOrNull() ?: error("Version '$this' cannot be treated as numeric!")
156+
157+
private fun String.versionToIntOrNull() = lowercase().removePrefix("v").toIntOrNull()
155158

156159
private val yaml =
157160
Yaml(

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,32 @@ class TypesProvidingTest :
509509
)
510510
}
511511
}
512+
513+
test("non-numeric version is provided and metadata in typing catalog exists") {
514+
// Given
515+
val metadata =
516+
"""
517+
"versionsWithTypings":
518+
- "v2"
519+
- "v3"
520+
- "v4"
521+
""".trimIndent()
522+
val fetchUri: (URI) -> String = {
523+
when (it) {
524+
URI(
525+
"https://raw.githubusercontent.com/typesafegithub/github-actions-typing-catalog/" +
526+
"main/typings/some-owner/some-name/metadata.yml",
527+
),
528+
-> metadata
529+
else -> throw IOException()
530+
}
531+
}
532+
val actionCoord = ActionCoords("some-owner", "some-name", "v6-beta")
533+
534+
// When
535+
val types = actionCoord.provideTypes(metadataRevision = CommitHash("some-hash"), fetchUri = fetchUri)
536+
537+
// Then
538+
types shouldBe Pair(emptyMap(), null)
539+
}
512540
})

0 commit comments

Comments
 (0)