Throw an error if the steps has a invalid uses directive (#500)
* Throw an error if the steps has a invalid uses directive * Refactor TestStepContextExecutor
This commit is contained in:
parent
e37b42a333
commit
2d1a946fb1
6 changed files with 73 additions and 17 deletions
|
@ -32,6 +32,12 @@ func (sc *StepContext) execJobContainer() common.Executor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type formatError string
|
||||||
|
|
||||||
|
func (e formatError) Error() string {
|
||||||
|
return fmt.Sprintf("Expected format {org}/{repo}[/path]@ref. Actual '%s' Input string was not in a correct format.", string(e))
|
||||||
|
}
|
||||||
|
|
||||||
// Executor for a step context
|
// Executor for a step context
|
||||||
func (sc *StepContext) Executor() common.Executor {
|
func (sc *StepContext) Executor() common.Executor {
|
||||||
rc := sc.RunContext
|
rc := sc.RunContext
|
||||||
|
@ -57,14 +63,8 @@ func (sc *StepContext) Executor() common.Executor {
|
||||||
)
|
)
|
||||||
case model.StepTypeUsesActionRemote:
|
case model.StepTypeUsesActionRemote:
|
||||||
remoteAction := newRemoteAction(step.Uses)
|
remoteAction := newRemoteAction(step.Uses)
|
||||||
if remoteAction.Ref == "" {
|
if remoteAction == nil {
|
||||||
// GitHub's document[^] describes:
|
return common.NewErrorExecutor(formatError(step.Uses))
|
||||||
// > We strongly recommend that you include the version of
|
|
||||||
// > the action you are using by specifying a Git ref, SHA, or Docker tag number.
|
|
||||||
// Actually, the workflow stops if there is the uses directive that hasn't @ref.
|
|
||||||
// [^]: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
|
|
||||||
msg := fmt.Sprintf("Expected format {org}/{repo}[/path]@ref. Actual '%s' Input string was not in a correct format.", step.Uses)
|
|
||||||
return common.NewErrorExecutor(fmt.Errorf("%s", msg))
|
|
||||||
}
|
}
|
||||||
if remoteAction.IsCheckout() && rc.getGithubContext().isLocalCheckout(step) {
|
if remoteAction.IsCheckout() && rc.getGithubContext().isLocalCheckout(step) {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
|
@ -401,19 +401,22 @@ func (ra *remoteAction) IsCheckout() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRemoteAction(action string) *remoteAction {
|
func newRemoteAction(action string) *remoteAction {
|
||||||
|
// GitHub's document[^] describes:
|
||||||
|
// > We strongly recommend that you include the version of
|
||||||
|
// > the action you are using by specifying a Git ref, SHA, or Docker tag number.
|
||||||
|
// Actually, the workflow stops if there is the uses directive that hasn't @ref.
|
||||||
|
// [^]: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
|
||||||
r := regexp.MustCompile(`^([^/@]+)/([^/@]+)(/([^@]*))?(@(.*))?$`)
|
r := regexp.MustCompile(`^([^/@]+)/([^/@]+)(/([^@]*))?(@(.*))?$`)
|
||||||
matches := r.FindStringSubmatch(action)
|
matches := r.FindStringSubmatch(action)
|
||||||
|
if len(matches) < 7 || matches[6] == "" {
|
||||||
ra := new(remoteAction)
|
return nil
|
||||||
ra.Org = matches[1]
|
|
||||||
ra.Repo = matches[2]
|
|
||||||
if len(matches) >= 5 {
|
|
||||||
ra.Path = matches[4]
|
|
||||||
}
|
}
|
||||||
if len(matches) >= 7 {
|
return &remoteAction{
|
||||||
ra.Ref = matches[6]
|
Org: matches[1],
|
||||||
|
Repo: matches[2],
|
||||||
|
Path: matches[4],
|
||||||
|
Ref: matches[6],
|
||||||
}
|
}
|
||||||
return ra
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/nektos/act/issues/228#issuecomment-629709055
|
// https://github.com/nektos/act/issues/228#issuecomment-629709055
|
||||||
|
|
25
pkg/runner/step_context_test.go
Normal file
25
pkg/runner/step_context_test.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package runner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nektos/act/pkg/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStepContextExecutor(t *testing.T) {
|
||||||
|
platforms := map[string]string{
|
||||||
|
"ubuntu-latest": "node:12.6-buster-slim",
|
||||||
|
}
|
||||||
|
tables := []TestJobFileInfo{
|
||||||
|
{"testdata", "uses-github-empty", "push", "Expected format {org}/{repo}[/path]@ref", platforms},
|
||||||
|
{"testdata", "uses-github-noref", "push", "Expected format {org}/{repo}[/path]@ref", platforms},
|
||||||
|
{"testdata", "uses-github-root", "push", "", platforms},
|
||||||
|
{"testdata", "uses-github-path", "push", "", platforms},
|
||||||
|
}
|
||||||
|
// These tests are sufficient to only check syntax.
|
||||||
|
ctx := common.WithDryrun(context.Background(), true)
|
||||||
|
for _, table := range tables {
|
||||||
|
runTestJobFile(ctx, t, table)
|
||||||
|
}
|
||||||
|
}
|
7
pkg/runner/testdata/uses-github-empty/push.yml
vendored
Normal file
7
pkg/runner/testdata/uses-github-empty/push.yml
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
name: uses-github-empty
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: empty
|
7
pkg/runner/testdata/uses-github-noref/push.yml
vendored
Normal file
7
pkg/runner/testdata/uses-github-noref/push.yml
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
name: uses-github-noref
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout
|
7
pkg/runner/testdata/uses-github-path/push.yml
vendored
Normal file
7
pkg/runner/testdata/uses-github-path/push.yml
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
name: uses-github-path
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: sergioramos/yarn-actions/install@v6
|
7
pkg/runner/testdata/uses-github-root/push.yml
vendored
Normal file
7
pkg/runner/testdata/uses-github-root/push.yml
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
name: uses-github-root
|
||||||
|
on: push
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
Loading…
Add table
Reference in a new issue