diff --git a/actions/parser.go b/actions/parser.go index f6eb25f6..a3864bf9 100644 --- a/actions/parser.go +++ b/actions/parser.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io/ioutil" + "io" "os" "path/filepath" @@ -31,15 +32,33 @@ func ParseWorkflows(workingDir string, workflowPath string) (Workflows, error) { return nil, err } + workflows, err := parseWorkflowsFile(workflowReader) + if err != nil { + return nil, err + } + workflows.WorkingDir = workingDir + workflows.WorkflowPath = workflowPath + workflows.TempDir, err = ioutil.TempDir("/tmp", "act-") + if err != nil { + return nil, err + } + + // TODO: add validation logic + // - check for circular dependencies + // - check for valid local path refs + // - check for valid dependencies + + return workflows, nil +} +func parseWorkflowsFile(workflowReader io.Reader) (*workflowsFile, error) { + buf := new(bytes.Buffer) - _, err = buf.ReadFrom(workflowReader) + _, err := buf.ReadFrom(workflowReader) if err != nil { log.Error(err) } workflows := new(workflowsFile) - workflows.WorkingDir = workingDir - workflows.WorkflowPath = workflowPath astFile, err := hcl.ParseBytes(buf.Bytes()) if err != nil { @@ -51,16 +70,6 @@ func ParseWorkflows(workingDir string, workflowPath string) (Workflows, error) { return nil, err } - workflows.TempDir, err = ioutil.TempDir("/tmp", "act-") - if err != nil { - return nil, err - } - - // TODO: add validation logic - // - check for circular dependencies - // - check for valid local path refs - // - check for valid dependencies - return workflows, nil } diff --git a/actions/parser_test.go b/actions/parser_test.go new file mode 100644 index 00000000..70d2a962 --- /dev/null +++ b/actions/parser_test.go @@ -0,0 +1,137 @@ +package actions + +import ( + "strings" + "testing" + + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +func TestParseWorkflowsFile(t *testing.T) { + log.SetLevel(log.DebugLevel) + + conf := ` + workflow "build-and-deploy" { + on = "push" + resolves = ["deploy"] + } + + action "build" { + uses = "./action1" + args = "echo 'build'" + } + + action "test" { + uses = "docker://ubuntu:18.04" + runs = "echo 'test'" + needs = ["build"] + } + + action "deploy" { + 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" + } + } + + 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" + } + ` + + workflows, err := parseWorkflowsFile(strings.NewReader(conf)) + + assert.Nil(t, err) + assert.Equal(t, 1, len(workflows.Workflow)) + + w, wName, _ := workflows.getWorkflow("push") + assert.Equal(t, "build-and-deploy", wName) + assert.ElementsMatch(t, []string{"deploy"}, w.Resolves) + + actions := []struct { + name string + uses string + needs []string + runs []string + args []string + secrets []string + }{ + {"build", + "./action1", + nil, + nil, + []string{"echo", "build"}, + nil, + }, + {"test", + "docker://ubuntu:18.04", + []string{"build"}, + []string{"echo", "test"}, + nil, + nil, + }, + {"deploy", + "./action2", + []string{"test"}, + nil, + []string{"echo", "deploy"}, + nil, + }, + {"docker-login", + "docker://docker", + nil, + []string{"sh", "-c", "echo $DOCKER_AUTH | docker login --username $REGISTRY_USER --password-stdin"}, + nil, + []string{"DOCKER_AUTH"}, + }, + {"unit-tests", + "./scripts/github_actions", + nil, + []string{"yarn", "test:ci-unittest", "||", "echo", "Unit tests failed, but running danger to present the results!", "2>&1"}, + nil, + nil, + }, + } + + for _, exp := range actions { + act, _ := workflows.getAction(exp.name) + assert.Equal(t, exp.uses, act.Uses, "[%s] Uses", exp.name) + if exp.needs == nil { + assert.Nil(t, act.Needs, "[%s] Needs", exp.name) + } else { + assert.ElementsMatch(t, exp.needs, act.Needs, "[%s] Needs", exp.name) + } + if exp.runs == nil { + assert.Nil(t, act.Runs, "[%s] Runs", exp.name) + } else { + assert.ElementsMatch(t, exp.runs, act.Runs, "[%s] Runs", exp.name) + } + if exp.args == nil { + assert.Nil(t, act.Args, "[%s] Args", exp.name) + } else { + assert.ElementsMatch(t, exp.args, act.Args, "[%s] Args", exp.name) + } + /* + if exp.env == nil { + assert.Nil(t, act.Env, "[%s] Env", exp.name) + } else { + assert.ElementsMatch(t, exp.env, act.Env, "[%s] Env", exp.name) + } + */ + if exp.secrets == nil { + assert.Nil(t, act.Secrets, "[%s] Secrets", exp.name) + } else { + assert.ElementsMatch(t, exp.secrets, act.Secrets, "[%s] Secrets", exp.name) + } + } +} diff --git a/go.mod b/go.mod index cec9d853..1fefde7f 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/docker/go-units v0.3.3 // indirect github.com/go-ini/ini v1.41.0 github.com/gogo/protobuf v1.2.0 // indirect + github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163 // indirect github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect github.com/gorilla/context v1.1.1 // indirect github.com/gorilla/mux v1.6.2 // indirect @@ -27,6 +28,7 @@ require ( github.com/spf13/cobra v0.0.3 github.com/spf13/pflag v1.0.3 // indirect github.com/stretchr/testify v1.2.2 + golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 // indirect golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 golang.org/x/net v0.0.0-20181220203305-927f97764cc3 // indirect golang.org/x/sys v0.0.0-20181030150119-7e31e0c00fa0 // indirect diff --git a/go.sum b/go.sum index 43c0366d..0fa150cb 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163 h1:beB+Da4k9B1zmgag78k3k1Bx4L/fdWr5FwNa0f8RxmY= +github.com/google/pprof v0.0.0-20190109223431-e84dfd68c163/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8= @@ -102,6 +104,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro= github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnWhgSqHD8= +golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045 h1:Pn8fQdvx+z1avAi7fdM2kRYWQNxGlavNDSyzrQg2SsU= +golang.org/x/arch v0.0.0-20181203225421-5a4828bb7045/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=