2021-06-03 13:06:57 +00:00
|
|
|
package jsonnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-03 13:17:39 +00:00
|
|
|
"fmt"
|
2021-06-04 15:01:14 +00:00
|
|
|
"strconv"
|
2021-06-03 13:06:57 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
|
|
|
|
|
|
|
"github.com/google/go-jsonnet"
|
|
|
|
)
|
|
|
|
|
2021-06-04 15:01:14 +00:00
|
|
|
const repo = "repo."
|
|
|
|
const build = "build."
|
|
|
|
const param = "param."
|
|
|
|
|
2021-06-03 13:06:57 +00:00
|
|
|
func Parse(req *core.ConvertArgs, template *core.Template, templateData map[string]interface{}) (string, error) {
|
|
|
|
vm := jsonnet.MakeVM()
|
|
|
|
vm.MaxStack = 500
|
|
|
|
vm.StringOutput = false
|
|
|
|
vm.ErrorFormatter.SetMaxStackTraceSize(20)
|
|
|
|
|
2021-06-04 15:01:14 +00:00
|
|
|
//map build/repo parameters
|
2021-06-07 09:27:45 +00:00
|
|
|
if req.Build != nil {
|
|
|
|
mapBuild(req.Build, vm)
|
|
|
|
}
|
|
|
|
if req.Repo != nil {
|
|
|
|
mapRepo(req.Repo, vm)
|
|
|
|
}
|
2021-06-04 15:01:14 +00:00
|
|
|
|
2021-06-03 13:06:57 +00:00
|
|
|
var jsonnetFile string
|
2021-06-07 10:20:55 +00:00
|
|
|
var jsonnetFileName string
|
2021-06-03 13:06:57 +00:00
|
|
|
if template != nil {
|
|
|
|
jsonnetFile = template.Data
|
2021-06-07 10:20:55 +00:00
|
|
|
jsonnetFileName = template.Name
|
2021-06-03 13:06:57 +00:00
|
|
|
} else {
|
|
|
|
jsonnetFile = req.Config.Data
|
2021-06-07 10:20:55 +00:00
|
|
|
jsonnetFileName = req.Repo.Config
|
2021-06-03 13:06:57 +00:00
|
|
|
}
|
|
|
|
// map external inputs
|
|
|
|
if len(templateData) != 0 {
|
|
|
|
for k, v := range templateData {
|
2021-06-03 13:17:39 +00:00
|
|
|
key := fmt.Sprintf("input." + k)
|
|
|
|
val := fmt.Sprint(v)
|
|
|
|
vm.ExtVar(key, val)
|
2021-06-03 13:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-04 15:01:14 +00:00
|
|
|
|
2021-06-03 13:06:57 +00:00
|
|
|
// convert the jsonnet file to yaml
|
|
|
|
buf := new(bytes.Buffer)
|
2021-06-07 10:20:55 +00:00
|
|
|
docs, err := vm.EvaluateSnippetStream(jsonnetFileName, jsonnetFile)
|
2021-06-03 13:06:57 +00:00
|
|
|
if err != nil {
|
2021-06-07 10:20:55 +00:00
|
|
|
doc, err2 := vm.EvaluateSnippet(jsonnetFileName, jsonnetFile)
|
2021-06-03 13:06:57 +00:00
|
|
|
if err2 != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
docs = append(docs, doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the jsonnet vm returns a stream of yaml documents
|
|
|
|
// that need to be combined into a single yaml file.
|
|
|
|
for _, doc := range docs {
|
|
|
|
buf.WriteString("---")
|
|
|
|
buf.WriteString("\n")
|
|
|
|
buf.WriteString(doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
2021-06-04 15:01:14 +00:00
|
|
|
|
|
|
|
func mapBuild(v *core.Build, vm *jsonnet.VM) {
|
|
|
|
vm.ExtVar(build+"event", v.Event)
|
|
|
|
vm.ExtVar(build+"action", v.Action)
|
|
|
|
vm.ExtVar(build+"environment", v.Deploy)
|
|
|
|
vm.ExtVar(build+"link", v.Link)
|
|
|
|
vm.ExtVar(build+"branch", v.Target)
|
|
|
|
vm.ExtVar(build+"source", v.Source)
|
|
|
|
vm.ExtVar(build+"before", v.Before)
|
|
|
|
vm.ExtVar(build+"after", v.After)
|
|
|
|
vm.ExtVar(build+"target", v.Target)
|
|
|
|
vm.ExtVar(build+"ref", v.Ref)
|
|
|
|
vm.ExtVar(build+"commit", v.After)
|
|
|
|
vm.ExtVar(build+"ref", v.Ref)
|
|
|
|
vm.ExtVar(build+"title", v.Title)
|
|
|
|
vm.ExtVar(build+"message", v.Message)
|
|
|
|
vm.ExtVar(build+"source_repo", v.Fork)
|
|
|
|
vm.ExtVar(build+"author_login", v.Author)
|
|
|
|
vm.ExtVar(build+"author_name", v.AuthorName)
|
|
|
|
vm.ExtVar(build+"author_email", v.AuthorEmail)
|
|
|
|
vm.ExtVar(build+"author_avatar", v.AuthorAvatar)
|
|
|
|
vm.ExtVar(build+"sender", v.Sender)
|
|
|
|
fromMap(v.Params, vm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapRepo(v *core.Repository, vm *jsonnet.VM) {
|
|
|
|
vm.ExtVar(repo+"uid", v.UID)
|
|
|
|
vm.ExtVar(repo+"name", v.Name)
|
|
|
|
vm.ExtVar(repo+"namespace", v.Namespace)
|
|
|
|
vm.ExtVar(repo+"slug", v.Slug)
|
|
|
|
vm.ExtVar(repo+"git_http_url", v.HTTPURL)
|
|
|
|
vm.ExtVar(repo+"git_ssh_url", v.SSHURL)
|
|
|
|
vm.ExtVar(repo+"link", v.Link)
|
|
|
|
vm.ExtVar(repo+"branch", v.Branch)
|
|
|
|
vm.ExtVar(repo+"config", v.Config)
|
|
|
|
vm.ExtVar(repo+"private", strconv.FormatBool(v.Private))
|
|
|
|
vm.ExtVar(repo+"visibility", v.Visibility)
|
|
|
|
vm.ExtVar(repo+"active", strconv.FormatBool(v.Active))
|
|
|
|
vm.ExtVar(repo+"trusted", strconv.FormatBool(v.Trusted))
|
|
|
|
vm.ExtVar(repo+"protected", strconv.FormatBool(v.Protected))
|
|
|
|
vm.ExtVar(repo+"ignore_forks", strconv.FormatBool(v.IgnoreForks))
|
|
|
|
vm.ExtVar(repo+"ignore_pull_requests", strconv.FormatBool(v.IgnorePulls))
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromMap(m map[string]string, vm *jsonnet.VM) {
|
|
|
|
for k, v := range m {
|
|
|
|
vm.ExtVar(build+param+k, v)
|
|
|
|
}
|
|
|
|
}
|