fad06fd
package main
import (
"fmt"
"github.com/nektos/act/pkg/workflowpattern"
)
func stringList(value interface{}) []string {
values := []string{}
switch value := value.(type) {
case string:
values = append(values, value)
case []interface{}:
for _, v := range value {
values = append(values, fmt.Sprint(v))
}
}
return values
}
func filterMatches(config map[string]interface{}, key string, paths []string) (bool, error) {
include, hasInclude := config[key]
exclude, hasExclude := config[key+"-ignore"]
if hasInclude && hasExclude {
return false, fmt.Errorf("%s and %s-ignore are mutually exclusive", key, key)
}
if hasInclude {
patterns, err := workflowpattern.CompilePatterns(stringList(include)...)
if err != nil {
return false, err
}
return !workflowpattern.Skip(patterns, paths, &workflowpattern.EmptyTraceWriter{}), nil
}
if hasExclude {
patterns, err := workflowpattern.CompilePatterns(stringList(exclude)...)
if err != nil {
return false, err
}
return !workflowpattern.Filter(patterns, paths, &workflowpattern.EmptyTraceWriter{}), nil
}
return true, nil
}
func triggerMatches(req request, raw map[string]interface{}) (bool, error) {
event, exists := object(raw["on"])[req.TriggerEvent]
if !exists {
return false, nil
}
config := object(event)
if types := stringList(config["types"]); len(types) > 0 {
found := false
for _, kind := range types {
if kind == req.Action {
found = true
}
}
if !found {
return false, nil
}
}
if req.TriggerEvent == "pull_request" && config["types"] == nil && req.Action != "opened" && req.Action != "synchronize" && req.Action != "reopened" {
return false, nil
}
if req.TriggerEvent == "workflow_run" {
names := stringList(config["workflows"])
if len(names) > 0 {
found := false
for _, name := range names {
if name == req.Expression {
found = true
}
}
if !found {
return false, nil
}
}
}
if req.TriggerEvent == "push" && req.Tag != "" {
_, branches := config["branches"]
_, branchesIgnore := config["branches-ignore"]
_, tags := config["tags"]
_, tagsIgnore := config["tags-ignore"]
if (branches || branchesIgnore) && !(tags || tagsIgnore) {
return false, nil
}
return filterMatches(config, "tags", []string{req.Tag})
}
if req.TriggerEvent == "push" || req.TriggerEvent == "pull_request" || req.TriggerEvent == "workflow_run" {
if req.TriggerEvent == "push" {
_, tags := config["tags"]
_, tagsIgnore := config["tags-ignore"]
_, branches := config["branches"]
_, branchesIgnore := config["branches-ignore"]
if (tags || tagsIgnore) && !(branches || branchesIgnore) {
return false, nil
}
}
matched, err := filterMatches(config, "branches", []string{req.Branch})
if err != nil || !matched {
return matched, err
}
}
if req.TriggerEvent == "push" || req.TriggerEvent == "pull_request" {
if config["paths"] != nil || config["paths-ignore"] != nil {
if len(req.Paths) == 0 {
return false, nil
}
return filterMatches(config, "paths", req.Paths)
}
}
return true, nil
}