From d56b4b325e5daf1b631c5a9b0e12feaf30c64d71 Mon Sep 17 00:00:00 2001 From: Steve Gill Date: Mon, 15 Nov 2021 21:51:55 -0800 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Kazuhiro Sera --- README.md | 12 ++++++------ example-workflows/Incoming webhook/main.yml | 2 +- index.js | 9 +++++---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 28a4740..61ad723 100644 --- a/README.md +++ b/README.md @@ -66,17 +66,17 @@ This route allows your GitHub Actions job to post a message in a Slack channel o SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} ``` -## Slack Incoming Webhook +## Slack Incoming Webhook Route -This route allows your GitHub Actions job to post a message in a Slack channel or direct message by utilizing [messaging.webhooks](https://api.slack.com/messaging/webhooks) API method. With this route, you can instantly post a message. +This route allows your GitHub Actions job to post a message in a Slack channel or direct message by utilizing [Incoming Webhooks](https://api.slack.com/messaging/webhooks). With this route, you can instantly post a message. Incoming Webhooks conform to the same rules and functionality as any of our other messaging APIs. You can make your posted messages as simple as a single line of text, or make them really useful with [interactive components](https://api.slack.com/messaging/interactivity). To make the message more expressive and useful use [Block Kit](https://api.slack.com/block-kit) to build and test visual components. ## Setup * [Create a Slack App](https://api.slack.com/apps) for your workspace (alternatively use an existing app you have already created and installed) -* Install the app to your workspace -* Invite the bot user into the channel (`/invite @bot_user_name`) +* Add the [incoming-webhook](https://api.slack.com/scopes/incoming-webhook) bot scope under **OAuth & Permissions** +* Install the app to your workspace (you will select a channel to notify) ### Usage @@ -87,8 +87,8 @@ Incoming Webhooks conform to the same rules and functionality as any of our othe with: payload: "{\"blocks\":[{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"You have a new request: \"}},{\"type\":\"section\",\"fields\":[{\"type\":\"mrkdwn\",\"text\":\"*Type:* Computer (laptop)\"},{\"type\":\"mrkdwn\",\"text\":\"*When:* Submitted Aut 10\"}]}]}" env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_BOT_TOKEN }} - SLACK_WEBHOOK_TYPE: INCOMING + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK ``` ## Contributing diff --git a/example-workflows/Incoming webhook/main.yml b/example-workflows/Incoming webhook/main.yml index b9af794..3ac67f8 100644 --- a/example-workflows/Incoming webhook/main.yml +++ b/example-workflows/Incoming webhook/main.yml @@ -12,5 +12,5 @@ jobs: payload: "{\"text\":\"Danny Torrence left a 1 star review for your property.\",\"blocks\":[{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"Danny Torrence left the following review for your property:\"}},{\"type\":\"section\",\"block_id\":\"section567\",\"text\":{\"type\":\"mrkdwn\",\"text\":\" \\n :star: \\n Doors had too many axe holes, guest in room 237 was far too rowdy, whole place felt stuck in the 1920s.\"},\"accessory\":{\"type\":\"image\",\"image_url\":\"https://is5-ssl.mzstatic.com/image/thumb/Purple3/v4/d3/72/5c/d3725c8f-c642-5d69-1904-aa36e4297885/source/256x256bb.jpg\",\"alt_text\":\"Haunted hotel image\"}},{\"type\":\"section\",\"block_id\":\"section789\",\"fields\":[{\"type\":\"mrkdwn\",\"text\":\"*Average Rating*\\n1.0\"}]}]}" env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} - SLACK_WEBHOOK_TYPE: incoming + SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK diff --git a/index.js b/index.js index e7fbdde..a137364 100644 --- a/index.js +++ b/index.js @@ -5,14 +5,15 @@ const flatten = require('flat'); const axios = require('axios'); const SLACK_WEBHOOK_TYPES = { - WORKFLOW_BUILDER: 'BUILDER', - WORKFLOW_INCOMING: 'INCOMING' + WORKFLOW_TRIGGER: 'WORKFLOW_TRIGGER', + INCOMING_WEBHOOK: 'INCOMING_WEBHOOK' } try { const botToken = process.env.SLACK_BOT_TOKEN; const webhookUrl = process.env.SLACK_WEBHOOK_URL; - const webhookType = process.env.SLACK_WEBHOOK_TYPE?.toUpperCase() || SLACK_WEBHOOK_TYPES.WORKFLOW_BUILDER; + // The default type is for Workflow Builder triggers. If you want to use this action for Incoming Webhooks, use the corresponding type instead. + const webhookType = process.env.SLACK_WEBHOOK_TYPE?.toUpperCase() || SLACK_WEBHOOK_TYPES.WORKFLOW_TRIGGER; let payload = core.getInput('payload'); @@ -54,7 +55,7 @@ try { payload = github.context.payload; } - if (webhookType === SLACK_WEBHOOK_TYPES.WORKFLOW_BUILDER) { + if (webhookType === SLACK_WEBHOOK_TYPES.WORKFLOW_TRIGGER) { // flatten JSON payload (no nested attributes) const flatPayload = flatten(payload);