Merge pull request #3087 from drone/feature/jsonnet-additional-params

Feature/jsonnet additional params
This commit is contained in:
Brad Rydzewski 2021-06-07 14:51:50 -04:00 committed by GitHub
commit ca3ac612c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,19 +3,31 @@ package jsonnet
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"strconv"
"github.com/drone/drone/core" "github.com/drone/drone/core"
"github.com/google/go-jsonnet" "github.com/google/go-jsonnet"
) )
const repo = "repo."
const build = "build."
const param = "param."
func Parse(req *core.ConvertArgs, template *core.Template, templateData map[string]interface{}) (string, error) { func Parse(req *core.ConvertArgs, template *core.Template, templateData map[string]interface{}) (string, error) {
// create the jsonnet vm
vm := jsonnet.MakeVM() vm := jsonnet.MakeVM()
vm.MaxStack = 500 vm.MaxStack = 500
vm.StringOutput = false vm.StringOutput = false
vm.ErrorFormatter.SetMaxStackTraceSize(20) vm.ErrorFormatter.SetMaxStackTraceSize(20)
//map build/repo parameters
if req.Build != nil {
mapBuild(req.Build, vm)
}
if req.Repo != nil {
mapRepo(req.Repo, vm)
}
var jsonnetFile string var jsonnetFile string
var jsonentFileName string var jsonentFileName string
if template != nil { if template != nil {
@ -33,6 +45,7 @@ func Parse(req *core.ConvertArgs, template *core.Template, templateData map[stri
vm.ExtVar(key, val) vm.ExtVar(key, val)
} }
} }
// convert the jsonnet file to yaml // convert the jsonnet file to yaml
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
docs, err := vm.EvaluateSnippetStream(jsonentFileName, jsonnetFile) docs, err := vm.EvaluateSnippetStream(jsonentFileName, jsonnetFile)
@ -54,3 +67,52 @@ func Parse(req *core.ConvertArgs, template *core.Template, templateData map[stri
return buf.String(), nil return buf.String(), nil
} }
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)
}
}