From 25749802ac57346b4bac8d750e1dcbc5f29c6a36 Mon Sep 17 00:00:00 2001 From: Jordan Evans Date: Fri, 21 Feb 2014 16:10:04 -0800 Subject: [PATCH] add irc notify plugin It is based on github.com/fluffle/goirc --- Makefile | 1 + pkg/plugin/notify/irc.go | 79 +++++++++++++++++++++++++++++++ pkg/plugin/notify/notification.go | 6 +++ 3 files changed, 86 insertions(+) diff --git a/Makefile b/Makefile index 4be0fb07..977bc46e 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ deps: go get github.com/dchest/authcookie go get github.com/dchest/passwordreset go get github.com/dchest/uniuri + go get github.com/fluffle/goirc #go get github.com/dotcloud/docker/archive #go get github.com/dotcloud/docker/utils #go get github.com/dotcloud/docker/pkg/term diff --git a/pkg/plugin/notify/irc.go b/pkg/plugin/notify/irc.go index a3131f13..a9810425 100644 --- a/pkg/plugin/notify/irc.go +++ b/pkg/plugin/notify/irc.go @@ -1 +1,80 @@ package notify + +import ( + "fmt" + + irc "github.com/fluffle/goirc/client" +) + +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"` + SSL bool `yaml:"ssl,omitempty"` + ClientStarted bool + Client *irc.Conn +} + +func (i *IRC) Connect() { + c := irc.SimpleClient(i.Nick) + c.SSL = i.SSL + connected := make(chan bool) + c.AddHandler(irc.CONNECTED, + func(conn *irc.Conn, line *irc.Line) { + conn.Join(i.Channel) + connected <- true}) + c.Connect(i.Server) + <-connected + i.Client = c + i.ClientStarted = true +} + +func (i *IRC) Send(context *Context) error { + + if !i.ClientStarted { + i.Connect() + } + + switch { + case context.Commit.Status == "Started" && i.Started: + return i.sendStarted(context) + case context.Commit.Status == "Success" && i.Success: + return i.sendSuccess(context) + case context.Commit.Status == "Failure" && i.Failure: + return i.sendFailure(context) + } + return nil +} + +func (i *IRC) sendStarted(context *Context) error { + msg := fmt.Sprintf(ircStartedMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + i.send(i.Channel, msg) + return nil +} + +func (i *IRC) sendFailure(context *Context) error { + msg := fmt.Sprintf(ircFailureMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + i.send(i.Channel, msg) + return nil +} + +func (i *IRC) sendSuccess(context *Context) error { + msg := fmt.Sprintf(ircSuccessMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author) + i.send(i.Channel, msg) + return nil +} + + +func (i *IRC) send(channel string, message string) error { + i.Client.Notice(channel, message) + return nil +} diff --git a/pkg/plugin/notify/notification.go b/pkg/plugin/notify/notification.go index a4c6feab..2083d018 100644 --- a/pkg/plugin/notify/notification.go +++ b/pkg/plugin/notify/notification.go @@ -31,6 +31,7 @@ type Notification struct { Email *Email `yaml:"email,omitempty"` Webhook *Webhook `yaml:"webhook,omitempty"` Hipchat *Hipchat `yaml:"hipchat,omitempty"` + Irc *IRC `yaml:"irc,omitempty"` } func (n *Notification) Send(context *Context) error { @@ -49,5 +50,10 @@ func (n *Notification) Send(context *Context) error { n.Hipchat.Send(context) } + // send irc notifications + if n.Irc != nil { + n.Irc.Send(context) + } + return nil }