Merge pull request #113 from jordane/jordane/add_irc_plugin

add irc notify plugin
This commit is contained in:
Brad Rydzewski 2014-02-22 15:33:58 -08:00
commit 24df6277b1
3 changed files with 86 additions and 0 deletions

View file

@ -15,6 +15,7 @@ deps:
go get github.com/dchest/authcookie go get github.com/dchest/authcookie
go get github.com/dchest/passwordreset go get github.com/dchest/passwordreset
go get github.com/dchest/uniuri 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/archive
#go get github.com/dotcloud/docker/utils #go get github.com/dotcloud/docker/utils
#go get github.com/dotcloud/docker/pkg/term #go get github.com/dotcloud/docker/pkg/term

View file

@ -1 +1,80 @@
package notify 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
}

View file

@ -31,6 +31,7 @@ type Notification struct {
Email *Email `yaml:"email,omitempty"` Email *Email `yaml:"email,omitempty"`
Webhook *Webhook `yaml:"webhook,omitempty"` Webhook *Webhook `yaml:"webhook,omitempty"`
Hipchat *Hipchat `yaml:"hipchat,omitempty"` Hipchat *Hipchat `yaml:"hipchat,omitempty"`
Irc *IRC `yaml:"irc,omitempty"`
} }
func (n *Notification) Send(context *Context) error { func (n *Notification) Send(context *Context) error {
@ -49,5 +50,10 @@ func (n *Notification) Send(context *Context) error {
n.Hipchat.Send(context) n.Hipchat.Send(context)
} }
// send irc notifications
if n.Irc != nil {
n.Irc.Send(context)
}
return nil return nil
} }