Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】从 ValidateNotificationBinding 中拆出 validateNotificationTemplate 与 validateNotificationEvents (#640) (#643)

fx-kurumif12f59f5725b
diff --git a/internal/domain/notification_template.go b/internal/domain/notification_template.go
index e8447cf9..e5804e76 100644
--- a/internal/domain/notification_template.go
+++ b/internal/domain/notification_template.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"regexp"
+ "slices"
"strings"
"unicode/utf8"
)
@@ -55,25 +56,37 @@ func ValidateNotificationBinding(binding NotificationBinding) error {
if len(binding.AIInstruction) > 4000 {
return fmt.Errorf("AI 通知编写要求不能超过 4000 字节")
}
- names := map[string]bool{}
- for _, name := range NotificationVariableNames {
- names[name] = true
+ if err := validateNotificationTemplate(binding.Template); err != nil {
+ return err
}
- for _, match := range notificationVariable.FindAllStringSubmatch(binding.Template, -1) {
- if !names[match[1]] {
+ return validateNotificationEvents(binding.Events, binding.Scope)
+}
+
+// validateNotificationTemplate accepts only whitelisted {{variable}}
+// placeholders and rejects any stray "{{" or "}}" left after removing them.
+func validateNotificationTemplate(template string) error {
+ for _, match := range notificationVariable.FindAllStringSubmatch(template, -1) {
+ if !slices.Contains(NotificationVariableNames, match[1]) {
return fmt.Errorf("通知模板包含不支持的变量:%s", match[1])
}
}
- rest := notificationVariable.ReplaceAllString(binding.Template, "")
+ rest := notificationVariable.ReplaceAllString(template, "")
if strings.Contains(rest, "{{") || strings.Contains(rest, "}}") {
return fmt.Errorf("通知模板的变量格式无效")
}
- if len(binding.Events) == 0 || len(binding.Events) > 3 {
+ return nil
+}
+
+// validateNotificationEvents requires one to three distinct supported events;
+// run-scoped bindings cannot fire on waiting.
+func validateNotificationEvents(events []string, scope string) error {
+ if len(events) == 0 || len(events) > 3 {
return fmt.Errorf("请选择通知触发条件")
}
seen := map[string]bool{}
- for _, event := range binding.Events {
- if seen[event] || (event != "succeeded" && event != "failed" && event != "waiting") || (binding.Scope == "run" && event == "waiting") {
+ for _, event := range events {
+ supported := event == "succeeded" || event == "failed" || event == "waiting"
+ if seen[event] || !supported || (scope == "run" && event == "waiting") {
return fmt.Errorf("不支持的通知触发条件")
}
seen[event] = true
diff --git a/internal/domain/notification_template_validate_test.go b/internal/domain/notification_template_validate_test.go
new file mode 100644
index 00000000..06bd3fdd
--- /dev/null
+++ b/internal/domain/notification_template_validate_test.go
@@ -0,0 +1,78 @@
+package domain
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestValidateNotificationTemplate(t *testing.T) {
+ for _, tc := range []struct {
+ name, template, want string
+ }{
+ {"empty", "", ""},
+ {"plain text", "流程已结束", ""},
+ {"all supported variables", "{{project}} {{ workflow }} {{node}} {{status}} {{url}} {{issue.title}} {{issue.id}} {{input}} {{result}} {{error}} {{task.name}} {{ai.summary}} {{ai.title}} {{ai.body}}", ""},
+ {"unknown variable", "{{status}} {{secret}}", "通知模板包含不支持的变量:secret"},
+ {"unfinished placeholder", "{{unfinished", "通知模板的变量格式无效"},
+ {"stray closing braces", "done }}", "通知模板的变量格式无效"},
+ {"invalid variable name", "{{ 1abc }}", "通知模板的变量格式无效"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ assertNotificationError(t, validateNotificationTemplate(tc.template), tc.want)
+ })
+ }
+}
+
+func TestValidateNotificationEvents(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ events []string
+ scope string
+ want string
+ }{
+ {"single node event", []string{"succeeded"}, "", ""},
+ {"all node events", []string{"succeeded", "failed", "waiting"}, "node", ""},
+ {"run without waiting", []string{"succeeded", "failed"}, "run", ""},
+ {"empty", nil, "", "请选择通知触发条件"},
+ {"more than three", []string{"succeeded", "failed", "waiting", "succeeded"}, "", "请选择通知触发条件"},
+ {"duplicate", []string{"failed", "failed"}, "", "不支持的通知触发条件"},
+ {"unknown", []string{"started"}, "", "不支持的通知触发条件"},
+ {"run waiting", []string{"succeeded", "waiting"}, "run", "不支持的通知触发条件"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ assertNotificationError(t, validateNotificationEvents(tc.events, tc.scope), tc.want)
+ })
+ }
+}
+
+func TestValidateNotificationBindingCheckOrder(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ binding NotificationBinding
+ want string
+ }{
+ {"valid", NotificationBinding{Events: []string{"succeeded"}, Template: "{{status}}"}, ""},
+ {"scope before template", NotificationBinding{Scope: "bad", Template: "{{secret}}"}, "通知触发范围无效"},
+ {"template length before variables", NotificationBinding{Template: strings.Repeat("{{secret}}", 1201)}, "通知模板不能超过 12000 字节"},
+ {"instruction length before variables", NotificationBinding{AIInstruction: strings.Repeat("x", 4001), Template: "{{secret}}"}, "AI 通知编写要求不能超过 4000 字节"},
+ {"template before events", NotificationBinding{Template: "{{secret}}"}, "通知模板包含不支持的变量:secret"},
+ {"events last", NotificationBinding{Template: "{{status}}", Scope: "run", Events: []string{"waiting"}}, "不支持的通知触发条件"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ assertNotificationError(t, ValidateNotificationBinding(tc.binding), tc.want)
+ })
+ }
+}
+
+func assertNotificationError(t *testing.T, err error, want string) {
+ t.Helper()
+ if want == "" {
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ return
+ }
+ if err == nil || err.Error() != want {
+ t.Fatalf("error = %v, want %q", err, want)
+ }
+}