mirror of
https://github.com/harness/drone.git
synced 2025-05-03 21:11:08 +08:00
Add unique and indexes to db scripts on namespace. Code refactor in conversion files & grouping drone imports
This commit is contained in:
parent
a3a4e7494c
commit
9dc11200d7
@ -49,8 +49,11 @@ func (p *starlarkPlugin) Convert(ctx context.Context, req *core.ConvertArgs) (*c
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
file, _ := starlark.Parse(req, nil, nil)
|
||||
file, err := starlark.Parse(req, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &core.Config{
|
||||
Data: *file,
|
||||
Data: file,
|
||||
}, nil
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/handler/api/errors"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"go.starlark.net/starlark"
|
||||
)
|
||||
@ -53,7 +54,7 @@ var (
|
||||
ErrCannotLoad = errors.New("starlark: cannot load external scripts")
|
||||
)
|
||||
|
||||
func Parse(req *core.ConvertArgs, template *core.Template, templateData map[string]interface{}) (file *string, err error) {
|
||||
func Parse(req *core.ConvertArgs, template *core.Template, templateData map[string]interface{}) (string, error) {
|
||||
thread := &starlark.Thread{
|
||||
Name: "drone",
|
||||
Load: noLoad,
|
||||
@ -76,7 +77,7 @@ func Parse(req *core.ConvertArgs, template *core.Template, templateData map[stri
|
||||
|
||||
globals, err := starlark.ExecFile(thread, starlarkFileName, starlarkFile, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
// find the main method in the starlark script and
|
||||
@ -84,11 +85,11 @@ func Parse(req *core.ConvertArgs, template *core.Template, templateData map[stri
|
||||
// is invalid.
|
||||
mainVal, ok := globals["main"]
|
||||
if !ok {
|
||||
return nil, ErrMainMissing
|
||||
return "", ErrMainMissing
|
||||
}
|
||||
main, ok := mainVal.(starlark.Callable)
|
||||
if !ok {
|
||||
return nil, ErrMainInvalid
|
||||
return "", ErrMainInvalid
|
||||
}
|
||||
|
||||
// create the input args and invoke the main method
|
||||
@ -102,7 +103,7 @@ func Parse(req *core.ConvertArgs, template *core.Template, templateData map[stri
|
||||
// execute the main method in the script.
|
||||
mainVal, err = starlark.Call(thread, main, args, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
@ -113,25 +114,24 @@ func Parse(req *core.ConvertArgs, template *core.Template, templateData map[stri
|
||||
buf.WriteString(separator)
|
||||
buf.WriteString(newline)
|
||||
if err := write(buf, item); err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
buf.WriteString(newline)
|
||||
}
|
||||
case *starlark.Dict:
|
||||
if err := write(buf, v); err != nil {
|
||||
return nil, err
|
||||
return "", err
|
||||
}
|
||||
default:
|
||||
return nil, ErrMainReturn
|
||||
return "", ErrMainReturn
|
||||
}
|
||||
|
||||
// this is a temporary workaround until we
|
||||
// implement a LimitWriter.
|
||||
if b := buf.Bytes(); len(b) > limit {
|
||||
return nil, nil
|
||||
return "", ErrMaximumSize
|
||||
}
|
||||
parsedFile := buf.String()
|
||||
return &parsedFile, nil
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func noLoad(_ *starlark.Thread, _ string) (starlark.StringDict, error) {
|
||||
|
@ -15,9 +15,10 @@
|
||||
package starlark
|
||||
|
||||
import (
|
||||
"github.com/drone/drone/core"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/drone/drone/core"
|
||||
)
|
||||
|
||||
func TestParseStarlark(t *testing.T) {
|
||||
@ -62,7 +63,7 @@ func TestParseStarlark(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
if want, got := *parsedFile, string(after); want != got {
|
||||
if want, got := parsedFile, string(after); want != got {
|
||||
t.Errorf("Want %q got %q", want, got)
|
||||
}
|
||||
}
|
||||
@ -100,7 +101,7 @@ func TestParseStarlarkNotTemplateFile(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
if want, got := *parsedFile, string(after); want != got {
|
||||
if want, got := parsedFile, string(after); want != got {
|
||||
t.Errorf("Want %q got %q", want, got)
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/drone/drone/core"
|
||||
|
||||
"github.com/drone/drone/plugin/converter/starlark"
|
||||
|
||||
"regexp"
|
||||
@ -78,7 +77,7 @@ func (p *templatePlugin) Convert(ctx context.Context, req *core.ConvertArgs) (*c
|
||||
return nil, err
|
||||
}
|
||||
return &core.Config{
|
||||
Data: *file,
|
||||
Data: file,
|
||||
}, nil
|
||||
}
|
||||
return nil, nil
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
|
||||
"github.com/drone/drone/core"
|
||||
"github.com/drone/drone/mock"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
|
@ -675,6 +675,8 @@ CREATE TABLE IF NOT EXISTS templates (
|
||||
,template_data BLOB
|
||||
,template_created INTEGER
|
||||
,template_updated INTEGER
|
||||
,UNIQUE(template_name)
|
||||
,UNIQUE(template_name, template_namespace)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ix_template_namespace ON templates (template_namespace);
|
||||
`
|
||||
|
@ -7,5 +7,7 @@ CREATE TABLE IF NOT EXISTS templates (
|
||||
,template_data BLOB
|
||||
,template_created INTEGER
|
||||
,template_updated INTEGER
|
||||
,UNIQUE(template_name)
|
||||
,UNIQUE(template_name, template_namespace)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ix_template_namespace ON templates (template_namespace);
|
||||
|
@ -653,5 +653,8 @@ CREATE TABLE IF NOT EXISTS templates (
|
||||
,template_data BYTEA
|
||||
,template_created INTEGER
|
||||
,template_updated INTEGER
|
||||
,UNIQUE(template_name, template_namespace)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ix_template_namespace ON templates (template_namespace);
|
||||
`
|
||||
|
@ -7,4 +7,7 @@ CREATE TABLE IF NOT EXISTS templates (
|
||||
,template_data BYTEA
|
||||
,template_created INTEGER
|
||||
,template_updated INTEGER
|
||||
,UNIQUE(template_name, template_namespace)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ix_template_namespace ON templates (template_namespace);
|
||||
|
@ -153,8 +153,8 @@ var migrations = []struct {
|
||||
stmt: createIndexLatestRepo,
|
||||
},
|
||||
{
|
||||
name: "create-table-template",
|
||||
stmt: createTableTemplate,
|
||||
name: "create-table-templates",
|
||||
stmt: createTableTemplates,
|
||||
},
|
||||
}
|
||||
|
||||
@ -647,7 +647,7 @@ CREATE INDEX IF NOT EXISTS ix_latest_repo ON latest (latest_repo_id);
|
||||
// 015_create_template_tables.sql
|
||||
//
|
||||
|
||||
var createTableTemplate = `
|
||||
var createTableTemplates = `
|
||||
CREATE TABLE IF NOT EXISTS templates (
|
||||
template_id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||
,template_name TEXT UNIQUE
|
||||
@ -655,5 +655,8 @@ CREATE TABLE IF NOT EXISTS templates (
|
||||
,template_data BLOB
|
||||
,template_created INTEGER
|
||||
,template_updated INTEGER
|
||||
,UNIQUE(template_name, template_namespace)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ix_template_namespace ON templates (template_namespace);
|
||||
`
|
||||
|
@ -1,4 +1,4 @@
|
||||
-- name: create-table-template
|
||||
-- name: create-table-templates
|
||||
|
||||
CREATE TABLE IF NOT EXISTS templates (
|
||||
template_id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||
@ -7,4 +7,7 @@ CREATE TABLE IF NOT EXISTS templates (
|
||||
,template_data BLOB
|
||||
,template_created INTEGER
|
||||
,template_updated INTEGER
|
||||
,UNIQUE(template_name, template_namespace)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS ix_template_namespace ON templates (template_namespace);
|
||||
|
Loading…
Reference in New Issue
Block a user