re-use config and convert plugin clients

This commit is contained in:
Brad Rydzewski 2020-01-03 17:53:47 -08:00
parent c42273084f
commit f1dd01e50c
4 changed files with 74 additions and 21 deletions

View file

@ -18,23 +18,27 @@ import (
// Global returns a configuration service that fetches the yaml
// configuration from a remote endpoint.
func Global(endpoint, signer string, skipVerify bool) core.ConfigService {
if endpoint == "" {
return new(global)
}
return &global{
endpoint: endpoint,
secret: signer,
skipVerify: skipVerify,
client: config.Client(
endpoint,
signer,
skipVerify,
),
}
}
type global struct {
endpoint string
secret string
skipVerify bool
client config.Plugin
}
func (g *global) Find(ctx context.Context, in *core.ConfigArgs) (*core.Config, error) {
if g.endpoint == "" {
if g.client == nil {
return nil, nil
}
// include a timeout to prevent an API call from
// hanging the build process indefinitely. The
// external service must return a request within
@ -46,8 +50,8 @@ func (g *global) Find(ctx context.Context, in *core.ConfigArgs) (*core.Config, e
Repo: toRepo(in.Repo),
Build: toBuild(in.Build),
}
client := config.Client(g.endpoint, g.secret, g.skipVerify)
res, err := client.Find(ctx, req)
res, err := g.client.Find(ctx, req)
if err != nil {
return nil, err
}

View file

@ -48,7 +48,7 @@ type memoize struct {
func (c *memoize) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Config, error) {
// this is a minor optimization that prevents caching if the
// base converter is a remote converter and is disabled.
if remote, ok := c.base.(*remote); ok == true && remote.endpoint == "" {
if remote, ok := c.base.(*remote); ok == true && remote.client == nil {
return nil, nil
}

View file

@ -19,23 +19,26 @@ import (
// Remote returns a conversion service that converts the
// configuration file using a remote http service.
func Remote(endpoint, signer, extension string, skipVerify bool) core.ConvertService {
if endpoint == "" {
return new(remote)
}
return &remote{
extension: extension,
endpoint: endpoint,
secret: signer,
skipVerify: skipVerify,
extension: extension,
client: converter.Client(
endpoint,
signer,
skipVerify,
),
}
}
type remote struct {
extension string
endpoint string
secret string
skipVerify bool
client converter.Plugin
extension string
}
func (g *remote) Convert(ctx context.Context, in *core.ConvertArgs) (*core.Config, error) {
if g.endpoint == "" {
if g.client == nil {
return nil, nil
}
if g.extension != "" {
@ -57,8 +60,8 @@ func (g *remote) Convert(ctx context.Context, in *core.ConvertArgs) (*core.Confi
Data: in.Config.Data,
},
}
client := converter.Client(g.endpoint, g.secret, g.skipVerify)
res, err := client.Convert(ctx, req)
res, err := g.client.Convert(ctx, req)
if err != nil {
return nil, err
}

View file

@ -5,3 +5,49 @@
// +build !oss
package converter
import (
"context"
"testing"
"github.com/drone/drone/core"
"github.com/h2non/gock"
)
func TestConvert(t *testing.T) {
defer gock.Off()
gock.New("https://company.com").
Post("/convert").
MatchHeader("Accept", "application/vnd.drone.convert.v1\\+json").
MatchHeader("Accept-Encoding", "identity").
MatchHeader("Content-Type", "application/json").
Reply(200).
BodyString(`{"data": "{ kind: pipeline, type: docker, name: default }"}`).
Done()
args := &core.ConvertArgs{
User: &core.User{Login: "octocat"},
Repo: &core.Repository{Slug: "octocat/hello-world", Config: ".drone.yml"},
Build: &core.Build{After: "6d144de7"},
Config: &core.Config{
Data: "{ kind: pipeline, name: default }",
},
}
service := Remote("https://company.com/convert", "GMEuUHQfmrMRsseWxi9YlIeBtn9lm6im", "", false)
result, err := service.Convert(context.Background(), args)
if err != nil {
t.Error(err)
return
}
if result.Data != "{ kind: pipeline, type: docker, name: default }" {
t.Errorf("unexpected file contents")
}
if gock.IsPending() {
t.Errorf("Unfinished requests")
return
}
}