Skip to content

Commit 94ee93f

Browse files
authored
Merge pull request #21 from chrisgavin/workflow-validation-warnings
Make workflow validation more robust and report invalid workflows as warnings.
2 parents b022ae2 + 509a5d8 commit 94ee93f

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

internal/locator/repository-workflow-locator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/chrisgavin/gh-dispatch/internal/workflow"
1111
"github.com/pkg/errors"
12+
log "github.com/sirupsen/logrus"
1213
)
1314

1415
func ListWorkflowsInRepository() (map[string]workflow.Workflow, error) {
@@ -43,7 +44,8 @@ func ListWorkflowsInRepository() (map[string]workflow.Workflow, error) {
4344
}
4445
loaded, err := workflow.ReadWorkflow(entry.Name(), bytes)
4546
if err != nil {
46-
return nil, errors.Wrap(err, "Unable to read workflow.")
47+
log.Warnf("Workflow \"%s\" is invalid: %s", entry.Name(), err)
48+
continue
4749
}
4850
if !loaded.Dispatchable {
4951
continue

internal/workflow/workflow.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func ReadWorkflow(name string, rawWorkflow []byte) (*Workflow, error) {
2525
parsed := make(map[string]interface{})
2626
err := yaml.Unmarshal(rawWorkflow, &parsed)
2727
if err != nil {
28-
return nil, errors.Wrap(err, "Unable to parse workflow.")
28+
return nil, errors.Wrap(err, "Unable to parse workflow as YAML.")
2929
}
3030
if on, ok := parsed["on"]; ok {
3131
switch typedOn := on.(type) {
@@ -41,28 +41,38 @@ func ReadWorkflow(name string, rawWorkflow []byte) (*Workflow, error) {
4141
for event, eventConfiguration := range typedOn {
4242
if event == workflowDispatch {
4343
workflow.Dispatchable = true
44-
// TODO: How many of the switches can be converted to type assertions and do we need to handle errors?
45-
switch typedEventConfiguration := eventConfiguration.(type) {
46-
case map[interface{}]interface{}:
44+
if eventConfiguration != nil {
45+
typedEventConfiguration, ok := eventConfiguration.(map[interface{}]interface{})
46+
if !ok {
47+
return nil, errors.Errorf("Workflow dispatch configuration had unexpected type %T.", eventConfiguration)
48+
}
4749
if inputs, ok := typedEventConfiguration["inputs"]; ok {
48-
switch typedInputs := inputs.(type) {
49-
case map[interface{}]interface{}:
50-
for inputName, inputConfiguration := range typedInputs {
51-
input := Input{
52-
Name: inputName.(string),
53-
}
54-
if inputDescription, ok := inputConfiguration.(map[interface{}]interface{})["description"]; ok {
55-
input.Description = inputDescription.(string)
50+
typedInputs, ok := inputs.(map[interface{}]interface{})
51+
if !ok {
52+
return nil, errors.Errorf("Workflow dispatch configuration inputs had unexpected type %T.", inputs)
53+
}
54+
for inputName, inputConfiguration := range typedInputs {
55+
typedInputConfiguration, ok := inputConfiguration.(map[interface{}]interface{})
56+
if !ok {
57+
return nil, errors.Errorf("Input configuration for %s had unexpected type %T.", inputName, inputConfiguration)
58+
}
59+
input := Input{
60+
Name: inputName.(string),
61+
}
62+
if inputDescription, ok := typedInputConfiguration["description"]; ok {
63+
input.Description, ok = inputDescription.(string)
64+
if !ok {
65+
return nil, errors.Errorf("Input description for %s had unexpected type %T.", inputName, inputDescription)
5666
}
57-
workflow.Inputs = append(workflow.Inputs, input)
5867
}
68+
workflow.Inputs = append(workflow.Inputs, input)
5969
}
6070
}
6171
}
6272
}
6373
}
6474
default:
65-
return nil, errors.Errorf("Unable to parse workflow \"on\" clause. Unexpected type %T.", on) // TODO: Should we error here?
75+
return nil, errors.Errorf("Unable to parse workflow \"on\" clause. Unexpected type %T.", on)
6676
}
6777
}
6878
return &workflow, nil

0 commit comments

Comments
 (0)