modified parameter injection strategy. see #538

This commit is contained in:
Brad Rydzewski 2014-10-12 13:02:53 -07:00
parent bbc0d37ad1
commit 629547813c
6 changed files with 59 additions and 18 deletions

View file

@ -15,9 +15,12 @@ services:
- postgres
- mysql
notify:
email:
recipients:
- brad@drone.io
gitter:
room_id: 76f5e5ec935c5b40259a
token: $$GITTER_TOKEN
on_started: false
on_success: false
on_failure: false
publish:
s3:

View file

@ -76,7 +76,7 @@ func PostHook(c web.C, w http.ResponseWriter, r *http.Request) {
// verify the commit hooks branch matches the list of approved
// branches (unless it is a pull request). Note that we don't really
// care if parsing the yaml fails here.
s, _ := script.ParseBuild(string(yml), map[string]string{})
s, _ := script.ParseBuild(string(yml))
if len(hook.PullRequest) == 0 && !s.MatchBranch(hook.Branch) {
w.WriteHeader(http.StatusOK)
return

View file

@ -81,7 +81,7 @@ func (d *Docker) Do(c context.Context, r *worker.Work) {
if err != nil {
log.Printf("Error parsing PARAMS for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
}
script, err := script.ParseBuild(r.Commit.Config, params)
script, err := script.ParseBuild(script.Inject(r.Commit.Config, params))
if err != nil {
log.Printf("Error parsing YAML for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
}

View file

@ -0,0 +1,16 @@
package script
import (
"strings"
)
func Inject(script string, params map[string]string) string {
if params == nil {
return script
}
injected := script
for k, v := range params {
injected = strings.Replace(injected, "$$"+k, v, -1)
}
return injected
}

View file

@ -0,0 +1,32 @@
package script
import (
"github.com/franela/goblin"
"testing"
)
func Test_Inject(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Inject params", func() {
g.It("Should replace vars with $$", func() {
s := "echo $$FOO $BAR"
m := map[string]string{}
m["FOO"] = "BAZ"
g.Assert("echo BAZ $BAR").Equal(Inject(s, m))
})
g.It("Should not replace vars with single $", func() {
s := "echo $FOO $BAR"
m := map[string]string{}
m["FOO"] = "BAZ"
g.Assert(s).Equal(Inject(s, m))
})
g.It("Should not replace vars in nil map", func() {
s := "echo $$FOO $BAR"
g.Assert(s).Equal(Inject(s, nil))
})
})
}

View file

@ -1,8 +1,6 @@
package script
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
@ -16,11 +14,11 @@ import (
"github.com/drone/drone/shared/build/repo"
)
func ParseBuild(data string, params map[string]string) (*Build, error) {
func ParseBuild(data string) (*Build, error) {
build := Build{}
// parse the build configuration file
err := yaml.Unmarshal(injectParams([]byte(data), params), &build)
err := yaml.Unmarshal([]byte(data), &build)
return &build, err
}
@ -30,15 +28,7 @@ func ParseBuildFile(filename string) (*Build, error) {
return nil, err
}
return ParseBuild(string(data), nil)
}
// injectParams injects params into data.
func injectParams(data []byte, params map[string]string) []byte {
for k, v := range params {
data = bytes.Replace(data, []byte(fmt.Sprintf("{{%s}}", k)), []byte(v), -1)
}
return data
return ParseBuild(string(data))
}
// Build stores the configuration details for