From 8bcb5d9c52df2339dcf1da460e13ca2f1101720c Mon Sep 17 00:00:00 2001 From: Alexey Chernenkov Date: Thu, 9 Apr 2015 21:17:55 +0500 Subject: [PATCH] Use HipChat notification API ver 2 HipChat API v1 is deprecated, but github.com/andybons/hipchat (used by Drone) does not support v2. So I've implemented v2 using native http.PostForm. --- plugin/notify/hipchat.go | 77 +++++++++++++++++++++++++---------- plugin/notify/hipchat_test.go | 44 +++++++++----------- 2 files changed, 75 insertions(+), 46 deletions(-) diff --git a/plugin/notify/hipchat.go b/plugin/notify/hipchat.go index d1d7f140..d1c70d76 100644 --- a/plugin/notify/hipchat.go +++ b/plugin/notify/hipchat.go @@ -2,8 +2,10 @@ package notify import ( "fmt" + "net/http" + "net/url" + "strconv" - "github.com/andybons/hipchat" "github.com/drone/drone/shared/model" ) @@ -21,12 +23,8 @@ type Hipchat struct { Failure bool `yaml:"on_failure,omitempty"` } -type HipchatClient interface { - PostMessage(req hipchat.MessageRequest) error -} - func (h *Hipchat) Send(context *model.Request) error { - client := &hipchat.Client{AuthToken: h.Token} + client := new(HipchatSimpleHTTPClient) 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: return h.sendFailure(client, context) } - return nil } @@ -51,29 +48,65 @@ func (h *Hipchat) buildLink(context *model.Request) string { 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) - 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 { 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 { msg := fmt.Sprintf(successMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author) - return h.send(client, hipchat.ColorGreen, hipchat.FormatHTML, msg, false) -} - -// helper function to send Hipchat requests -func (h *Hipchat) send(client HipchatClient, color, format, message string, notify bool) error { - req := hipchat.MessageRequest{ - RoomId: h.Room, - From: "Drone", - Message: message, - Color: color, - MessageFormat: format, - Notify: notify, + req := HipchatMessageRequest{ + RoomId: h.Room, + AuthToken: h.Token, + Color: "green", + Message: msg, + Notify: false, } - 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 +} diff --git a/plugin/notify/hipchat_test.go b/plugin/notify/hipchat_test.go index 5f3e809d..e9a8cc7e 100644 --- a/plugin/notify/hipchat_test.go +++ b/plugin/notify/hipchat_test.go @@ -3,15 +3,14 @@ package notify import ( "testing" - "github.com/andybons/hipchat" "github.com/drone/drone/shared/model" ) 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 return nil } @@ -49,13 +48,12 @@ func Test_SendStarted(t *testing.T) { request.Commit.Status = "Started" subject.SendWithClient(client, request) - expected := hipchat.MessageRequest{ - RoomId: "SampleRoom", - From: "Drone", - Message: "Building owner/repo#abc (example) by Test User
- Test Commit", - Color: hipchat.ColorYellow, - MessageFormat: hipchat.FormatHTML, - Notify: false, + expected := HipchatMessageRequest{ + RoomId: "SampleRoom", + AuthToken: "foo", + Color: "yellow", + Message: "Building owner/repo#abc (example) by Test User
- Test Commit", + Notify: false, } if client.Request != expected { @@ -67,13 +65,12 @@ func Test_SendSuccess(t *testing.T) { request.Commit.Status = "Success" subject.SendWithClient(client, request) - expected := hipchat.MessageRequest{ - RoomId: "SampleRoom", - From: "Drone", - Message: "Success owner/repo#abc (example) by Test User", - Color: hipchat.ColorGreen, - MessageFormat: hipchat.FormatHTML, - Notify: false, + expected := HipchatMessageRequest{ + RoomId: "SampleRoom", + AuthToken: "foo", + Color: "green", + Message: "Success owner/repo#abc (example) by Test User", + Notify: false, } if client.Request != expected { @@ -85,13 +82,12 @@ func Test_SendFailure(t *testing.T) { request.Commit.Status = "Failure" subject.SendWithClient(client, request) - expected := hipchat.MessageRequest{ - RoomId: "SampleRoom", - From: "Drone", - Message: "Failed owner/repo#abc (example) by Test User", - Color: hipchat.ColorRed, - MessageFormat: hipchat.FormatHTML, - Notify: true, + expected := HipchatMessageRequest{ + RoomId: "SampleRoom", + AuthToken: "foo", + Color: "red", + Message: "Failed owner/repo#abc (example) by Test User", + Notify: true, } if client.Request != expected {