From ac1c7bb9fa400ac793eeef98ad26c5d48b40f0ba Mon Sep 17 00:00:00 2001 From: nevalla Date: Tue, 26 Aug 2014 10:57:36 +0300 Subject: [PATCH] implement initial flowdock notification --- pkg/plugin/notify/flowdock.go | 89 +++++++++++++++++++++++++++++++ pkg/plugin/notify/notification.go | 16 ++++-- 2 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 pkg/plugin/notify/flowdock.go diff --git a/pkg/plugin/notify/flowdock.go b/pkg/plugin/notify/flowdock.go new file mode 100644 index 000000000..8d56014c3 --- /dev/null +++ b/pkg/plugin/notify/flowdock.go @@ -0,0 +1,89 @@ +package notify + +import ( + "fmt" + "strings" + "net/url" + "github.com/stvp/flowdock" +) + +const ( + flowdockStartedSubject = "Building %s (%s)" + flowdockSuccessSubject = "Build: %s (%s) is SUCCESS" + flowdockFailureSubject = "Build: %s (%s) is FAILED" + flowdockMessage = "

%s

\nBuild: %s
\nResult: %s
\nAuthor: %s
Commit: %s
\nRepository Url: %s" + flowdockBuildOkEmail = "build+ok@flowdock.com" + flowdockBuildFailEmail = "build+fail@flowdock.com"; +) + +type Flowdock struct { + Token string `yaml:"token,omitempty"` + Source string `yaml:"source,omitempty"` + Tags string `yaml:"tags,omitempty"` + Started bool `yaml:"on_started,omitempty"` + Success bool `yaml:"on_success,omitempty"` + Failure bool `yaml:"on_failure,omitempty"` +} + +func (f *Flowdock) Send(context *Context) error { + switch { + case context.Commit.Status == "Started" && f.Started: + return f.sendStarted(context) + case context.Commit.Status == "Success" && f.Success: + return f.sendSuccess(context) + case context.Commit.Status == "Failure" && f.Failure: + return f.sendFailure(context) + } + + return nil +} + +func getBuildUrl(context *Context) string { + branchQuery := url.Values{} + if context.Commit.Branch != "" { + branchQuery.Set("branch", context.Commit.Branch) + } + + return fmt.Sprintf("%s/%s/commit/%s?%s", context.Host, context.Repo.Slug, context.Commit.Hash, branchQuery.Encode()) +} + +func getRepoUrl(context *Context) string { + return fmt.Sprintf("%s/%s", context.Host, context.Repo.Slug) +} + +func getMessage(context *Context) string { + buildUrl := fmt.Sprintf("%s", getBuildUrl(context), context.Commit.HashShort()) + return fmt.Sprintf(flowdockMessage, context.Repo.Name, buildUrl, context.Commit.Status, context.Commit.Author, context.Commit.Message, getRepoUrl(context)) +} + +func (f *Flowdock) sendStarted(context *Context) error { + fromAddress := context.Commit.Author + subject := fmt.Sprintf(flowdockStartedSubject, context.Repo.Name, context.Commit.Branch) + msg := getMessage(context) + tags := strings.Split(f.Tags, ",") + return f.send(fromAddress, subject, msg, tags) +} + +func (f *Flowdock) sendFailure(context *Context) error { + fromAddress := flowdockBuildFailEmail + tags := strings.Split(f.Tags, ",") + subject := fmt.Sprintf(flowdockFailureSubject, context.Repo.Name, context.Commit.Branch) + msg := getMessage(context) + return f.send(fromAddress, subject, msg, tags) +} + +func (f *Flowdock) sendSuccess(context *Context) error { + fromAddress := flowdockBuildOkEmail + tags := strings.Split(f.Tags, ",") + subject := fmt.Sprintf(flowdockSuccessSubject, context.Repo.Name, context.Commit.Branch) + msg := getMessage(context) + return f.send(fromAddress, subject, msg, tags) +} + +// helper function to send Flowdock requests +func (f *Flowdock) send(fromAddress, subject, message string, tags []string) error { + + c := flowdock.Client{Token: f.Token, Source: f.Source, FromName: "drone.io", FromAddress: fromAddress, Tags: tags} + + return c.Inbox(subject, message) +} diff --git a/pkg/plugin/notify/notification.go b/pkg/plugin/notify/notification.go index 31b2ce3ef..c0df619ef 100644 --- a/pkg/plugin/notify/notification.go +++ b/pkg/plugin/notify/notification.go @@ -28,11 +28,12 @@ type Sender interface { // for notifying a user, or group of users, // when their Build has completed. type Notification struct { - 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 `yaml:"email,omitempty"` + Webhook *Webhook `yaml:"webhook,omitempty"` + Hipchat *Hipchat `yaml:"hipchat,omitempty"` + Irc *IRC `yaml:"irc,omitempty"` + Slack *Slack `yaml:"slack,omitempty"` + Flowdock *Flowdock `yaml:"flowdock,omitempty"` } func (n *Notification) Send(context *Context) error { @@ -61,5 +62,10 @@ func (n *Notification) Send(context *Context) error { n.Slack.Send(context) } + // send flowdock notifications + if n.Flowdock != nil { + n.Flowdock.Send(context) + } + return nil }