Skip to content

Commit

Permalink
Merge pull request #125 from walter-cd/suppress-notification
Browse files Browse the repository at this point in the history
Suppress notification messages
  • Loading branch information
mizzy committed Jul 29, 2015
2 parents b5a7198 + 63fa075 commit 2831ecf
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 26 deletions.
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ REPO_PATH="${ORG_PATH}/walter"

export GOBIN=${PWD}/bin
export GOPATH=${PWD}/gopath
export GOFMTPATH=" ./stages ./engine ./config ./pipelines ./walter ./version"
export GOFMTPATH="./stages ./engine ./config ./messengers ./pipelines ./walter ./version"

rm -f ${GOPATH}/src/${REPO_PATH}
mkdir -p ${GOPATH}/src/${ORG_PATH}
Expand Down
7 changes: 7 additions & 0 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ func (self *Parser) mapMessenger(messengerMap map[interface{}]interface{}) (mess
fieldVal := newMessengerValue.Field(i)
if fieldVal.Type() == reflect.ValueOf("string").Type() {
fieldVal.SetString(self.EnvVariables.Replace(messengerOptVal.(string)))
} else if fieldVal.Type().String() == "messengers.BaseMessenger" {
elements := messengerOptVal.([]interface{})
suppressor := fieldVal.Interface().(messengers.BaseMessenger)
for _, element := range elements {
suppressor.SuppressFields = append(suppressor.SuppressFields, element.(string))
}
fieldVal.Set(reflect.ValueOf(suppressor))
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions config/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ func TestParseConfWithMessengerBlock(t *testing.T) {
assert.Equal(t, "yyyy", messenger.From)
}

func TestParseConfWithMessengerBlockWithSupress(t *testing.T) {
configData, err := ReadConfigBytes([]byte(`
messenger:
type: hipchat
room_id: foobar
token: xxxx
from: yyyy
suppress:
- stderr
- stdout
pipeline:
- name: command_stage_1
type: shell
file: ../stages/test_sample.sh
`))
assert.Nil(t, err)
parser := &Parser{ConfigData: configData, EnvVariables: NewEnvVariables()}
result, err := parser.Parse()
messenger, ok := result.Reporter.(*messengers.HipChat)
assert.Nil(t, err)
assert.Equal(t, true, ok)
assert.Equal(t, "foobar", messenger.RoomId)
assert.Equal(t, "xxxx", messenger.Token)
assert.Equal(t, "yyyy", messenger.From)
assert.Equal(t, 2, len(messenger.SuppressFields))
}

func TestParseConfWithInvalidStage(t *testing.T) {
configData, err := ReadConfigBytes([]byte(`pipeline:
- name: command_stage_1
Expand Down
1 change: 1 addition & 0 deletions messengers/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package messengers

// FakeMessenger is a Messenger type used only for testing.
type FakeMessenger struct {
BaseMessenger
}

func (self *FakeMessenger) Post(messege string) bool {
Expand Down
7 changes: 4 additions & 3 deletions messengers/hipchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import (
// HipChat is a client which reports the pipeline results to the HipChat server.
// The client uses V1 of the HipChat API.
type HipChat struct {
RoomId string `config:"room_id"`
Token string `config:"token"`
From string `config:"from"`
BaseMessenger `config:"suppress"`
RoomId string `config:"room_id"`
Token string `config:"token"`
From string `config:"from"`
}

// TODO: make hipchat api endpoint configurable for on-premises servers
Expand Down
11 changes: 6 additions & 5 deletions messengers/hipchat2.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import (
// HipChat2 is a client which reports the pipeline results to the HipChat server.
// The client uses V2 of the HipChat API.
type HipChat2 struct {
RoomID string `config:"room_id"`
Token string `config:"token"`
From string `config:"from"`
BaseURL string `config:"base_url"`
client *hipchat.Client
BaseMessenger `config:"suppress"`
RoomID string `config:"room_id"`
Token string `config:"token"`
From string `config:"from"`
BaseURL string `config:"base_url"`
client *hipchat.Client
}

// Post sends a new HipChat message using V2 of the API
Expand Down
18 changes: 18 additions & 0 deletions messengers/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ import (
// services such as Slack or HipChat.
type Messenger interface {
Post(string) bool
Suppress(string) bool
}

type BaseMessenger struct {
SuppressFields []string `config:"suppress"`
}

func (self *BaseMessenger) Post(messege string) bool {
return true
}

func (self *BaseMessenger) Suppress(output_type string) bool {
for _, suppress := range self.SuppressFields {
if suppress == output_type {
return true
}
}
return false
}

// InitMessenger generates a spefified Messenger client objet.
Expand Down
21 changes: 11 additions & 10 deletions messengers/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import (

// Slack is a client which reports the pipeline results to the Slack chennel.
type Slack struct {
Channel string `config:"channel" json:"channel"`
UserName string `config:"username" json:"username"`
IconEmoji string `config:"icon" json:"icon_emoji,omitempty"`
IconUrl string `config:"icon_url" json:"icon_url,omitempty"`
IncomingUrl string `config:"url" json:"-"` // not map to json
BaseMessenger `config:"suppress"`
Channel string `config:"channel" json:"channel"`
UserName string `config:"username" json:"username"`
IconEmoji string `config:"icon" json:"icon_emoji,omitempty"`
IconUrl string `config:"icon_url" json:"icon_url,omitempty"`
IncomingUrl string `config:"url" json:"-"` // not map to json
}

// To avoid the infinite recursion
Expand All @@ -46,12 +47,12 @@ func (self *Slack) Post(message string) bool {

var color string

if strings.Contains(message, ", true") {
color = "good"
} else if strings.Contains(message, ", skipped") {
color = "warning"
} else {
if strings.Contains(message, "[RESULT] Failed") {
color = "danger"
} else if strings.Contains(message, "[RESULT] Skipped") {
color = "warning"
} else if strings.Contains(message, "[RESULT] Succeeded") {
color = "good"
}

attachment := map[string]string{
Expand Down
24 changes: 17 additions & 7 deletions pipelines/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,29 @@ type Resources struct {
}

// ReportStageResult throw the results of specified stage to the messenger services.
func (self *Resources) ReportStageResult(stage stages.Stage, result string) {
func (self *Resources) ReportStageResult(stage stages.Stage, resultStr string) {
name := stage.GetStageName()
self.Reporter.Post(
fmt.Sprintf("Stage execution results: %+v, %+v", name, result))
if !self.Reporter.Suppress("result") {
if resultStr == "true" {
self.Reporter.Post(
fmt.Sprintf("[%s][RESULT] Succeeded", name))
} else if resultStr == "skipped" {
self.Reporter.Post(
fmt.Sprintf("[%s][RESULT] Skipped", name))
} else {
self.Reporter.Post(
fmt.Sprintf("[%s][RESULT] Failed", name))
}
}

if stage.GetStageOpts().ReportingFullOutput {
if out := stage.GetOutResult(); len(out) > 0 {
if out := stage.GetOutResult(); (len(out) > 0) && (!self.Reporter.Suppress("stdout")) {
self.Reporter.Post(
fmt.Sprintf("[%s] %s", name, stage.GetOutResult()))
fmt.Sprintf("[%s][STDOUT] %s", name, stage.GetOutResult()))
}
if err := stage.GetErrResult(); len(err) > 0 {
if err := stage.GetErrResult(); len(err) > 0 && (!self.Reporter.Suppress("stderr")) {
self.Reporter.Post(
fmt.Sprintf("[%s][ERROR] %s", name, stage.GetErrResult()))
fmt.Sprintf("[%s][STDERR] %s", name, stage.GetErrResult()))
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions pipelines/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (mock *MockMessenger) Post(msg string) bool {
return true
}

func (self *MockMessenger) Suppress(output_type string) bool {
return false
}

func TestReportStageResult(t *testing.T) {
mock := &MockMessenger{}
p := Resources{
Expand Down

0 comments on commit 2831ecf

Please sign in to comment.