Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow whitespaces in action params #107

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ object GenerativePlugin extends AutoPlugin {
s"environment: ${wrap(environment.name)}"
}

def compileEnv(env: Map[String, String], prefix: String = "env"): String =
def compileEnv(env: Map[String, String], prefix: String = "env", ignoreWhiteSpace: Boolean = false): String =
if (env.isEmpty) {
""
} else {
val rendered = env map {
case (key, value) =>
if (!isSafeString(key) || key.indexOf(' ') >= 0)
val whitSpaceValidation = if(ignoreWhiteSpace) false else key.indexOf(' ') >= 0

if (!isSafeString(key) || whitSpaceValidation)
sys.error(s"'$key' is not a valid environment variable name")

s"""$key: ${wrap(value)}"""
Expand Down Expand Up @@ -315,7 +317,7 @@ ${indent(rendered.mkString("\n"), 1)}"""
renderedShell + "run: " + wrap(commands.mkString("\n")) + renderParams(params)

def renderParams(params: Map[String, String]): String = {
val renderedParamsPre = compileEnv(params, prefix = "with")
val renderedParamsPre = compileEnv(params, prefix = "with", ignoreWhiteSpace = true)
val renderedParams = if (renderedParamsPre.isEmpty)
""
else
Expand Down
16 changes: 16 additions & 0 deletions src/test/scala/sbtghactions/GenerativePluginSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ class GenerativePluginSpec extends Specification {
Run(List("users"), timeout = Some(1.hour)),
"") mustEqual "- timeout-minutes: 60\n run: users"
}

"compile a run with parameters with space" in {
compileStep(
Run(List("echo foo"), params = Map("abc space" -> "def", "cafe test 1" -> "@42")),
"") mustEqual """- run: echo foo
| with:
| abc space: def
| cafe test 1: '@42'""".stripMargin
}

"throw error run with environment variables with space" in {
compileStep(
Run(List("echo foo"), env = Map("abc space" -> "def", "cafe test 1" -> "@42")),
"") must throwA[RuntimeException]
}

}

"job compilation" should {
Expand Down