fix: fail workflow if the job name is invalid (#596)
This commit is contained in:
parent
5044ec6c43
commit
94d736a602
4 changed files with 56 additions and 0 deletions
|
@ -1,11 +1,13 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
@ -92,6 +94,12 @@ func NewWorkflowPlanner(path string) (WorkflowPlanner, error) {
|
||||||
if workflow.Name == "" {
|
if workflow.Name == "" {
|
||||||
workflow.Name = file.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)
|
wp.workflows = append(wp.workflows, workflow)
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
|
|
36
pkg/model/planner_test.go
Normal file
36
pkg/model/planner_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
0
pkg/model/testdata/empty-workflow/push.yml
vendored
Normal file
0
pkg/model/testdata/empty-workflow/push.yml
vendored
Normal file
12
pkg/model/testdata/invalid-job-name/push.yml
vendored
Normal file
12
pkg/model/testdata/invalid-job-name/push.yml
vendored
Normal file
|
@ -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
|
Loading…
Reference in a new issue