Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/slack-bot-api
Browse files Browse the repository at this point in the history
# Conflicts:
#	pkg/services/slack/slack_config.go
#	pkg/services/slack/slack_json.go
  • Loading branch information
piksel committed Jul 18, 2021
2 parents 362e413 + 5d12d75 commit 1e9e148
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
17 changes: 10 additions & 7 deletions pkg/services/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ type Service struct {

// Send a notification message to log
func (service *Service) Send(message string, params *types.Params) error {
if params == nil {
params = &types.Params{}
data := types.Params{}
if params != nil {
for key, value := range *params {
data[key] = value
}
}
(*params)["message"] = message
return service.doSend(params)
data["message"] = message
return service.doSend(data)
}

func (service *Service) doSend(params *types.Params) error {
msg := (*params)["message"]
func (service *Service) doSend(data types.Params) error {
msg := data["message"]
if tpl, found := service.GetTemplate("message"); found {
wc := &strings.Builder{}
if err := tpl.Execute(wc, params); err != nil {
if err := tpl.Execute(wc, data); err != nil {
return fmt.Errorf("failed to write template to log: %s", err)
}
msg = wc.String()
Expand Down
65 changes: 65 additions & 0 deletions pkg/services/logger/logger_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package logger_test

import (
unit "github.com/containrrr/shoutrrr/pkg/services/logger"
"github.com/containrrr/shoutrrr/pkg/types"

"github.com/containrrr/shoutrrr/pkg/util"
"github.com/onsi/gomega/gbytes"

"log"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestLogger(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Logger Suite")
}

var _ = Describe("the logger service", func() {

When("sending a notification", func() {

It("should output the message to the log", func() {
logbuf := gbytes.NewBuffer()
service := &unit.Service{}
_ = service.Initialize(util.URLMust(`logger://`), log.New(logbuf, "", 0))

err := service.Send(`Failed - Requires Toaster Repair Level 10`, nil)
Expect(err).NotTo(HaveOccurred())

Eventually(logbuf).Should(gbytes.Say("Failed - Requires Toaster Repair Level 10"))
})

It("should not mutate the passed params", func() {
service := &unit.Service{}
_ = service.Initialize(util.URLMust(`logger://`), nil)
params := types.Params{}
err := service.Send(`Failed - Requires Toaster Repair Level 10`, &params)
Expect(err).NotTo(HaveOccurred())

Expect(params).To(BeEmpty())
})

When("when a template has been added", func() {
It("should render template with params", func() {
logbuf := gbytes.NewBuffer()
service := &unit.Service{}
_ = service.Initialize(util.URLMust(`logger://`), log.New(logbuf, "", 0))
err := service.SetTemplateString(`message`, `{{.level}}: {{.message}}`)
Expect(err).NotTo(HaveOccurred())

params := types.Params{
"level": "warning",
}
err = service.Send(`Requires Toaster Repair Level 10`, &params)
Expect(err).NotTo(HaveOccurred())

Eventually(logbuf).Should(gbytes.Say("warning: Requires Toaster Repair Level 10"))
})
})
})
})
4 changes: 3 additions & 1 deletion pkg/services/slack/slack_config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package slack

import (
"net/url"

"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
"net/url"
)

// Config for the slack service
Expand All @@ -16,6 +17,7 @@ type Config struct {
Color string `key:"color" optional:"default border color" desc:"Message left-hand border color"`
Title string `key:"title" optional:"omitted" desc:"Prepended text above the message"`
Channel string `url:"host" desc:"Channel to send messages to in Cxxxxxxxxxx format"`
ThreadTS string `key:"thread_ts" optional:"" desc:"ts value of the parent message (to send message as reply in thread)"`
}

// GetURL returns a URL representation of it's current field values
Expand Down
2 changes: 2 additions & 0 deletions pkg/services/slack/slack_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type MessagePayload struct {
BotName string `json:"username,omitempty"`
Blocks []block `json:"blocks,omitempty"`
Attachments []attachment `json:"attachments,omitempty"`
ThreadTS string `json:"thread_ts,omitempty"`
Channel string `json:"channel,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Expand Down Expand Up @@ -78,6 +79,7 @@ func CreateJSONPayload(config *Config, message string) interface{} {
}

payload := MessagePayload{
ThreadTS: config.ThreadTS,
Text: config.Title,
BotName: config.BotName,
Attachments: atts,
Expand Down

0 comments on commit 1e9e148

Please sign in to comment.