From 6f3997f4e13e8cda1c4b6e69441fe3be48913a5a Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 9 Sep 2014 23:19:04 -0700 Subject: [PATCH] moved IRC and Webhooks to their own packages updated email template to include link to build updated readme to include correct badge --- README.md | 2 +- plugin/notify/email/template.go | 10 +++- plugin/notify/irc.go | 63 -------------------------- plugin/notify/irc/irc.go | 63 ++++++++++++++++++++++++++ plugin/notify/notification.go | 14 +++--- plugin/notify/slack.go | 13 ++++++ plugin/notify/{ => webhook}/webhook.go | 10 ++-- server/worker/worker.go | 5 +- 8 files changed, 102 insertions(+), 78 deletions(-) delete mode 100644 plugin/notify/irc.go create mode 100644 plugin/notify/irc/irc.go rename plugin/notify/{ => webhook}/webhook.go (77%) diff --git a/README.md b/README.md index 7345320a..661548f7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](http://test.drone.io/v1/badge/github.com/drone/drone/status.svg?branch=master)](http://test.drone.io/github.com/drone/drone) +[![Build Status](http://test.drone.io/v1/badge/github.com/drone/drone/status.svg?branch=exp)](http://test.drone.io/github.com/drone/drone) [![GoDoc](https://godoc.org/github.com/drone/drone?status.png)](https://godoc.org/github.com/drone/drone) diff --git a/plugin/notify/email/template.go b/plugin/notify/email/template.go index 54a27b3b..378fac8b 100644 --- a/plugin/notify/email/template.go +++ b/plugin/notify/email/template.go @@ -15,7 +15,10 @@ Content-Type: text/html; charset="UTF-8" // default success email template var successTemplate = template.Must(template.New("_").Parse(` -

Build was Successful

+

+ Build was Successful + (see results) +

Repository : {{.Repo.Owner}}/{{.Repo.Name}}

Commit : {{.Commit.ShaShort}}

Author : {{.Commit.Author}}

@@ -26,7 +29,10 @@ var successTemplate = template.Must(template.New("_").Parse(` // default failure email template var failureTemplate = template.Must(template.New("_").Parse(` -

Build Failed

+

+ Build Failed + (see results) +

Repository : {{.Repo.Owner}}/{{.Repo.Name}}

Commit : {{.Commit.ShaShort}}

Author : {{.Commit.Author}}

diff --git a/plugin/notify/irc.go b/plugin/notify/irc.go deleted file mode 100644 index 3698a2db..00000000 --- a/plugin/notify/irc.go +++ /dev/null @@ -1,63 +0,0 @@ -package notify - -import ( - "fmt" - - "github.com/drone/drone/shared/model" - "github.com/thoj/go-ircevent" -) - -const ( - ircStartedMessage = "Building: %s, commit %s, author %s" - ircSuccessMessage = "Success: %s, commit %s, author %s" - ircFailureMessage = "Failed: %s, commit %s, author %s" -) - -type IRC struct { - Channel string `yaml:"channel,omitempty"` - Nick string `yaml:"nick,omitempty"` - Server string `yaml:"server,omitempty"` - Started *bool `yaml:"on_started,omitempty"` - Success *bool `yaml:"on_success,omitempty"` - Failure *bool `yaml:"on_failure,omitempty"` -} - -func (i *IRC) Send(req *model.Request) error { - switch { - case req.Commit.Status == "Started" && i.Started != nil && *i.Started == true: - return i.sendStarted(req) - case req.Commit.Status == "Success" && i.Success != nil && *i.Success == true: - return i.sendSuccess(req) - case req.Commit.Status == "Failure" && i.Failure != nil && *i.Failure == true: - return i.sendFailure(req) - } - return nil -} - -func (i *IRC) sendStarted(req *model.Request) error { - msg := fmt.Sprintf(ircStartedMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author) - return i.send(i.Channel, msg) -} - -func (i *IRC) sendFailure(req *model.Request) error { - msg := fmt.Sprintf(ircFailureMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author) - return i.send(i.Channel, msg) -} - -func (i *IRC) sendSuccess(req *model.Request) error { - msg := fmt.Sprintf(ircSuccessMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author) - return i.send(i.Channel, msg) -} - -// send is a helper function that will send notice messages -// to the connected IRC client -func (i *IRC) send(channel string, message string) error { - client := irc.IRC(i.Nick, i.Nick) - if client != nil { - return fmt.Errorf("Error creating IRC client") - } - defer client.Disconnect() - client.Connect(i.Server) - client.Notice(channel, message) - return nil -} diff --git a/plugin/notify/irc/irc.go b/plugin/notify/irc/irc.go new file mode 100644 index 00000000..7e232677 --- /dev/null +++ b/plugin/notify/irc/irc.go @@ -0,0 +1,63 @@ +package irc + +import ( + "fmt" + + "github.com/drone/drone/shared/model" + "github.com/thoj/go-ircevent" +) + +const ( + MessageStarted = "Building: %s, commit %s, author %s" + MessageSuccess = "Success: %s, commit %s, author %s" + MessageFailure = "Failed: %s, commit %s, author %s" +) + +type IRC struct { + Channel string + Nick string + Server string + Started *bool `yaml:"on_started,omitempty"` + Success *bool `yaml:"on_success,omitempty"` + Failure *bool `yaml:"on_failure,omitempty"` +} + +func (i *IRC) Send(req *model.Request) error { + switch { + case req.Commit.Status == model.StatusStarted && i.Started != nil && *i.Started == true: + return i.sendStarted(req) + case req.Commit.Status == model.StatusSuccess && i.Success != nil && *i.Success == true: + return i.sendSuccess(req) + case req.Commit.Status == model.StatusFailure && i.Failure != nil && *i.Failure == true: + return i.sendFailure(req) + } + return nil +} + +func (i *IRC) sendStarted(req *model.Request) error { + msg := fmt.Sprintf(MessageStarted, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author) + return i.send(i.Channel, msg) +} + +func (i *IRC) sendFailure(req *model.Request) error { + msg := fmt.Sprintf(MessageFailure, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author) + return i.send(i.Channel, msg) +} + +func (i *IRC) sendSuccess(req *model.Request) error { + msg := fmt.Sprintf(MessageSuccess, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author) + return i.send(i.Channel, msg) +} + +// send is a helper function that will send notice messages +// to the connected IRC client +func (i *IRC) send(channel string, message string) error { + client := irc.IRC(i.Nick, i.Nick) + if client != nil { + return fmt.Errorf("Error creating IRC client") + } + defer client.Disconnect() + client.Connect(i.Server) + client.Notice(channel, message) + return nil +} diff --git a/plugin/notify/notification.go b/plugin/notify/notification.go index 6a9fd0bb..3f263aca 100644 --- a/plugin/notify/notification.go +++ b/plugin/notify/notification.go @@ -5,6 +5,8 @@ import ( "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" ) @@ -16,13 +18,13 @@ type Sender interface { // for notifying a user, or group of users, // when their Build has completed. type Notification struct { - Email *email.Email `yaml:"email,omitempty"` - Webhook *Webhook `yaml:"webhook,omitempty"` - Hipchat *Hipchat `yaml:"hipchat,omitempty"` - Irc *IRC `yaml:"irc,omitempty"` - Slack *Slack `yaml:"slack,omitempty"` + 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"` - GitHub github.GitHub `yaml:"--"` + GitHub github.GitHub `yaml:"--"` } func (n *Notification) Send(context *model.Request) error { diff --git a/plugin/notify/slack.go b/plugin/notify/slack.go index 35bf3376..e02069d8 100644 --- a/plugin/notify/slack.go +++ b/plugin/notify/slack.go @@ -1,8 +1,10 @@ package notify import ( + "bytes" "encoding/json" "fmt" + "net/http" "github.com/drone/drone/shared/model" ) @@ -79,3 +81,14 @@ func (s *Slack) send(msg string) error { return nil } + +// helper fuction to sent HTTP Post requests +// with JSON data as the payload. +func sendJson(url string, payload []byte) { + buf := bytes.NewBuffer(payload) + resp, err := http.Post(url, "application/json", buf) + if err != nil { + return + } + resp.Body.Close() +} diff --git a/plugin/notify/webhook.go b/plugin/notify/webhook/webhook.go similarity index 77% rename from plugin/notify/webhook.go rename to plugin/notify/webhook/webhook.go index 998cc160..b35b69f5 100644 --- a/plugin/notify/webhook.go +++ b/plugin/notify/webhook/webhook.go @@ -1,4 +1,4 @@ -package notify +package webhook import ( "bytes" @@ -10,15 +10,15 @@ import ( type Webhook struct { URL []string `yaml:"urls,omitempty"` - Success bool `yaml:"on_success,omitempty"` - Failure bool `yaml:"on_failure,omitempty"` + Success *bool `yaml:"on_success,omitempty"` + Failure *bool `yaml:"on_failure,omitempty"` } func (w *Webhook) Send(context *model.Request) error { switch { - case context.Commit.Status == "Success" && w.Success: + case context.Commit.Status == model.StatusSuccess && w.Success != nil && *w.Success == true: return w.send(context) - case context.Commit.Status == "Failure" && w.Failure: + case context.Commit.Status == model.StatusFailure && w.Failure != nil && *w.Success == true: return w.send(context) } diff --git a/server/worker/worker.go b/server/worker/worker.go index 9fb75467..47ac54a1 100644 --- a/server/worker/worker.go +++ b/server/worker/worker.go @@ -98,7 +98,10 @@ func (w *worker) Execute(r *model.Request) { // parse the parameters and build script. The script has already // been parsed in the hook, so we can be confident it will succeed. // that being said, we should clean this up - params, _ := r.Repo.ParamMap() + params, err := r.Repo.ParamMap() + if err != nil { + log.Printf("Error parsing PARAMS for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error()) + } script, err := script.ParseBuild(r.Commit.Config, params) if err != nil { log.Printf("Error parsing YAML for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())