diff options
author | hackercat <me@hackerc.at> | 2021-04-02 16:01:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-02 16:01:45 +0200 |
commit | 94d736a602dcfc7db2568d41a16e222bf1bcc416 (patch) | |
tree | 370e51daeea28e0cf79cafe436eadaac49c12cbb | |
parent | fix: remove HOME env var (#594) (diff) | |
download | forgejo-act-94d736a602dcfc7db2568d41a16e222bf1bcc416.tar.xz forgejo-act-94d736a602dcfc7db2568d41a16e222bf1bcc416.zip |
fix: fail workflow if the job name is invalid (#596)
-rw-r--r-- | pkg/model/planner.go | 8 | ||||
-rw-r--r-- | pkg/model/planner_test.go | 36 | ||||
-rw-r--r-- | pkg/model/testdata/empty-workflow/push.yml | 0 | ||||
-rw-r--r-- | pkg/model/testdata/invalid-job-name/push.yml | 12 |
4 files changed, 56 insertions, 0 deletions
diff --git a/pkg/model/planner.go b/pkg/model/planner.go index 09034a3..c80cb32 100644 --- a/pkg/model/planner.go +++ b/pkg/model/planner.go @@ -1,11 +1,13 @@ package model import ( + "fmt" "io" "io/ioutil" "math" "os" "path/filepath" + "regexp" "sort" "github.com/pkg/errors" @@ -92,6 +94,12 @@ func NewWorkflowPlanner(path string) (WorkflowPlanner, error) { if workflow.Name == "" { workflow.Name = file.Name() } + jobNameRegex := regexp.MustCompile(`^([[:alpha:]_][[:alnum:]_\-]*)$`) + for k := range workflow.Jobs { + if ok := jobNameRegex.MatchString(k); !ok { + return nil, fmt.Errorf("The workflow is not valid. %s: Job name %s is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'", workflow.Name, k) + } + } wp.workflows = append(wp.workflows, workflow) f.Close() } diff --git a/pkg/model/planner_test.go b/pkg/model/planner_test.go new file mode 100644 index 0000000..14765be --- /dev/null +++ b/pkg/model/planner_test.go @@ -0,0 +1,36 @@ +package model + +import ( + "path/filepath" + "testing" + + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +type TestJobFileInfo struct { + workflowPath string + errorMessage string +} + +func TestPlanner(t *testing.T) { + tables := []TestJobFileInfo{ + {"invalid-job-name", "The workflow is not valid. invalid-job-name: Job name invalid-JOB-Name-v1.2.3-docker_hub is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'"}, + {"empty-workflow", "unable to read workflow, push.yml file is empty: EOF"}, + + {"", ""}, // match whole directory + } + log.SetLevel(log.DebugLevel) + + workdir, err := filepath.Abs("testdata") + assert.NoError(t, err, workdir) + for _, table := range tables { + fullWorkflowPath := filepath.Join(workdir, table.workflowPath) + _, err = NewWorkflowPlanner(fullWorkflowPath) + if table.errorMessage == "" { + assert.NoError(t, err, "WorkflowPlanner should exit without any error") + } else { + assert.EqualError(t, err, table.errorMessage) + } + } +} diff --git a/pkg/model/testdata/empty-workflow/push.yml b/pkg/model/testdata/empty-workflow/push.yml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pkg/model/testdata/empty-workflow/push.yml diff --git a/pkg/model/testdata/invalid-job-name/push.yml b/pkg/model/testdata/invalid-job-name/push.yml new file mode 100644 index 0000000..cc3951b --- /dev/null +++ b/pkg/model/testdata/invalid-job-name/push.yml @@ -0,0 +1,12 @@ +name: invalid-job-name +on: push + +jobs: + invalid-JOB-Name-v1.2.3-docker_hub: + runs-on: ubuntu-latest + steps: + - run: echo hi + valid-JOB-Name-v123-docker_hub: + runs-on: ubuntu-latest + steps: + - run: echo hi |