mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-25 01:40:30 +00:00
fixed IRC plugin
This commit is contained in:
parent
5c26f79940
commit
5eb0aab517
2 changed files with 32 additions and 54 deletions
|
@ -1,4 +1,4 @@
|
||||||
image: go1.2
|
image: go1.3
|
||||||
git:
|
git:
|
||||||
path: github.com/drone/drone
|
path: github.com/drone/drone
|
||||||
env:
|
env:
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/drone/drone/shared/model"
|
"github.com/drone/drone/shared/model"
|
||||||
irc "github.com/fluffle/goirc/client"
|
"github.com/thoj/go-ircevent"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -20,66 +20,44 @@ type IRC struct {
|
||||||
Started bool `yaml:"on_started,omitempty"`
|
Started bool `yaml:"on_started,omitempty"`
|
||||||
Success bool `yaml:"on_success,omitempty"`
|
Success bool `yaml:"on_success,omitempty"`
|
||||||
Failure bool `yaml:"on_failure,omitempty"`
|
Failure bool `yaml:"on_failure,omitempty"`
|
||||||
SSL bool `yaml:"ssl,omitempty"`
|
|
||||||
ClientStarted bool
|
|
||||||
Client *irc.Conn
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRC) Connect() {
|
func (i *IRC) Send(req *model.Request) error {
|
||||||
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.ClientStarted = true
|
|
||||||
i.Client = c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *IRC) Send(context *model.Request) error {
|
|
||||||
switch {
|
switch {
|
||||||
case context.Commit.Status == "Started" && i.Started:
|
case req.Commit.Status == "Started" && i.Started:
|
||||||
return i.sendStarted(context)
|
return i.sendStarted(req)
|
||||||
case context.Commit.Status == "Success" && i.Success:
|
case req.Commit.Status == "Success" && i.Success:
|
||||||
return i.sendSuccess(context)
|
return i.sendSuccess(req)
|
||||||
case context.Commit.Status == "Failure" && i.Failure:
|
case req.Commit.Status == "Failure" && i.Failure:
|
||||||
return i.sendFailure(context)
|
return i.sendFailure(req)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRC) sendStarted(context *model.Request) error {
|
func (i *IRC) sendStarted(req *model.Request) error {
|
||||||
msg := fmt.Sprintf(ircStartedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
msg := fmt.Sprintf(ircStartedMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
|
||||||
i.send(i.Channel, msg)
|
return i.send(i.Channel, msg)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRC) sendFailure(context *model.Request) error {
|
func (i *IRC) sendFailure(req *model.Request) error {
|
||||||
msg := fmt.Sprintf(ircFailureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
msg := fmt.Sprintf(ircFailureMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
|
||||||
i.send(i.Channel, msg)
|
return i.send(i.Channel, msg)
|
||||||
if i.ClientStarted {
|
|
||||||
i.Client.Quit()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRC) sendSuccess(context *model.Request) error {
|
func (i *IRC) sendSuccess(req *model.Request) error {
|
||||||
msg := fmt.Sprintf(ircSuccessMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
|
msg := fmt.Sprintf(ircSuccessMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
|
||||||
i.send(i.Channel, msg)
|
return i.send(i.Channel, msg)
|
||||||
if i.ClientStarted {
|
|
||||||
i.Client.Quit()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// send is a helper function that will send notice messages
|
||||||
|
// to the connected IRC client
|
||||||
func (i *IRC) send(channel string, message string) error {
|
func (i *IRC) send(channel string, message string) error {
|
||||||
if !i.ClientStarted {
|
client := irc.IRC(i.Nick, i.Nick)
|
||||||
i.Connect()
|
if client != nil {
|
||||||
|
return fmt.Errorf("Error creating IRC client")
|
||||||
}
|
}
|
||||||
i.Client.Notice(channel, message)
|
defer client.Disconnect()
|
||||||
|
client.Connect(i.Server)
|
||||||
|
client.Notice(channel, message)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue