From d1ac862623d36716da156b2071784564bb3be02b Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Fri, 13 Jan 2017 15:53:13 +0800 Subject: [PATCH] feat: support multiple yaml transform extensions --- agent/agent.go | 2 +- drone/agent/agent.go | 4 +-- drone/agent/exec.go | 2 +- yaml/transform/rpc.go | 22 +++++++++---- yaml/transform/rpc_test.go | 64 +++++++++++++++++++++++++++++++++++++- 5 files changed, 83 insertions(+), 11 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index c39aa2573..c16d11c29 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -28,7 +28,7 @@ type Agent struct { Timeout time.Duration Platform string Namespace string - Extension string + Extension []string Disable []string Escalate []string Netrc []string diff --git a/drone/agent/agent.go b/drone/agent/agent.go index a41c87ff7..a3e459259 100644 --- a/drone/agent/agent.go +++ b/drone/agent/agent.go @@ -133,7 +133,7 @@ var AgentCmd = cli.Command{ Name: "pull", Usage: "always pull latest plugin images", }, - cli.StringFlag{ + cli.StringSliceFlag{ EnvVar: "DRONE_YAML_EXTENSION", Name: "extension", Usage: "custom plugin extension endpoint", @@ -197,7 +197,7 @@ func start(c *cli.Context) { privileged: c.StringSlice("privileged"), pull: c.BoolT("pull"), logs: int64(c.Int("max-log-size")) * 1000000, - extension: c.String("extension"), + extension: c.StringSlice("extension"), }, } diff --git a/drone/agent/exec.go b/drone/agent/exec.go index 895e6522c..db486deca 100644 --- a/drone/agent/exec.go +++ b/drone/agent/exec.go @@ -19,7 +19,7 @@ type config struct { pull bool logs int64 timeout time.Duration - extension string + extension []string } type pipeline struct { diff --git a/yaml/transform/rpc.go b/yaml/transform/rpc.go index 852cac138..f45df75ad 100644 --- a/yaml/transform/rpc.go +++ b/yaml/transform/rpc.go @@ -10,12 +10,7 @@ import ( "github.com/drone/drone/yaml" ) -// RemoteTransform makes remote transform requests. -func RemoteTransform(c *yaml.Config, url string) error { - if url == "" { - return nil - } - +func convertTransform(c *yaml.Config, url string) error { var buf bytes.Buffer // encode yaml in json format @@ -41,3 +36,18 @@ func RemoteTransform(c *yaml.Config, url string) error { } return fmt.Errorf(string(out)) } + +// RemoteTransform makes remote transform requests. +func RemoteTransform(c *yaml.Config, url []string) error { + if len(url) == 0 { + return nil + } + + for _, u := range url { + if err := convertTransform(c, u); err != nil { + return err + } + } + + return nil +} diff --git a/yaml/transform/rpc_test.go b/yaml/transform/rpc_test.go index 12e0ce8ad..c194676fa 100644 --- a/yaml/transform/rpc_test.go +++ b/yaml/transform/rpc_test.go @@ -51,7 +51,7 @@ func Test_rpc_transform(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(handleNetrcRemoval)) defer server.Close() - err := RemoteTransform(c, server.URL) + err := RemoteTransform(c, []string{server.URL}) g.Assert(err == nil).IsTrue() g.Assert(c.Pipeline[0].Image).Equal("golang") g.Assert(c.Pipeline[0].Environment["DRONE_BRANCH"]).Equal("master") @@ -62,3 +62,65 @@ func Test_rpc_transform(t *testing.T) { }) }) } + +func handleGoogleRemoval(w http.ResponseWriter, r *http.Request) { + c := new(yaml.Config) + err := json.NewDecoder(r.Body).Decode(c) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + return + } + for _, container := range c.Pipeline { + if strings.HasPrefix(container.Image, "plugins/git") { + continue + } + container.Environment["DRONE_GOOGLE_USERNAME"] = "" + container.Environment["DRONE_GOOGLE_PASSWORD"] = "" + } + json.NewEncoder(w).Encode(c) +} + +func Test_rpc_multiple_transform(t *testing.T) { + g := goblin.Goblin(t) + g.Describe("rpc transform", func() { + + g.It("should mutate the multiple yaml", func() { + c := newConfig(&yaml.Container{ + Image: "golang", + Environment: map[string]string{ + "DRONE_NETRC_USERNAME": "foo", + "DRONE_NETRC_PASSWORD": "bar", + "DRONE_BRANCH": "master", + "DRONE_GOOGLE_USERNAME": "foo", + "DRONE_GOOGLE_PASSWORD": "bar", + }, + Commands: []string{ + "go build", + "go test", + }, + }) + + server1 := httptest.NewServer(http.HandlerFunc(handleNetrcRemoval)) + defer server1.Close() + + server2 := httptest.NewServer(http.HandlerFunc(handleGoogleRemoval)) + defer server2.Close() + + // testing missing urls + err := RemoteTransform(c, []string{}) + g.Assert(err == nil).IsTrue() + + err = RemoteTransform(c, []string{server1.URL, server2.URL}) + g.Assert(err == nil).IsTrue() + g.Assert(c.Pipeline[0].Image).Equal("golang") + g.Assert(c.Pipeline[0].Environment["DRONE_BRANCH"]).Equal("master") + g.Assert(c.Pipeline[0].Environment["DRONE_NETRC_USERNAME"]).Equal("") + g.Assert(c.Pipeline[0].Environment["DRONE_NETRC_PASSWORD"]).Equal("") + g.Assert(c.Pipeline[0].Environment["DRONE_GOOGLE_USERNAME"]).Equal("") + g.Assert(c.Pipeline[0].Environment["DRONE_GOOGLE_PASSWORD"]).Equal("") + g.Assert(c.Pipeline[0].Commands[0]).Equal("go build") + g.Assert(c.Pipeline[0].Commands[1]).Equal("go test") + }) + }) +}