Skip to content

Commit

Permalink
Adjust Slack message format to match HipChat's
Browse files Browse the repository at this point in the history
Also add some tests for formatting. See #586.
  • Loading branch information
ciarand committed Oct 23, 2014
1 parent 063f1f4 commit 0a0151f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 26 deletions.
28 changes: 28 additions & 0 deletions plugin/notify/notify_test.go
Original file line number Diff line number Diff line change
@@ -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:https://examplehost.com",
Repo: &model.Repo{
Host: "examplegit.com",
Owner: "owner",
Name: "repo",
},
Commit: &model.Commit{
Sha: "abc",
Branch: "example",
},
}
expected := "http:https://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)
}
}
13 changes: 8 additions & 5 deletions plugin/notify/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
72 changes: 51 additions & 21 deletions plugin/notify/slack_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,57 @@
package notify

import (
"github.com/drone/drone/shared/model"
"testing"
)

func Test_getBuildUrl(t *testing.T) {
c := &model.Request{
Host: "http:https://examplehost.com",
Repo: &model.Repo{
Host: "examplegit.com",
Owner: "owner",
Name: "repo",
},
Commit: &model.Commit{
Sha: "abc",
Branch: "example",
},
import "testing"

/*
var request = &model.Request{
Host: "http:https://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",
},
}
*/

var slackExpectedLink = "<owner/repo#abc|http:https://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)
}
expected := "http:https://examplehost.com/examplegit.com/owner/repo/example/abc"
output := getBuildUrl(c)
}

func Test_slackFailureMessage(t *testing.T) {
actual := (&Slack{}).getMessage(request, slackFailureMessage)

expected := "*Failed* " + slackExpectedBase

if output != expected {
t.Errorf("Failed to build url. Expected: %s, got %s", expected, output)
if actual != expected {
t.Errorf("Invalid getStarted message for Slack. Expected %v, got %v", expected, actual)
}
}

0 comments on commit 0a0151f

Please sign in to comment.