mirror of
https://github.com/harness/drone.git
synced 2025-05-04 09:52: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
|
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{
|
return &core.Config{
|
||||||
Data: *file,
|
Data: file,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
|
|
||||||
"github.com/drone/drone/core"
|
"github.com/drone/drone/core"
|
||||||
"github.com/drone/drone/handler/api/errors"
|
"github.com/drone/drone/handler/api/errors"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"go.starlark.net/starlark"
|
"go.starlark.net/starlark"
|
||||||
)
|
)
|
||||||
@ -53,7 +54,7 @@ var (
|
|||||||
ErrCannotLoad = errors.New("starlark: cannot load external scripts")
|
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{
|
thread := &starlark.Thread{
|
||||||
Name: "drone",
|
Name: "drone",
|
||||||
Load: noLoad,
|
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)
|
globals, err := starlark.ExecFile(thread, starlarkFileName, starlarkFile, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the main method in the starlark script and
|
// 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.
|
// is invalid.
|
||||||
mainVal, ok := globals["main"]
|
mainVal, ok := globals["main"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrMainMissing
|
return "", ErrMainMissing
|
||||||
}
|
}
|
||||||
main, ok := mainVal.(starlark.Callable)
|
main, ok := mainVal.(starlark.Callable)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrMainInvalid
|
return "", ErrMainInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
// create the input args and invoke the main method
|
// 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.
|
// execute the main method in the script.
|
||||||
mainVal, err = starlark.Call(thread, main, args, nil)
|
mainVal, err = starlark.Call(thread, main, args, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
@ -113,25 +114,24 @@ func Parse(req *core.ConvertArgs, template *core.Template, templateData map[stri
|
|||||||
buf.WriteString(separator)
|
buf.WriteString(separator)
|
||||||
buf.WriteString(newline)
|
buf.WriteString(newline)
|
||||||
if err := write(buf, item); err != nil {
|
if err := write(buf, item); err != nil {
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
buf.WriteString(newline)
|
buf.WriteString(newline)
|
||||||
}
|
}
|
||||||
case *starlark.Dict:
|
case *starlark.Dict:
|
||||||
if err := write(buf, v); err != nil {
|
if err := write(buf, v); err != nil {
|
||||||
return nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil, ErrMainReturn
|
return "", ErrMainReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is a temporary workaround until we
|
// this is a temporary workaround until we
|
||||||
// implement a LimitWriter.
|
// implement a LimitWriter.
|
||||||
if b := buf.Bytes(); len(b) > limit {
|
if b := buf.Bytes(); len(b) > limit {
|
||||||
return nil, nil
|
return "", ErrMaximumSize
|
||||||
}
|
}
|
||||||
parsedFile := buf.String()
|
return buf.String(), nil
|
||||||
return &parsedFile, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func noLoad(_ *starlark.Thread, _ string) (starlark.StringDict, error) {
|
func noLoad(_ *starlark.Thread, _ string) (starlark.StringDict, error) {
|
||||||
|
@ -15,9 +15,10 @@
|
|||||||
package starlark
|
package starlark
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/drone/drone/core"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/drone/drone/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseStarlark(t *testing.T) {
|
func TestParseStarlark(t *testing.T) {
|
||||||
@ -62,7 +63,7 @@ func TestParseStarlark(t *testing.T) {
|
|||||||
return
|
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)
|
t.Errorf("Want %q got %q", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -100,7 +101,7 @@ func TestParseStarlarkNotTemplateFile(t *testing.T) {
|
|||||||
return
|
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)
|
t.Errorf("Want %q got %q", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/drone/drone/core"
|
"github.com/drone/drone/core"
|
||||||
|
|
||||||
"github.com/drone/drone/plugin/converter/starlark"
|
"github.com/drone/drone/plugin/converter/starlark"
|
||||||
|
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -78,7 +77,7 @@ func (p *templatePlugin) Convert(ctx context.Context, req *core.ConvertArgs) (*c
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &core.Config{
|
return &core.Config{
|
||||||
Data: *file,
|
Data: file,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
|
|
||||||
"github.com/drone/drone/core"
|
"github.com/drone/drone/core"
|
||||||
"github.com/drone/drone/mock"
|
"github.com/drone/drone/mock"
|
||||||
|
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -675,6 +675,8 @@ CREATE TABLE IF NOT EXISTS templates (
|
|||||||
,template_data BLOB
|
,template_data BLOB
|
||||||
,template_created INTEGER
|
,template_created INTEGER
|
||||||
,template_updated 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_data BLOB
|
||||||
,template_created INTEGER
|
,template_created INTEGER
|
||||||
,template_updated 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_data BYTEA
|
||||||
,template_created INTEGER
|
,template_created INTEGER
|
||||||
,template_updated 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_data BYTEA
|
||||||
,template_created INTEGER
|
,template_created INTEGER
|
||||||
,template_updated 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,
|
stmt: createIndexLatestRepo,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "create-table-template",
|
name: "create-table-templates",
|
||||||
stmt: createTableTemplate,
|
stmt: createTableTemplates,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -647,7 +647,7 @@ CREATE INDEX IF NOT EXISTS ix_latest_repo ON latest (latest_repo_id);
|
|||||||
// 015_create_template_tables.sql
|
// 015_create_template_tables.sql
|
||||||
//
|
//
|
||||||
|
|
||||||
var createTableTemplate = `
|
var createTableTemplates = `
|
||||||
CREATE TABLE IF NOT EXISTS templates (
|
CREATE TABLE IF NOT EXISTS templates (
|
||||||
template_id INTEGER PRIMARY KEY AUTOINCREMENT
|
template_id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||||
,template_name TEXT UNIQUE
|
,template_name TEXT UNIQUE
|
||||||
@ -655,5 +655,8 @@ CREATE TABLE IF NOT EXISTS templates (
|
|||||||
,template_data BLOB
|
,template_data BLOB
|
||||||
,template_created INTEGER
|
,template_created INTEGER
|
||||||
,template_updated 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 (
|
CREATE TABLE IF NOT EXISTS templates (
|
||||||
template_id INTEGER PRIMARY KEY AUTOINCREMENT
|
template_id INTEGER PRIMARY KEY AUTOINCREMENT
|
||||||
@ -7,4 +7,7 @@ CREATE TABLE IF NOT EXISTS templates (
|
|||||||
,template_data BLOB
|
,template_data BLOB
|
||||||
,template_created INTEGER
|
,template_created INTEGER
|
||||||
,template_updated 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