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.
This commit is contained in:
Alexey Chernenkov 2015-04-09 21:17:55 +05:00
parent aa3eeda9b8
commit 8bcb5d9c52
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{
}
// 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, RoomId: h.Room,
From: "Drone", AuthToken: h.Token,
Message: message, Color: "green",
Color: color, Message: msg,
MessageFormat: format, Notify: false,
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,12 +48,11 @@ 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",
Color: "yellow",
Message: "Building <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User <br> - Test Commit", 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: hipchat.ColorYellow,
MessageFormat: hipchat.FormatHTML,
Notify: false, Notify: false,
} }
@ -67,12 +65,11 @@ 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",
Color: "green",
Message: "Success <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User", Message: "Success <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User",
Color: hipchat.ColorGreen,
MessageFormat: hipchat.FormatHTML,
Notify: false, Notify: false,
} }
@ -85,12 +82,11 @@ 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",
Color: "red",
Message: "Failed <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User", Message: "Failed <a href=\"http://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User",
Color: hipchat.ColorRed,
MessageFormat: hipchat.FormatHTML,
Notify: true, Notify: true,
} }