Adjust Slack message format to match HipChat's

Also add some tests for formatting. See #586.
This commit is contained in:
Ciaran Downey 2014-10-22 22:26:14 -07:00
parent 063f1f4830
commit 0a0151fa5e
3 changed files with 87 additions and 26 deletions

View file

@ -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)
}
}

View file

@ -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 {

View file

@ -1,12 +1,9 @@
package notify
import (
"github.com/drone/drone/shared/model"
"testing"
)
import "testing"
func Test_getBuildUrl(t *testing.T) {
c := &model.Request{
/*
var request = &model.Request{
Host: "http://examplehost.com",
Repo: &model.Repo{
Host: "examplegit.com",
@ -16,12 +13,45 @@ func Test_getBuildUrl(t *testing.T) {
Commit: &model.Commit{
Sha: "abc",
Branch: "example",
Status: "Started",
Message: "Test Commit",
Author: "Test User",
},
User: &model.User{
Login: "TestUser",
},
}
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)
var slackExpectedLink = "<owner/repo#abc|http://examplehost.com/examplegit.com/owner/repo/example/abc>"
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)
}
}