Skip to content

Commit

Permalink
fix: error with special characters in the visual editor Groovy Scrip
Browse files Browse the repository at this point in the history
  • Loading branch information
yudong2015 committed Aug 26, 2024
1 parent 370675c commit af53c5f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/api/devops/v1alpha3/steptemplate_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func (t *StepTemplateSpec) Render(param map[string]interface{}, secret *v1.Secre
switch t.Runtime {
case "dsl":
output, err = dslRender(t.Template, param, secret)
case "script":
output, err = scriptRender(t.Template, param, secret)
case "shell":
fallthrough
default:
Expand Down Expand Up @@ -135,6 +137,28 @@ func dslRender(dslTpl string, param map[string]interface{}, secret *v1.Secret) (
return
}

func scriptRender(scriptTpl string, param map[string]interface{}, secret *v1.Secret) (output string, err error) {
if output, err = dslRender(scriptTpl, param, secret); err == nil {
escapedOutput := strings.ReplaceAll(output, `\`, `\\`)
escapedOutput = strings.ReplaceAll(escapedOutput, "\n", "\\n")
escapedOutput = strings.ReplaceAll(escapedOutput, `"`, `\"`)

output = fmt.Sprintf(`{
"arguments": [
{
"key": "scriptBlock",
"value": {
"isLiteral": true,
"value": "%s"
}
}
],
"name": "script"
}`, escapedOutput)
}
return
}

func shellRender(shellTpl string, param map[string]interface{}, secret *v1.Secret) (output string, err error) {
if output, err = dslRender(shellTpl, param, secret); err == nil {
escapedOutput := strings.ReplaceAll(output, `\`, `\\`)
Expand Down

0 comments on commit af53c5f

Please sign in to comment.