Skip to content

Commit

Permalink
ref: revert the webhook remove (#49)
Browse files Browse the repository at this point in the history
* ref: revert the webhook remove

* fix: test fix

* input validation

* step.yml update
  • Loading branch information
BirmacherAkos authored and godrei committed Oct 9, 2018
1 parent 32c1857 commit 5cd9ec2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.bitrise*
.bitrise*
.gows.user.yml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ You can share your Step or step version with the [bitrise CLI](https://github.co
## Trigger a new release

- __merge every code changes__ to the `master` branch
- __push the new version tag__ to the `master` branch
- __push the new version tag__ to the `master` branch
9 changes: 6 additions & 3 deletions bitrise.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
format_version: 6
format_version: 5
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

app:
Expand Down Expand Up @@ -33,7 +33,7 @@ workflows:
title: On Success - without channel
is_skippable: false
inputs:
- api_token: $SLACK_API_TOKEN
- webhook_url: $SLACK_WEBHOOK_URL
- channel: ""
- from_username: step-dev-test
- message: On Success test - without channel param
Expand All @@ -55,7 +55,7 @@ workflows:
title: On Success - with an image / gif
is_skippable: false
inputs:
- api_token: $SLACK_API_TOKEN
- webhook_url: $SLACK_WEBHOOK_URL
- channel: $SLACK_CHANNEL
- from_username: step-dev-test
- message: |
Expand All @@ -78,6 +78,7 @@ workflows:
title: Should escape backslash+n as newline char
is_skippable: false
inputs:
- webhook_url: $SLACK_WEBHOOK_URL
- api_token: $SLACK_API_TOKEN
- channel: $SLACK_CHANNEL
- from_username: step-dev-test
Expand All @@ -92,6 +93,7 @@ workflows:
title: On Error
is_skippable: false
inputs:
- webhook_url: $SLACK_WEBHOOK_URL
- api_token: $SLACK_API_TOKEN
- channel: $SLACK_CHANNEL
- from_username_on_error: step-dev-test-ON-ERROR
Expand All @@ -101,6 +103,7 @@ workflows:
- path::./:
is_skippable: false
inputs:
- webhook_url: $SLACK_WEBHOOK_URL
- api_token: $SLACK_API_TOKEN
- channel: no-channel-like-this
missing-webhook-url-test:
Expand Down
1 change: 1 addition & 0 deletions gows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package_name: github.com/bitrise-io/steps-slack-message
33 changes: 28 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type Config struct {
Debug bool `env:"is_debug_mode,opt[yes,no]"`

// Message
APIToken stepconf.Secret `env:"api_token,required"`
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"`
Expand Down Expand Up @@ -54,8 +55,6 @@ type Config struct {
Buttons string `env:"buttons"`
}

const messageURL = "https://slack.com/api/chat.postMessage"

// success is true if the build is successful, false otherwise.
var success = os.Getenv("BITRISE_BUILD_STATUS") == "0"

Expand Down Expand Up @@ -110,9 +109,17 @@ func postMessage(conf Config, msg Message) error {
}
log.Debugf("Request to Slack: %s\n", b)

req, err := http.NewRequest("POST", messageURL, bytes.NewReader(b))
url := string(conf.WebhookURL)
if string(conf.APIToken) != "" {
url = "https://slack.com/api/chat.postMessage"
}

req, err := http.NewRequest("POST", url, bytes.NewReader(b))
req.Header.Add("Content-Type", "application/json; charset=utf-8")
req.Header.Add("Authorization", "Bearer "+string(conf.APIToken))

if string(conf.APIToken) != "" {
req.Header.Add("Authorization", "Bearer "+string(conf.APIToken))
}

client := &http.Client{}

Expand All @@ -138,6 +145,17 @@ func postMessage(conf Config, msg Message) error {
return nil
}

func validate(conf Config) error {
if conf.APIToken == "" && conf.WebhookURL == "" {
return fmt.Errorf("Both API Token and WebhookURL is empty. You need to provide one of them. If you want to use incoming webhooks provide the webhook url. I you want to use a bot to send a message provide the bot API token")
}

if conf.APIToken != "" && conf.WebhookURL != "" {
log.Warnf("Both API Token and WebhookURL is provided. Using the webhook url")
}
return nil
}

func main() {
var conf Config
if err := stepconf.Parse(&conf); err != nil {
Expand All @@ -147,6 +165,11 @@ func main() {
stepconf.Print(conf)
log.SetEnableDebugLog(conf.Debug)

if err := validate(conf); err != nil {
log.Errorf("Error: %s\n", err)
os.Exit(1)
}

msg := newMessage(conf)
if err := postMessage(conf, msg); err != nil {
log.Errorf("Error: %s", err)
Expand Down
13 changes: 13 additions & 0 deletions step.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -e
THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

go_package_name="github.com/bitrise-io/steps-slack-message"
full_package_path="${THIS_SCRIPT_DIR}/go/src/${go_package_name}"
mkdir -p "${full_package_path}"

rsync -avh --quiet "${THIS_SCRIPT_DIR}/" "${full_package_path}/"

export GOPATH="${THIS_SCRIPT_DIR}/go"
export GO15VENDOREXPERIMENT=1
go run "${full_package_path}/main.go"
24 changes: 20 additions & 4 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ summary: Send a [Slack](https://slack.com/) message to a channel or group.
description: |
Send a [Slack](https://slack.com/) message to a channel or group.
You have to create a bot with an API token at https://my.slack.com/apps/manage/custom-integrations
You can either setup a **bot user with API token** or setup **Incoming WebHook integration**.
To register an **Incoming WebHook integration** visit: https://api.slack.com/incoming-webhooks
To setup a **bot with an API token** visit: https://api.slack.com/bot-users
*Don't forget to enable this Step to **Always run** if
you want to get notified about failed builds as well!*
Expand All @@ -25,7 +29,6 @@ deps:
toolkit:
go:
package_name: github.com/bitrise-io/steps-slack-message

inputs:
- is_debug_mode: "no"
opts:
Expand All @@ -38,10 +41,23 @@ inputs:
- "no"

# Message inputs
- webhook_url:
opts:
title: "Slack Webhook URL (Webhook or API token is required)"
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
- api_token:
opts:
title: Slack API token for your Bot
is_required: true
title: "Slack API token (Webhook or API token is required)"
description: |
**Either webhook\_url or api\_token input is required.**
To setup a **bot with an API token** visit: https://api.slack.com/bot-users
is_required: false
is_sensitive: true
- channel:
opts:
Expand Down

0 comments on commit 5cd9ec2

Please sign in to comment.