1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-12 08:23:48 +02:00
woodpecker/plugin/notify/notification.go

131 lines
2.8 KiB
Go
Raw Normal View History

2014-02-07 13:10:01 +03:00
package notify
import (
2014-10-02 17:04:32 +03:00
"bytes"
"fmt"
2014-08-04 00:45:50 +03:00
"log"
2014-10-02 17:04:32 +03:00
"net/http"
2014-08-04 00:45:50 +03:00
"github.com/drone/drone/plugin/notify/email"
"github.com/drone/drone/plugin/notify/github"
"github.com/drone/drone/plugin/notify/irc"
"github.com/drone/drone/plugin/notify/webhook"
"github.com/drone/drone/shared/model"
2014-02-07 13:10:01 +03:00
)
type Sender interface {
Send(context *model.Request) error
2014-02-07 13:10:01 +03:00
}
// Notification stores the configuration details
// for notifying a user, or group of users,
// when their Build has completed.
type Notification struct {
Email *email.Email `yaml:"email,omitempty"`
Webhook *webhook.Webhook `yaml:"webhook,omitempty"`
Hipchat *Hipchat `yaml:"hipchat,omitempty"`
Irc *irc.IRC `yaml:"irc,omitempty"`
Slack *Slack `yaml:"slack,omitempty"`
Gitter *Gitter `yaml:"gitter,omitempty"`
Flowdock *Flowdock `yaml:"flowdock,omitempty"`
GitHub github.GitHub `yaml:"--"`
2014-02-07 13:10:01 +03:00
}
func (n *Notification) Send(context *model.Request) error {
2014-02-07 13:10:01 +03:00
// send email notifications
2014-08-04 00:45:50 +03:00
if n.Email != nil {
err := n.Email.Send(context)
if err != nil {
log.Println(err)
}
}
// send webhook notifications
2014-02-07 13:10:01 +03:00
if n.Webhook != nil {
2014-08-04 00:45:50 +03:00
err := n.Webhook.Send(context)
if err != nil {
log.Println(err)
}
2014-02-07 13:10:01 +03:00
}
2014-08-04 00:45:50 +03:00
// send hipchat notifications
2014-02-07 13:10:01 +03:00
if n.Hipchat != nil {
2014-08-04 00:45:50 +03:00
err := n.Hipchat.Send(context)
if err != nil {
log.Println(err)
}
2014-02-07 13:10:01 +03:00
}
2014-03-06 21:45:21 +03:00
// send irc notifications
if n.Irc != nil {
2014-08-04 00:45:50 +03:00
err := n.Irc.Send(context)
if err != nil {
log.Println(err)
}
2014-03-06 21:45:21 +03:00
}
2014-03-26 13:43:40 +03:00
// send slack notifications
if n.Slack != nil {
2014-08-04 00:45:50 +03:00
err := n.Slack.Send(context)
if err != nil {
log.Println(err)
}
2014-03-26 13:43:40 +03:00
}
2014-10-02 17:04:32 +03:00
// send gitter notifications
if n.Gitter != nil {
err := n.Gitter.Send(context)
if err != nil {
log.Println(err)
}
}
// send gitter notifications
if n.Flowdock != nil {
err := n.Flowdock.Send(context)
if err != nil {
log.Println(err)
}
}
// send email notifications
// TODO (bradrydzewski) need to improve this code
githubStatus := new(github.GitHub)
if err := githubStatus.Send(context); err != nil {
log.Println(err)
}
2014-02-07 13:10:01 +03:00
return nil
}
2014-10-02 17:04:32 +03:00
func getBuildUrl(context *model.Request) string {
return fmt.Sprintf("%s/%s/%s/%s/%s/%s", context.Host, context.Repo.Host, context.Repo.Owner, context.Repo.Name, context.Commit.Branch, context.Commit.Sha)
}
// helper fuction to sent HTTP Post requests
// with JSON data as the payload.
func sendJson(url string, payload []byte, headers map[string]string) error {
2014-10-02 17:04:32 +03:00
client := &http.Client{}
buf := bytes.NewBuffer(payload)
req, err := http.NewRequest("POST", url, buf)
if err != nil {
return err
}
2014-10-02 17:04:32 +03:00
req.Header.Set("Content-Type", "application/json")
if headers != nil {
for k, v := range headers {
req.Header.Add(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return err
2014-10-02 17:04:32 +03:00
}
resp.Body.Close()
return nil
2014-10-02 17:04:32 +03:00
}