64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"fmt"
|
|
"pipelines/internal/protocol"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func parseNotify(text string) (*protocol.NotificationPolicy, error) {
|
|
values := map[string]string{}
|
|
for _, line := range strings.Split(text, "\n") {
|
|
if strings.TrimSpace(line) == "" {
|
|
continue
|
|
}
|
|
key, value, ok := strings.Cut(line, "=")
|
|
key = strings.TrimSpace(key)
|
|
value = strings.TrimSpace(value)
|
|
if !ok || value == "" {
|
|
return nil, fmt.Errorf("invalid notify setting %q", line)
|
|
}
|
|
if _, exists := values[key]; exists {
|
|
return nil, fmt.Errorf("duplicate notify setting %s", key)
|
|
}
|
|
switch key {
|
|
case "on", "destination", "artifacts":
|
|
default:
|
|
return nil, fmt.Errorf("unknown notify setting %s", key)
|
|
}
|
|
values[key] = value
|
|
}
|
|
// Accept legacy destination syntax without rewriting immutable sources.
|
|
// All stage notification hooks now use the server's global connection.
|
|
if destination := values["destination"]; destination != "" && !regexp.MustCompile(`^[a-z][a-z0-9_]{0,63}$`).MatchString(destination) {
|
|
return nil, fmt.Errorf("invalid legacy notify destination")
|
|
}
|
|
p := &protocol.NotificationPolicy{Destination: "system"}
|
|
parse := func(text string, allowed map[string]bool) ([]string, error) {
|
|
var result []string
|
|
seen := map[string]bool{}
|
|
for _, item := range strings.Split(text, ",") {
|
|
item = strings.TrimSpace(item)
|
|
if !allowed[item] || seen[item] {
|
|
return nil, fmt.Errorf("invalid or duplicate notify value %q", item)
|
|
}
|
|
seen[item] = true
|
|
result = append(result, item)
|
|
}
|
|
return result, nil
|
|
}
|
|
var err error
|
|
p.On, err = parse(values["on"], map[string]bool{"success": true, "failure": true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if values["artifacts"] != "" {
|
|
p.Artifacts, err = parse(values["artifacts"], map[string]bool{"markdown": true, "files": true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return p, nil
|
|
}
|