Skip to content

Commit

Permalink
Added support to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
extmkv committed Sep 13, 2023
1 parent 88a2c07 commit 640355a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
61 changes: 39 additions & 22 deletions main.go
Expand Up @@ -35,36 +35,41 @@ 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"`
FooterIcon string `env:"footer_icon"`
FooterOnError string `env:"footer_on_error"`
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"`
PipelineBuildStatus string `env:"pipeline_build_status"`

// Step Outputs
ThreadTsOutputVariableName string `env:"output_thread_ts"`
TsOutputVariableName string `env:"output_ts"`
}

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

Expand All @@ -99,6 +105,7 @@ type config struct {

// Step Outputs
ThreadTsOutputVariableName string `env:"output_thread_ts"`
TsOutputVariableName string `env:"output_ts"`
}

// ensureNewlines replaces all \n substrings with newline characters.
Expand Down Expand Up @@ -130,6 +137,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 +155,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 +246,14 @@ 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,
TsOutputVariableName: inp.TsOutputVariableName,
Ts: selectValue(inp.Ts, inp.TsOnError),
}
return config

Expand All @@ -249,6 +265,7 @@ func main() {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}

stepconf.Print(input)
log.SetEnableDebugLog(input.Debug)

Expand Down
3 changes: 3 additions & 0 deletions message.go
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
26 changes: 26 additions & 0 deletions step.yml
Expand Up @@ -186,6 +186,17 @@ 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
description: Updates the message.
- ts_on_error:
opts:
title: Message Timestamp if the build failed
description: Updates the message if the build failed.
category: If Build Failed

- reply_broadcast: "no"
opts:
title: Reply Broadcast
Expand Down Expand Up @@ -315,6 +326,21 @@ inputs:
description: |
Renders a small icon beside the footer text
It will be scaled down to 16px by 16px.
- 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_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 640355a

Please sign in to comment.