feat: support multiple yaml transform extensions

This commit is contained in:
Bo-Yi Wu 2017-01-13 15:53:13 +08:00
parent 928130d7fa
commit d1ac862623
5 changed files with 83 additions and 11 deletions

View file

@ -28,7 +28,7 @@ type Agent struct {
Timeout time.Duration Timeout time.Duration
Platform string Platform string
Namespace string Namespace string
Extension string Extension []string
Disable []string Disable []string
Escalate []string Escalate []string
Netrc []string Netrc []string

View file

@ -133,7 +133,7 @@ var AgentCmd = cli.Command{
Name: "pull", Name: "pull",
Usage: "always pull latest plugin images", Usage: "always pull latest plugin images",
}, },
cli.StringFlag{ cli.StringSliceFlag{
EnvVar: "DRONE_YAML_EXTENSION", EnvVar: "DRONE_YAML_EXTENSION",
Name: "extension", Name: "extension",
Usage: "custom plugin extension endpoint", Usage: "custom plugin extension endpoint",
@ -197,7 +197,7 @@ func start(c *cli.Context) {
privileged: c.StringSlice("privileged"), privileged: c.StringSlice("privileged"),
pull: c.BoolT("pull"), pull: c.BoolT("pull"),
logs: int64(c.Int("max-log-size")) * 1000000, logs: int64(c.Int("max-log-size")) * 1000000,
extension: c.String("extension"), extension: c.StringSlice("extension"),
}, },
} }

View file

@ -19,7 +19,7 @@ type config struct {
pull bool pull bool
logs int64 logs int64
timeout time.Duration timeout time.Duration
extension string extension []string
} }
type pipeline struct { type pipeline struct {

View file

@ -10,12 +10,7 @@ import (
"github.com/drone/drone/yaml" "github.com/drone/drone/yaml"
) )
// RemoteTransform makes remote transform requests. func convertTransform(c *yaml.Config, url string) error {
func RemoteTransform(c *yaml.Config, url string) error {
if url == "" {
return nil
}
var buf bytes.Buffer var buf bytes.Buffer
// encode yaml in json format // encode yaml in json format
@ -41,3 +36,18 @@ func RemoteTransform(c *yaml.Config, url string) error {
} }
return fmt.Errorf(string(out)) 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
}

View file

@ -51,7 +51,7 @@ func Test_rpc_transform(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(handleNetrcRemoval)) server := httptest.NewServer(http.HandlerFunc(handleNetrcRemoval))
defer server.Close() defer server.Close()
err := RemoteTransform(c, server.URL) err := RemoteTransform(c, []string{server.URL})
g.Assert(err == nil).IsTrue() g.Assert(err == nil).IsTrue()
g.Assert(c.Pipeline[0].Image).Equal("golang") 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_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")
})
})
}