diff --git a/plugin/notify/hipchat.go b/plugin/notify/hipchat.go index 8d0c74b0e..d1d7f1406 100644 --- a/plugin/notify/hipchat.go +++ b/plugin/notify/hipchat.go @@ -21,14 +21,23 @@ 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} + return h.SendWithClient(client, context) +} + +func (h *Hipchat) SendWithClient(client HipchatClient, context *model.Request) error { switch { case context.Commit.Status == "Started" && h.Started: - return h.sendStarted(context) + return h.sendStarted(client, context) case context.Commit.Status == "Success" && h.Success: - return h.sendSuccess(context) + return h.sendSuccess(client, context) case context.Commit.Status == "Failure" && h.Failure: - return h.sendFailure(context) + return h.sendFailure(client, context) } return nil @@ -40,24 +49,23 @@ func (h *Hipchat) buildLink(context *model.Request) string { return fmt.Sprintf("%s#%s", url, repoName, context.Commit.ShaShort()) } -func (h *Hipchat) sendStarted(context *model.Request) error { - msg := fmt.Sprintf(startedMessage, h.buildLink(context), context.Commit.Branch, context.User.Login, context.Commit.Message) - return h.send(hipchat.ColorYellow, hipchat.FormatHTML, msg, false) +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) } -func (h *Hipchat) sendFailure(context *model.Request) error { - msg := fmt.Sprintf(failureMessage, h.buildLink(context), context.Commit.Branch, context.User.Login) - return h.send(hipchat.ColorRed, hipchat.FormatHTML, msg, true) +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) } -func (h *Hipchat) sendSuccess(context *model.Request) error { - msg := fmt.Sprintf(successMessage, h.buildLink(context), context.Commit.Branch, context.User.Login) - return h.send(hipchat.ColorGreen, hipchat.FormatHTML, msg, false) +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(color, format, message string, notify bool) error { - c := hipchat.Client{AuthToken: h.Token} +func (h *Hipchat) send(client HipchatClient, color, format, message string, notify bool) error { req := hipchat.MessageRequest{ RoomId: h.Room, From: "Drone", @@ -67,5 +75,5 @@ func (h *Hipchat) send(color, format, message string, notify bool) error { Notify: notify, } - return c.PostMessage(req) + return client.PostMessage(req) } diff --git a/plugin/notify/hipchat_test.go b/plugin/notify/hipchat_test.go new file mode 100644 index 000000000..5f3e809d4 --- /dev/null +++ b/plugin/notify/hipchat_test.go @@ -0,0 +1,100 @@ +package notify + +import ( + "testing" + + "github.com/andybons/hipchat" + "github.com/drone/drone/shared/model" +) + +type MockHipchatClient struct { + Request hipchat.MessageRequest +} + +func (c *MockHipchatClient) PostMessage(req hipchat.MessageRequest) error { + c.Request = req + return nil +} + +var client = &MockHipchatClient{} + +var subject = &Hipchat{ + Room: "SampleRoom", + Token: "foo", + Started: true, + Success: true, + Failure: true, +} + +var request = &model.Request{ + Host: "http://examplehost.com", + Repo: &model.Repo{ + Host: "examplegit.com", + Owner: "owner", + Name: "repo", + }, + Commit: &model.Commit{ + Sha: "abc", + Branch: "example", + Status: "Started", + Message: "Test Commit", + Author: "Test User", + }, + User: &model.User{ + Login: "TestUser", + }, +} + +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, + } + + if client.Request != expected { + t.Errorf("Invalid hipchat payload. Expected: %v, got %v", expected, client.Request) + } +} + +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, + } + + if client.Request != expected { + t.Errorf("Invalid hipchat payload. Expected: %v, got %v", expected, client.Request) + } +} + +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, + } + + if client.Request != expected { + t.Errorf("Invalid hipchat payload. Expected: %v, got %v", expected, client.Request) + } +}