fix: panic on regex in main.workflow

fixes #12
This commit is contained in:
Casey Lee 2019-01-18 13:28:53 -08:00
parent 36303ce43d
commit a04141c444
No known key found for this signature in database
GPG key ID: 4CC378651BF9C168
4 changed files with 56 additions and 30 deletions

View file

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"strings"
"github.com/hashicorp/hcl" "github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast" "github.com/hashicorp/hcl/hcl/ast"
@ -54,7 +55,7 @@ func cleanWorkflowsAST(node ast.Node) (ast.Node, bool) {
} }
quote := literalType.Token.Text[0] quote := literalType.Token.Text[0]
for _, part := range parts { 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{ listType.Add(&ast.LiteralType{
Token: token.Token{ Token: token.Token{
Type: token.STRING, Type: token.STRING,

View file

@ -13,40 +13,50 @@ func TestParseWorkflowsFile(t *testing.T) {
conf := ` conf := `
workflow "build-and-deploy" { workflow "build-and-deploy" {
on = "push" on = "push"
resolves = ["deploy"] resolves = ["deploy"]
} }
action "build" { action "build" {
uses = "./action1" uses = "./action1"
args = "echo 'build'" args = "echo 'build'"
} }
action "test" { action "test" {
uses = "docker://ubuntu:18.04" uses = "docker://ubuntu:18.04"
runs = "echo 'test'" runs = "echo 'test'"
needs = ["build"] needs = ["build"]
} }
action "deploy" { action "deploy" {
uses = "./action2" uses = "./action2"
args = ["echo","deploy"] args = ["echo","deploy"]
needs = ["test"] needs = ["test"]
} }
action "docker-login" { action "docker-login" {
uses = "docker://docker" uses = "docker://docker"
runs = ["sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"] runs = ["sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"]
secrets = ["DOCKER_AUTH"] secrets = ["DOCKER_AUTH"]
env = { env = {
REGISTRY_USER = "username" REGISTRY_USER = "username"
} }
} }
action "unit-tests" { action "unit-tests" {
uses = "./scripts/github_actions" uses = "./scripts/github_actions"
runs = "yarn test:ci-unittest || echo \"Unit tests failed, but running danger to present the results!\" 2>&1" 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)) workflows, err := parseWorkflowsFile(strings.NewReader(conf))
@ -101,6 +111,20 @@ func TestParseWorkflowsFile(t *testing.T) {
nil, nil,
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 { for _, exp := range actions {

View file

@ -128,11 +128,11 @@ func (runner *runnerImpl) applyEnvironment(env map[string]string) {
env["GITHUB_REPOSITORY"] = repo env["GITHUB_REPOSITORY"] = repo
} }
branch, err := common.FindGitBranch(repoPath) ref, err := common.FindGitRef(repoPath)
if err != nil { if err != nil {
log.Warningf("unable to get git branch: %v", err) log.Warningf("unable to get git ref: %v", err)
} else { } else {
env["GITHUB_REF"] = fmt.Sprintf("refs/heads/%s", branch) env["GITHUB_REF"] = ref
} }
} }

View file

@ -30,20 +30,20 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) {
return "", "", err return "", "", err
} }
head, err := findGitHead(file) ref, err := FindGitRef(file)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
var refBuf []byte var refBuf []byte
if strings.HasPrefix(head, "refs/") { if strings.HasPrefix(ref, "refs/") {
// load commitid ref // 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 { if err != nil {
return "", "", err return "", "", err
} }
} else { } else {
refBuf = []byte(head) refBuf = []byte(ref)
} }
log.Debugf("Found revision: %s", refBuf) 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 // FindGitBranch get the current git branch
func FindGitBranch(file string) (string, error) { func FindGitBranch(file string) (string, error) {
head, err := findGitHead(file) ref, err := FindGitRef(file)
if err != nil { if err != nil {
return "", err return "", err
} }
// get branch name // get branch name
branch := strings.TrimPrefix(head, "refs/heads/") branch := strings.TrimPrefix(ref, "refs/heads/")
log.Debugf("Found branch: %s", branch) log.Debugf("Found branch: %s", branch)
return branch, nil 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) gitDir, err := findGitDirectory(file)
if err != nil { if err != nil {
return "", err return "", err