Add plain text fallback messages for slack plugin
This commit is contained in:
parent
7354ad8e95
commit
65f938888e
2 changed files with 57 additions and 11 deletions
|
@ -10,8 +10,11 @@ import (
|
||||||
const (
|
const (
|
||||||
slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
|
slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
|
||||||
slackStartedMessage = "*Building* <%s|%s> (%s) by %s"
|
slackStartedMessage = "*Building* <%s|%s> (%s) by %s"
|
||||||
|
slackStartedFallbackMessage = "Building %s (%s) by %s"
|
||||||
slackSuccessMessage = "*Success* <%s|%s> (%s) by %s"
|
slackSuccessMessage = "*Success* <%s|%s> (%s) by %s"
|
||||||
|
slackSuccessFallbackMessage = "Success %s (%s) by %s"
|
||||||
slackFailureMessage = "*Failed* <%s|%s> (%s) by %s"
|
slackFailureMessage = "*Failed* <%s|%s> (%s) by %s"
|
||||||
|
slackFailureFallbackMessage = "Failed %s (%s) by %s"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Slack struct {
|
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)
|
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 {
|
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 {
|
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 {
|
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
|
// 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 {
|
type Attachment struct {
|
||||||
Fallback string `json:"fallback"`
|
Fallback string `json:"fallback"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
|
@ -68,7 +81,7 @@ func (s *Slack) send(msg string, color string) error {
|
||||||
|
|
||||||
attachments := []Attachment{
|
attachments := []Attachment{
|
||||||
Attachment{
|
Attachment{
|
||||||
msg,
|
fallback,
|
||||||
msg,
|
msg,
|
||||||
color,
|
color,
|
||||||
[]string{"fallback", "text"},
|
[]string{"fallback", "text"},
|
||||||
|
|
|
@ -23,8 +23,11 @@ var request = &model.Request{
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var slackExpectedLink = "<http://examplehost.com/examplegit.com/owner/repo/example/abc|owner/repo#abc>"
|
var (
|
||||||
var slackExpectedBase = slackExpectedLink + " (example) by Test User"
|
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) {
|
func Test_slackStartedMessage(t *testing.T) {
|
||||||
actual := (&Slack{}).getMessage(request, slackStartedMessage)
|
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) {
|
func Test_slackSuccessMessage(t *testing.T) {
|
||||||
actual := (&Slack{}).getMessage(request, slackSuccessMessage)
|
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) {
|
func Test_slackFailureMessage(t *testing.T) {
|
||||||
actual := (&Slack{}).getMessage(request, slackFailureMessage)
|
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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue