mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-03 13:11:48 +02:00
905a1640f1
* feat(announce): added Slack notification options This feature adds support for specifying a richer content in Slack announcements. We may now specify "blocks" and "attachments" to produce better-looking announcement messages. * fixes #2986 The goreleaser configuration only exposes the top-level structures and does not check the validity of the Slack API internal structures. This way, we do not inject hard dependencies on changes in the Slack API. Notice: untyped config parsing introduces a little hack to have yaml and JSON marshaling work together properly. This hack won't be necessary with yaml.v3. How this has been tested? ------------------------- * Added unit tests for the config parsing * Added a (skipped) e2e test. For now, this requires a valid Slack webhook, so I've been able to test this manually. Signed-off-by: Frederic BIDON <fredbi@yahoo.com> * added more unit tests Signed-off-by: Frederic BIDON <fredbi@yahoo.com> * removed yaml.v2 hack Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package slack
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/caarlos0/env/v6"
|
|
"github.com/goreleaser/goreleaser/internal/tmpl"
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
"github.com/slack-go/slack"
|
|
)
|
|
|
|
const (
|
|
defaultUsername = `GoReleaser`
|
|
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .ReleaseURL }}`
|
|
)
|
|
|
|
type Pipe struct{}
|
|
|
|
func (Pipe) String() string { return "slack" }
|
|
func (Pipe) Skip(ctx *context.Context) bool { return !ctx.Config.Announce.Slack.Enabled }
|
|
|
|
type Config struct {
|
|
Webhook string `env:"SLACK_WEBHOOK,notEmpty"`
|
|
}
|
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
|
if ctx.Config.Announce.Slack.MessageTemplate == "" {
|
|
ctx.Config.Announce.Slack.MessageTemplate = defaultMessageTemplate
|
|
}
|
|
if ctx.Config.Announce.Slack.Username == "" {
|
|
ctx.Config.Announce.Slack.Username = defaultUsername
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (Pipe) Announce(ctx *context.Context) error {
|
|
msg, err := tmpl.New(ctx).Apply(ctx.Config.Announce.Slack.MessageTemplate)
|
|
if err != nil {
|
|
return fmt.Errorf("announce: failed to announce to slack: %w", err)
|
|
}
|
|
|
|
var cfg Config
|
|
if err := env.Parse(&cfg); err != nil {
|
|
return fmt.Errorf("announce: failed to announce to slack: %w", err)
|
|
}
|
|
|
|
log.Infof("posting: '%s'", msg)
|
|
|
|
// optional processing of advanced formatting options
|
|
blocks, attachments, err := parseAdvancedFormatting(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
wm := &slack.WebhookMessage{
|
|
Username: ctx.Config.Announce.Slack.Username,
|
|
IconEmoji: ctx.Config.Announce.Slack.IconEmoji,
|
|
IconURL: ctx.Config.Announce.Slack.IconURL,
|
|
Channel: ctx.Config.Announce.Slack.Channel,
|
|
Text: msg,
|
|
|
|
// optional enrichments
|
|
Blocks: blocks,
|
|
Attachments: attachments,
|
|
}
|
|
|
|
err = slack.PostWebhook(cfg.Webhook, wm)
|
|
if err != nil {
|
|
return fmt.Errorf("announce: failed to announce to slack: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func parseAdvancedFormatting(ctx *context.Context) (*slack.Blocks, []slack.Attachment, error) {
|
|
var blocks *slack.Blocks
|
|
if in := ctx.Config.Announce.Slack.Blocks; len(in) > 0 {
|
|
blocks = &slack.Blocks{BlockSet: make([]slack.Block, 0, len(in))}
|
|
|
|
if err := unmarshal(ctx, in, blocks); err != nil {
|
|
return nil, nil, fmt.Errorf("announce: slack blocks: %w", err)
|
|
}
|
|
}
|
|
|
|
var attachments []slack.Attachment
|
|
if in := ctx.Config.Announce.Slack.Attachments; len(in) > 0 {
|
|
attachments = make([]slack.Attachment, 0, len(in))
|
|
|
|
if err := unmarshal(ctx, in, &attachments); err != nil {
|
|
return nil, nil, fmt.Errorf("announce: slack attachments: %w", err)
|
|
}
|
|
}
|
|
|
|
return blocks, attachments, nil
|
|
}
|
|
|
|
func unmarshal(ctx *context.Context, in interface{}, target interface{}) error {
|
|
jazon, err := json.Marshal(in)
|
|
if err != nil {
|
|
return fmt.Errorf("announce: failed to marshal input as JSON: %w", err)
|
|
}
|
|
|
|
tplApplied, err := tmpl.New(ctx).Apply(string(jazon))
|
|
if err != nil {
|
|
return fmt.Errorf("announce: failed to evaluate template: %w", err)
|
|
}
|
|
|
|
if err = json.Unmarshal([]byte(tplApplied), target); err != nil {
|
|
return fmt.Errorf("announce: failed to unmarshal into target: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|