updated notification plugins to use model.Request

This commit is contained in:
Brad Rydzewski 2014-07-12 19:01:34 -07:00
parent ecff154c56
commit 91f7a63d21
7 changed files with 31 additions and 46 deletions

View file

@ -1,6 +1,8 @@
package notify package notify
//import "github.com/drone/drone/pkg/mail" import (
"github.com/drone/drone/shared/model"
)
type Email struct { type Email struct {
Recipients []string `yaml:"recipients,omitempty"` Recipients []string `yaml:"recipients,omitempty"`
@ -10,7 +12,7 @@ type Email struct {
// Send will send an email, either success or failure, // Send will send an email, either success or failure,
// based on the Commit Status. // based on the Commit Status.
func (e *Email) Send(context *Context) error { func (e *Email) Send(context *model.Request) error {
switch { switch {
case context.Commit.Status == "Success" && e.Success != "never": case context.Commit.Status == "Success" && e.Success != "never":
return e.sendSuccess(context) return e.sendSuccess(context)
@ -23,7 +25,7 @@ func (e *Email) Send(context *Context) error {
// sendFailure sends email notifications to the list of // sendFailure sends email notifications to the list of
// recipients indicating the build failed. // recipients indicating the build failed.
func (e *Email) sendFailure(context *Context) error { func (e *Email) sendFailure(context *model.Request) error {
// loop through and email recipients // loop through and email recipients
//for _, email := range e.Recipients { //for _, email := range e.Recipients {
//if err := mail.SendFailure(context.Repo.Name, context.Commit.HashShort(), email, context); err != nil { //if err := mail.SendFailure(context.Repo.Name, context.Commit.HashShort(), email, context); err != nil {
@ -35,7 +37,7 @@ func (e *Email) sendFailure(context *Context) error {
// sendSuccess sends email notifications to the list of // sendSuccess sends email notifications to the list of
// recipients indicating the build was a success. // recipients indicating the build was a success.
func (e *Email) sendSuccess(context *Context) error { func (e *Email) sendSuccess(context *model.Request) error {
// loop through and email recipients // loop through and email recipients
//for _, email := range e.Recipients { //for _, email := range e.Recipients {
// if err := mail.SendSuccess(context.Repo.Name, context.Commit.HashShort(), email, context); err != nil { // if err := mail.SendSuccess(context.Repo.Name, context.Commit.HashShort(), email, context); err != nil {

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/andybons/hipchat" "github.com/andybons/hipchat"
"github.com/drone/drone/shared/model"
) )
const ( const (
@ -20,7 +21,7 @@ type Hipchat struct {
Failure bool `yaml:"on_failure,omitempty"` Failure bool `yaml:"on_failure,omitempty"`
} }
func (h *Hipchat) Send(context *Context) error { func (h *Hipchat) Send(context *model.Request) error {
switch { switch {
case context.Commit.Status == "Started" && h.Started: case context.Commit.Status == "Started" && h.Started:
return h.sendStarted(context) return h.sendStarted(context)
@ -33,17 +34,17 @@ func (h *Hipchat) Send(context *Context) error {
return nil return nil
} }
func (h *Hipchat) sendStarted(context *Context) error { func (h *Hipchat) sendStarted(context *model.Request) error {
msg := fmt.Sprintf(startedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author) msg := fmt.Sprintf(startedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
return h.send(hipchat.ColorYellow, hipchat.FormatHTML, msg) return h.send(hipchat.ColorYellow, hipchat.FormatHTML, msg)
} }
func (h *Hipchat) sendFailure(context *Context) error { func (h *Hipchat) sendFailure(context *model.Request) error {
msg := fmt.Sprintf(failureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author) msg := fmt.Sprintf(failureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
return h.send(hipchat.ColorRed, hipchat.FormatHTML, msg) return h.send(hipchat.ColorRed, hipchat.FormatHTML, msg)
} }
func (h *Hipchat) sendSuccess(context *Context) error { func (h *Hipchat) sendSuccess(context *model.Request) error {
msg := fmt.Sprintf(successMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author) msg := fmt.Sprintf(successMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
return h.send(hipchat.ColorGreen, hipchat.FormatHTML, msg) return h.send(hipchat.ColorGreen, hipchat.FormatHTML, msg)
} }

View file

@ -3,6 +3,7 @@ package notify
import ( import (
"fmt" "fmt"
"github.com/drone/drone/shared/model"
irc "github.com/fluffle/goirc/client" irc "github.com/fluffle/goirc/client"
) )
@ -39,7 +40,7 @@ func (i *IRC) Connect() {
i.Client = c i.Client = c
} }
func (i *IRC) Send(context *Context) error { func (i *IRC) Send(context *model.Request) error {
switch { switch {
case context.Commit.Status == "Started" && i.Started: case context.Commit.Status == "Started" && i.Started:
return i.sendStarted(context) return i.sendStarted(context)
@ -51,13 +52,13 @@ func (i *IRC) Send(context *Context) error {
return nil return nil
} }
func (i *IRC) sendStarted(context *Context) error { func (i *IRC) sendStarted(context *model.Request) error {
msg := fmt.Sprintf(ircStartedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author) msg := fmt.Sprintf(ircStartedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
i.send(i.Channel, msg) i.send(i.Channel, msg)
return nil return nil
} }
func (i *IRC) sendFailure(context *Context) error { func (i *IRC) sendFailure(context *model.Request) error {
msg := fmt.Sprintf(ircFailureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author) msg := fmt.Sprintf(ircFailureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
i.send(i.Channel, msg) i.send(i.Channel, msg)
if i.ClientStarted { if i.ClientStarted {
@ -66,7 +67,7 @@ func (i *IRC) sendFailure(context *Context) error {
return nil return nil
} }
func (i *IRC) sendSuccess(context *Context) error { func (i *IRC) sendSuccess(context *model.Request) error {
msg := fmt.Sprintf(ircSuccessMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author) msg := fmt.Sprintf(ircSuccessMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
i.send(i.Channel, msg) i.send(i.Channel, msg)
if i.ClientStarted { if i.ClientStarted {

View file

@ -4,24 +4,8 @@ import (
"github.com/drone/drone/shared/model" "github.com/drone/drone/shared/model"
) )
// Context represents the context of an
// in-progress build request.
type Context struct {
// Global settings
Host string
// User that owns the repository
User *model.User
// Repository being built.
Repo *model.Repo
// Commit being built
Commit *model.Commit
}
type Sender interface { type Sender interface {
Send(context *Context) error Send(context *model.Request) error
} }
// Notification stores the configuration details // Notification stores the configuration details
@ -35,12 +19,7 @@ type Notification struct {
Slack *Slack `yaml:"slack,omitempty"` Slack *Slack `yaml:"slack,omitempty"`
} }
func (n *Notification) Send(context *Context) error { func (n *Notification) Send(context *model.Request) error {
// send email notifications
if n.Email != nil {
n.Email.Send(context)
}
// send email notifications // send email notifications
if n.Webhook != nil { if n.Webhook != nil {
n.Webhook.Send(context) n.Webhook.Send(context)

View file

@ -3,6 +3,8 @@ package notify
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/drone/drone/shared/model"
) )
const ( const (
@ -22,7 +24,7 @@ type Slack struct {
Failure bool `yaml:"on_failure,omitempty"` Failure bool `yaml:"on_failure,omitempty"`
} }
func (s *Slack) Send(context *Context) error { func (s *Slack) Send(context *model.Request) error {
switch { switch {
case context.Commit.Status == "Started" && s.Started: case context.Commit.Status == "Started" && s.Started:
return s.sendStarted(context) return s.sendStarted(context)
@ -35,24 +37,24 @@ func (s *Slack) Send(context *Context) error {
return nil return nil
} }
func getBuildUrl(context *Context) string { func getBuildUrl(context *model.Request) string {
return fmt.Sprintf("%s/%s/%s/%s/branch/%s/commit/%s", context.Host, context.Repo.Host, context.Repo.Owner, context.Repo.Name, context.Commit.Branch, context.Commit.Sha) 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)
} }
func getMessage(context *Context, message string) string { func getMessage(context *model.Request, message string) string {
url := getBuildUrl(context) url := getBuildUrl(context)
return fmt.Sprintf(message, context.Repo.Name, url, context.Commit.ShaShort(), context.Commit.Author) return fmt.Sprintf(message, context.Repo.Name, url, context.Commit.ShaShort(), context.Commit.Author)
} }
func (s *Slack) sendStarted(context *Context) error { func (s *Slack) sendStarted(context *model.Request) error {
return s.send(getMessage(context, slackStartedMessage)) return s.send(getMessage(context, slackStartedMessage))
} }
func (s *Slack) sendSuccess(context *Context) error { func (s *Slack) sendSuccess(context *model.Request) error {
return s.send(getMessage(context, slackSuccessMessage)) return s.send(getMessage(context, slackSuccessMessage))
} }
func (s *Slack) sendFailure(context *Context) error { func (s *Slack) sendFailure(context *model.Request) error {
return s.send(getMessage(context, slackFailureMessage)) return s.send(getMessage(context, slackFailureMessage))
} }

View file

@ -6,7 +6,7 @@ import (
) )
func Test_getBuildUrl(t *testing.T) { func Test_getBuildUrl(t *testing.T) {
c := &Context{ c := &model.Request{
Host: "http://examplehost.com", Host: "http://examplehost.com",
Repo: &model.Repo{ Repo: &model.Repo{
Host: "examplegit.com", Host: "examplegit.com",
@ -18,7 +18,7 @@ func Test_getBuildUrl(t *testing.T) {
Branch: "example", Branch: "example",
}, },
} }
expected := "http://examplehost.com/examplegit.com/owner/repo/branch/example/commit/abc" expected := "http://examplehost.com/examplegit.com/owner/repo/example/abc"
output := getBuildUrl(c) output := getBuildUrl(c)
if output != expected { if output != expected {

View file

@ -14,7 +14,7 @@ type Webhook struct {
Failure bool `yaml:"on_failure,omitempty"` Failure bool `yaml:"on_failure,omitempty"`
} }
func (w *Webhook) Send(context *Context) error { func (w *Webhook) Send(context *model.Request) error {
switch { switch {
case context.Commit.Status == "Success" && w.Success: case context.Commit.Status == "Success" && w.Success:
return w.send(context) return w.send(context)
@ -26,7 +26,7 @@ func (w *Webhook) Send(context *Context) error {
} }
// helper function to send HTTP requests // helper function to send HTTP requests
func (w *Webhook) send(context *Context) error { func (w *Webhook) send(context *model.Request) error {
// data will get posted in this format // data will get posted in this format
data := struct { data := struct {
Owner *model.User `json:"owner"` Owner *model.User `json:"owner"`