Skip to content

Commit

Permalink
Merge pull request #77 from timonwong/api-templates
Browse files Browse the repository at this point in the history
Refactor: Prefer config global template in UI
  • Loading branch information
timonwong authored Dec 11, 2019
2 parents be3f745 + 7f057af commit 5992f9f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,21 @@ 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("<error creating config string: %s>", err)
}
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"`
Expand Down
25 changes: 10 additions & 15 deletions notifier/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 != "" {
Expand Down
9 changes: 5 additions & 4 deletions web/apiv1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,20 @@ func (api *API) serveTemplates(r *http.Request) apiFuncResult {
}

conf := api.config()
defaultMessage := conf.GetDefaultMessage()
templates := []templateInfo{
{
Target: "<default>",
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{
Expand Down
2 changes: 1 addition & 1 deletion web/dingtalk/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5992f9f

Please sign in to comment.