Merge pull request #973 from 907th/hipchat_api_v2

Use HipChat API ver 2 for notifications
This commit is contained in:
Brad Rydzewski 2015-04-16 23:38:18 -07:00
commit af9c1e9bd7
2 changed files with 75 additions and 46 deletions

View file

@ -2,8 +2,10 @@ package notify
import ( import (
"fmt" "fmt"
"net/http"
"net/url"
"strconv"
"github.com/andybons/hipchat"
"github.com/drone/drone/shared/model" "github.com/drone/drone/shared/model"
) )
@ -21,12 +23,8 @@ type Hipchat struct {
Failure bool `yaml:"on_failure,omitempty"` Failure bool `yaml:"on_failure,omitempty"`
} }
type HipchatClient interface {
PostMessage(req hipchat.MessageRequest) error
}
func (h *Hipchat) Send(context *model.Request) error { func (h *Hipchat) Send(context *model.Request) error {
client := &hipchat.Client{AuthToken: h.Token} client := new(HipchatSimpleHTTPClient)
return h.SendWithClient(client, context) return h.SendWithClient(client, context)
} }
@ -39,7 +37,6 @@ func (h *Hipchat) SendWithClient(client HipchatClient, context *model.Request) e
case context.Commit.Status == "Failure" && h.Failure: case context.Commit.Status == "Failure" && h.Failure:
return h.sendFailure(client, context) return h.sendFailure(client, context)
} }
return nil return nil
} }
@ -51,29 +48,65 @@ func (h *Hipchat) buildLink(context *model.Request) string {
func (h *Hipchat) sendStarted(client HipchatClient, context *model.Request) error { func (h *Hipchat) sendStarted(client HipchatClient, context *model.Request) error {
msg := fmt.Sprintf(startedMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author, context.Commit.Message) msg := fmt.Sprintf(startedMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author, context.Commit.Message)
return h.send(client, hipchat.ColorYellow, hipchat.FormatHTML, msg, false) req := HipchatMessageRequest{
RoomId: h.Room,
AuthToken: h.Token,
Color: "yellow",
Message: msg,
Notify: false,
}
return client.PostMessage(req)
} }
func (h *Hipchat) sendFailure(client HipchatClient, context *model.Request) error { func (h *Hipchat) sendFailure(client HipchatClient, context *model.Request) error {
msg := fmt.Sprintf(failureMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author) msg := fmt.Sprintf(failureMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author)
return h.send(client, hipchat.ColorRed, hipchat.FormatHTML, msg, true) req := HipchatMessageRequest{
RoomId: h.Room,
AuthToken: h.Token,
Color: "red",
Message: msg,
Notify: true,
}
return client.PostMessage(req)
} }
func (h *Hipchat) sendSuccess(client HipchatClient, context *model.Request) error { func (h *Hipchat) sendSuccess(client HipchatClient, context *model.Request) error {
msg := fmt.Sprintf(successMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author) msg := fmt.Sprintf(successMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author)
return h.send(client, hipchat.ColorGreen, hipchat.FormatHTML, msg, false) req := HipchatMessageRequest{
} RoomId: h.Room,
AuthToken: h.Token,
// helper function to send Hipchat requests Color: "green",
func (h *Hipchat) send(client HipchatClient, color, format, message string, notify bool) error { Message: msg,
req := hipchat.MessageRequest{ Notify: false,
RoomId: h.Room,
From: "Drone",
Message: message,
Color: color,
MessageFormat: format,
Notify: notify,
} }
return client.PostMessage(req) return client.PostMessage(req)
} }
// HipChat client
type HipchatClient interface {
PostMessage(req HipchatMessageRequest) error
}
type HipchatMessageRequest struct {
RoomId string
Color string
Message string
Notify bool
AuthToken string
}
type HipchatSimpleHTTPClient struct{}
func (*HipchatSimpleHTTPClient) PostMessage(req HipchatMessageRequest) error {
hipchat_uri := fmt.Sprintf("https://api.hipchat.com/v2/room/%s/notification", req.RoomId)
_, err := http.PostForm(hipchat_uri,
url.Values{
"color": {req.Color},
"message": {req.Message},
"notify": {strconv.FormatBool(req.Notify)},
"message_format": {"html"},
"auth_token": {req.AuthToken},
})
return err
}

View file

@ -3,15 +3,14 @@ package notify
import ( import (
"testing" "testing"
"github.com/andybons/hipchat"
"github.com/drone/drone/shared/model" "github.com/drone/drone/shared/model"
) )
type MockHipchatClient struct { type MockHipchatClient struct {
Request hipchat.MessageRequest Request HipchatMessageRequest
} }
func (c *MockHipchatClient) PostMessage(req hipchat.MessageRequest) error { func (c *MockHipchatClient) PostMessage(req HipchatMessageRequest) error {
c.Request = req c.Request = req
return nil return nil
} }
@ -49,13 +48,12 @@ func Test_SendStarted(t *testing.T) {
request.Commit.Status = "Started" request.Commit.Status = "Started"
subject.SendWithClient(client, request) subject.SendWithClient(client, request)
expected := hipchat.MessageRequest{ expected := HipchatMessageRequest{
RoomId: "SampleRoom", RoomId: "SampleRoom",
From: "Drone", AuthToken: "foo",
Message: "Building <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User <br> - Test Commit", Color: "yellow",
Color: hipchat.ColorYellow, Message: "Building <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User <br> - Test Commit",
MessageFormat: hipchat.FormatHTML, Notify: false,
Notify: false,
} }
if client.Request != expected { if client.Request != expected {
@ -67,13 +65,12 @@ func Test_SendSuccess(t *testing.T) {
request.Commit.Status = "Success" request.Commit.Status = "Success"
subject.SendWithClient(client, request) subject.SendWithClient(client, request)
expected := hipchat.MessageRequest{ expected := HipchatMessageRequest{
RoomId: "SampleRoom", RoomId: "SampleRoom",
From: "Drone", AuthToken: "foo",
Message: "Success <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User", Color: "green",
Color: hipchat.ColorGreen, Message: "Success <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User",
MessageFormat: hipchat.FormatHTML, Notify: false,
Notify: false,
} }
if client.Request != expected { if client.Request != expected {
@ -85,13 +82,12 @@ func Test_SendFailure(t *testing.T) {
request.Commit.Status = "Failure" request.Commit.Status = "Failure"
subject.SendWithClient(client, request) subject.SendWithClient(client, request)
expected := hipchat.MessageRequest{ expected := HipchatMessageRequest{
RoomId: "SampleRoom", RoomId: "SampleRoom",
From: "Drone", AuthToken: "foo",
Message: "Failed <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User", Color: "red",
Color: hipchat.ColorRed, Message: "Failed <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User",
MessageFormat: hipchat.FormatHTML, Notify: true,
Notify: true,
} }
if client.Request != expected { if client.Request != expected {