Skip to content

Commit

Permalink
Support sending messages into thread (#80)
Browse files Browse the repository at this point in the history
* Update step.yml to define new inputs

* Add ThreadTs and ReplyBroadcast options to Message struct

* Read ThreadTs and ReplyBroadcast parameters and apply to Message
  • Loading branch information
liamnichols committed Apr 28, 2022
1 parent ce2f9f8 commit eb741f9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
50 changes: 32 additions & 18 deletions main.go
Expand Up @@ -19,20 +19,24 @@ type Config struct {
Debug bool `env:"is_debug_mode,opt[yes,no]"`

// Message
WebhookURL stepconf.Secret `env:"webhook_url"`
WebhookURLOnError stepconf.Secret `env:"webhook_url_on_error"`
APIToken stepconf.Secret `env:"api_token"`
Channel string `env:"channel"`
ChannelOnError string `env:"channel_on_error"`
Text string `env:"text"`
TextOnError string `env:"text_on_error"`
IconEmoji string `env:"emoji"`
IconEmojiOnError string `env:"emoji_on_error"`
IconURL string `env:"icon_url"`
IconURLOnError string `env:"icon_url_on_error"`
LinkNames bool `env:"link_names,opt[yes,no]"`
Username string `env:"from_username"`
UsernameOnError string `env:"from_username_on_error"`
WebhookURL stepconf.Secret `env:"webhook_url"`
WebhookURLOnError stepconf.Secret `env:"webhook_url_on_error"`
APIToken stepconf.Secret `env:"api_token"`
Channel string `env:"channel"`
ChannelOnError string `env:"channel_on_error"`
Text string `env:"text"`
TextOnError string `env:"text_on_error"`
IconEmoji string `env:"emoji"`
IconEmojiOnError string `env:"emoji_on_error"`
IconURL string `env:"icon_url"`
IconURLOnError string `env:"icon_url_on_error"`
LinkNames bool `env:"link_names,opt[yes,no]"`
Username string `env:"from_username"`
UsernameOnError string `env:"from_username_on_error"`
ThreadTs string `env:"thread_ts"`
ThreadTsOnError string `env:"thread_ts_on_error"`
ReplyBroadcast bool `env:"reply_broadcast,opt[yes,no]"`
ReplyBroadcastOnError bool `env:"reply_broadcast_on_error,opt[yes,no]"`

// Attachment
Color string `env:"color,required"`
Expand Down Expand Up @@ -67,6 +71,14 @@ func selectValue(ifSuccess, ifFailed string) string {
return ifFailed
}

// selectBool chooses the right boolean value based on the result of the build.
func selectBool(ifSuccess, ifFailed bool) bool {
if success {
return ifSuccess
}
return ifFailed
}

// ensureNewlines replaces all \n substrings with newline characters.
func ensureNewlines(s string) string {
return strings.Replace(s, "\\n", "\n", -1)
Expand All @@ -91,10 +103,12 @@ func newMessage(c Config) Message {
FooterIcon: c.FooterIcon,
Buttons: parseButtons(c.Buttons),
}},
IconEmoji: selectValue(c.IconEmoji, c.IconEmojiOnError),
IconURL: selectValue(c.IconURL, c.IconURLOnError),
LinkNames: c.LinkNames,
Username: selectValue(c.Username, c.UsernameOnError),
IconEmoji: selectValue(c.IconEmoji, c.IconEmojiOnError),
IconURL: selectValue(c.IconURL, c.IconURLOnError),
LinkNames: c.LinkNames,
Username: selectValue(c.Username, c.UsernameOnError),
ThreadTs: selectValue(c.ThreadTs, c.ThreadTsOnError),
ReplyBroadcast: selectBool(c.ReplyBroadcast, c.ReplyBroadcastOnError),
}
if c.TimeStamp {
msg.Attachments[0].TimeStamp = int(time.Now().Unix())
Expand Down
6 changes: 6 additions & 0 deletions message.go
Expand Up @@ -30,6 +30,12 @@ type Message struct {

// Username specifies the bot's username for the message.
Username string `json:"username,omitempty"`

// Provide another message's ts value to make this message a reply.
ThreadTs string `json:"thread_ts,omitempty"`

// Used in conjunction with thread_ts and indicates whether reply should be made visible to everyone in the channel or conversation.
ReplyBroadcast bool `json:"reply_broadcast,omitempty"`
}

// Attachment adds more context to a slack chat message.
Expand Down
25 changes: 25 additions & 0 deletions step.yml
Expand Up @@ -177,6 +177,31 @@ inputs:
leave this option empty then the default one will be used.
category: If Build Failed

- thread_ts:
opts:
title: Thread Timestamp
description: Sends the message as a reply to the message with the given ts if set (in a thread).
- thread_ts_on_error:
opts:
title: Thread Timestamp if the build failed
description: Sends the message as a reply to the message with the given ts if set (in a thread) if the build failed.
category: If Build Failed
- reply_broadcast: "no"
opts:
title: Reply Broadcast
description: Used in conjunction with thread_ts and indicates whether reply should be made visible to everyone in the channel or conversation
value_options:
- "yes"
- "no"
- reply_broadcast_on_error: "no"
opts:
title: Reply Broadcast if the build failed
description: Used in conjunction with thread_ts and indicates whether reply should be made visible to everyone in the channel or conversation
category: If Build Failed
value_options:
- "yes"
- "no"

# Attachment inputs

- color: "#3bc3a3"
Expand Down

0 comments on commit eb741f9

Please sign in to comment.