mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-04-26 13:34:45 +00:00
modified parameter injection strategy. see #538
This commit is contained in:
parent
bbc0d37ad1
commit
629547813c
6 changed files with 59 additions and 18 deletions
|
@ -15,9 +15,12 @@ services:
|
||||||
- postgres
|
- postgres
|
||||||
- mysql
|
- mysql
|
||||||
notify:
|
notify:
|
||||||
email:
|
gitter:
|
||||||
recipients:
|
room_id: 76f5e5ec935c5b40259a
|
||||||
- brad@drone.io
|
token: $$GITTER_TOKEN
|
||||||
|
on_started: false
|
||||||
|
on_success: false
|
||||||
|
on_failure: false
|
||||||
|
|
||||||
publish:
|
publish:
|
||||||
s3:
|
s3:
|
||||||
|
|
|
@ -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
|
// verify the commit hooks branch matches the list of approved
|
||||||
// branches (unless it is a pull request). Note that we don't really
|
// branches (unless it is a pull request). Note that we don't really
|
||||||
// care if parsing the yaml fails here.
|
// 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) {
|
if len(hook.PullRequest) == 0 && !s.MatchBranch(hook.Branch) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
return
|
return
|
||||||
|
|
|
@ -81,7 +81,7 @@ func (d *Docker) Do(c context.Context, r *worker.Work) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error parsing PARAMS for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
|
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 {
|
if err != nil {
|
||||||
log.Printf("Error parsing YAML for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
|
log.Printf("Error parsing YAML for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
|
||||||
}
|
}
|
||||||
|
|
16
shared/build/script/inject.go
Normal file
16
shared/build/script/inject.go
Normal 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
|
||||||
|
}
|
32
shared/build/script/inject_test.go
Normal file
32
shared/build/script/inject_test.go
Normal 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))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
|
@ -1,8 +1,6 @@
|
||||||
package script
|
package script
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -16,11 +14,11 @@ import (
|
||||||
"github.com/drone/drone/shared/build/repo"
|
"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{}
|
build := Build{}
|
||||||
|
|
||||||
// parse the build configuration file
|
// parse the build configuration file
|
||||||
err := yaml.Unmarshal(injectParams([]byte(data), params), &build)
|
err := yaml.Unmarshal([]byte(data), &build)
|
||||||
return &build, err
|
return &build, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,15 +28,7 @@ func ParseBuildFile(filename string) (*Build, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ParseBuild(string(data), nil)
|
return ParseBuild(string(data))
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build stores the configuration details for
|
// Build stores the configuration details for
|
||||||
|
|
Loading…
Reference in a new issue