From 65f938888e174ef400a5869d8829874d178fc83c Mon Sep 17 00:00:00 2001 From: ap4y Date: Tue, 11 Nov 2014 17:03:37 +1300 Subject: [PATCH] Add plain text fallback messages for slack plugin --- plugin/notify/slack.go | 31 ++++++++++++++++++++++--------- plugin/notify/slack_test.go | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/plugin/notify/slack.go b/plugin/notify/slack.go index 4e2a43f4..356948ce 100644 --- a/plugin/notify/slack.go +++ b/plugin/notify/slack.go @@ -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"}, diff --git a/plugin/notify/slack_test.go b/plugin/notify/slack_test.go index 789ea03f..78ecb6ee 100644 --- a/plugin/notify/slack_test.go +++ b/plugin/notify/slack_test.go @@ -23,8 +23,11 @@ var request = &model.Request{ } */ -var slackExpectedLink = "" -var slackExpectedBase = slackExpectedLink + " (example) by Test User" +var ( + slackExpectedLink = "" + 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) + } +}