diff --git a/plugin/notify/email.go b/plugin/notify/email.go index c903ad81e..ea90986b5 100644 --- a/plugin/notify/email.go +++ b/plugin/notify/email.go @@ -1,6 +1,8 @@ package notify -//import "github.com/drone/drone/pkg/mail" +import ( + "github.com/drone/drone/shared/model" +) type Email struct { Recipients []string `yaml:"recipients,omitempty"` @@ -10,7 +12,7 @@ type Email struct { // Send will send an email, either success or failure, // based on the Commit Status. -func (e *Email) Send(context *Context) error { +func (e *Email) Send(context *model.Request) error { switch { case context.Commit.Status == "Success" && e.Success != "never": return e.sendSuccess(context) @@ -23,7 +25,7 @@ func (e *Email) Send(context *Context) error { // sendFailure sends email notifications to the list of // 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 //for _, email := range e.Recipients { //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 // 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 //for _, email := range e.Recipients { // if err := mail.SendSuccess(context.Repo.Name, context.Commit.HashShort(), email, context); err != nil { diff --git a/plugin/notify/hipchat.go b/plugin/notify/hipchat.go index a880ba76e..563604e79 100644 --- a/plugin/notify/hipchat.go +++ b/plugin/notify/hipchat.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/andybons/hipchat" + "github.com/drone/drone/shared/model" ) const ( @@ -20,7 +21,7 @@ type Hipchat struct { Failure bool `yaml:"on_failure,omitempty"` } -func (h *Hipchat) Send(context *Context) error { +func (h *Hipchat) Send(context *model.Request) error { switch { case context.Commit.Status == "Started" && h.Started: return h.sendStarted(context) @@ -33,17 +34,17 @@ func (h *Hipchat) Send(context *Context) error { 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) 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) 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) return h.send(hipchat.ColorGreen, hipchat.FormatHTML, msg) } diff --git a/plugin/notify/irc.go b/plugin/notify/irc.go index 64f6ef2de..990110e2e 100644 --- a/plugin/notify/irc.go +++ b/plugin/notify/irc.go @@ -3,6 +3,7 @@ package notify import ( "fmt" + "github.com/drone/drone/shared/model" irc "github.com/fluffle/goirc/client" ) @@ -39,7 +40,7 @@ func (i *IRC) Connect() { i.Client = c } -func (i *IRC) Send(context *Context) error { +func (i *IRC) Send(context *model.Request) error { switch { case context.Commit.Status == "Started" && i.Started: return i.sendStarted(context) @@ -51,13 +52,13 @@ func (i *IRC) Send(context *Context) error { 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) i.send(i.Channel, msg) 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) i.send(i.Channel, msg) if i.ClientStarted { @@ -66,7 +67,7 @@ func (i *IRC) sendFailure(context *Context) error { 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) i.send(i.Channel, msg) if i.ClientStarted { diff --git a/plugin/notify/notification.go b/plugin/notify/notification.go index dede5082d..523624704 100644 --- a/plugin/notify/notification.go +++ b/plugin/notify/notification.go @@ -4,24 +4,8 @@ import ( "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 { - Send(context *Context) error + Send(context *model.Request) error } // Notification stores the configuration details @@ -35,12 +19,7 @@ type Notification struct { Slack *Slack `yaml:"slack,omitempty"` } -func (n *Notification) Send(context *Context) error { - // send email notifications - if n.Email != nil { - n.Email.Send(context) - } - +func (n *Notification) Send(context *model.Request) error { // send email notifications if n.Webhook != nil { n.Webhook.Send(context) diff --git a/plugin/notify/slack.go b/plugin/notify/slack.go index 7fcd95e32..35bf33769 100644 --- a/plugin/notify/slack.go +++ b/plugin/notify/slack.go @@ -3,6 +3,8 @@ package notify import ( "encoding/json" "fmt" + + "github.com/drone/drone/shared/model" ) const ( @@ -22,7 +24,7 @@ type Slack struct { Failure bool `yaml:"on_failure,omitempty"` } -func (s *Slack) Send(context *Context) error { +func (s *Slack) Send(context *model.Request) error { switch { case context.Commit.Status == "Started" && s.Started: return s.sendStarted(context) @@ -35,24 +37,24 @@ func (s *Slack) Send(context *Context) error { return nil } -func getBuildUrl(context *Context) 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) +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) } -func getMessage(context *Context, message string) string { +func getMessage(context *model.Request, message string) string { url := getBuildUrl(context) 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)) } -func (s *Slack) sendSuccess(context *Context) error { +func (s *Slack) sendSuccess(context *model.Request) error { 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)) } diff --git a/plugin/notify/slack_test.go b/plugin/notify/slack_test.go index 48d795bc1..81493e11d 100644 --- a/plugin/notify/slack_test.go +++ b/plugin/notify/slack_test.go @@ -6,7 +6,7 @@ import ( ) func Test_getBuildUrl(t *testing.T) { - c := &Context{ + c := &model.Request{ Host: "http://examplehost.com", Repo: &model.Repo{ Host: "examplegit.com", @@ -18,7 +18,7 @@ func Test_getBuildUrl(t *testing.T) { 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) if output != expected { diff --git a/plugin/notify/webhook.go b/plugin/notify/webhook.go index a18493270..22d325636 100644 --- a/plugin/notify/webhook.go +++ b/plugin/notify/webhook.go @@ -14,7 +14,7 @@ type Webhook struct { Failure bool `yaml:"on_failure,omitempty"` } -func (w *Webhook) Send(context *Context) error { +func (w *Webhook) Send(context *model.Request) error { switch { case context.Commit.Status == "Success" && w.Success: return w.send(context) @@ -26,7 +26,7 @@ func (w *Webhook) Send(context *Context) error { } // 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 := struct { Owner *model.User `json:"owner"`