re-use config and convert plugin clients
This commit is contained in:
parent
c42273084f
commit
f1dd01e50c
4 changed files with 74 additions and 21 deletions
|
@ -18,23 +18,27 @@ import (
|
||||||
// Global returns a configuration service that fetches the yaml
|
// Global returns a configuration service that fetches the yaml
|
||||||
// configuration from a remote endpoint.
|
// configuration from a remote endpoint.
|
||||||
func Global(endpoint, signer string, skipVerify bool) core.ConfigService {
|
func Global(endpoint, signer string, skipVerify bool) core.ConfigService {
|
||||||
|
if endpoint == "" {
|
||||||
|
return new(global)
|
||||||
|
}
|
||||||
return &global{
|
return &global{
|
||||||
endpoint: endpoint,
|
client: config.Client(
|
||||||
secret: signer,
|
endpoint,
|
||||||
skipVerify: skipVerify,
|
signer,
|
||||||
|
skipVerify,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type global struct {
|
type global struct {
|
||||||
endpoint string
|
client config.Plugin
|
||||||
secret string
|
|
||||||
skipVerify bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *global) Find(ctx context.Context, in *core.ConfigArgs) (*core.Config, error) {
|
func (g *global) Find(ctx context.Context, in *core.ConfigArgs) (*core.Config, error) {
|
||||||
if g.endpoint == "" {
|
if g.client == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// include a timeout to prevent an API call from
|
// include a timeout to prevent an API call from
|
||||||
// hanging the build process indefinitely. The
|
// hanging the build process indefinitely. The
|
||||||
// external service must return a request within
|
// 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),
|
Repo: toRepo(in.Repo),
|
||||||
Build: toBuild(in.Build),
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ type memoize struct {
|
||||||
func (c *memoize) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Config, error) {
|
func (c *memoize) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Config, error) {
|
||||||
// this is a minor optimization that prevents caching if the
|
// this is a minor optimization that prevents caching if the
|
||||||
// base converter is a remote converter and is disabled.
|
// 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
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,23 +19,26 @@ import (
|
||||||
// Remote returns a conversion service that converts the
|
// Remote returns a conversion service that converts the
|
||||||
// configuration file using a remote http service.
|
// configuration file using a remote http service.
|
||||||
func Remote(endpoint, signer, extension string, skipVerify bool) core.ConvertService {
|
func Remote(endpoint, signer, extension string, skipVerify bool) core.ConvertService {
|
||||||
|
if endpoint == "" {
|
||||||
|
return new(remote)
|
||||||
|
}
|
||||||
return &remote{
|
return &remote{
|
||||||
extension: extension,
|
extension: extension,
|
||||||
endpoint: endpoint,
|
client: converter.Client(
|
||||||
secret: signer,
|
endpoint,
|
||||||
skipVerify: skipVerify,
|
signer,
|
||||||
|
skipVerify,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type remote struct {
|
type remote struct {
|
||||||
extension string
|
client converter.Plugin
|
||||||
endpoint string
|
extension string
|
||||||
secret string
|
|
||||||
skipVerify bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *remote) Convert(ctx context.Context, in *core.ConvertArgs) (*core.Config, error) {
|
func (g *remote) Convert(ctx context.Context, in *core.ConvertArgs) (*core.Config, error) {
|
||||||
if g.endpoint == "" {
|
if g.client == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
if g.extension != "" {
|
if g.extension != "" {
|
||||||
|
@ -57,8 +60,8 @@ func (g *remote) Convert(ctx context.Context, in *core.ConvertArgs) (*core.Confi
|
||||||
Data: in.Config.Data,
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,3 +5,49 @@
|
||||||
// +build !oss
|
// +build !oss
|
||||||
|
|
||||||
package converter
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue