Skip to content

Commit

Permalink
Merge pull request #67 from arbourd/add-text
Browse files Browse the repository at this point in the history
  • Loading branch information
arbourd authored Sep 7, 2020
2 parents 32c4df4 + 9fd3368 commit 12c60a0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
2 changes: 2 additions & 0 deletions concourse/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
12 changes: 10 additions & 2 deletions out/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Alert struct {
IconURL string
Message string
MessageFile string
Text string
TextFile string
Disabled bool
}

Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions out/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
14 changes: 14 additions & 0 deletions out/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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,
Expand All @@ -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}
Expand Down
1 change: 1 addition & 0 deletions slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 12c60a0

Please sign in to comment.