mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-21 21:30:48 +02:00
Fix notifications and old instance cleanup (#748)
Co-authored-by: Simon Aronsson <simme@arcticbit.se>
This commit is contained in:
parent
06e705d538
commit
40ab6fd5ba
7 changed files with 123 additions and 53 deletions
|
@ -1,9 +1,11 @@
|
|||
package notifications
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/containrrr/shoutrrr/pkg/format"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
shoutrrrSmtp "github.com/containrrr/shoutrrr/pkg/services/smtp"
|
||||
|
@ -75,10 +77,16 @@ func (e *emailTypeNotifier) GetURL() string {
|
|||
UseHTML: false,
|
||||
}
|
||||
|
||||
pkr := format.NewPropKeyResolver(conf)
|
||||
var err error
|
||||
if len(e.User) > 0 {
|
||||
conf.Set("auth", "Plain")
|
||||
err = pkr.Set("auth", "Plain")
|
||||
} else {
|
||||
conf.Set("auth", "None")
|
||||
err = pkr.Set("auth", "None")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Could not set auth type for email notifier: %v", err)
|
||||
}
|
||||
|
||||
return conf.GetURL().String()
|
||||
|
|
|
@ -22,19 +22,32 @@ func TestActions(t *testing.T) {
|
|||
}
|
||||
|
||||
var _ = Describe("notifications", func() {
|
||||
// TODO: Either, we delete this test or we need to pass it valid URLs in the cobra command.
|
||||
// ---
|
||||
// When("getting notifiers from a types array", func() {
|
||||
// It("should return the same amount of notifiers a string entries", func() {
|
||||
|
||||
// notifier := ¬ifications.Notifier{}
|
||||
// notifiers := notifier.GetNotificationTypes(&cobra.Command{}, []log.Level{}, []string{"slack", "email"})
|
||||
// Expect(len(notifiers)).To(Equal(2))
|
||||
// })
|
||||
// })
|
||||
Describe("the slack notifier", func() {
|
||||
builderFn := notifications.NewSlackNotifier
|
||||
|
||||
When("passing a discord url to the slack notifier", func() {
|
||||
channel := "123456789"
|
||||
token := "abvsihdbau"
|
||||
expected := fmt.Sprintf("discord://%s@%s", token, channel)
|
||||
buildArgs := func(url string) []string {
|
||||
return []string{
|
||||
"--notifications",
|
||||
"slack",
|
||||
"--notification-slack-hook-url",
|
||||
url,
|
||||
}
|
||||
}
|
||||
|
||||
It("should return a discord url when using a hook url with the domain discord.com", func() {
|
||||
hookURL := fmt.Sprintf("https://%s/api/webhooks/%s/%s/slack", "discord.com", channel, token)
|
||||
testURL(builderFn, buildArgs(hookURL), expected)
|
||||
})
|
||||
It("should return a discord url when using a hook url with the domain discordapp.com", func() {
|
||||
hookURL := fmt.Sprintf("https://%s/api/webhooks/%s/%s/slack", "discordapp.com", channel, token)
|
||||
testURL(builderFn, buildArgs(hookURL), expected)
|
||||
})
|
||||
})
|
||||
When("converting a slack service config into a shoutrrr url", func() {
|
||||
builderFn := notifications.NewSlackNotifier
|
||||
|
||||
It("should return the expected URL", func() {
|
||||
|
||||
|
@ -43,9 +56,8 @@ var _ = Describe("notifications", func() {
|
|||
tokenB := "bbb"
|
||||
tokenC := "ccc"
|
||||
|
||||
password := fmt.Sprintf("%s-%s-%s", tokenA, tokenB, tokenC)
|
||||
hookURL := fmt.Sprintf("https://hooks.slack.com/services/%s/%s/%s", tokenA, tokenB, tokenC)
|
||||
expectedOutput := fmt.Sprintf("slack://%s:%s@%s/%s/%s", username, password, tokenA, tokenB, tokenC)
|
||||
expectedOutput := fmt.Sprintf("slack://%s@%s/%s/%s", username, tokenA, tokenB, tokenC)
|
||||
|
||||
args := []string{
|
||||
"--notification-slack-hook-url",
|
||||
|
@ -144,8 +156,8 @@ func buildExpectedURL(username string, password string, host string, port int, f
|
|||
|
||||
subject := fmt.Sprintf("Watchtower updates on %s", hostname)
|
||||
|
||||
var template = "smtp://%s:%s@%s:%d/?fromAddress=%s&fromName=Watchtower&toAddresses=%s&auth=%s&subject=%s&startTls=Yes&useHTML=No"
|
||||
return fmt.Sprintf(template, username, password, host, port, from, to, auth, subject)
|
||||
var template = "smtp://%s:%s@%s:%d/?auth=%s&encryption=None&fromaddress=%s&fromname=Watchtower&starttls=Yes&subject=%s&toaddresses=%s&usehtml=No"
|
||||
return fmt.Sprintf(template, username, password, host, port, auth, from, subject, to)
|
||||
}
|
||||
|
||||
type builderFn = func(c *cobra.Command, acceptedLogLevels []log.Level) types.ConvertableNotifier
|
||||
|
|
|
@ -3,6 +3,7 @@ package notifications
|
|||
import (
|
||||
"strings"
|
||||
|
||||
shoutrrrDisco "github.com/containrrr/shoutrrr/pkg/services/discord"
|
||||
shoutrrrSlack "github.com/containrrr/shoutrrr/pkg/services/slack"
|
||||
t "github.com/containrrr/watchtower/pkg/types"
|
||||
"github.com/johntdyer/slackrus"
|
||||
|
@ -31,6 +32,7 @@ func newSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Convert
|
|||
channel, _ := flags.GetString("notification-slack-channel")
|
||||
emoji, _ := flags.GetString("notification-slack-icon-emoji")
|
||||
iconURL, _ := flags.GetString("notification-slack-icon-url")
|
||||
|
||||
n := &slackTypeNotifier{
|
||||
SlackrusHook: slackrus.SlackrusHook{
|
||||
HookURL: hookURL,
|
||||
|
@ -45,16 +47,25 @@ func newSlackNotifier(c *cobra.Command, acceptedLogLevels []log.Level) t.Convert
|
|||
}
|
||||
|
||||
func (s *slackTypeNotifier) GetURL() string {
|
||||
trimmedURL := strings.TrimRight(s.HookURL, "/")
|
||||
trimmedURL = strings.TrimLeft(trimmedURL, "https://")
|
||||
parts := strings.Split(trimmedURL, "/")
|
||||
|
||||
if parts[0] == "discord.com" || parts[0] == "discordapp.com" {
|
||||
log.Debug("Detected a discord slack wrapper URL, using shoutrrr discord service")
|
||||
conf := &shoutrrrDisco.Config{
|
||||
Channel: parts[len(parts)-3],
|
||||
Token: parts[len(parts)-2],
|
||||
}
|
||||
return conf.GetURL().String()
|
||||
}
|
||||
|
||||
rawTokens := strings.Replace(s.HookURL, "https://hooks.slack.com/services/", "", 1)
|
||||
tokens := strings.Split(rawTokens, "/")
|
||||
|
||||
conf := &shoutrrrSlack.Config{
|
||||
BotName: s.Username,
|
||||
Token: shoutrrrSlack.Token{
|
||||
A: tokens[0],
|
||||
B: tokens[1],
|
||||
C: tokens[2],
|
||||
},
|
||||
Token: tokens,
|
||||
}
|
||||
|
||||
return conf.GetURL().String()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue