Adding upstream version 0.8.9.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
3b2c48b5e4
commit
c0c4addb85
285 changed files with 25880 additions and 0 deletions
142
pkg/services/slack/slack.go
Normal file
142
pkg/services/slack/slack.go
Normal file
|
@ -0,0 +1,142 @@
|
|||
package slack
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/util/jsonclient"
|
||||
)
|
||||
|
||||
// apiPostMessage is the Slack API endpoint for sending messages.
|
||||
const (
|
||||
apiPostMessage = "https://slack.com/api/chat.postMessage"
|
||||
defaultHTTPTimeout = 10 * time.Second // defaultHTTPTimeout is the default timeout for HTTP requests.
|
||||
)
|
||||
|
||||
// Service sends notifications to a pre-configured Slack channel or user.
|
||||
type Service struct {
|
||||
standard.Standard
|
||||
Config *Config
|
||||
pkr format.PropKeyResolver
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// Send delivers a notification message to Slack.
|
||||
func (service *Service) Send(message string, params *types.Params) error {
|
||||
config := service.Config
|
||||
|
||||
if err := service.pkr.UpdateConfigFromParams(config, params); err != nil {
|
||||
return fmt.Errorf("updating config from params: %w", err)
|
||||
}
|
||||
|
||||
payload := CreateJSONPayload(config, message)
|
||||
|
||||
var err error
|
||||
if config.Token.IsAPIToken() {
|
||||
err = service.sendAPI(config, payload)
|
||||
} else {
|
||||
err = service.sendWebhook(config, payload)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send slack notification: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Initialize configures the service with a URL and logger.
|
||||
func (service *Service) Initialize(configURL *url.URL, logger types.StdLogger) error {
|
||||
service.SetLogger(logger)
|
||||
service.Config = &Config{}
|
||||
service.pkr = format.NewPropKeyResolver(service.Config)
|
||||
service.client = &http.Client{
|
||||
Timeout: defaultHTTPTimeout,
|
||||
}
|
||||
|
||||
return service.Config.setURL(&service.pkr, configURL)
|
||||
}
|
||||
|
||||
// GetID returns the service identifier.
|
||||
func (service *Service) GetID() string {
|
||||
return Scheme
|
||||
}
|
||||
|
||||
// sendAPI sends a notification using the Slack API.
|
||||
func (service *Service) sendAPI(config *Config, payload any) error {
|
||||
response := APIResponse{}
|
||||
jsonClient := jsonclient.NewClient()
|
||||
jsonClient.Headers().Set("Authorization", config.Token.Authorization())
|
||||
|
||||
if err := jsonClient.Post(apiPostMessage, payload, &response); err != nil {
|
||||
return fmt.Errorf("posting to Slack API: %w", err)
|
||||
}
|
||||
|
||||
if !response.Ok {
|
||||
if response.Error != "" {
|
||||
return fmt.Errorf("%w: %v", ErrAPIResponseFailure, response.Error)
|
||||
}
|
||||
|
||||
return ErrUnknownAPIError
|
||||
}
|
||||
|
||||
if response.Warning != "" {
|
||||
service.Logf("Slack API warning: %q", response.Warning)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendWebhook sends a notification using a Slack webhook.
|
||||
func (service *Service) sendWebhook(config *Config, payload any) error {
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal payload: %w", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultHTTPTimeout)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(
|
||||
ctx,
|
||||
http.MethodPost,
|
||||
config.Token.WebhookURL(),
|
||||
bytes.NewBuffer(payloadBytes),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create webhook request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", jsonclient.ContentType)
|
||||
|
||||
res, err := service.client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to invoke webhook: %w", err)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
resBytes, _ := io.ReadAll(res.Body)
|
||||
response := string(resBytes)
|
||||
|
||||
switch response {
|
||||
case "":
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%w: %v", ErrWebhookStatusFailure, res.Status)
|
||||
}
|
||||
|
||||
fallthrough
|
||||
case "ok":
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("%w: %v", ErrWebhookResponseFailure, response)
|
||||
}
|
||||
}
|
91
pkg/services/slack/slack_config.go
Normal file
91
pkg/services/slack/slack_config.go
Normal file
|
@ -0,0 +1,91 @@
|
|||
package slack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/format"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
const (
|
||||
// Scheme is the identifying part of this service's configuration URL.
|
||||
Scheme = "slack"
|
||||
)
|
||||
|
||||
// Config for the slack service.
|
||||
type Config struct {
|
||||
standard.EnumlessConfig
|
||||
BotName string `desc:"Bot name" key:"botname,username" optional:"uses bot default"`
|
||||
Icon string `desc:"Use emoji or URL as icon (based on presence of http(s):// prefix)" key:"icon,icon_emoji,icon_url" optional:"" default:""`
|
||||
Token Token `desc:"API Bot token" url:"user,pass"`
|
||||
Color string `desc:"Message left-hand border color" key:"color" optional:"default border color"`
|
||||
Title string `desc:"Prepended text above the message" key:"title" optional:"omitted"`
|
||||
Channel string `desc:"Channel to send messages to in Cxxxxxxxxxx format" url:"host"`
|
||||
ThreadTS string `desc:"ts value of the parent message (to send message as reply in thread)" key:"thread_ts" optional:""`
|
||||
}
|
||||
|
||||
// GetURL returns a URL representation of it's current field values.
|
||||
func (config *Config) GetURL() *url.URL {
|
||||
resolver := format.NewPropKeyResolver(config)
|
||||
|
||||
return config.getURL(&resolver)
|
||||
}
|
||||
|
||||
// SetURL updates a ServiceConfig from a URL representation of it's field values.
|
||||
func (config *Config) SetURL(url *url.URL) error {
|
||||
resolver := format.NewPropKeyResolver(config)
|
||||
|
||||
return config.setURL(&resolver, url)
|
||||
}
|
||||
|
||||
func (config *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
|
||||
return &url.URL{
|
||||
User: config.Token.UserInfo(),
|
||||
Host: config.Channel,
|
||||
Scheme: Scheme,
|
||||
ForceQuery: false,
|
||||
RawQuery: format.BuildQuery(resolver),
|
||||
}
|
||||
}
|
||||
|
||||
func (config *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
|
||||
var token string
|
||||
|
||||
var err error
|
||||
|
||||
if len(serviceURL.Path) > 1 {
|
||||
// Reading legacy config URL format
|
||||
token = serviceURL.Hostname() + serviceURL.Path
|
||||
config.Channel = "webhook"
|
||||
config.BotName = serviceURL.User.Username()
|
||||
} else {
|
||||
token = serviceURL.User.String()
|
||||
config.Channel = serviceURL.Hostname()
|
||||
}
|
||||
|
||||
if serviceURL.String() != "slack://dummy@dummy.com" {
|
||||
if err = config.Token.SetFromProp(token); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
config.Token.raw = token // Set raw token without validation
|
||||
}
|
||||
|
||||
for key, vals := range serviceURL.Query() {
|
||||
if err := resolver.Set(key, vals[0]); err != nil {
|
||||
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateConfigFromURL to use within the slack service.
|
||||
func CreateConfigFromURL(serviceURL *url.URL) (*Config, error) {
|
||||
config := Config{}
|
||||
err := config.SetURL(serviceURL)
|
||||
|
||||
return &config, err
|
||||
}
|
21
pkg/services/slack/slack_errors.go
Normal file
21
pkg/services/slack/slack_errors.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package slack
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrInvalidToken is returned when the specified token does not match any known formats.
|
||||
var ErrInvalidToken = errors.New("invalid slack token format")
|
||||
|
||||
// ErrMismatchedTokenSeparators is returned if the token uses different separators between parts (of the recognized `/-,`).
|
||||
var ErrMismatchedTokenSeparators = errors.New("invalid webhook token format")
|
||||
|
||||
// ErrAPIResponseFailure indicates a failure in the Slack API response.
|
||||
var ErrAPIResponseFailure = errors.New("api response failure")
|
||||
|
||||
// ErrUnknownAPIError indicates an unknown error from the Slack API.
|
||||
var ErrUnknownAPIError = errors.New("unknown error from Slack API")
|
||||
|
||||
// ErrWebhookStatusFailure indicates a failure due to an unexpected webhook status code.
|
||||
var ErrWebhookStatusFailure = errors.New("webhook status failure")
|
||||
|
||||
// ErrWebhookResponseFailure indicates a failure in the webhook response content.
|
||||
var ErrWebhookResponseFailure = errors.New("webhook response failure")
|
125
pkg/services/slack/slack_json.go
Normal file
125
pkg/services/slack/slack_json.go
Normal file
|
@ -0,0 +1,125 @@
|
|||
package slack
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Constants for Slack API limits.
|
||||
const (
|
||||
MaxAttachments = 100 // Maximum number of attachments allowed by Slack API
|
||||
)
|
||||
|
||||
var iconURLPattern = regexp.MustCompile(`https?://`)
|
||||
|
||||
// MessagePayload used within the Slack service.
|
||||
type MessagePayload struct {
|
||||
Text string `json:"text"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type block struct {
|
||||
Type string `json:"type"`
|
||||
Text blockText `json:"text"`
|
||||
}
|
||||
|
||||
type blockText struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type attachment struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Fallback string `json:"fallback,omitempty"`
|
||||
Text string `json:"text"`
|
||||
Color string `json:"color,omitempty"`
|
||||
Fields []legacyField `json:"fields,omitempty"`
|
||||
Footer string `json:"footer,omitempty"`
|
||||
Time int `json:"ts,omitempty"`
|
||||
}
|
||||
|
||||
type legacyField struct {
|
||||
Title string `json:"title"`
|
||||
Value string `json:"value"`
|
||||
Short bool `json:"short,omitempty"`
|
||||
}
|
||||
|
||||
// APIResponse is the default generic response message sent from the API.
|
||||
type APIResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Error string `json:"error"`
|
||||
Warning string `json:"warning"`
|
||||
MetaData struct {
|
||||
Warnings []string `json:"warnings"`
|
||||
} `json:"response_metadata"`
|
||||
}
|
||||
|
||||
// CreateJSONPayload compatible with the slack post message API.
|
||||
func CreateJSONPayload(config *Config, message string) any {
|
||||
lines := strings.Split(message, "\n")
|
||||
// Pre-allocate atts with a capacity of min(len(lines), MaxAttachments)
|
||||
atts := make([]attachment, 0, minInt(len(lines), MaxAttachments))
|
||||
|
||||
for i, line := range lines {
|
||||
// When MaxAttachments have been reached, append the remaining lines to the last attachment
|
||||
if i >= MaxAttachments {
|
||||
atts[MaxAttachments-1].Text += "\n" + line
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
atts = append(atts, attachment{
|
||||
Text: line,
|
||||
Color: config.Color,
|
||||
})
|
||||
}
|
||||
|
||||
// Remove last attachment if empty
|
||||
if len(atts) > 0 && atts[len(atts)-1].Text == "" {
|
||||
atts = atts[:len(atts)-1]
|
||||
}
|
||||
|
||||
payload := MessagePayload{
|
||||
ThreadTS: config.ThreadTS,
|
||||
Text: config.Title,
|
||||
BotName: config.BotName,
|
||||
Attachments: atts,
|
||||
}
|
||||
|
||||
payload.SetIcon(config.Icon)
|
||||
|
||||
if config.Channel != "webhook" {
|
||||
payload.Channel = config.Channel
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
// SetIcon sets the appropriate icon field in the payload based on whether the input is a URL or not.
|
||||
func (p *MessagePayload) SetIcon(icon string) {
|
||||
p.IconURL = ""
|
||||
p.IconEmoji = ""
|
||||
|
||||
if icon != "" {
|
||||
if iconURLPattern.MatchString(icon) {
|
||||
p.IconURL = icon
|
||||
} else {
|
||||
p.IconEmoji = icon
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// minInt returns the smaller of two integers.
|
||||
func minInt(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
332
pkg/services/slack/slack_test.go
Normal file
332
pkg/services/slack/slack_test.go
Normal file
|
@ -0,0 +1,332 @@
|
|||
package slack_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jarcoal/httpmock"
|
||||
"github.com/onsi/ginkgo/v2"
|
||||
"github.com/onsi/gomega"
|
||||
"github.com/onsi/gomega/format"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/internal/testutils"
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/services/slack"
|
||||
)
|
||||
|
||||
const (
|
||||
TestWebhookURL = "https://hooks.slack.com/services/AAAAAAAAA/BBBBBBBBB/123456789123456789123456"
|
||||
)
|
||||
|
||||
func TestSlack(t *testing.T) {
|
||||
format.CharactersAroundMismatchToInclude = 20
|
||||
|
||||
gomega.RegisterFailHandler(ginkgo.Fail)
|
||||
ginkgo.RunSpecs(t, "Shoutrrr Slack Suite")
|
||||
}
|
||||
|
||||
var (
|
||||
service *slack.Service
|
||||
envSlackURL *url.URL
|
||||
logger *log.Logger
|
||||
_ = ginkgo.BeforeSuite(func() {
|
||||
service = &slack.Service{}
|
||||
logger = log.New(ginkgo.GinkgoWriter, "Test", log.LstdFlags)
|
||||
envSlackURL, _ = url.Parse(os.Getenv("SHOUTRRR_SLACK_URL"))
|
||||
})
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("the slack service", func() {
|
||||
ginkgo.When("running integration tests", func() {
|
||||
ginkgo.It("should not error out", func() {
|
||||
if envSlackURL.String() == "" {
|
||||
return
|
||||
}
|
||||
|
||||
serviceURL, _ := url.Parse(envSlackURL.String())
|
||||
err := service.Initialize(serviceURL, testutils.TestLogger())
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
err = service.Send("This is an integration test message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("returns the correct service identifier", func() {
|
||||
gomega.Expect(service.GetID()).To(gomega.Equal("slack"))
|
||||
})
|
||||
})
|
||||
|
||||
// xoxb:123456789012-1234567890123-4mt0t4l1YL3g1T5L4cK70k3N
|
||||
|
||||
ginkgo.When("given a token with a malformed part", func() {
|
||||
ginkgo.It("should return an error if part A is not 9 letters", func() {
|
||||
expectErrorMessageGivenURL(
|
||||
slack.ErrInvalidToken,
|
||||
"slack://lol@12345678/123456789/123456789123456789123456",
|
||||
)
|
||||
})
|
||||
ginkgo.It("should return an error if part B is not 9 letters", func() {
|
||||
expectErrorMessageGivenURL(
|
||||
slack.ErrInvalidToken,
|
||||
"slack://lol@123456789/12345678/123456789123456789123456",
|
||||
)
|
||||
})
|
||||
ginkgo.It("should return an error if part C is not 24 letters", func() {
|
||||
expectErrorMessageGivenURL(
|
||||
slack.ErrInvalidToken,
|
||||
"slack://123456789/123456789/12345678912345678912345",
|
||||
)
|
||||
})
|
||||
})
|
||||
ginkgo.When("given a token missing a part", func() {
|
||||
ginkgo.It("should return an error if the missing part is A", func() {
|
||||
expectErrorMessageGivenURL(
|
||||
slack.ErrInvalidToken,
|
||||
"slack://lol@/123456789/123456789123456789123456",
|
||||
)
|
||||
})
|
||||
ginkgo.It("should return an error if the missing part is B", func() {
|
||||
expectErrorMessageGivenURL(slack.ErrInvalidToken, "slack://lol@123456789//123456789")
|
||||
})
|
||||
ginkgo.It("should return an error if the missing part is C", func() {
|
||||
expectErrorMessageGivenURL(slack.ErrInvalidToken, "slack://lol@123456789/123456789/")
|
||||
})
|
||||
})
|
||||
ginkgo.Describe("the slack config", func() {
|
||||
ginkgo.When("parsing the configuration URL", func() {
|
||||
ginkgo.When("given a config using the legacy format", func() {
|
||||
ginkgo.It("should be converted to the new format after de-/serialization", func() {
|
||||
oldURL := "slack://testbot@AAAAAAAAA/BBBBBBBBB/123456789123456789123456?color=3f00fe&title=Test+title"
|
||||
newURL := "slack://hook:AAAAAAAAA-BBBBBBBBB-123456789123456789123456@webhook?botname=testbot&color=3f00fe&title=Test+title"
|
||||
|
||||
config := &slack.Config{}
|
||||
err := config.SetURL(testutils.URLMust(oldURL))
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "verifying")
|
||||
|
||||
gomega.Expect(config.GetURL().String()).To(gomega.Equal(newURL))
|
||||
})
|
||||
})
|
||||
})
|
||||
ginkgo.When("the URL contains an invalid property", func() {
|
||||
testURL := testutils.URLMust(
|
||||
"slack://hook:AAAAAAAAA-BBBBBBBBB-123456789123456789123456@webhook?bass=dirty",
|
||||
)
|
||||
err := (&slack.Config{}).SetURL(testURL)
|
||||
gomega.Expect(err).To(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should be identical after de-/serialization", func() {
|
||||
testURL := "slack://hook:AAAAAAAAA-BBBBBBBBB-123456789123456789123456@webhook?botname=testbot&color=3f00fe&title=Test+title"
|
||||
|
||||
config := &slack.Config{}
|
||||
err := config.SetURL(testutils.URLMust(testURL))
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "verifying")
|
||||
|
||||
outputURL := config.GetURL()
|
||||
gomega.Expect(outputURL.String()).To(gomega.Equal(testURL))
|
||||
})
|
||||
ginkgo.When("generating a config object", func() {
|
||||
ginkgo.It(
|
||||
"should use the default botname if the argument list contains three strings",
|
||||
func() {
|
||||
slackURL, _ := url.Parse("slack://AAAAAAAAA/BBBBBBBBB/123456789123456789123456")
|
||||
config, configError := slack.CreateConfigFromURL(slackURL)
|
||||
|
||||
gomega.Expect(configError).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(config.BotName).To(gomega.BeEmpty())
|
||||
},
|
||||
)
|
||||
ginkgo.It("should set the botname if the argument list is three", func() {
|
||||
slackURL, _ := url.Parse(
|
||||
"slack://testbot@AAAAAAAAA/BBBBBBBBB/123456789123456789123456",
|
||||
)
|
||||
config, configError := slack.CreateConfigFromURL(slackURL)
|
||||
|
||||
gomega.Expect(configError).NotTo(gomega.HaveOccurred())
|
||||
gomega.Expect(config.BotName).To(gomega.Equal("testbot"))
|
||||
})
|
||||
ginkgo.It("should return an error if the argument list is shorter than three", func() {
|
||||
slackURL, _ := url.Parse("slack://AAAAAAAA")
|
||||
|
||||
_, configError := slack.CreateConfigFromURL(slackURL)
|
||||
gomega.Expect(configError).To(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
ginkgo.When("getting credentials from token", func() {
|
||||
ginkgo.It("should return a valid webhook URL for the given token", func() {
|
||||
token := tokenMust("AAAAAAAAA/BBBBBBBBB/123456789123456789123456")
|
||||
gomega.Expect(token.WebhookURL()).To(gomega.Equal(TestWebhookURL))
|
||||
})
|
||||
ginkgo.It(
|
||||
"should return a valid authorization header value for the given token",
|
||||
func() {
|
||||
token := tokenMust("xoxb:AAAAAAAAA-BBBBBBBBB-123456789123456789123456")
|
||||
expected := "Bearer xoxb-AAAAAAAAA-BBBBBBBBB-123456789123456789123456"
|
||||
gomega.Expect(token.Authorization()).To(gomega.Equal(expected))
|
||||
},
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("creating the payload", func() {
|
||||
ginkgo.Describe("the icon fields", func() {
|
||||
payload := slack.MessagePayload{}
|
||||
ginkgo.It("should set IconURL when the configured icon looks like an URL", func() {
|
||||
payload.SetIcon("https://example.com/logo.png")
|
||||
gomega.Expect(payload.IconURL).To(gomega.Equal("https://example.com/logo.png"))
|
||||
gomega.Expect(payload.IconEmoji).To(gomega.BeEmpty())
|
||||
})
|
||||
ginkgo.It(
|
||||
"should set IconEmoji when the configured icon does not look like an URL",
|
||||
func() {
|
||||
payload.SetIcon("tanabata_tree")
|
||||
gomega.Expect(payload.IconEmoji).To(gomega.Equal("tanabata_tree"))
|
||||
gomega.Expect(payload.IconURL).To(gomega.BeEmpty())
|
||||
},
|
||||
)
|
||||
ginkgo.It("should clear both fields when icon is empty", func() {
|
||||
payload.SetIcon("")
|
||||
gomega.Expect(payload.IconEmoji).To(gomega.BeEmpty())
|
||||
gomega.Expect(payload.IconURL).To(gomega.BeEmpty())
|
||||
})
|
||||
})
|
||||
ginkgo.When("when more than 99 lines are being sent", func() {
|
||||
ginkgo.It("should append the exceeding lines to the last attachment", func() {
|
||||
config := slack.Config{}
|
||||
sb := strings.Builder{}
|
||||
for i := 1; i <= 110; i++ {
|
||||
sb.WriteString(fmt.Sprintf("Line %d\n", i))
|
||||
}
|
||||
payload := slack.CreateJSONPayload(&config, sb.String()).(slack.MessagePayload)
|
||||
atts := payload.Attachments
|
||||
|
||||
fmt.Fprint(
|
||||
ginkgo.GinkgoWriter,
|
||||
"\nLines: ",
|
||||
len(atts),
|
||||
" Last: ",
|
||||
atts[len(atts)-1],
|
||||
"\n",
|
||||
)
|
||||
|
||||
gomega.Expect(atts).To(gomega.HaveLen(100))
|
||||
gomega.Expect(atts[len(atts)-1].Text).To(gomega.ContainSubstring("Line 110"))
|
||||
})
|
||||
})
|
||||
ginkgo.When("when the last message line ends with a newline", func() {
|
||||
ginkgo.It("should not send an empty attachment", func() {
|
||||
payload := slack.CreateJSONPayload(&slack.Config{}, "One\nTwo\nThree\n").(slack.MessagePayload)
|
||||
atts := payload.Attachments
|
||||
gomega.Expect(atts[len(atts)-1].Text).NotTo(gomega.BeEmpty())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ginkgo.Describe("sending the payload", func() {
|
||||
ginkgo.When("sending via webhook URL", func() {
|
||||
var err error
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
|
||||
ginkgo.It("should not report an error if the server accepts the payload", func() {
|
||||
serviceURL, _ := url.Parse(
|
||||
"slack://testbot@AAAAAAAAA/BBBBBBBBB/123456789123456789123456",
|
||||
)
|
||||
err = service.Initialize(serviceURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
TestWebhookURL,
|
||||
httpmock.NewStringResponder(200, ""),
|
||||
)
|
||||
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should not panic if an error occurs when sending the payload", func() {
|
||||
serviceURL, _ := url.Parse(
|
||||
"slack://testbot@AAAAAAAAA/BBBBBBBBB/123456789123456789123456",
|
||||
)
|
||||
err = service.Initialize(serviceURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
TestWebhookURL,
|
||||
httpmock.NewErrorResponder(errors.New("dummy error")),
|
||||
)
|
||||
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).To(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
ginkgo.When("sending via bot API", func() {
|
||||
var err error
|
||||
ginkgo.BeforeEach(func() {
|
||||
httpmock.Activate()
|
||||
})
|
||||
ginkgo.AfterEach(func() {
|
||||
httpmock.DeactivateAndReset()
|
||||
})
|
||||
|
||||
ginkgo.It("should not report an error if the server accepts the payload", func() {
|
||||
serviceURL := testutils.URLMust(
|
||||
"slack://xoxb:123456789012-1234567890123-4mt0t4l1YL3g1T5L4cK70k3N@C0123456789",
|
||||
)
|
||||
err = service.Initialize(serviceURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
targetURL := "https://slack.com/api/chat.postMessage"
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
targetURL,
|
||||
testutils.JSONRespondMust(200, slack.APIResponse{
|
||||
Ok: true,
|
||||
}),
|
||||
)
|
||||
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should not panic if an error occurs when sending the payload", func() {
|
||||
serviceURL := testutils.URLMust(
|
||||
"slack://xoxb:123456789012-1234567890123-4mt0t4l1YL3g1T5L4cK70k3N@C0123456789",
|
||||
)
|
||||
err = service.Initialize(serviceURL, logger)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
targetURL := "https://slack.com/api/chat.postMessage"
|
||||
httpmock.RegisterResponder(
|
||||
"POST",
|
||||
targetURL,
|
||||
testutils.JSONRespondMust(200, slack.APIResponse{
|
||||
Error: "someone turned off the internet",
|
||||
}),
|
||||
)
|
||||
|
||||
err = service.Send("Message", nil)
|
||||
gomega.Expect(err).To(gomega.HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
func tokenMust(rawToken string) *slack.Token {
|
||||
token, err := slack.ParseToken(rawToken)
|
||||
gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
return token
|
||||
}
|
||||
|
||||
func expectErrorMessageGivenURL(expected error, rawURL string) {
|
||||
err := service.Initialize(testutils.URLMust(rawURL), testutils.TestLogger())
|
||||
gomega.ExpectWithOffset(1, err).To(gomega.HaveOccurred())
|
||||
gomega.ExpectWithOffset(1, err).To(gomega.Equal(expected))
|
||||
}
|
154
pkg/services/slack/slack_token.go
Normal file
154
pkg/services/slack/slack_token.go
Normal file
|
@ -0,0 +1,154 @@
|
|||
package slack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/nicholas-fedor/shoutrrr/pkg/types"
|
||||
)
|
||||
|
||||
const webhookBase = "https://hooks.slack.com/services/"
|
||||
|
||||
// Token type identifiers.
|
||||
const (
|
||||
HookTokenIdentifier = "hook"
|
||||
UserTokenIdentifier = "xoxp"
|
||||
BotTokenIdentifier = "xoxb"
|
||||
)
|
||||
|
||||
// Token length and offset constants.
|
||||
const (
|
||||
MinTokenLength = 3 // Minimum length for a valid token string
|
||||
TypeIdentifierLength = 4 // Length of the type identifier (e.g., "xoxb", "hook")
|
||||
TypeIdentifierOffset = 5 // Offset to skip type identifier and separator (e.g., "xoxb:")
|
||||
Part1Length = 9 // Expected length of part 1 in token
|
||||
Part2Length = 9 // Expected length of part 2 in token
|
||||
Part3Length = 24 // Expected length of part 3 in token
|
||||
)
|
||||
|
||||
// Token match group indices.
|
||||
const (
|
||||
tokenMatchFull = iota // Full match
|
||||
tokenMatchType // Type identifier (e.g., "xoxb", "hook")
|
||||
tokenMatchPart1 // First part of the token
|
||||
tokenMatchSep1 // First separator
|
||||
tokenMatchPart2 // Second part of the token
|
||||
tokenMatchSep2 // Second separator
|
||||
tokenMatchPart3 // Third part of the token
|
||||
tokenMatchCount // Total number of match groups
|
||||
)
|
||||
|
||||
var tokenPattern = regexp.MustCompile(
|
||||
`(?:(?P<type>xox.|hook)[-:]|:?)(?P<p1>[A-Z0-9]{` + strconv.Itoa(
|
||||
Part1Length,
|
||||
) + `,})(?P<s1>[-/,])(?P<p2>[A-Z0-9]{` + strconv.Itoa(
|
||||
Part2Length,
|
||||
) + `,})(?P<s2>[-/,])(?P<p3>[A-Za-z0-9]{` + strconv.Itoa(
|
||||
Part3Length,
|
||||
) + `,})`,
|
||||
)
|
||||
|
||||
var _ types.ConfigProp = &Token{}
|
||||
|
||||
// Token is a Slack API token or a Slack webhook token.
|
||||
type Token struct {
|
||||
raw string
|
||||
}
|
||||
|
||||
// SetFromProp sets the token from a property value, implementing the types.ConfigProp interface.
|
||||
func (token *Token) SetFromProp(propValue string) error {
|
||||
if len(propValue) < MinTokenLength {
|
||||
return ErrInvalidToken
|
||||
}
|
||||
|
||||
match := tokenPattern.FindStringSubmatch(propValue)
|
||||
if match == nil || len(match) != tokenMatchCount {
|
||||
return ErrInvalidToken
|
||||
}
|
||||
|
||||
typeIdentifier := match[tokenMatchType]
|
||||
if typeIdentifier == "" {
|
||||
typeIdentifier = HookTokenIdentifier
|
||||
}
|
||||
|
||||
token.raw = fmt.Sprintf("%s:%s-%s-%s",
|
||||
typeIdentifier, match[tokenMatchPart1], match[tokenMatchPart2], match[tokenMatchPart3])
|
||||
|
||||
if match[tokenMatchSep1] != match[tokenMatchSep2] {
|
||||
return ErrMismatchedTokenSeparators
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPropValue returns the token as a property value, implementing the types.ConfigProp interface.
|
||||
func (token *Token) GetPropValue() (string, error) {
|
||||
if token == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return token.raw, nil
|
||||
}
|
||||
|
||||
// TypeIdentifier returns the type identifier of the token.
|
||||
func (token *Token) TypeIdentifier() string {
|
||||
return token.raw[:TypeIdentifierLength]
|
||||
}
|
||||
|
||||
// ParseToken parses and normalizes a token string.
|
||||
func ParseToken(str string) (*Token, error) {
|
||||
token := &Token{}
|
||||
if err := token.SetFromProp(str); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// String returns the token in normalized format with dashes (-) as separator.
|
||||
func (token *Token) String() string {
|
||||
return token.raw
|
||||
}
|
||||
|
||||
// UserInfo returns a url.Userinfo struct populated from the token.
|
||||
func (token *Token) UserInfo() *url.Userinfo {
|
||||
return url.UserPassword(token.raw[:TypeIdentifierLength], token.raw[TypeIdentifierOffset:])
|
||||
}
|
||||
|
||||
// IsAPIToken returns whether the identifier is set to anything else but the webhook identifier (`hook`).
|
||||
func (token *Token) IsAPIToken() bool {
|
||||
return token.TypeIdentifier() != HookTokenIdentifier
|
||||
}
|
||||
|
||||
// WebhookURL returns the corresponding Webhook URL for the token.
|
||||
func (token *Token) WebhookURL() string {
|
||||
stringBuilder := strings.Builder{}
|
||||
stringBuilder.WriteString(webhookBase)
|
||||
stringBuilder.Grow(len(token.raw) - TypeIdentifierOffset)
|
||||
|
||||
for i := TypeIdentifierOffset; i < len(token.raw); i++ {
|
||||
c := token.raw[i]
|
||||
if c == '-' {
|
||||
c = '/'
|
||||
}
|
||||
|
||||
stringBuilder.WriteByte(c)
|
||||
}
|
||||
|
||||
return stringBuilder.String()
|
||||
}
|
||||
|
||||
// Authorization returns the corresponding `Authorization` HTTP header value for the token.
|
||||
func (token *Token) Authorization() string {
|
||||
stringBuilder := strings.Builder{}
|
||||
stringBuilder.WriteString("Bearer ")
|
||||
stringBuilder.Grow(len(token.raw))
|
||||
stringBuilder.WriteString(token.raw[:TypeIdentifierLength])
|
||||
stringBuilder.WriteRune('-')
|
||||
stringBuilder.WriteString(token.raw[TypeIdentifierOffset:])
|
||||
|
||||
return stringBuilder.String()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue