Skip to content

Commit

Permalink
Added support to TS (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
extmkv authored Nov 24, 2023
1 parent 88a2c07 commit 73c8736
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
57 changes: 35 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,33 @@ type Input struct {
UsernameOnError string `env:"from_username_on_error"`
ThreadTs string `env:"thread_ts"`
ThreadTsOnError string `env:"thread_ts_on_error"`
Ts string `env:"ts"`
TsOnError string `env:"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"`
ColorOnError string `env:"color_on_error"`
PreText string `env:"pretext"`
PreTextOnError string `env:"pretext_on_error"`
AuthorName string `env:"author_name"`
Title string `env:"title"`
TitleOnError string `env:"title_on_error"`
TitleLink string `env:"title_link"`
Message string `env:"message"`
MessageOnError string `env:"message_on_error"`
ImageURL string `env:"image_url"`
ImageURLOnError string `env:"image_url_on_error"`
ThumbURL string `env:"thumb_url"`
ThumbURLOnError string `env:"thumb_url_on_error"`
Footer string `env:"footer"`
FooterIcon string `env:"footer_icon"`
TimeStamp bool `env:"timestamp,opt[yes,no]"`
Fields string `env:"fields"`
Buttons string `env:"buttons"`
Color string `env:"color,required"`
ColorOnError string `env:"color_on_error"`
PreText string `env:"pretext"`
PreTextOnError string `env:"pretext_on_error"`
AuthorName string `env:"author_name"`
Title string `env:"title"`
TitleOnError string `env:"title_on_error"`
TitleLink string `env:"title_link"`
Message string `env:"message"`
MessageOnError string `env:"message_on_error"`
ImageURL string `env:"image_url"`
ImageURLOnError string `env:"image_url_on_error"`
ThumbURL string `env:"thumb_url"`
ThumbURLOnError string `env:"thumb_url_on_error"`
Footer string `env:"footer"`
FooterOnError string `env:"footer_on_error"`
FooterIcon string `env:"footer_icon"`
FooterIconOnError string `env:"footer_icon_on_error"`
TimeStamp bool `env:"timestamp,opt[yes,no]"`
Fields string `env:"fields"`
Buttons string `env:"buttons"`

// Status
BuildStatus string `env:"build_status"`
Expand All @@ -79,6 +83,7 @@ type config struct {
IconURL string
Username string
ThreadTs string
Ts string
ReplyBroadcast bool
LinkNames bool `env:"link_names,opt[yes,no]"`

Expand Down Expand Up @@ -130,6 +135,7 @@ func newMessage(c config) Message {
LinkNames: c.LinkNames,
Username: c.Username,
ThreadTs: c.ThreadTs,
Ts: c.Ts,
ReplyBroadcast: c.ReplyBroadcast,
}
if c.TimeStamp {
Expand All @@ -147,8 +153,14 @@ func postMessage(conf config, msg Message) error {
log.Debugf("Request to Slack: %s\n", b)

url := strings.TrimSpace(conf.WebhookURL)
ts := strings.TrimSpace(conf.Ts)

if url == "" {
url = "https://slack.com/api/chat.postMessage"
if ts == "" {
url = "https://slack.com/api/chat.postMessage"
} else {
url = "https://slack.com/api/chat.update"
}
}

req, err := http.NewRequest("POST", url, bytes.NewReader(b))
Expand Down Expand Up @@ -232,12 +244,13 @@ func parseInputIntoConfig(inp *Input) config {
ThumbURL: selectValue(inp.ThumbURL, inp.ThumbURLOnError),
AuthorName: inp.AuthorName,
TitleLink: inp.TitleLink,
Footer: inp.Footer,
FooterIcon: inp.FooterIcon,
Footer: selectValue(inp.Footer, inp.FooterOnError),
FooterIcon: selectValue(inp.FooterIcon, inp.FooterIconOnError),
TimeStamp: inp.TimeStamp,
Fields: inp.Fields,
Buttons: inp.Buttons,
ThreadTsOutputVariableName: inp.ThreadTsOutputVariableName,
Ts: selectValue(inp.Ts, inp.TsOnError),
}
return config

Expand Down
3 changes: 3 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Message struct {
// Provide another message's ts value to make this message a reply.
ThreadTs string `json:"thread_ts,omitempty"`

// Provide another message's ts value to make message to update
Ts string `json:"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"`
}
Expand Down
36 changes: 36 additions & 0 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@ inputs:
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

- ts:
opts:
title: Message Timestamp
summary: Timestamp of the message to be updated.
description: |-
Timestamp of the message to be updated.
When **Message Timestamp** is provided an existing Slack message will be updated, identified by the provided timestamp.
Example: `"1405894322.002768"`.
- ts_on_error:
opts:
title: Message Timestamp if the build failed
summary: Timestamp of the message to be updated if the build failed.
description: |-
Timestamp of the message to be updated if the build failed.
When **Message Timestamp if the build failed** is provided an existing Slack message will be updated, identified by the provided timestamp.
Example: `"1405894322.002768"`.
category: If Build Failed

- reply_broadcast: "no"
opts:
title: Reply Broadcast
Expand Down Expand Up @@ -309,12 +330,27 @@ inputs:
The footer adds some brief text to help contextualize and identify an attachment.
Limited to 300 characters.
- footer_on_error: "Bitrise"
opts:
title: "Footer adds some brief text as footer if the build failed"
description: |
The footer adds some brief text to help contextualize and identify an attachment.
Limited to 300 characters.
category: If Build Failed
- footer_icon: "https://github.com/bitrise-io.png?size=16"
opts:
title: "Renders a small icon beside the footer text"
description: |
Renders a small icon beside the footer text
It will be scaled down to 16px by 16px.
- footer_icon_on_error: "https://github.com/bitrise-io.png?size=16"
opts:
title: "Renders a small icon beside the footer text if the build failed"
description: |
Renders a small icon beside the footer text
It will be scaled down to 16px by 16px.
category: If Build Failed
- timestamp: "yes"
opts:
title: "Show the current time as part of the attachment's footer?"
Expand Down

0 comments on commit 73c8736

Please sign in to comment.