From 0a0151fa5e988ec8e117c91fa66b40803b861e9c Mon Sep 17 00:00:00 2001 From: Ciaran Downey Date: Wed, 22 Oct 2014 22:26:14 -0700 Subject: [PATCH] Adjust Slack message format to match HipChat's Also add some tests for formatting. See #586. --- plugin/notify/notify_test.go | 28 ++++++++++++++ plugin/notify/slack.go | 13 ++++--- plugin/notify/slack_test.go | 72 +++++++++++++++++++++++++----------- 3 files changed, 87 insertions(+), 26 deletions(-) create mode 100644 plugin/notify/notify_test.go diff --git a/plugin/notify/notify_test.go b/plugin/notify/notify_test.go new file mode 100644 index 00000000..8faca408 --- /dev/null +++ b/plugin/notify/notify_test.go @@ -0,0 +1,28 @@ +package notify + +import ( + "testing" + + "github.com/drone/drone/shared/model" +) + +func Test_getBuildUrl(t *testing.T) { + c := &model.Request{ + Host: "http://examplehost.com", + Repo: &model.Repo{ + Host: "examplegit.com", + Owner: "owner", + Name: "repo", + }, + Commit: &model.Commit{ + Sha: "abc", + Branch: "example", + }, + } + expected := "http://examplehost.com/examplegit.com/owner/repo/example/abc" + output := getBuildUrl(c) + + if output != expected { + t.Errorf("Failed to build url. Expected: %s, got %s", expected, output) + } +} diff --git a/plugin/notify/slack.go b/plugin/notify/slack.go index 9d20cee7..22320b4b 100644 --- a/plugin/notify/slack.go +++ b/plugin/notify/slack.go @@ -9,9 +9,9 @@ import ( const ( slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s" - slackStartedMessage = "*Building* %s, commit <%s|%s>, author %s" - slackSuccessMessage = "*Success* %s, commit <%s|%s>, author %s" - slackFailureMessage = "*Failed* %s, commit <%s|%s>, author %s" + slackStartedMessage = "*Building* <%s|%s> (%s) by %s" + slackSuccessMessage = "*Success* <%s|%s> (%s) by %s" + slackFailureMessage = "*Failed* <%s|%s> (%s) by %s" ) type Slack struct { @@ -39,11 +39,14 @@ func (s *Slack) Send(context *model.Request) error { func (s *Slack) getMessage(context *model.Request, message string) string { url := getBuildUrl(context) - return fmt.Sprintf(message, context.Repo.Name, url, context.Commit.ShaShort(), context.Commit.Author) + // drone/drone#3333333 + linktext := context.Repo.Owner + "/" + context.Repo.Name + "#" + context.Commit.ShaShort() + + return fmt.Sprintf(message, linktext, url, context.Commit.Branch, context.Commit.Author) } func (s *Slack) sendStarted(context *model.Request) error { - return s.send(s.getMessage(context, slackStartedMessage), "warning") + return s.send(s.getMessage(context, slackStartedMessage)+"\n - "+context.Commit.Message, "warning") } func (s *Slack) sendSuccess(context *model.Request) error { diff --git a/plugin/notify/slack_test.go b/plugin/notify/slack_test.go index 81493e11..93ee35ae 100644 --- a/plugin/notify/slack_test.go +++ b/plugin/notify/slack_test.go @@ -1,27 +1,57 @@ package notify -import ( - "github.com/drone/drone/shared/model" - "testing" -) +import "testing" -func Test_getBuildUrl(t *testing.T) { - c := &model.Request{ - Host: "http://examplehost.com", - Repo: &model.Repo{ - Host: "examplegit.com", - Owner: "owner", - Name: "repo", - }, - Commit: &model.Commit{ - Sha: "abc", - Branch: "example", - }, - } - expected := "http://examplehost.com/examplegit.com/owner/repo/example/abc" - output := getBuildUrl(c) +/* +var request = &model.Request{ + Host: "http://examplehost.com", + Repo: &model.Repo{ + Host: "examplegit.com", + Owner: "owner", + Name: "repo", + }, + Commit: &model.Commit{ + Sha: "abc", + Branch: "example", + Status: "Started", + Message: "Test Commit", + Author: "Test User", + }, + User: &model.User{ + Login: "TestUser", + }, +} +*/ - if output != expected { - t.Errorf("Failed to build url. Expected: %s, got %s", expected, output) +var slackExpectedLink = "" +var slackExpectedBase = slackExpectedLink + " (example) by Test User" + +func Test_slackStartedMessage(t *testing.T) { + actual := (&Slack{}).getMessage(request, slackStartedMessage) + + expected := "*Building* " + slackExpectedBase + + if actual != expected { + t.Errorf("Invalid getStarted message for Slack. Expected %v, got %v", expected, actual) + } +} + +func Test_slackSuccessMessage(t *testing.T) { + actual := (&Slack{}).getMessage(request, slackSuccessMessage) + + expected := "*Success* " + slackExpectedBase + + if actual != expected { + t.Errorf("Invalid getStarted message for Slack. Expected %v, got %v", expected, actual) + } +} + +func Test_slackFailureMessage(t *testing.T) { + actual := (&Slack{}).getMessage(request, slackFailureMessage) + + expected := "*Failed* " + slackExpectedBase + + if actual != expected { + t.Errorf("Invalid getStarted message for Slack. Expected %v, got %v", expected, actual) } }