mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-08-06 17:40:58 +00:00
feat: add notifications control to the workflow (#151)
Reviewed-on: https://code.forgejo.org/forgejo/act/pulls/151 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
parent
5cf8d53087
commit
01032808ea
2 changed files with 77 additions and 0 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue