From c4b60867b5a19029cfbb038a0f56e17e1ac84651 Mon Sep 17 00:00:00 2001 From: Mike Date: Wed, 22 Sep 2021 09:09:51 -0700 Subject: [PATCH] allow a different webhook to be used to send messages on build failures --- main.go | 29 +++++++++++++++-------------- step.yml | 9 +++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 0ea4122..47fdb9f 100644 --- a/main.go +++ b/main.go @@ -19,19 +19,20 @@ type Config struct { Debug bool `env:"is_debug_mode,opt[yes,no]"` // Message - WebhookURL stepconf.Secret `env:"webhook_url"` - 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"` // Attachment Color string `env:"color,required"` @@ -109,7 +110,7 @@ func postMessage(conf Config, msg Message) error { } log.Debugf("Request to Slack: %s\n", b) - url := strings.TrimSpace(string(conf.WebhookURL)) + url := strings.TrimSpace(selectValue(string(conf.WebhookURL), string(conf.WebhookURLOnError))) if url == "" { url = "https://slack.com/api/chat.postMessage" } diff --git a/step.yml b/step.yml index 10a255e..f70683f 100644 --- a/step.yml +++ b/step.yml @@ -72,6 +72,15 @@ inputs: To register an **Incoming WebHook integration** visit: https://api.slack.com/incoming-webhooks is_required: false is_sensitive: true + - webhook_url_on_error: + opts: + title: "Slack Webhook URL (Webhook or API token is required) if the build failed" + description: | + **Either webhook\_url or api\_token input is required.** + To register an **Incoming WebHook integration** visit: https://api.slack.com/incoming-webhooks + is_required: false + is_sensitive: true + category: If Build Failed - api_token: opts: title: "Slack API token (Webhook or API token is required)"