diff --git a/library/src/main/kotlin/it/krzeminski/githubactions/domain/triggers/WorkflowDispatch.kt b/library/src/main/kotlin/it/krzeminski/githubactions/domain/triggers/WorkflowDispatch.kt index bc63a4bdc4..2d54892130 100644 --- a/library/src/main/kotlin/it/krzeminski/githubactions/domain/triggers/WorkflowDispatch.kt +++ b/library/src/main/kotlin/it/krzeminski/githubactions/domain/triggers/WorkflowDispatch.kt @@ -1,3 +1,22 @@ package it.krzeminski.githubactions.domain.triggers -class WorkflowDispatch : Trigger() +// https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_dispatchinputs +class WorkflowDispatch( + val inputs: Map = emptyMap(), +) : Trigger() { + + enum class Type { + Choice, + Environment, + Boolean, + String, + } + + class Input( + val description: String, + val required: Boolean, + val type: Type, + val options: List = emptyList(), + val default: String? = null, + ) +} diff --git a/library/src/main/kotlin/it/krzeminski/githubactions/yaml/TriggersToYaml.kt b/library/src/main/kotlin/it/krzeminski/githubactions/yaml/TriggersToYaml.kt index 46385c904c..574dfc37c8 100644 --- a/library/src/main/kotlin/it/krzeminski/githubactions/yaml/TriggersToYaml.kt +++ b/library/src/main/kotlin/it/krzeminski/githubactions/yaml/TriggersToYaml.kt @@ -1,3 +1,5 @@ +@file:Suppress("TooManyFunctions") + package it.krzeminski.githubactions.yaml import it.krzeminski.githubactions.domain.triggers.PullRequest @@ -23,6 +25,7 @@ import it.krzeminski.githubactions.domain.triggers.Push import it.krzeminski.githubactions.domain.triggers.Schedule import it.krzeminski.githubactions.domain.triggers.Trigger import it.krzeminski.githubactions.domain.triggers.WorkflowDispatch +import it.krzeminski.githubactions.domain.triggers.WorkflowDispatch.Type fun List.triggersToYaml(): String = this @@ -31,7 +34,7 @@ fun List.triggersToYaml(): String = private fun Trigger.toYamlString() = when (this) { - is WorkflowDispatch -> "workflow_dispatch:" + is WorkflowDispatch -> toYaml() is Push -> toYaml() is PullRequest -> toYaml() is PullRequestTarget -> toYaml() @@ -93,11 +96,42 @@ private fun Schedule.toYaml() = buildString { } }.removeSuffix("\n") -private fun StringBuilder.printIfHasElements(items: List?, name: String) { +private fun WorkflowDispatch.toYaml(): String = buildString { + appendLine("workflow_dispatch:") + if (inputs.isNotEmpty()) { + appendLine(" inputs:") + for ((key, input) in inputs) { + appendLine(" $key:") + appendLine(input.toYaml()) + } + } +}.removeSuffix("\n") + +private fun WorkflowDispatch.Input.toYaml(): String = buildString { + val space = " " + appendLine("${space}description: '$description'") + appendLine("${space}type: ${type.toYaml()}") + appendLine("${space}required: $required") + if (default != null) appendLine("${space}default: '$default'") + printIfHasElements(options, "options", space = " ") +}.removeSuffix("\n") + +private fun Type.toYaml(): String = when (this) { + Type.Choice -> "choice" + Type.Environment -> "environment" + Type.Boolean -> "boolean" + Type.String -> "string" +} + +private fun StringBuilder.printIfHasElements( + items: List?, + name: String, + space: String = " ", +) { if (!items.isNullOrEmpty()) { - appendLine(" $name:") + appendLine("$name:".prependIndent(space)) items.forEach { - appendLine(" - '$it'") + appendLine(" - '$it'".prependIndent(space)) } } } diff --git a/library/src/test/kotlin/it/krzeminski/githubactions/yaml/TriggersToYamlTest.kt b/library/src/test/kotlin/it/krzeminski/githubactions/yaml/TriggersToYamlTest.kt index 3766abd8cd..f2ea858176 100644 --- a/library/src/test/kotlin/it/krzeminski/githubactions/yaml/TriggersToYamlTest.kt +++ b/library/src/test/kotlin/it/krzeminski/githubactions/yaml/TriggersToYamlTest.kt @@ -40,6 +40,69 @@ class TriggersToYamlTest : DescribeSpec({ |workflow_dispatch: """.trimMargin() } + + it("renders with all parameters") { + + // given + val triggers = listOf( + WorkflowDispatch( + inputs = mapOf( + "logLevel" to WorkflowDispatch.Input( + description = "Log level", + type = WorkflowDispatch.Type.Choice, + required = true, + default = "warning", + options = listOf("info", "warning", "debug"), + ), + "tags" to WorkflowDispatch.Input( + description = "Test scenario tags", + type = WorkflowDispatch.Type.Boolean, + required = false, + ), + "environment" to WorkflowDispatch.Input( + description = "Environment to run tests against", + type = WorkflowDispatch.Type.Environment, + required = true, + ), + "greeting" to WorkflowDispatch.Input( + description = "Hello {greeting}", + type = WorkflowDispatch.Type.String, + required = true, + ), + ) + ) + ) + + // when + val yaml = triggers.triggersToYaml() + + // then + yaml shouldBe """ + |workflow_dispatch: + | inputs: + | logLevel: + | description: 'Log level' + | type: choice + | required: true + | default: 'warning' + | options: + | - 'info' + | - 'warning' + | - 'debug' + | tags: + | description: 'Test scenario tags' + | type: boolean + | required: false + | environment: + | description: 'Environment to run tests against' + | type: environment + | required: true + | greeting: + | description: 'Hello {greeting}' + | type: string + | required: true + """.trimMargin() + } } describe("push") {