diff --git a/act/model/workflow.go b/act/model/workflow.go index 6d068e95..54c33983 100644 --- a/act/model/workflow.go +++ b/act/model/workflow.go @@ -21,6 +21,8 @@ type Workflow struct { Env map[string]string `yaml:"env"` Jobs map[string]*Job `yaml:"jobs"` Defaults Defaults `yaml:"defaults"` + + RawNotifications yaml.Node `yaml:"notifications"` } // On events for the workflow @@ -761,3 +763,19 @@ func decodeNode(node yaml.Node, out interface{}) bool { } return true } + +func (w *Workflow) Notifications() (bool, error) { + switch w.RawNotifications.Kind { + case 0: + return false, nil + case yaml.ScalarNode: + var val bool + err := w.RawNotifications.Decode(&val) + if err != nil { + return false, fmt.Errorf("notifications: failed to decode: %v", w.RawNotifications.Kind) + } + return val, nil + default: + return false, fmt.Errorf("notifications: unknown type: %v", w.RawNotifications.Kind) + } +} diff --git a/act/model/workflow_test.go b/act/model/workflow_test.go index ffc3ca2c..45cce2ea 100644 --- a/act/model/workflow_test.go +++ b/act/model/workflow_test.go @@ -1,6 +1,7 @@ package model import ( + "fmt" "strings" "testing" @@ -108,6 +109,64 @@ jobs: assert.Contains(t, workflow.On(), "push") } +func TestReadWorkflow_Notifications(t *testing.T) { + for _, testCase := range []struct { + expected bool + hasErr bool + snippet string + }{ + { + expected: false, + hasErr: false, + snippet: "# nothing", + }, + { + expected: true, + hasErr: false, + snippet: "notifications: true", + }, + { + expected: false, + hasErr: false, + snippet: "notifications: false", + }, + { + hasErr: true, + snippet: "notifications: invalid", + }, + { + hasErr: true, + snippet: "notifications: [1,2]", + }, + } { + t.Run(testCase.snippet, func(t *testing.T) { + yaml := fmt.Sprintf(` +name: name-455 +on: push + +%s + +jobs: + valid-JOB-Name-455: + runs-on: docker + steps: + - run: echo hi +`, testCase.snippet) + + workflow, err := ReadWorkflow(strings.NewReader(yaml)) + assert.NoError(t, err, "read workflow should succeed") + + notification, err := workflow.Notifications() + if testCase.hasErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, testCase.expected, notification) + } + }) + } +} + func TestReadWorkflow_ListEvent(t *testing.T) { yaml := ` name: local-action-docker-url