2014-03-26 13:43:40 +03:00
|
|
|
package notify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-04-06 12:05:30 +03:00
|
|
|
"fmt"
|
2014-03-26 13:43:40 +03:00
|
|
|
|
2014-04-06 12:05:30 +03:00
|
|
|
//"github.com/drone/drone/pkg/model"
|
2014-03-26 13:43:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
|
2014-04-06 12:05:30 +03:00
|
|
|
slackStartedMessage = "Building %s, commit %s, author %s"
|
|
|
|
slackSuccessMessage = "<b>Success</b> %s, commit %s, author %s"
|
|
|
|
slackFailureMessage = "<b>Failed</b> %s, commit %s, author %s"
|
2014-03-26 13:43:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Slack struct {
|
|
|
|
Team string `yaml:"team,omitempty"`
|
|
|
|
Channel string `yaml:"channel,omitempty"`
|
|
|
|
Username string `yaml:"username,omitempty"`
|
|
|
|
Token string `yaml:"token,omitempty"`
|
|
|
|
Started bool `yaml:"on_started,omitempty"`
|
|
|
|
Success bool `yaml:"on_success,omitempty"`
|
|
|
|
Failure bool `yaml:"on_failure,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Slack) Send(context *Context) error {
|
|
|
|
switch {
|
|
|
|
case context.Commit.Status == "Started" && s.Started:
|
|
|
|
return s.sendStarted(context)
|
|
|
|
case context.Commit.Status == "Success" && s.Success:
|
|
|
|
return s.sendSuccess(context)
|
|
|
|
case context.Commit.Status == "Failure" && s.Failure:
|
|
|
|
return s.sendFailure(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Slack) sendStarted(context *Context) error {
|
2014-04-06 12:05:30 +03:00
|
|
|
msg := fmt.Sprintf(slackStartedMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
|
2014-03-26 13:43:40 +03:00
|
|
|
return s.send(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Slack) sendSuccess(context *Context) error {
|
2014-04-06 12:05:30 +03:00
|
|
|
msg := fmt.Sprintf(slackSuccessMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
|
2014-03-26 13:43:40 +03:00
|
|
|
return s.send(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Slack) sendFailure(context *Context) error {
|
2014-04-06 12:05:30 +03:00
|
|
|
msg := fmt.Sprintf(slackFailureMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
|
2014-03-26 13:43:40 +03:00
|
|
|
return s.send(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to send HTTP requests
|
|
|
|
func (s *Slack) send(msg string) error {
|
|
|
|
// data will get posted in this format
|
|
|
|
data := struct {
|
|
|
|
Channel string `json:"channel"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
}{s.Channel, s.Username, msg}
|
|
|
|
|
|
|
|
// data json encoded
|
|
|
|
payload, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// send payload
|
2014-04-06 12:05:30 +03:00
|
|
|
url := fmt.Sprintf(slackEndpoint, s.Team, s.Token)
|
2014-03-26 13:43:40 +03:00
|
|
|
go sendJson(url, payload)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|