Skip to content

Commit

Permalink
Merge pull request harness#755 from Bugagazavr/katoim
Browse files Browse the repository at this point in the history
Added KatoIM support
  • Loading branch information
bradrydzewski committed Dec 13, 2014
2 parents f79e091 + 3d244fc commit e70c8d5
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
139 changes: 139 additions & 0 deletions plugin/notify/katoim/katoim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package katoim

import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/drone/drone/shared/model"
)

const (
katoimEndpoint = "https://api.kato.im/rooms/%s/simple"
katoimStartedMessage = "*Building* %s, commit [%s](%s), author %s"
katoimSuccessMessage = "*Success* %s, commit [%s](%s), author %s"
katoimFailureMessage = "*Failed* %s, commit [%s](%s), author %s"

NotifyTrue = "true"
NotifyFalse = "false"
NotifyOn = "on"
NotifyOff = "off"
NotifyNever = "never"
NotifyAlways = "always"
)

type KatoIM struct {
RoomID string `yaml:"room_id,omitempty"`
Started string `yaml:"on_started,omitempty"`
Success string `yaml:"on_success,omitempty"`
Failure string `yaml:"on_failure,omitempty"`
}

func (k *KatoIM) Send(context *model.Request) error {
switch {
case context.Commit.Status == model.StatusStarted:
return k.sendStarted(context)
case context.Commit.Status == model.StatusSuccess:
return k.sendSuccess(context)
case context.Commit.Status == model.StatusFailure:
return k.sendFailure(context)
}

return nil
}

func (k *KatoIM) getMessage(context *model.Request, message string) string {
url := getBuildUrl(context)
return fmt.Sprintf(message, context.Repo.Name, context.Commit.ShaShort(), url, context.Commit.Author)
}

// sendStarted disabled by default
func (k *KatoIM) sendStarted(context *model.Request) error {
switch k.Started {
case NotifyTrue, NotifyAlways, NotifyOn:
return k.send(k.getMessage(context, katoimStartedMessage), "yellow")
default:
return nil
}
}

// sendSuccess enabled by default
func (k *KatoIM) sendSuccess(context *model.Request) error {
switch k.Success {
case NotifyFalse, NotifyNever, NotifyOff:
return nil
case NotifyTrue, NotifyAlways, NotifyOn, "":
return k.send(k.getMessage(context, katoimSuccessMessage), "green")
default:
return nil
}
}

// sendFailure enabled by default
func (k *KatoIM) sendFailure(context *model.Request) error {
switch k.Failure {
case NotifyFalse, NotifyNever, NotifyOff:
return nil
case NotifyTrue, NotifyAlways, NotifyOn, "":
return k.send(k.getMessage(context, katoimFailureMessage), "red")
default:
return nil
}
}

// helper function to send HTTP requests
func (k *KatoIM) send(msg, color string) error {
// data will get posted in this format
data := struct {
Text string `json:"text"`
Color string `json:"color"`
Renderer string `json:"renderer"`
From string `json:"from"`
}{msg, color, "markdown", "Drone"}

// data json encoded
payload, err := json.Marshal(data)
if err != nil {
return err
}

// send payload
url := fmt.Sprintf(katoimEndpoint, k.RoomID)

// create headers
headers := make(map[string]string)
headers["Accept"] = "application/json"

return sendJson(url, payload, headers)
}

func getBuildUrl(context *model.Request) string {
return fmt.Sprintf("%s/%s/%s/%s/%s/%s", context.Host, context.Repo.Host, context.Repo.Owner, context.Repo.Name, context.Commit.Branch, context.Commit.Sha)
}

// helper fuction to sent HTTP Post requests
// with JSON data as the payload.
func sendJson(url string, payload []byte, headers map[string]string) error {
client := &http.Client{}
buf := bytes.NewBuffer(payload)

req, err := http.NewRequest("POST", url, buf)
if err != nil {
return err
}

req.Header.Set("Content-Type", "application/json")
if headers != nil {
for k, v := range headers {
req.Header.Add(k, v)
}
}

resp, err := client.Do(req)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
10 changes: 10 additions & 0 deletions plugin/notify/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/drone/drone/plugin/notify/email"
"github.com/drone/drone/plugin/notify/github"
"github.com/drone/drone/plugin/notify/irc"
"github.com/drone/drone/plugin/notify/katoim"
"github.com/drone/drone/plugin/notify/webhook"
"github.com/drone/drone/shared/model"
)
Expand All @@ -28,6 +29,7 @@ type Notification struct {
Slack *Slack `yaml:"slack,omitempty"`
Gitter *Gitter `yaml:"gitter,omitempty"`
Flowdock *Flowdock `yaml:"flowdock,omitempty"`
KatoIM *katoim.KatoIM `yaml:"katoim,omitempty"`

GitHub github.GitHub `yaml:"--"`
}
Expand Down Expand Up @@ -89,6 +91,14 @@ func (n *Notification) Send(context *model.Request) error {
}
}

// send kato-im notifications
if n.KatoIM != nil {
err := n.KatoIM.Send(context)
if err != nil {
log.Println(err)
}
}

// send email notifications
// TODO (bradrydzewski) need to improve this code
githubStatus := new(github.GitHub)
Expand Down

0 comments on commit e70c8d5

Please sign in to comment.