1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00

Add plain text fallback messages for slack plugin

This commit is contained in:
ap4y 2014-11-11 17:03:37 +13:00
parent 7354ad8e95
commit 65f938888e
2 changed files with 57 additions and 11 deletions

View File

@ -8,10 +8,13 @@ import (
)
const (
slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
slackStartedMessage = "*Building* <%s|%s> (%s) by %s"
slackSuccessMessage = "*Success* <%s|%s> (%s) by %s"
slackFailureMessage = "*Failed* <%s|%s> (%s) by %s"
slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
slackStartedMessage = "*Building* <%s|%s> (%s) by %s"
slackStartedFallbackMessage = "Building %s (%s) by %s"
slackSuccessMessage = "*Success* <%s|%s> (%s) by %s"
slackSuccessFallbackMessage = "Success %s (%s) by %s"
slackFailureMessage = "*Failed* <%s|%s> (%s) by %s"
slackFailureFallbackMessage = "Failed %s (%s) by %s"
)
type Slack struct {
@ -45,20 +48,30 @@ func (s *Slack) getMessage(context *model.Request, message string) string {
return fmt.Sprintf(message, url, linktext, context.Commit.Branch, context.Commit.Author)
}
func (s *Slack) getFallbackMessage(context *model.Request, message string) string {
// drone/drone#3333333
text := context.Repo.Owner + "/" + context.Repo.Name + "#" + context.Commit.ShaShort()
return fmt.Sprintf(message, text, context.Commit.Branch, context.Commit.Author)
}
func (s *Slack) sendStarted(context *model.Request) error {
return s.send(s.getMessage(context, slackStartedMessage)+"\n - "+context.Commit.Message, "warning")
return s.send(s.getMessage(context, slackStartedMessage)+"\n - "+context.Commit.Message,
s.getFallbackMessage(context, slackStartedFallbackMessage), "warning")
}
func (s *Slack) sendSuccess(context *model.Request) error {
return s.send(s.getMessage(context, slackSuccessMessage), "good")
return s.send(s.getMessage(context, slackSuccessMessage),
s.getFallbackMessage(context, slackSuccessFallbackMessage), "good")
}
func (s *Slack) sendFailure(context *model.Request) error {
return s.send(s.getMessage(context, slackFailureMessage), "danger")
return s.send(s.getMessage(context, slackFailureMessage),
s.getFallbackMessage(context, slackFailureFallbackMessage), "danger")
}
// helper function to send HTTP requests
func (s *Slack) send(msg string, color string) error {
func (s *Slack) send(msg string, fallback string, color string) error {
type Attachment struct {
Fallback string `json:"fallback"`
Text string `json:"text"`
@ -68,7 +81,7 @@ func (s *Slack) send(msg string, color string) error {
attachments := []Attachment{
Attachment{
msg,
fallback,
msg,
color,
[]string{"fallback", "text"},

View File

@ -23,8 +23,11 @@ var request = &model.Request{
}
*/
var slackExpectedLink = "<http://examplehost.com/examplegit.com/owner/repo/example/abc|owner/repo#abc>"
var slackExpectedBase = slackExpectedLink + " (example) by Test User"
var (
slackExpectedLink = "<http://examplehost.com/examplegit.com/owner/repo/example/abc|owner/repo#abc>"
slackExpectedFallbackText = "owner/repo#abc (example) by Test User"
slackExpectedBase = slackExpectedLink + " (example) by Test User"
)
func Test_slackStartedMessage(t *testing.T) {
actual := (&Slack{}).getMessage(request, slackStartedMessage)
@ -36,6 +39,16 @@ func Test_slackStartedMessage(t *testing.T) {
}
}
func Test_slackStartedFallbackMessage(t *testing.T) {
actual := (&Slack{}).getFallbackMessage(request, slackStartedFallbackMessage)
expected := "Building " + slackExpectedFallbackText
if actual != expected {
t.Errorf("Invalid fallback started message for Slack. Expected %v, got %v", expected, actual)
}
}
func Test_slackSuccessMessage(t *testing.T) {
actual := (&Slack{}).getMessage(request, slackSuccessMessage)
@ -46,6 +59,16 @@ func Test_slackSuccessMessage(t *testing.T) {
}
}
func Test_slackSuccessFallbackMessage(t *testing.T) {
actual := (&Slack{}).getFallbackMessage(request, slackSuccessFallbackMessage)
expected := "Success " + slackExpectedFallbackText
if actual != expected {
t.Errorf("Invalid success fallback message for Slack. Expected %v, got %v", expected, actual)
}
}
func Test_slackFailureMessage(t *testing.T) {
actual := (&Slack{}).getMessage(request, slackFailureMessage)
@ -55,3 +78,13 @@ func Test_slackFailureMessage(t *testing.T) {
t.Errorf("Invalid getStarted message for Slack. Expected %v, got %v", expected, actual)
}
}
func Test_slackFailureFallbackMessage(t *testing.T) {
actual := (&Slack{}).getFallbackMessage(request, slackFailureFallbackMessage)
expected := "Failed " + slackExpectedFallbackText
if actual != expected {
t.Errorf("Invalid failure fallback message for Slack. Expected %v, got %v", expected, actual)
}
}