github.com/unclejack/drone@v0.2.1-0.20140918182345-831b034aa33b/pkg/plugin/notify/hipchat.go (about)

     1  package notify
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/andybons/hipchat"
     7  )
     8  
     9  const (
    10  	startedMessage = "Building %s, commit %s, author %s"
    11  	successMessage = "<b>Success</b> %s, commit %s, author %s"
    12  	failureMessage = "<b>Failed</b> %s, commit %s, author %s"
    13  )
    14  
    15  type Hipchat struct {
    16  	Room    string `yaml:"room,omitempty"`
    17  	Token   string `yaml:"token,omitempty"`
    18  	Started bool   `yaml:"on_started,omitempty"`
    19  	Success bool   `yaml:"on_success,omitempty"`
    20  	Failure bool   `yaml:"on_failure,omitempty"`
    21  }
    22  
    23  func (h *Hipchat) Send(context *Context) error {
    24  	switch {
    25  	case context.Commit.Status == "Started" && h.Started:
    26  		return h.sendStarted(context)
    27  	case context.Commit.Status == "Success" && h.Success:
    28  		return h.sendSuccess(context)
    29  	case context.Commit.Status == "Failure" && h.Failure:
    30  		return h.sendFailure(context)
    31  	}
    32  
    33  	return nil
    34  }
    35  
    36  func (h *Hipchat) sendStarted(context *Context) error {
    37  	msg := fmt.Sprintf(startedMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
    38  	return h.send(hipchat.ColorYellow, hipchat.FormatHTML, msg)
    39  }
    40  
    41  func (h *Hipchat) sendFailure(context *Context) error {
    42  	msg := fmt.Sprintf(failureMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
    43  	return h.send(hipchat.ColorRed, hipchat.FormatHTML, msg)
    44  }
    45  
    46  func (h *Hipchat) sendSuccess(context *Context) error {
    47  	msg := fmt.Sprintf(successMessage, context.Repo.Name, context.Commit.HashShort(), context.Commit.Author)
    48  	return h.send(hipchat.ColorGreen, hipchat.FormatHTML, msg)
    49  }
    50  
    51  // helper function to send Hipchat requests
    52  func (h *Hipchat) send(color, format, message string) error {
    53  	c := hipchat.Client{AuthToken: h.Token}
    54  	req := hipchat.MessageRequest{
    55  		RoomId:        h.Room,
    56  		From:          "Drone",
    57  		Message:       message,
    58  		Color:         color,
    59  		MessageFormat: format,
    60  		Notify:        true,
    61  	}
    62  
    63  	return c.PostMessage(req)
    64  }