1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-24 10:07:21 +02:00
woodpecker/plugin/notify/slack.go

97 lines
2.5 KiB
Go
Raw Normal View History

2014-03-26 13:43:40 +03:00
package notify
import (
"encoding/json"
2014-04-06 12:05:30 +03:00
"fmt"
2014-10-12 01:41:20 +03:00
"github.com/drone/drone/shared/model"
2014-03-26 13:43:40 +03:00
)
const (
2014-04-06 23:04:13 +03:00
slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s"
slackStartedMessage = "*Building* <%s|%s> (%s) by %s"
slackSuccessMessage = "*Success* <%s|%s> (%s) by %s"
slackFailureMessage = "*Failed* <%s|%s> (%s) by %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"`
}
2014-10-12 01:41:20 +03:00
func (s *Slack) Send(context *model.Request) error {
2014-03-26 13:43:40 +03:00
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
}
2014-10-12 01:41:20 +03:00
func (s *Slack) getMessage(context *model.Request, message string) string {
2014-04-09 12:39:23 +03:00
url := getBuildUrl(context)
// 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)
2014-04-09 12:39:23 +03:00
}
2014-10-12 01:41:20 +03:00
func (s *Slack) sendStarted(context *model.Request) error {
return s.send(s.getMessage(context, slackStartedMessage)+"\n - "+context.Commit.Message, "warning")
2014-03-26 13:43:40 +03:00
}
2014-10-12 01:41:20 +03:00
func (s *Slack) sendSuccess(context *model.Request) error {
return s.send(s.getMessage(context, slackSuccessMessage), "good")
2014-03-26 13:43:40 +03:00
}
2014-10-12 01:41:20 +03:00
func (s *Slack) sendFailure(context *model.Request) error {
return s.send(s.getMessage(context, slackFailureMessage), "danger")
2014-03-26 13:43:40 +03:00
}
// helper function to send HTTP requests
func (s *Slack) send(msg string, color string) error {
type Attachment struct {
Fallback string `json:"fallback"`
Text string `json:"text"`
Color string `json:"color"`
MrkdwnIn []string `json:"mrkdwn_in"`
}
attachments := []Attachment{
Attachment{
msg,
msg,
color,
[]string{"fallback", "text"},
},
}
2014-03-26 13:43:40 +03:00
// data will get posted in this format
data := struct {
Channel string `json:"channel"`
Username string `json:"username"`
Attachments []Attachment `json:"attachments"`
}{s.Channel, s.Username, attachments}
2014-03-26 13:43:40 +03:00
// 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-10-12 01:41:20 +03:00
go sendJson(url, payload, nil)
2014-10-02 17:04:32 +03:00
return nil
}