From 9fd3368077f7c920157fee83e7e3e433b7a0783e Mon Sep 17 00:00:00 2001 From: Dylan Arbour Date: Mon, 7 Sep 2020 14:55:43 -0400 Subject: [PATCH] Add text field to Slack Alert --- README.md | 2 ++ concourse/resource.go | 2 ++ out/alert.go | 12 ++++++++++-- out/alert_test.go | 4 ++-- out/main.go | 14 ++++++++++++++ slack/slack.go | 1 + 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 650906c..a7eaaa3 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ Sends a structured message to Slack based on the alert type. - `channel_file`: *Optional.* File containing text which overrides `channel`. If the file cannot be read, `channel` will be used instead. - `message`: *Optional.* The status message at the top of the alert. Defaults to name of alert type. - `message_file`: *Optional.* File containing text which overrides `message`. If the file cannot be read, `message` will be used instead. +- `text`: *Optional.* The status text at the top of the alert. Defaults to name of alert type. +- `text_file`: *Optional.* File containing text which overrides `text`. If the file cannot be read, `text` will be used instead. - `color`: *Optional.* The color of the notification bar as a hexadecimal. Defaults to the icon color of the alert type. - `disable`: *Optional.* Disables the alert. Defaults to `false`. diff --git a/concourse/resource.go b/concourse/resource.go index ecbc345..b339dd5 100644 --- a/concourse/resource.go +++ b/concourse/resource.go @@ -37,6 +37,8 @@ type OutParams struct { Color string `json:"color"` Message string `json:"message"` MessageFile string `json:"message_file"` + Text string `json:"text"` + TextFile string `json:"text_file"` Disable bool `json:"disable"` } diff --git a/out/alert.go b/out/alert.go index aed0daa..1656e57 100644 --- a/out/alert.go +++ b/out/alert.go @@ -11,6 +11,8 @@ type Alert struct { IconURL string Message string MessageFile string + Text string + TextFile string Disabled bool } @@ -73,17 +75,23 @@ func NewAlert(input *concourse.OutRequest) Alert { if alert.Disabled == false { alert.Disabled = input.Source.Disable } + alert.Channel = input.Params.Channel if alert.Channel == "" { alert.Channel = input.Source.Channel } + alert.ChannelFile = input.Params.ChannelFile + if input.Params.Message != "" { alert.Message = input.Params.Message } + alert.MessageFile = input.Params.MessageFile + if input.Params.Color != "" { alert.Color = input.Params.Color } - alert.MessageFile = input.Params.MessageFile - alert.ChannelFile = input.Params.ChannelFile + + alert.Text = input.Params.Text + alert.TextFile = input.Params.TextFile return alert } diff --git a/out/alert_test.go b/out/alert_test.go index 3aa474a..a12609a 100644 --- a/out/alert_test.go +++ b/out/alert_test.go @@ -20,9 +20,9 @@ func TestNewAlert(t *testing.T) { "custom params": { input: &concourse.OutRequest{ Source: concourse.Source{Channel: "general"}, - Params: concourse.OutParams{Channel: "custom-channel", Color: "#ffffff", Message: "custom-message", Disable: true}, + Params: concourse.OutParams{Channel: "custom-channel", Color: "#ffffff", Message: "custom-message", Text: "custom-text", Disable: true}, }, - want: Alert{Type: "default", Channel: "custom-channel", Color: "#ffffff", IconURL: "https://ci.concourse-ci.org/public/images/favicon-pending.png", Message: "custom-message", Disabled: true}, + want: Alert{Type: "default", Channel: "custom-channel", Color: "#ffffff", IconURL: "https://ci.concourse-ci.org/public/images/favicon-pending.png", Message: "custom-message", Text: "custom-text", Disabled: true}, }, "custom source": { input: &concourse.OutRequest{ diff --git a/out/main.go b/out/main.go index d1c5f16..0fba9fa 100644 --- a/out/main.go +++ b/out/main.go @@ -18,6 +18,7 @@ import ( func buildMessage(alert Alert, m concourse.BuildMetadata, path string) *slack.Message { message := alert.Message channel := alert.Channel + text := alert.Text // Open and read message file if set if alert.MessageFile != "" { @@ -43,6 +44,18 @@ func buildMessage(alert Alert, m concourse.BuildMetadata, path string) *slack.Me } } + // Open and read text file if set + if alert.TextFile != "" { + file := filepath.Join(path, alert.TextFile) + f, err := ioutil.ReadFile(file) + + if err != nil { + fmt.Fprintf(os.Stderr, "error reading text_file: %v\nwill default to text instead\n", err) + } else { + text = strings.TrimSpace(string(f)) + } + } + attachment := slack.Attachment{ Fallback: fmt.Sprintf("%s -- %s", fmt.Sprintf("%s: %s/%s/%s", message, m.PipelineName, m.JobName, m.BuildName), m.URL), AuthorName: message, @@ -61,6 +74,7 @@ func buildMessage(alert Alert, m concourse.BuildMetadata, path string) *slack.Me Short: true, }, }, + Text: text, } return &slack.Message{Attachments: []slack.Attachment{attachment}, Channel: channel} diff --git a/slack/slack.go b/slack/slack.go index a36f81d..7562e0a 100644 --- a/slack/slack.go +++ b/slack/slack.go @@ -23,6 +23,7 @@ type Attachment struct { Fields []Field `json:"fields"` Footer string `json:"footer"` FooterIcon string `json:"footer_icon"` + Text string `json:"text"` } // Field represents a Slack API message attachment's fields