mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-27 04:11:03 +00:00
Use DI for hipchat client, add tests for hipchat notifications via mocked client
This commit is contained in:
parent
7e021f9eb7
commit
8e6d6012aa
2 changed files with 123 additions and 15 deletions
|
@ -21,14 +21,23 @@ 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}
|
||||||
|
return h.SendWithClient(client, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hipchat) SendWithClient(client HipchatClient, context *model.Request) error {
|
||||||
switch {
|
switch {
|
||||||
case context.Commit.Status == "Started" && h.Started:
|
case context.Commit.Status == "Started" && h.Started:
|
||||||
return h.sendStarted(context)
|
return h.sendStarted(client, context)
|
||||||
case context.Commit.Status == "Success" && h.Success:
|
case context.Commit.Status == "Success" && h.Success:
|
||||||
return h.sendSuccess(context)
|
return h.sendSuccess(client, context)
|
||||||
case context.Commit.Status == "Failure" && h.Failure:
|
case context.Commit.Status == "Failure" && h.Failure:
|
||||||
return h.sendFailure(context)
|
return h.sendFailure(client, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -40,24 +49,23 @@ func (h *Hipchat) buildLink(context *model.Request) string {
|
||||||
return fmt.Sprintf("<a href=\"%s\">%s#%s</a>", url, repoName, context.Commit.ShaShort())
|
return fmt.Sprintf("<a href=\"%s\">%s#%s</a>", url, repoName, context.Commit.ShaShort())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hipchat) sendStarted(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.User.Login, context.Commit.Message)
|
msg := fmt.Sprintf(startedMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author, context.Commit.Message)
|
||||||
return h.send(hipchat.ColorYellow, hipchat.FormatHTML, msg, false)
|
return h.send(client, hipchat.ColorYellow, hipchat.FormatHTML, msg, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hipchat) sendFailure(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.User.Login)
|
msg := fmt.Sprintf(failureMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author)
|
||||||
return h.send(hipchat.ColorRed, hipchat.FormatHTML, msg, true)
|
return h.send(client, hipchat.ColorRed, hipchat.FormatHTML, msg, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hipchat) sendSuccess(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.User.Login)
|
msg := fmt.Sprintf(successMessage, h.buildLink(context), context.Commit.Branch, context.Commit.Author)
|
||||||
return h.send(hipchat.ColorGreen, hipchat.FormatHTML, msg, false)
|
return h.send(client, hipchat.ColorGreen, hipchat.FormatHTML, msg, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper function to send Hipchat requests
|
// helper function to send Hipchat requests
|
||||||
func (h *Hipchat) send(color, format, message string, notify bool) error {
|
func (h *Hipchat) send(client HipchatClient, color, format, message string, notify bool) error {
|
||||||
c := hipchat.Client{AuthToken: h.Token}
|
|
||||||
req := hipchat.MessageRequest{
|
req := hipchat.MessageRequest{
|
||||||
RoomId: h.Room,
|
RoomId: h.Room,
|
||||||
From: "Drone",
|
From: "Drone",
|
||||||
|
@ -67,5 +75,5 @@ func (h *Hipchat) send(color, format, message string, notify bool) error {
|
||||||
Notify: notify,
|
Notify: notify,
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.PostMessage(req)
|
return client.PostMessage(req)
|
||||||
}
|
}
|
||||||
|
|
100
plugin/notify/hipchat_test.go
Normal file
100
plugin/notify/hipchat_test.go
Normal file
|
@ -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 <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,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 <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,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 <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,
|
||||||
|
}
|
||||||
|
|
||||||
|
if client.Request != expected {
|
||||||
|
t.Errorf("Invalid hipchat payload. Expected: %v, got %v", expected, client.Request)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue