2014-03-26 10:43:40 +00:00
|
|
|
package notify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-04-06 09:05:30 +00:00
|
|
|
"fmt"
|
2014-10-11 22:41:20 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/shared/model"
|
2014-03-26 10:43:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-11-11 04:03:37 +00:00
|
|
|
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"
|
2015-03-05 09:57:49 +00:00
|
|
|
drone_icon = "https://avatars.githubusercontent.com/drone"
|
2014-03-26 10:43:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Slack struct {
|
2014-10-23 06:00:29 +00:00
|
|
|
WebhookUrl string `yaml:"webhook_url,omitempty"`
|
|
|
|
Channel string `yaml:"channel,omitempty"`
|
|
|
|
Username string `yaml:"username,omitempty"`
|
|
|
|
Started bool `yaml:"on_started,omitempty"`
|
|
|
|
Success bool `yaml:"on_success,omitempty"`
|
|
|
|
Failure bool `yaml:"on_failure,omitempty"`
|
2015-06-19 14:58:49 +00:00
|
|
|
Change bool `yaml:"on_change,omitempty"`
|
2014-03-26 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
2014-10-11 22:41:20 +00:00
|
|
|
func (s *Slack) Send(context *model.Request) error {
|
2014-03-26 10:43:40 +00:00
|
|
|
switch {
|
|
|
|
case context.Commit.Status == "Started" && s.Started:
|
|
|
|
return s.sendStarted(context)
|
2015-06-19 14:58:49 +00:00
|
|
|
case context.Commit.Status == "Success":
|
|
|
|
if s.Success || (s.Change && context.Prior.Status != context.Commit.Status) {
|
|
|
|
return s.sendSuccess(context)
|
|
|
|
}
|
|
|
|
case context.Commit.Status == "Failure":
|
|
|
|
if s.Failure || (s.Change && context.Prior.Status != context.Commit.Status) {
|
|
|
|
return s.sendFailure(context)
|
|
|
|
}
|
2014-03-26 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-11 22:41:20 +00:00
|
|
|
func (s *Slack) getMessage(context *model.Request, message string) string {
|
2014-04-09 09:39:23 +00:00
|
|
|
url := getBuildUrl(context)
|
2014-10-23 05:26:14 +00:00
|
|
|
// drone/drone#3333333
|
|
|
|
linktext := context.Repo.Owner + "/" + context.Repo.Name + "#" + context.Commit.ShaShort()
|
|
|
|
|
2014-11-04 19:39:33 +00:00
|
|
|
return fmt.Sprintf(message, url, linktext, context.Commit.Branch, context.Commit.Author)
|
2014-04-09 09:39:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 04:03:37 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2014-10-11 22:41:20 +00:00
|
|
|
func (s *Slack) sendStarted(context *model.Request) error {
|
2014-11-11 04:03:37 +00:00
|
|
|
return s.send(s.getMessage(context, slackStartedMessage)+"\n - "+context.Commit.Message,
|
|
|
|
s.getFallbackMessage(context, slackStartedFallbackMessage), "warning")
|
2014-03-26 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
2014-10-11 22:41:20 +00:00
|
|
|
func (s *Slack) sendSuccess(context *model.Request) error {
|
2014-11-11 04:03:37 +00:00
|
|
|
return s.send(s.getMessage(context, slackSuccessMessage),
|
|
|
|
s.getFallbackMessage(context, slackSuccessFallbackMessage), "good")
|
2014-03-26 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
2014-10-11 22:41:20 +00:00
|
|
|
func (s *Slack) sendFailure(context *model.Request) error {
|
2014-11-11 04:03:37 +00:00
|
|
|
return s.send(s.getMessage(context, slackFailureMessage),
|
|
|
|
s.getFallbackMessage(context, slackFailureFallbackMessage), "danger")
|
2014-03-26 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to send HTTP requests
|
2014-11-11 04:03:37 +00:00
|
|
|
func (s *Slack) send(msg string, fallback string, color string) error {
|
2014-09-10 12:40:59 +00:00
|
|
|
type Attachment struct {
|
|
|
|
Fallback string `json:"fallback"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
MrkdwnIn []string `json:"mrkdwn_in"`
|
|
|
|
}
|
|
|
|
|
|
|
|
attachments := []Attachment{
|
|
|
|
Attachment{
|
2014-11-11 04:03:37 +00:00
|
|
|
fallback,
|
2014-09-10 12:40:59 +00:00
|
|
|
msg,
|
|
|
|
color,
|
|
|
|
[]string{"fallback", "text"},
|
|
|
|
},
|
|
|
|
}
|
2014-03-26 10:43:40 +00:00
|
|
|
// data will get posted in this format
|
|
|
|
data := struct {
|
2014-09-10 12:40:59 +00:00
|
|
|
Channel string `json:"channel"`
|
|
|
|
Username string `json:"username"`
|
2015-03-05 09:57:49 +00:00
|
|
|
Icon string `json:"icon_url"`
|
2014-09-10 12:40:59 +00:00
|
|
|
Attachments []Attachment `json:"attachments"`
|
2015-03-05 09:57:49 +00:00
|
|
|
}{s.Channel, s.Username, drone_icon, attachments}
|
2014-03-26 10:43:40 +00:00
|
|
|
|
|
|
|
// data json encoded
|
|
|
|
payload, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-06 02:48:16 +00:00
|
|
|
return sendJson(s.WebhookUrl, payload, nil)
|
2014-09-10 06:19:04 +00:00
|
|
|
}
|