-
-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2425 Signed-off-by: Furkan <furkan.turkal@trendyol.com>
- Loading branch information
Showing
8 changed files
with
209 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package slack | ||
|
||
import ( | ||
"fmt" | ||
"github.com/slack-go/slack" | ||
|
||
"github.com/apex/log" | ||
"github.com/caarlos0/env/v6" | ||
"github.com/goreleaser/goreleaser/internal/pipe" | ||
"github.com/goreleaser/goreleaser/internal/tmpl" | ||
"github.com/goreleaser/goreleaser/pkg/context" | ||
) | ||
|
||
const ( | ||
defaultUsername = `GoReleaser` | ||
defaultMessageTemplate = `{{ .ProjectName }} {{ .Tag }} is out! Check it out at {{ .GitURL }}/releases/tag/{{ .Tag }}` | ||
) | ||
|
||
type Pipe struct{} | ||
|
||
func (Pipe) String() string { return "slack" } | ||
|
||
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 { | ||
if ctx.SkipAnnounce { | ||
return pipe.ErrSkipAnnounceEnabled | ||
} | ||
if !ctx.Config.Announce.Slack.Enabled { | ||
return pipe.ErrSkipDisabledPipe | ||
} | ||
|
||
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) | ||
|
||
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, | ||
} | ||
|
||
err = slack.PostWebhook(cfg.Webhook, wm) | ||
if err != nil { | ||
return fmt.Errorf("announce: failed to announce to slack: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package slack | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/goreleaser/goreleaser/internal/testlib" | ||
"github.com/goreleaser/goreleaser/pkg/config" | ||
"github.com/goreleaser/goreleaser/pkg/context" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStringer(t *testing.T) { | ||
require.Equal(t, Pipe{}.String(), "slack") | ||
} | ||
|
||
func TestDefault(t *testing.T) { | ||
ctx := context.New(config.Project{}) | ||
require.NoError(t, Pipe{}.Default(ctx)) | ||
require.Equal(t, ctx.Config.Announce.Slack.MessageTemplate, defaultMessageTemplate) | ||
} | ||
|
||
func TestAnnounceDisabled(t *testing.T) { | ||
ctx := context.New(config.Project{}) | ||
require.NoError(t, Pipe{}.Default(ctx)) | ||
testlib.AssertSkipped(t, Pipe{}.Announce(ctx)) | ||
} | ||
|
||
func TestAnnounceInvalidTemplate(t *testing.T) { | ||
ctx := context.New(config.Project{ | ||
Announce: config.Announce{ | ||
Slack: config.Slack{ | ||
Enabled: true, | ||
MessageTemplate: "{{ .Foo }", | ||
}, | ||
}, | ||
}) | ||
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to slack: template: tmpl:1: unexpected "}" in operand`) | ||
} | ||
|
||
func TestAnnounceMissingEnv(t *testing.T) { | ||
ctx := context.New(config.Project{ | ||
Announce: config.Announce{ | ||
Slack: config.Slack{ | ||
Enabled: true, | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, Pipe{}.Default(ctx)) | ||
require.EqualError(t, Pipe{}.Announce(ctx), `announce: failed to announce to slack: env: environment variable "SLACK_WEBHOOK" should not be empty`) | ||
} | ||
|
||
func TestAnnounceSkipAnnounce(t *testing.T) { | ||
ctx := context.New(config.Project{ | ||
Announce: config.Announce{ | ||
Slack: config.Slack{ | ||
Enabled: true, | ||
}, | ||
}, | ||
}) | ||
ctx.SkipAnnounce = true | ||
testlib.AssertSkipped(t, Pipe{}.Announce(ctx)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aa28f31
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: