From 29664be7b87b81ae0d0b12b0adc741f66463e9ae Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Wed, 26 Mar 2014 18:43:40 +0800 Subject: [PATCH 1/4] Initial slack notification work --- pkg/plugin/notify/notification.go | 6 +++ pkg/plugin/notify/slack.go | 87 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 pkg/plugin/notify/slack.go diff --git a/pkg/plugin/notify/notification.go b/pkg/plugin/notify/notification.go index 2f0b43fa9..31b2ce3ef 100644 --- a/pkg/plugin/notify/notification.go +++ b/pkg/plugin/notify/notification.go @@ -32,6 +32,7 @@ type Notification struct { Webhook *Webhook `yaml:"webhook,omitempty"` Hipchat *Hipchat `yaml:"hipchat,omitempty"` Irc *IRC `yaml:"irc,omitempty"` + Slack *Slack `yaml:"slack,omitempty"` } func (n *Notification) Send(context *Context) error { @@ -55,5 +56,10 @@ func (n *Notification) Send(context *Context) error { n.Irc.Send(context) } + // send slack notifications + if n.Slack != nil { + n.Slack.Send(context) + } + return nil } diff --git a/pkg/plugin/notify/slack.go b/pkg/plugin/notify/slack.go new file mode 100644 index 000000000..edf68966a --- /dev/null +++ b/pkg/plugin/notify/slack.go @@ -0,0 +1,87 @@ +package notify + +import ( + "bytes" + "encoding/json" + "net/http" + + "github.com/drone/drone/pkg/model" +) + +const ( + slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s" + startedMessage = "Building %s, commit %s, author %s" + successMessage = "Success %s, commit %s, author %s" + failureMessage = "Failed %s, commit %s, author %s" +) + +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"` +} + +func (s *Slack) Send(context *Context) error { + 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 +} + +func (s *Slack) sendStarted(context *Context) error { + msg := fmt.Sprintf(startedMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + return s.send(msg) +} + +func (s *Slack) sendSuccess(context *Context) error { + msg := fmt.Sprintf(successMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + return s.send(msg) +} + +func (s *Slack) sendFailure(context *Context) error { + msg := fmt.Sprintf(failureMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + return s.send(msg) +} + +// helper function to send HTTP requests +func (s *Slack) send(msg string) error { + // data will get posted in this format + data := struct { + Channel string `json:"channel"` + Username string `json:"username"` + Text string `json:"text"` + }{s.Channel, s.Username, msg} + + // data json encoded + payload, err := json.Marshal(data) + if err != nil { + return err + } + + // send payload + url = fmt.Sprintf(slackEndpoint, s.Team, s.Token) + go sendJson(url, payload) + + 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() +} From d7338cfdb29121417e86ce45499c1bb1aa0ad3b0 Mon Sep 17 00:00:00 2001 From: vagrant Date: Sun, 6 Apr 2014 09:05:30 +0000 Subject: [PATCH 2/4] It builds --- pkg/plugin/notify/slack.go | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/pkg/plugin/notify/slack.go b/pkg/plugin/notify/slack.go index edf68966a..060017bae 100644 --- a/pkg/plugin/notify/slack.go +++ b/pkg/plugin/notify/slack.go @@ -1,18 +1,17 @@ package notify import ( - "bytes" "encoding/json" - "net/http" + "fmt" - "github.com/drone/drone/pkg/model" + //"github.com/drone/drone/pkg/model" ) const ( slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s" - startedMessage = "Building %s, commit %s, author %s" - successMessage = "Success %s, commit %s, author %s" - failureMessage = "Failed %s, commit %s, author %s" + slackStartedMessage = "Building %s, commit %s, author %s" + slackSuccessMessage = "Success %s, commit %s, author %s" + slackFailureMessage = "Failed %s, commit %s, author %s" ) type Slack struct { @@ -39,17 +38,17 @@ func (s *Slack) Send(context *Context) error { } func (s *Slack) sendStarted(context *Context) error { - msg := fmt.Sprintf(startedMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + msg := fmt.Sprintf(slackStartedMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) return s.send(msg) } func (s *Slack) sendSuccess(context *Context) error { - msg := fmt.Sprintf(successMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + msg := fmt.Sprintf(slackSuccessMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) return s.send(msg) } func (s *Slack) sendFailure(context *Context) error { - msg := fmt.Sprintf(failureMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + msg := fmt.Sprintf(slackFailureMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) return s.send(msg) } @@ -69,19 +68,9 @@ func (s *Slack) send(msg string) error { } // send payload - url = fmt.Sprintf(slackEndpoint, s.Team, s.Token) + url := fmt.Sprintf(slackEndpoint, s.Team, s.Token) go sendJson(url, payload) 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() -} From 45324bc65a97848e4fe380050dd4b61eec136b14 Mon Sep 17 00:00:00 2001 From: Marcus Ramberg Date: Sun, 6 Apr 2014 22:04:13 +0200 Subject: [PATCH 3/4] Slack uses markdown, not html --- pkg/plugin/notify/slack.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/plugin/notify/slack.go b/pkg/plugin/notify/slack.go index 060017bae..14c8f01d7 100644 --- a/pkg/plugin/notify/slack.go +++ b/pkg/plugin/notify/slack.go @@ -8,10 +8,10 @@ import ( ) const ( - slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s" + slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s" slackStartedMessage = "Building %s, commit %s, author %s" - slackSuccessMessage = "Success %s, commit %s, author %s" - slackFailureMessage = "Failed %s, commit %s, author %s" + slackSuccessMessage = "*Success* %s, commit %s, author %s" + slackFailureMessage = "*Failed* %s, commit %s, author %s" ) type Slack struct { @@ -73,4 +73,3 @@ func (s *Slack) send(msg string) error { return nil } - From cecbbbbc764d0c6128e5d22c0b4c8385a7288d19 Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Tue, 8 Apr 2014 09:08:51 +0800 Subject: [PATCH 4/4] Minor message change --- pkg/plugin/notify/slack.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/plugin/notify/slack.go b/pkg/plugin/notify/slack.go index 14c8f01d7..d02e457fc 100644 --- a/pkg/plugin/notify/slack.go +++ b/pkg/plugin/notify/slack.go @@ -9,7 +9,7 @@ import ( const ( slackEndpoint = "https://%s.slack.com/services/hooks/incoming-webhook?token=%s" - slackStartedMessage = "Building %s, commit %s, author %s" + slackStartedMessage = "*Building* %s, commit %s, author %s" slackSuccessMessage = "*Success* %s, commit %s, author %s" slackFailureMessage = "*Failed* %s, commit %s, author %s" )