Skip to content

Commit a968bb2

Browse files
authored
fix(server): return HTTP 404 if no action found (#1381)
Part of #1318.
1 parent a3fd707 commit a968bb2

File tree

9 files changed

+65
-42
lines changed

9 files changed

+65
-42
lines changed

action-binding-generator/src/main/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/Generation.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,28 @@ private object Properties {
6868

6969
public fun ActionCoords.generateBinding(
7070
metadataRevision: MetadataRevision,
71-
metadata: Metadata = this.fetchMetadata(metadataRevision),
72-
inputTypings: Pair<Map<String, Typing>, TypingActualSource?> = provideTypes(metadataRevision),
71+
metadata: Metadata? = null,
72+
inputTypings: Pair<Map<String, Typing>, TypingActualSource?>? = null,
7373
clientType: ClientType = ClientType.BUNDLED_WITH_LIB,
74-
): ActionBinding {
74+
): ActionBinding? {
7575
require(this.version.removePrefix("v").toIntOrNull() != null) {
7676
"Only major versions are supported, and '${this.version}' was given!"
7777
}
78+
val metadataResolved = metadata ?: this.fetchMetadata(metadataRevision) ?: return null
79+
val metadataProcessed = metadataResolved.removeDeprecatedInputsIfNameClash()
7880

79-
val metadataProcessed = metadata.removeDeprecatedInputsIfNameClash()
81+
val inputTypingsResolved = inputTypings ?: this.provideTypes(metadataRevision)
8082

8183
val className = this.buildActionClassName(includeVersion = clientType == ClientType.BUNDLED_WITH_LIB)
8284
val actionBindingSourceCode =
83-
generateActionBindingSourceCode(metadataProcessed, this, inputTypings.first, className)
85+
generateActionBindingSourceCode(metadataProcessed, this, inputTypingsResolved.first, className)
8486
val packageName = owner.toKotlinPackageName()
8587
return ActionBinding(
8688
kotlinCode = actionBindingSourceCode,
8789
filePath = "github-workflows-kt/src/gen/kotlin/io/github/typesafegithub/workflows/actions/$packageName/$className.kt",
8890
className = className,
8991
packageName = packageName,
90-
typingActualSource = inputTypings.second,
92+
typingActualSource = inputTypingsResolved.second,
9193
)
9294
}
9395

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.domain.CommitHa
55
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.FromLockfile
66
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.MetadataRevision
77
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.NewestForVersion
8-
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrint
98
import io.github.typesafegithub.workflows.actionbindinggenerator.utils.myYaml
109
import kotlinx.serialization.Serializable
1110
import kotlinx.serialization.decodeFromString
@@ -55,28 +54,23 @@ internal val ActionCoords.gitHubUrl: String get() = "https://github.com/$owner/$
5554
public fun ActionCoords.fetchMetadata(
5655
metadataRevision: MetadataRevision,
5756
fetchUri: (URI) -> String = ::fetchUri,
58-
): Metadata {
57+
): Metadata? {
5958
val gitRef =
6059
when (metadataRevision) {
6160
is CommitHash -> metadataRevision.value
6261
NewestForVersion -> this.version
6362
FromLockfile -> this.getCommitHashFromFileSystem()
6463
}
6564
val list = listOf(actionYmlUrl(gitRef), actionYamlUrl(gitRef))
66-
val metadataYaml =
67-
list.firstNotNullOfOrNull { url ->
68-
try {
69-
println(" ... from $url")
70-
fetchUri(URI(url))
71-
} catch (e: IOException) {
72-
null
73-
}
74-
} ?: error(
75-
"$prettyPrint\n†Can't fetch any of those URLs:\n- ${list.joinToString(separator = "\n- ")}\n" +
76-
"Check release page $releasesUrl",
77-
)
7865

79-
return myYaml.decodeFromString(metadataYaml)
66+
return list.firstNotNullOfOrNull { url ->
67+
try {
68+
println(" ... from $url")
69+
fetchUri(URI(url))
70+
} catch (e: IOException) {
71+
null
72+
}
73+
}?.let { myYaml.decodeFromString(it) }
8074
}
8175

8276
private fun ActionCoords.getCommitHashFromFileSystem(): String =

action-binding-generator/src/test/kotlin/io/github/typesafegithub/workflows/actionbindinggenerator/generation/GenerationTest.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.github.typesafegithub.workflows.actionbindinggenerator.typing.IntegerW
1515
import io.github.typesafegithub.workflows.actionbindinggenerator.typing.ListOfTypings
1616
import io.github.typesafegithub.workflows.actionbindinggenerator.typing.StringTyping
1717
import io.kotest.core.spec.style.FunSpec
18+
import io.kotest.matchers.shouldNotBe
1819

1920
class GenerationTest : FunSpec({
2021
val actionManifestWithAllTypesOfInputsAndSomeOutput =
@@ -141,7 +142,8 @@ class GenerationTest : FunSpec({
141142
val binding = coords.generateBinding(metadataRevision = FromLockfile, metadata = actionManifest)
142143

143144
// then
144-
binding.shouldMatchFile("SimpleActionWithRequiredStringInputsV3.kt")
145+
binding shouldNotBe null
146+
binding?.shouldMatchFile("SimpleActionWithRequiredStringInputsV3.kt")
145147
}
146148

147149
test("action with various combinations of input parameters describing being required or optional") {
@@ -186,7 +188,8 @@ class GenerationTest : FunSpec({
186188
val binding = coords.generateBinding(metadataRevision = FromLockfile, metadata = actionManifest)
187189

188190
// then
189-
binding.shouldMatchFile("ActionWithSomeOptionalInputsV3.kt")
191+
binding shouldNotBe null
192+
binding?.shouldMatchFile("ActionWithSomeOptionalInputsV3.kt")
190193
}
191194

192195
test("action with all types of inputs") {
@@ -206,7 +209,8 @@ class GenerationTest : FunSpec({
206209
)
207210

208211
// then
209-
binding.shouldMatchFile("ActionWithAllTypesOfInputsV3.kt")
212+
binding shouldNotBe null
213+
binding?.shouldMatchFile("ActionWithAllTypesOfInputsV3.kt")
210214
}
211215

212216
test("action with outputs") {
@@ -236,7 +240,8 @@ class GenerationTest : FunSpec({
236240
val binding = coords.generateBinding(metadataRevision = FromLockfile, metadata = actionManifest)
237241

238242
// then
239-
binding.shouldMatchFile("ActionWithOutputsV3.kt")
243+
binding shouldNotBe null
244+
binding?.shouldMatchFile("ActionWithOutputsV3.kt")
240245
}
241246

242247
test("action with no inputs") {
@@ -255,7 +260,8 @@ class GenerationTest : FunSpec({
255260
val binding = coords.generateBinding(metadataRevision = FromLockfile, metadata = actionManifest)
256261

257262
// then
258-
binding.shouldMatchFile("ActionWithNoInputsV3.kt")
263+
binding shouldNotBe null
264+
binding?.shouldMatchFile("ActionWithNoInputsV3.kt")
259265
}
260266

261267
test("action v2 deprecated by v3") {
@@ -274,7 +280,7 @@ class GenerationTest : FunSpec({
274280
val binding = coords.generateBinding(metadataRevision = FromLockfile, metadata = actionManifest)
275281

276282
// then
277-
binding.shouldMatchFile("DeprecatedActionV2.kt")
283+
binding?.shouldMatchFile("DeprecatedActionV2.kt")
278284
}
279285

280286
test("action with deprecated input resolving to the same Kotlin field name") {
@@ -307,7 +313,8 @@ class GenerationTest : FunSpec({
307313
val binding = coords.generateBinding(metadataRevision = FromLockfile, metadata = actionManifest)
308314

309315
// then
310-
binding.shouldMatchFile("ActionWithDeprecatedInputAndNameClashV2.kt")
316+
binding shouldNotBe null
317+
binding?.shouldMatchFile("ActionWithDeprecatedInputAndNameClashV2.kt")
311318
}
312319

313320
test("action with inputs sharing type") {
@@ -354,7 +361,8 @@ class GenerationTest : FunSpec({
354361
)
355362

356363
// then
357-
binding.shouldMatchFile("ActionWithInputsSharingTypeV3.kt")
364+
binding shouldNotBe null
365+
binding?.shouldMatchFile("ActionWithInputsSharingTypeV3.kt")
358366
}
359367

360368
test("action binding generated for the versioned JAR") {
@@ -375,6 +383,7 @@ class GenerationTest : FunSpec({
375383
)
376384

377385
// then
378-
binding.shouldMatchFile("ActionForGeneratedJar.kt")
386+
binding shouldNotBe null
387+
binding?.shouldMatchFile("ActionForGeneratedJar.kt")
379388
}
380389
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private suspend fun generateBindings(): List<Pair<ActionBindingRequest, ActionBi
2929
) { actionBindingRequest ->
3030
println("Generating ${actionBindingRequest.actionCoords.prettyPrint}")
3131
val binding = actionBindingRequest.actionCoords.generateBinding(metadataRevision = FromLockfile)
32-
Pair(actionBindingRequest, binding)
32+
Pair(actionBindingRequest, binding ?: error("Couldn't generate binding for ${actionBindingRequest.actionCoords}"))
3333
}
3434
requestsAndBindings.forEach { (_, binding) ->
3535
with(Paths.get(binding.filePath).toFile()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private suspend fun createPullRequest(
8585
private fun ActionBindingRequest.generateBindingForCommit(commitHash: String): ActionBinding =
8686
actionCoords.generateBinding(
8787
metadataRevision = CommitHash(commitHash),
88-
)
88+
) ?: error("Couldn't generate binding for ${this.actionCoords}")
8989

9090
private suspend fun ActionCoords.fetchCommitHash(githubToken: String): String? {
9191
suspend fun fetch(detailsUrl: String): String? {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fun List<GithubRef>.versions(): List<Version> =
6868
fun ActionCoords.suggestNewerVersion(
6969
existingVersions: List<Version>,
7070
availableVersions: List<Version>,
71-
fetchMeta: ActionCoords.(MetadataRevision) -> Metadata = { this.fetchMetadata(it) },
71+
fetchMeta: ActionCoords.(MetadataRevision) -> Metadata = { this.fetchMetadata(it) ?: error("Couldn't get metadata for $this!") },
7272
): String? {
7373
if (availableVersions.isEmpty()) {
7474
return null

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,17 @@ fun main() {
4040
version = version,
4141
)
4242
val bindingArtifacts =
43-
bindingsCache.get(actionCoords) {
44-
actionCoords.buildVersionArtifacts()
43+
if (bindingsCache.get(actionCoords) != null) {
44+
bindingsCache.get(actionCoords)!!
45+
} else {
46+
val versionArtifacts = actionCoords.buildVersionArtifacts()
47+
if (versionArtifacts != null) {
48+
bindingsCache.put(actionCoords, versionArtifacts)
49+
versionArtifacts
50+
} else {
51+
call.respondText("Not found", status = HttpStatusCode.NotFound)
52+
return@get
53+
}
4554
}
4655
val file = call.parameters["file"]!!
4756
if (file in bindingArtifacts) {
@@ -71,8 +80,17 @@ fun main() {
7180
version = version,
7281
)
7382
val bindingArtifacts =
74-
bindingsCache.get(actionCoords) {
75-
actionCoords.buildVersionArtifacts()
83+
if (bindingsCache.get(actionCoords) != null) {
84+
bindingsCache.get(actionCoords)!!
85+
} else {
86+
val versionArtifacts = actionCoords.buildVersionArtifacts()
87+
if (versionArtifacts != null) {
88+
bindingsCache.put(actionCoords, versionArtifacts)
89+
versionArtifacts
90+
} else {
91+
call.respondText("Not found", status = HttpStatusCode.NotFound)
92+
return@head
93+
}
7694
}
7795
if (file in bindingArtifacts) {
7896
call.respondText("Exists", status = HttpStatusCode.OK)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ internal fun buildJar(
2424
owner: String,
2525
name: String,
2626
version: String,
27-
): ByteArray {
28-
val binding = generateBinding(owner = owner, name = name, version = version)
27+
): ByteArray? {
28+
val binding = generateBinding(owner = owner, name = name, version = version) ?: return null
2929
val pathWithJarContents = binding.compileBinding()
3030
val byteArrayOutputStream = ByteArrayOutputStream()
3131
byteArrayOutputStream.createZipFile(pathWithJarContents)
@@ -36,7 +36,7 @@ private fun generateBinding(
3636
owner: String,
3737
name: String,
3838
version: String,
39-
): ActionBinding {
39+
): ActionBinding? {
4040
val actionCoords =
4141
ActionCoords(
4242
owner = owner,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ data class TextArtifact(val data: String) : Artifact
99

1010
data class JarArtifact(val data: ByteArray) : Artifact
1111

12-
fun ActionCoords.buildVersionArtifacts(): Map<String, Artifact> {
13-
val jar = buildJar(owner = owner, name = name.replace("__", "/"), version = version)
12+
fun ActionCoords.buildVersionArtifacts(): Map<String, Artifact>? {
13+
val jar = buildJar(owner = owner, name = name.replace("__", "/"), version = version) ?: return null
1414
val pom = buildPomFile(owner = owner, name = name.replace("__", "/"), version = version)
1515
val module = buildModuleFile(owner = owner, name = name.replace("__", "/"), version = version)
1616
return mapOf(

0 commit comments

Comments
 (0)