Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HipChat API ver 2 for notifications #973

Merged
merged 1 commit into from
Apr 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
Alexey Chernenkov committed Apr 14, 2015
commit 8bcb5d9c52df2339dcf1da460e13ca2f1101720c
75 changes: 54 additions & 21 deletions plugin/notify/hipchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package notify

import (
"fmt"
"net/http"
"net/url"
"strconv"

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

Expand All @@ -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)
}

Expand All @@ -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
}

Expand All @@ -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)
req := HipchatMessageRequest{
RoomId: h.Room,
AuthToken: h.Token,
Color: "green",
Message: msg,
Notify: false,
}
return client.PostMessage(req)
}

// 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,
}
// HipChat client

return client.PostMessage(req)
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
}
44 changes: 20 additions & 24 deletions plugin/notify/hipchat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 <a href=\"http:https://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,
expected := HipchatMessageRequest{
RoomId: "SampleRoom",
AuthToken: "foo",
Color: "yellow",
Message: "Building <a href=\"http:https://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User <br> - Test Commit",
Notify: false,
}

if client.Request != expected {
Expand All @@ -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 <a href=\"http:https://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User",
Color: hipchat.ColorGreen,
MessageFormat: hipchat.FormatHTML,
Notify: false,
expected := HipchatMessageRequest{
RoomId: "SampleRoom",
AuthToken: "foo",
Color: "green",
Message: "Success <a href=\"http:https://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User",
Notify: false,
}

if client.Request != expected {
Expand All @@ -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 <a href=\"http:https://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User",
Color: hipchat.ColorRed,
MessageFormat: hipchat.FormatHTML,
Notify: true,
expected := HipchatMessageRequest{
RoomId: "SampleRoom",
AuthToken: "foo",
Color: "red",
Message: "Failed <a href=\"http:https://examplehost.com/examplegit.com/owner/repo/example/abc\">owner/repo#abc</a> (example) by Test User",
Notify: true,
}

if client.Request != expected {
Expand Down