Apply suggestions from code review
Co-authored-by: Kazuhiro Sera <[email protected]>
This commit is contained in:
co-authored by
Kazuhiro Sera
parent
1fe391acbf
commit
d56b4b325e
@@ -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
|
||||
|
||||
@@ -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\":\"<https://example.com|Overlook Hotel> \\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
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user