From a04141c4440be5c0f003d5441e4e2a97bc09060e Mon Sep 17 00:00:00 2001 From: Casey Lee Date: Fri, 18 Jan 2019 13:28:53 -0800 Subject: [PATCH] fix: panic on regex in main.workflow fixes #12 --- actions/parser.go | 3 +- actions/parser_test.go | 62 +++++++++++++++++++++++++++++------------- actions/runner_exec.go | 6 ++-- common/git.go | 15 +++++----- 4 files changed, 56 insertions(+), 30 deletions(-) diff --git a/actions/parser.go b/actions/parser.go index 6eedd63..2497a4b 100644 --- a/actions/parser.go +++ b/actions/parser.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "strings" "github.com/hashicorp/hcl" "github.com/hashicorp/hcl/hcl/ast" @@ -54,7 +55,7 @@ func cleanWorkflowsAST(node ast.Node) (ast.Node, bool) { } quote := literalType.Token.Text[0] for _, part := range parts { - part = fmt.Sprintf("%c%s%c", quote, part, quote) + part = fmt.Sprintf("%c%s%c", quote, strings.Replace(part, "\\", "\\\\", -1), quote) listType.Add(&ast.LiteralType{ Token: token.Token{ Type: token.STRING, diff --git a/actions/parser_test.go b/actions/parser_test.go index 70d2a96..9826e21 100644 --- a/actions/parser_test.go +++ b/actions/parser_test.go @@ -13,40 +13,50 @@ func TestParseWorkflowsFile(t *testing.T) { conf := ` workflow "build-and-deploy" { - on = "push" - resolves = ["deploy"] + on = "push" + resolves = ["deploy"] } action "build" { - uses = "./action1" - args = "echo 'build'" + uses = "./action1" + args = "echo 'build'" } action "test" { - uses = "docker://ubuntu:18.04" - runs = "echo 'test'" - needs = ["build"] + uses = "docker://ubuntu:18.04" + runs = "echo 'test'" + needs = ["build"] } action "deploy" { - uses = "./action2" - args = ["echo","deploy"] - needs = ["test"] + uses = "./action2" + args = ["echo","deploy"] + needs = ["test"] } action "docker-login" { - uses = "docker://docker" - runs = ["sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"] - secrets = ["DOCKER_AUTH"] - env = { - REGISTRY_USER = "username" - } + uses = "docker://docker" + runs = ["sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"] + secrets = ["DOCKER_AUTH"] + env = { + REGISTRY_USER = "username" + } } action "unit-tests" { - uses = "./scripts/github_actions" - runs = "yarn test:ci-unittest || echo \"Unit tests failed, but running danger to present the results!\" 2>&1" - } + uses = "./scripts/github_actions" + runs = "yarn test:ci-unittest || echo \"Unit tests failed, but running danger to present the results!\" 2>&1" + } + + action "regex-in-args" { + uses = "actions/bin/filter@master" + args = "tag v?[0-9]+\\.[0-9]+\\.[0-9]+" + } + + action "regex-in-args-array" { + uses = "actions/bin/filter@master" + args = ["tag","v?[0-9]+\\.[0-9]+\\.[0-9]+"] + } ` workflows, err := parseWorkflowsFile(strings.NewReader(conf)) @@ -101,6 +111,20 @@ func TestParseWorkflowsFile(t *testing.T) { nil, nil, }, + {"regex-in-args", + "actions/bin/filter@master", + nil, + nil, + []string{"tag", `v?[0-9]+\.[0-9]+\.[0-9]+`}, + nil, + }, + {"regex-in-args-array", + "actions/bin/filter@master", + nil, + nil, + []string{"tag", `v?[0-9]+\.[0-9]+\.[0-9]+`}, + nil, + }, } for _, exp := range actions { diff --git a/actions/runner_exec.go b/actions/runner_exec.go index b6be9a3..14a00d0 100644 --- a/actions/runner_exec.go +++ b/actions/runner_exec.go @@ -128,11 +128,11 @@ func (runner *runnerImpl) applyEnvironment(env map[string]string) { env["GITHUB_REPOSITORY"] = repo } - branch, err := common.FindGitBranch(repoPath) + ref, err := common.FindGitRef(repoPath) if err != nil { - log.Warningf("unable to get git branch: %v", err) + log.Warningf("unable to get git ref: %v", err) } else { - env["GITHUB_REF"] = fmt.Sprintf("refs/heads/%s", branch) + env["GITHUB_REF"] = ref } } diff --git a/common/git.go b/common/git.go index 0335220..4d2995b 100644 --- a/common/git.go +++ b/common/git.go @@ -30,20 +30,20 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) { return "", "", err } - head, err := findGitHead(file) + ref, err := FindGitRef(file) if err != nil { return "", "", err } var refBuf []byte - if strings.HasPrefix(head, "refs/") { + if strings.HasPrefix(ref, "refs/") { // load commitid ref - refBuf, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", gitDir, head)) + refBuf, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", gitDir, ref)) if err != nil { return "", "", err } } else { - refBuf = []byte(head) + refBuf = []byte(ref) } log.Debugf("Found revision: %s", refBuf) @@ -52,18 +52,19 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) { // FindGitBranch get the current git branch func FindGitBranch(file string) (string, error) { - head, err := findGitHead(file) + ref, err := FindGitRef(file) if err != nil { return "", err } // get branch name - branch := strings.TrimPrefix(head, "refs/heads/") + branch := strings.TrimPrefix(ref, "refs/heads/") log.Debugf("Found branch: %s", branch) return branch, nil } -func findGitHead(file string) (string, error) { +// FindGitRef get the current git ref +func FindGitRef(file string) (string, error) { gitDir, err := findGitDirectory(file) if err != nil { return "", err