Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for validator #18

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions internal/plugin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"github.com/drone/drone-go/drone"
"github.com/drone/drone-go/plugin/converter"
"github.com/drone/drone-go/plugin/logger"
"github.com/drone/drone-go/plugin/validator"
)

type Router struct {
logger logger.Logger
convertPlugins []converter.Plugin
convertHandler http.Handler
logger logger.Logger
convertPlugins []converter.Plugin
convertHandler http.Handler
validatePlugins []validator.Plugin
validateHandler http.Handler
}

type RouterOption func(*Router)
Expand All @@ -23,6 +26,12 @@ func WithConvertPlugins(plugins ...converter.Plugin) RouterOption {
}
}

func WithValidatePlugins(plugins ...validator.Plugin) RouterOption {
return func(r *Router) {
r.validatePlugins = append(r.validatePlugins, plugins...)
}
}

func WithLogger(l logger.Logger) RouterOption {
return func(r *Router) {
r.logger = l
Expand All @@ -39,6 +48,7 @@ func NewRouter(secret string, opts ...RouterOption) *Router {
}

router.convertHandler = converter.Handler(router, secret, router.logger)
router.validateHandler = validator.Handler(secret, router, router.logger)

return router
}
Expand All @@ -57,10 +67,22 @@ func (r *Router) Convert(ctx context.Context, req *converter.Request) (*drone.Co
return &req.Config, nil
}

func (r *Router) Validate(ctx context.Context, req *validator.Request) error {
for _, plugin := range r.validatePlugins {
if err := plugin.Validate(ctx, req); err != nil {
return err
}
}

return nil
}

func (r *Router) ServeHTTP(res http.ResponseWriter, req *http.Request) {
switch req.Header.Get("Accept") {
case converter.V1:
r.convertHandler.ServeHTTP(res, req)
case validator.V1:
r.validateHandler.ServeHTTP(res, req)
default:
http.Error(res, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
Expand Down
52 changes: 52 additions & 0 deletions internal/plugin/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package plugin

import (
"context"
"errors"
"strings"
"testing"

"github.com/drone/drone-go/drone"
"github.com/drone/drone-go/plugin/converter"
"github.com/drone/drone-go/plugin/validator"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -49,3 +52,52 @@ func TestConvertRouter(t *testing.T) {
assert.Equal(t, test.want, conf.Data, test.description)
}
}

// stringValidate returns an error if the passed in string is not in the validator requests Config.Data
type stringValidate struct {
s string
}

func newStringValidate(s string) *stringValidate {
return &stringValidate{s: s}
}

func (p *stringValidate) Validate(ctx context.Context, req *validator.Request) error {
if !strings.Contains(req.Config.Data, p.s) {
return errors.New(p.s)
}

return nil
}

func TestValidateRouter(t *testing.T) {
t.Parallel()

tests := []struct {
description string
router *Router
input string
err error
}{
{
description: "test without validate plugins",
router: NewRouter(""),
},
{
description: "test passing validate plugin",
router: NewRouter("", WithValidatePlugins(newStringValidate("one"))),
input: "one",
},
{
description: "test with multiple validate plugins and an error",
router: NewRouter("", WithValidatePlugins(newStringValidate("one"), newStringValidate("two"))),
input: "one",
err: errors.New("two"),
},
}

for _, test := range tests {
req := &validator.Request{Config: drone.Config{Data: test.input}}
assert.Equal(t, test.err, test.router.Validate(context.TODO(), req))
}
}