2021-05-28 15:59:00 +00:00
|
|
|
// Copyright 2019 Drone IO, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-06-04 08:59:46 +00:00
|
|
|
// +build !oss
|
|
|
|
|
2021-05-27 13:58:23 +00:00
|
|
|
package converter
|
|
|
|
|
|
|
|
import (
|
2021-08-24 12:20:44 +00:00
|
|
|
"bytes"
|
2021-05-27 13:58:23 +00:00
|
|
|
"context"
|
2021-06-17 09:19:51 +00:00
|
|
|
"database/sql"
|
2021-05-27 13:58:23 +00:00
|
|
|
"errors"
|
2021-08-24 12:20:44 +00:00
|
|
|
"path/filepath"
|
2021-06-03 13:20:28 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2021-08-24 12:20:44 +00:00
|
|
|
templating "text/template"
|
2021-05-28 15:59:00 +00:00
|
|
|
|
2021-05-27 13:58:23 +00:00
|
|
|
"github.com/drone/drone/core"
|
2021-06-03 13:15:41 +00:00
|
|
|
"github.com/drone/drone/plugin/converter/jsonnet"
|
2021-05-28 15:59:00 +00:00
|
|
|
"github.com/drone/drone/plugin/converter/starlark"
|
2021-08-24 12:20:44 +00:00
|
|
|
"github.com/drone/funcmap"
|
2021-05-27 13:58:23 +00:00
|
|
|
|
2021-05-28 15:59:00 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2021-05-27 13:58:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-08-31 14:51:39 +00:00
|
|
|
// templateFileRE regex to verifying kind is template.
|
2021-09-01 14:06:03 +00:00
|
|
|
templateFileRE = regexp.MustCompilePOSIX("^kind:[[:space:]]+template[[:space:]]?+$")
|
2021-08-31 14:51:39 +00:00
|
|
|
errTemplateNotFound = errors.New("template converter: template name given not found")
|
|
|
|
errTemplateSyntaxErrors = errors.New("template converter: there is a problem with the yaml file provided")
|
|
|
|
errTemplateExtensionInvalid = errors.New("template extension invalid. must be yaml, starlark or jsonnet")
|
2021-05-27 13:58:23 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 10:35:44 +00:00
|
|
|
func Template(templateStore core.TemplateStore, stepLimit uint64) core.ConvertService {
|
2021-05-27 13:58:23 +00:00
|
|
|
return &templatePlugin{
|
|
|
|
templateStore: templateStore,
|
2021-09-03 10:35:44 +00:00
|
|
|
stepLimit: stepLimit,
|
2021-05-27 13:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type templatePlugin struct {
|
|
|
|
templateStore core.TemplateStore
|
2021-09-03 10:35:44 +00:00
|
|
|
stepLimit uint64
|
2021-05-27 13:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *templatePlugin) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Config, error) {
|
|
|
|
// check type is yaml
|
|
|
|
if strings.HasSuffix(req.Repo.Config, ".yml") == false {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-08-24 12:20:44 +00:00
|
|
|
|
2021-05-27 13:58:23 +00:00
|
|
|
// check kind is template
|
2021-08-31 14:51:39 +00:00
|
|
|
if templateFileRE.MatchString(req.Config.Data) == false {
|
2021-05-27 13:58:23 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// map to templateArgs
|
|
|
|
var templateArgs core.TemplateArgs
|
|
|
|
err := yaml.Unmarshal([]byte(req.Config.Data), &templateArgs)
|
|
|
|
if err != nil {
|
2021-08-31 14:51:39 +00:00
|
|
|
return nil, errTemplateSyntaxErrors
|
2021-05-27 13:58:23 +00:00
|
|
|
}
|
|
|
|
// get template from db
|
2021-05-28 15:59:00 +00:00
|
|
|
template, err := p.templateStore.FindName(ctx, templateArgs.Load, req.Repo.Namespace)
|
2021-06-17 09:19:51 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2021-08-31 14:51:39 +00:00
|
|
|
return nil, errTemplateNotFound
|
2021-05-27 13:58:23 +00:00
|
|
|
}
|
2021-06-17 09:19:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-02 16:23:02 +00:00
|
|
|
|
2021-08-24 12:20:44 +00:00
|
|
|
switch filepath.Ext(templateArgs.Load) {
|
|
|
|
case ".yml", ".yaml":
|
|
|
|
return parseYaml(req, template, templateArgs)
|
|
|
|
case ".star", ".starlark", ".script":
|
2021-09-03 10:35:44 +00:00
|
|
|
return parseStarlark(req, template, templateArgs, p.stepLimit)
|
2021-08-24 12:20:44 +00:00
|
|
|
case ".jsonnet":
|
|
|
|
return parseJsonnet(req, template, templateArgs)
|
|
|
|
default:
|
2021-08-31 14:51:39 +00:00
|
|
|
return nil, errTemplateExtensionInvalid
|
2021-08-24 12:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-27 13:58:23 +00:00
|
|
|
|
2021-08-24 12:20:44 +00:00
|
|
|
func parseYaml(req *core.ConvertArgs, template *core.Template, templateArgs core.TemplateArgs) (*core.Config, error) {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"build": toBuild(req.Build),
|
|
|
|
"repo": toRepo(req.Repo),
|
|
|
|
"input": templateArgs.Data,
|
|
|
|
}
|
|
|
|
tmpl, err := templating.New(template.Name).Funcs(funcmap.SafeFuncs).Parse(template.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var out bytes.Buffer
|
|
|
|
err = tmpl.Execute(&out, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-27 13:58:23 +00:00
|
|
|
}
|
2021-08-24 12:20:44 +00:00
|
|
|
return &core.Config{
|
|
|
|
Data: out.String(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseJsonnet(req *core.ConvertArgs, template *core.Template, templateArgs core.TemplateArgs) (*core.Config, error) {
|
|
|
|
file, err := jsonnet.Parse(req, template, templateArgs.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-06-03 13:06:57 +00:00
|
|
|
}
|
2021-08-24 12:20:44 +00:00
|
|
|
return &core.Config{
|
|
|
|
Data: file,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-06-03 13:06:57 +00:00
|
|
|
|
2021-09-03 10:35:44 +00:00
|
|
|
func parseStarlark(req *core.ConvertArgs, template *core.Template, templateArgs core.TemplateArgs, stepLimit uint64) (*core.Config, error) {
|
|
|
|
file, err := starlark.Parse(req, template, templateArgs.Data, stepLimit)
|
2021-08-24 12:20:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &core.Config{
|
|
|
|
Data: file,
|
|
|
|
}, nil
|
2021-05-27 13:58:23 +00:00
|
|
|
}
|