From 25779af4bf1e7762d8ff2d3be985c2fcde7bb1e2 Mon Sep 17 00:00:00 2001 From: hackercat Date: Thu, 1 Apr 2021 20:36:41 +0200 Subject: [PATCH] fix: return error if both `run:`/`uses:` keys are used (#593) fixes https://github.com/nektos/act/issues/536 --- pkg/model/workflow.go | 6 ++++ pkg/model/workflow_test.go | 36 +++++++++++++++++-- pkg/runner/step_context.go | 2 ++ pkg/runner/step_context_test.go | 3 ++ .../uses-and-run-in-one-step/push.yml | 10 ++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 pkg/runner/testdata/uses-and-run-in-one-step/push.yml diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go index cc519b7..255b5e0 100644 --- a/pkg/model/workflow.go +++ b/pkg/model/workflow.go @@ -292,11 +292,17 @@ const ( // StepTypeUsesActionRemote is all steps that have a `uses` that is a reference to a github repo StepTypeUsesActionRemote + + // StepTypeInvalid is for steps that have invalid step action + StepTypeInvalid ) // Type returns the type of the step func (s *Step) Type() StepType { if s.Run != "" { + if s.Uses != "" { + return StepTypeInvalid + } return StepTypeRun } else if strings.HasPrefix(s.Uses, "docker://") { return StepTypeUsesDockerURL diff --git a/pkg/model/workflow_test.go b/pkg/model/workflow_test.go index 92df87d..1a96969 100644 --- a/pkg/model/workflow_test.go +++ b/pkg/model/workflow_test.go @@ -49,7 +49,7 @@ jobs: func TestReadWorkflow_MapEvent(t *testing.T) { yaml := ` name: local-action-docker-url -on: +on: push: branches: - master @@ -82,7 +82,7 @@ jobs: steps: - uses: ./actions/docker-url test2: - container: + container: image: nginx:latest env: foo: bar @@ -98,3 +98,35 @@ jobs: assert.Contains(t, workflow.Jobs["test2"].Container().Image, "nginx:latest") assert.Contains(t, workflow.Jobs["test2"].Container().Env["foo"], "bar") } + +func TestReadWorkflow_StepsTypes(t *testing.T) { + yaml := ` +name: invalid step definition + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: test1 + uses: actions/checkout@v2 + run: echo + - name: test2 + run: echo + - name: test3 + uses: actions/checkout@v2 + - name: test4 + uses: docker://nginx:latest + - name: test5 + uses: ./local-action +` + + workflow, err := ReadWorkflow(strings.NewReader(yaml)) + assert.NoError(t, err, "read workflow should succeed") + assert.Len(t, workflow.Jobs, 1) + assert.Len(t, workflow.Jobs["test"].Steps, 5) + assert.Equal(t, workflow.Jobs["test"].Steps[0].Type(), StepTypeInvalid) + assert.Equal(t, workflow.Jobs["test"].Steps[1].Type(), StepTypeRun) + assert.Equal(t, workflow.Jobs["test"].Steps[2].Type(), StepTypeUsesActionRemote) + assert.Equal(t, workflow.Jobs["test"].Steps[3].Type(), StepTypeUsesDockerURL) + assert.Equal(t, workflow.Jobs["test"].Steps[4].Type(), StepTypeUsesActionLocal) +} diff --git a/pkg/runner/step_context.go b/pkg/runner/step_context.go index 7d4ffcf..49d1c75 100644 --- a/pkg/runner/step_context.go +++ b/pkg/runner/step_context.go @@ -87,6 +87,8 @@ func (sc *StepContext) Executor() common.Executor { sc.setupAction(actionDir, remoteAction.Path), sc.runAction(actionDir, remoteAction.Path), ) + case model.StepTypeInvalid: + return common.NewErrorExecutor(fmt.Errorf("Invalid run/uses syntax for job:%s step:%+v", rc.Run, step)) } return common.NewErrorExecutor(fmt.Errorf("Unable to determine how to run job:%s step:%+v", rc.Run, step)) diff --git a/pkg/runner/step_context_test.go b/pkg/runner/step_context_test.go index 84a1b66..920f43e 100644 --- a/pkg/runner/step_context_test.go +++ b/pkg/runner/step_context_test.go @@ -12,10 +12,13 @@ func TestStepContextExecutor(t *testing.T) { "ubuntu-latest": "node:12.20.1-buster-slim", } tables := []TestJobFileInfo{ + {"testdata", "uses-and-run-in-one-step", "push", "Invalid run/uses syntax for job:test step:Test", platforms, "linux/amd64"}, {"testdata", "uses-github-empty", "push", "Expected format {org}/{repo}[/path]@ref", platforms, "linux/amd64"}, {"testdata", "uses-github-noref", "push", "Expected format {org}/{repo}[/path]@ref", platforms, "linux/amd64"}, {"testdata", "uses-github-root", "push", "", platforms, "linux/amd64"}, {"testdata", "uses-github-path", "push", "", platforms, "linux/amd64"}, + + {"testdata", "uses-and-run-in-one-step", "push", "Invalid run/uses syntax for job:test step:Test", platforms, "linux/arm64"}, {"testdata", "uses-github-empty", "push", "Expected format {org}/{repo}[/path]@ref", platforms, "linux/arm64"}, {"testdata", "uses-github-noref", "push", "Expected format {org}/{repo}[/path]@ref", platforms, "linux/arm64"}, {"testdata", "uses-github-root", "push", "", platforms, "linux/arm64"}, diff --git a/pkg/runner/testdata/uses-and-run-in-one-step/push.yml b/pkg/runner/testdata/uses-and-run-in-one-step/push.yml new file mode 100644 index 0000000..bbdf868 --- /dev/null +++ b/pkg/runner/testdata/uses-and-run-in-one-step/push.yml @@ -0,0 +1,10 @@ +name: uses-and-run-in-one-step +on: push + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Test + run: echo + uses: actions/checkout@v2