moved IRC and Webhooks to their own packages

updated email template to include link to build
updated readme to include correct badge
This commit is contained in:
Brad Rydzewski 2014-09-09 23:19:04 -07:00
parent bf7f9c823c
commit 6f3997f4e1
8 changed files with 102 additions and 78 deletions

View file

@ -1,4 +1,4 @@
[![Build Status](http://test.drone.io/v1/badge/github.com/drone/drone/status.svg?branch=master)](http://test.drone.io/github.com/drone/drone) [![Build Status](http://test.drone.io/v1/badge/github.com/drone/drone/status.svg?branch=exp)](http://test.drone.io/github.com/drone/drone)
[![GoDoc](https://godoc.org/github.com/drone/drone?status.png)](https://godoc.org/github.com/drone/drone) [![GoDoc](https://godoc.org/github.com/drone/drone?status.png)](https://godoc.org/github.com/drone/drone)

View file

@ -15,7 +15,10 @@ Content-Type: text/html; charset="UTF-8"
// default success email template // default success email template
var successTemplate = template.Must(template.New("_").Parse(` var successTemplate = template.Must(template.New("_").Parse(`
<p><b>Build was Successful</b></p> <p>
<b>Build was Successful</b>
(<a href="{{.Host}}/{{.Repo.Host}}/{{.Repo.Owner}}/{{.Repo.Name}}/{{.Commit.Branch}}/{{.Commit.Sha}}">see results</a>)
</p>
<p>Repository : {{.Repo.Owner}}/{{.Repo.Name}}</p> <p>Repository : {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit : {{.Commit.ShaShort}}</p> <p>Commit : {{.Commit.ShaShort}}</p>
<p>Author : {{.Commit.Author}}</p> <p>Author : {{.Commit.Author}}</p>
@ -26,7 +29,10 @@ var successTemplate = template.Must(template.New("_").Parse(`
// default failure email template // default failure email template
var failureTemplate = template.Must(template.New("_").Parse(` var failureTemplate = template.Must(template.New("_").Parse(`
<p><b>Build Failed</b></p> <p>
<b>Build Failed</b>
(<a href="{{.Host}}/{{.Repo.Host}}/{{.Repo.Owner}}/{{.Repo.Name}}/{{.Commit.Branch}}/{{.Commit.Sha}}">see results</a>)
</p>
<p>Repository : {{.Repo.Owner}}/{{.Repo.Name}}</p> <p>Repository : {{.Repo.Owner}}/{{.Repo.Name}}</p>
<p>Commit : {{.Commit.ShaShort}}</p> <p>Commit : {{.Commit.ShaShort}}</p>
<p>Author : {{.Commit.Author}}</p> <p>Author : {{.Commit.Author}}</p>

View file

@ -1,63 +0,0 @@
package notify
import (
"fmt"
"github.com/drone/drone/shared/model"
"github.com/thoj/go-ircevent"
)
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"`
}
func (i *IRC) Send(req *model.Request) error {
switch {
case req.Commit.Status == "Started" && i.Started != nil && *i.Started == true:
return i.sendStarted(req)
case req.Commit.Status == "Success" && i.Success != nil && *i.Success == true:
return i.sendSuccess(req)
case req.Commit.Status == "Failure" && i.Failure != nil && *i.Failure == true:
return i.sendFailure(req)
}
return nil
}
func (i *IRC) sendStarted(req *model.Request) error {
msg := fmt.Sprintf(ircStartedMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
return i.send(i.Channel, msg)
}
func (i *IRC) sendFailure(req *model.Request) error {
msg := fmt.Sprintf(ircFailureMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
return i.send(i.Channel, msg)
}
func (i *IRC) sendSuccess(req *model.Request) error {
msg := fmt.Sprintf(ircSuccessMessage, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
return i.send(i.Channel, msg)
}
// send is a helper function that will send notice messages
// to the connected IRC client
func (i *IRC) send(channel string, message string) error {
client := irc.IRC(i.Nick, i.Nick)
if client != nil {
return fmt.Errorf("Error creating IRC client")
}
defer client.Disconnect()
client.Connect(i.Server)
client.Notice(channel, message)
return nil
}

63
plugin/notify/irc/irc.go Normal file
View file

@ -0,0 +1,63 @@
package irc
import (
"fmt"
"github.com/drone/drone/shared/model"
"github.com/thoj/go-ircevent"
)
const (
MessageStarted = "Building: %s, commit %s, author %s"
MessageSuccess = "Success: %s, commit %s, author %s"
MessageFailure = "Failed: %s, commit %s, author %s"
)
type IRC struct {
Channel string
Nick string
Server string
Started *bool `yaml:"on_started,omitempty"`
Success *bool `yaml:"on_success,omitempty"`
Failure *bool `yaml:"on_failure,omitempty"`
}
func (i *IRC) Send(req *model.Request) error {
switch {
case req.Commit.Status == model.StatusStarted && i.Started != nil && *i.Started == true:
return i.sendStarted(req)
case req.Commit.Status == model.StatusSuccess && i.Success != nil && *i.Success == true:
return i.sendSuccess(req)
case req.Commit.Status == model.StatusFailure && i.Failure != nil && *i.Failure == true:
return i.sendFailure(req)
}
return nil
}
func (i *IRC) sendStarted(req *model.Request) error {
msg := fmt.Sprintf(MessageStarted, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
return i.send(i.Channel, msg)
}
func (i *IRC) sendFailure(req *model.Request) error {
msg := fmt.Sprintf(MessageFailure, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
return i.send(i.Channel, msg)
}
func (i *IRC) sendSuccess(req *model.Request) error {
msg := fmt.Sprintf(MessageSuccess, req.Repo.Name, req.Commit.ShaShort(), req.Commit.Author)
return i.send(i.Channel, msg)
}
// send is a helper function that will send notice messages
// to the connected IRC client
func (i *IRC) send(channel string, message string) error {
client := irc.IRC(i.Nick, i.Nick)
if client != nil {
return fmt.Errorf("Error creating IRC client")
}
defer client.Disconnect()
client.Connect(i.Server)
client.Notice(channel, message)
return nil
}

View file

@ -5,6 +5,8 @@ import (
"github.com/drone/drone/plugin/notify/email" "github.com/drone/drone/plugin/notify/email"
"github.com/drone/drone/plugin/notify/github" "github.com/drone/drone/plugin/notify/github"
"github.com/drone/drone/plugin/notify/irc"
"github.com/drone/drone/plugin/notify/webhook"
"github.com/drone/drone/shared/model" "github.com/drone/drone/shared/model"
) )
@ -17,9 +19,9 @@ type Sender interface {
// when their Build has completed. // when their Build has completed.
type Notification struct { type Notification struct {
Email *email.Email `yaml:"email,omitempty"` Email *email.Email `yaml:"email,omitempty"`
Webhook *Webhook `yaml:"webhook,omitempty"` Webhook *webhook.Webhook `yaml:"webhook,omitempty"`
Hipchat *Hipchat `yaml:"hipchat,omitempty"` Hipchat *Hipchat `yaml:"hipchat,omitempty"`
Irc *IRC `yaml:"irc,omitempty"` Irc *irc.IRC `yaml:"irc,omitempty"`
Slack *Slack `yaml:"slack,omitempty"` Slack *Slack `yaml:"slack,omitempty"`
GitHub github.GitHub `yaml:"--"` GitHub github.GitHub `yaml:"--"`

View file

@ -1,8 +1,10 @@
package notify package notify
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"github.com/drone/drone/shared/model" "github.com/drone/drone/shared/model"
) )
@ -79,3 +81,14 @@ func (s *Slack) send(msg string) error {
return nil 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()
}

View file

@ -1,4 +1,4 @@
package notify package webhook
import ( import (
"bytes" "bytes"
@ -10,15 +10,15 @@ import (
type Webhook struct { type Webhook struct {
URL []string `yaml:"urls,omitempty"` URL []string `yaml:"urls,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"`
} }
func (w *Webhook) Send(context *model.Request) error { func (w *Webhook) Send(context *model.Request) error {
switch { switch {
case context.Commit.Status == "Success" && w.Success: case context.Commit.Status == model.StatusSuccess && w.Success != nil && *w.Success == true:
return w.send(context) return w.send(context)
case context.Commit.Status == "Failure" && w.Failure: case context.Commit.Status == model.StatusFailure && w.Failure != nil && *w.Success == true:
return w.send(context) return w.send(context)
} }

View file

@ -98,7 +98,10 @@ func (w *worker) Execute(r *model.Request) {
// parse the parameters and build script. The script has already // parse the parameters and build script. The script has already
// been parsed in the hook, so we can be confident it will succeed. // been parsed in the hook, so we can be confident it will succeed.
// that being said, we should clean this up // that being said, we should clean this up
params, _ := r.Repo.ParamMap() params, err := r.Repo.ParamMap()
if err != nil {
log.Printf("Error parsing PARAMS for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
}
script, err := script.ParseBuild(r.Commit.Config, params) script, err := script.ParseBuild(r.Commit.Config, params)
if err != nil { if err != nil {
log.Printf("Error parsing YAML for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error()) log.Printf("Error parsing YAML for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())