1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-30 08:06:52 +02:00

Merge pull request #460 from carnivalmobile/verbose_hipchat_notifications

Verbose hipchat notifications
This commit is contained in:
Brad Rydzewski 2014-09-17 07:54:10 -07:00
commit 9743533fed
2 changed files with 133 additions and 19 deletions

View File

@ -8,9 +8,9 @@ import (
)
const (
startedMessage = "Building %s, commit %s, author %s"
successMessage = "<b>Success</b> %s, commit %s, author %s"
failureMessage = "<b>Failed</b> %s, commit %s, author %s"
startedMessage = "Building %s (%s) by %s <br> - %s"
successMessage = "Success %s (%s) by %s"
failureMessage = "Failed %s (%s) by %s"
)
type Hipchat struct {
@ -21,45 +21,59 @@ 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
}
func (h *Hipchat) sendStarted(context *model.Request) error {
msg := fmt.Sprintf(startedMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
return h.send(hipchat.ColorYellow, hipchat.FormatHTML, msg)
func (h *Hipchat) buildLink(context *model.Request) string {
repoName := context.Repo.Owner + "/" + context.Repo.Name
url := context.Host + "/" + context.Repo.Host + "/" + repoName + "/" + context.Commit.Branch + "/" + context.Commit.Sha
return fmt.Sprintf("<a href=\"%s\">%s#%s</a>", url, repoName, context.Commit.ShaShort())
}
func (h *Hipchat) sendFailure(context *model.Request) error {
msg := fmt.Sprintf(failureMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
return h.send(hipchat.ColorRed, hipchat.FormatHTML, msg)
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) sendSuccess(context *model.Request) error {
msg := fmt.Sprintf(successMessage, context.Repo.Name, context.Commit.ShaShort(), context.Commit.Author)
return h.send(hipchat.ColorGreen, hipchat.FormatHTML, msg)
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(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) 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",
Message: message,
Color: color,
MessageFormat: format,
Notify: true,
Notify: notify,
}
return c.PostMessage(req)
return client.PostMessage(req)
}

View 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)
}
}