Skip to content

Commit

Permalink
[#64] Implement WorkflowDispatch inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfayard authored Feb 16, 2022
1 parent b3c28a5 commit 2ad0463
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, Input> = emptyMap(),
) : Trigger() {

enum class Type {
Choice,
Environment,
Boolean,
String,
}

class Input(
val description: String,
val required: Boolean,
val type: Type,
val options: List<String> = emptyList(),
val default: String? = null,
)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("TooManyFunctions")

package it.krzeminski.githubactions.yaml

import it.krzeminski.githubactions.domain.triggers.PullRequest
Expand All @@ -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<Trigger>.triggersToYaml(): String =
this
Expand All @@ -31,7 +34,7 @@ fun List<Trigger>.triggersToYaml(): String =

private fun Trigger.toYamlString() =
when (this) {
is WorkflowDispatch -> "workflow_dispatch:"
is WorkflowDispatch -> toYaml()
is Push -> toYaml()
is PullRequest -> toYaml()
is PullRequestTarget -> toYaml()
Expand Down Expand Up @@ -93,11 +96,42 @@ private fun Schedule.toYaml() = buildString {
}
}.removeSuffix("\n")

private fun StringBuilder.printIfHasElements(items: List<String>?, 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<String>?,
name: String,
space: String = " ",
) {
if (!items.isNullOrEmpty()) {
appendLine(" $name:")
appendLine("$name:".prependIndent(space))
items.forEach {
appendLine(" - '$it'")
appendLine(" - '$it'".prependIndent(space))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 2ad0463

Please sign in to comment.