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"
"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,

View file

@ -47,6 +47,16 @@ func TestParseWorkflowsFile(t *testing.T) {
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 {

View file

@ -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
}
}

View file

@ -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