Merge remote-tracking branch 'origin/exp' into exp

This commit is contained in:
Brad Rydzewski 2014-09-23 20:49:23 -07:00
commit 2632fcb86e
3 changed files with 135 additions and 20 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)
}
}

View file

@ -14,6 +14,7 @@ import (
"strings"
"github.com/dotcloud/docker/pkg/term"
"github.com/dotcloud/docker/pkg/stdcopy"
"github.com/dotcloud/docker/utils"
)
@ -208,7 +209,7 @@ func (c *Client) hijack(method, path string, setRawTerminal bool, out io.Writer)
if setRawTerminal {
_, err = io.Copy(out, br)
} else {
_, err = utils.StdCopy(out, out, br)
_, err = stdcopy.StdCopy(out, out, br)
}
errStdout <- err