diff --git a/Makefile b/Makefile index 12318d24..14f1a7d9 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ DOCKER_ARCHS ?= amd64 armv7 arm64 REACT_APP_PATH = web/ui/react-app -REACT_APP_SOURCE_FILES = $(wildcard $(REACT_APP_PATH)/public/* $(REACT_APP_PATH)/src/* $(REACT_APP_PATH)/tsconfig.json) +REACT_APP_SOURCE_FILES = $(wildcard $(REACT_APP_PATH)/public/* $(REACT_APP_PATH)/src/* $(REACT_APP_PATH)/tsconfig.json $(shell find $(REACT_APP_PATH)/src -type f -print)) REACT_APP_OUTPUT_DIR = web/ui/static/react REACT_APP_NODE_MODULES_PATH = $(REACT_APP_PATH)/node_modules diff --git a/config/config.go b/config/config.go index 2d2d51dd..9fd8cc2f 100644 --- a/config/config.go +++ b/config/config.go @@ -72,7 +72,7 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } -func (c Config) String() string { +func (c *Config) String() string { b, err := yaml.Marshal(c) if err != nil { return fmt.Sprintf("", err) @@ -80,6 +80,13 @@ func (c Config) String() string { return string(b) } +func (c *Config) GetDefaultMessage() TargetMessage { + if c.DefaultMessage != nil { + return *c.DefaultMessage + } + return DefaultTargetMessage +} + type Target struct { URL *SecretURL `yaml:"url,omitempty"` Secret Secret `yaml:"secret,omitempty"` diff --git a/notifier/notification.go b/notifier/notification.go index 2bfa2624..5bb470bd 100644 --- a/notifier/notification.go +++ b/notifier/notification.go @@ -27,23 +27,18 @@ type DingNotificationBuilder struct { } func NewDingNotificationBuilder(tmpl *template.Template, conf *config.Config, target *config.Target) *DingNotificationBuilder { + // Message template from the following order: + // target level > config global level > builtin global level + var ( - titleTpl = config.DefaultTargetMessage.Title - textTpl = config.DefaultTargetMessage.Text + defaultMessage = conf.GetDefaultMessage() + titleTpl = defaultMessage.Title + textTpl = defaultMessage.Text ) - // Message template from the following order: - // target level > config global level > builtin global level - var candidates = []*config.TargetMessage{ - target.Message, - conf.DefaultMessage, - } - for _, candidate := range candidates { - if candidate != nil { - titleTpl = candidate.Title - textTpl = candidate.Text - break - } + if target.Message != nil { + titleTpl = target.Message.Title + textTpl = target.Message.Text } return &DingNotificationBuilder{ @@ -91,7 +86,7 @@ func (r *DingNotificationBuilder) Build(m *models.WebhookMessage) (*models.DingT return notification, nil } -func SendNotification(httpClient *http.Client, target *config.Target, notification *models.DingTalkNotification) (*models.DingTalkNotificationResponse, error) { +func SendNotification(notification *models.DingTalkNotification, httpClient *http.Client, target *config.Target) (*models.DingTalkNotificationResponse, error) { targetURL := *target.URL // Calculate signature when secret is provided if target.Secret != "" { diff --git a/web/apiv1/api.go b/web/apiv1/api.go index a2557a17..b140256c 100644 --- a/web/apiv1/api.go +++ b/web/apiv1/api.go @@ -133,19 +133,20 @@ func (api *API) serveTemplates(r *http.Request) apiFuncResult { } conf := api.config() + defaultMessage := conf.GetDefaultMessage() templates := []templateInfo{ { Target: "", - Title: config.DefaultTargetMessage.Title, - Text: config.DefaultTargetMessage.Text, + Title: defaultMessage.Title, + Text: defaultMessage.Text, }, } for name, target := range conf.Targets { if target.Message == nil { templates = append(templates, templateInfo{ Target: name, - Title: config.DefaultTargetMessage.Title, - Text: config.DefaultTargetMessage.Text, + Title: defaultMessage.Title, + Text: defaultMessage.Text, }) } else { templates = append(templates, templateInfo{ diff --git a/web/dingtalk/dingtalk.go b/web/dingtalk/dingtalk.go index 3c15e476..9c8281eb 100644 --- a/web/dingtalk/dingtalk.go +++ b/web/dingtalk/dingtalk.go @@ -92,7 +92,7 @@ func (api *API) serveSend(w http.ResponseWriter, r *http.Request) { return } - robotResp, err := notifier.SendNotification(httpClient, &target, notification) + robotResp, err := notifier.SendNotification(notification, httpClient, &target) if err != nil { level.Error(logger).Log("msg", "Failed to send notification", "err", err) http.Error(w, "Bad Request", http.StatusBadRequest)