feat: introduce an option to send payload file json without replacing variables (#299)
Co-authored-by: Ethan Zimbelman <[email protected]>
This commit is contained in:
@@ -76,6 +76,21 @@ or
|
||||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
|
||||
```
|
||||
|
||||
> To send the payload file JSON as is, without replacing templated values with
|
||||
> `github.context` or `github.env`, set `payload-file-path-parsed` to `false`.
|
||||
> Default: `true`.
|
||||
|
||||
```yaml
|
||||
- name: Send custom JSON data to Slack workflow
|
||||
id: slack
|
||||
uses: slackapi/[email protected]
|
||||
with:
|
||||
payload-file-path: "./payload-slack-content.json"
|
||||
payload-file-path-parsed: false
|
||||
env:
|
||||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
|
||||
```
|
||||
|
||||
### Technique 2: Slack App
|
||||
|
||||
By creating a new Slack app or using an existing one, this approach allows your GitHub Actions job to post a message in a Slack channel or direct message by utilizing the [chat.postMessage](https://api.slack.com/methods/chat.postMessage) API method. Using this approach you can instantly post a message without setting up Slack workflows.
|
||||
|
||||
@@ -13,6 +13,10 @@ inputs:
|
||||
payload-file-path: # path to JSON payload to send to Slack via webhook
|
||||
description: 'path to JSON payload to send to Slack if webhook route. If not supplied, json from GitHub event will be sent instead. If payload is provided, it will take preference over this field'
|
||||
required: false
|
||||
payload-file-path-parsed:
|
||||
description: 'Replace templated variables in the JSON payload file with values from the GitHub context and environment variables'
|
||||
default: true
|
||||
required: false
|
||||
update-ts: # The timestamp of a previous message posted to update it instead of posting a new message
|
||||
description: 'The timestamp of a previous message posted. It will update the existing message instead of posting a new message'
|
||||
required: false
|
||||
|
||||
+14
-9
@@ -6,7 +6,6 @@ const axios = require('axios');
|
||||
const markup = require('markup-js');
|
||||
const { HttpsProxyAgent } = require('https-proxy-agent');
|
||||
const { parseURL } = require('whatwg-url');
|
||||
|
||||
const { createWebClient } = require('./web-client');
|
||||
|
||||
const SLACK_WEBHOOK_TYPES = {
|
||||
@@ -34,15 +33,19 @@ module.exports = async function slackSend(core) {
|
||||
|
||||
const payloadFilePath = core.getInput('payload-file-path');
|
||||
|
||||
/** Option to replace templated context variables in the payload file JSON */
|
||||
const payloadFilePathParsed = core.getBooleanInput('payload-file-path-parsed');
|
||||
|
||||
let webResponse;
|
||||
|
||||
if (payloadFilePath && !payload) {
|
||||
try {
|
||||
payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8');
|
||||
// parse github context variables
|
||||
const context = { github: github.context, env: process.env };
|
||||
const payloadString = payload.replaceAll('${{', '{{');
|
||||
payload = markup.up(payloadString, context);
|
||||
if (payloadFilePathParsed) {
|
||||
const context = { github: github.context, env: process.env };
|
||||
const payloadString = payload.replaceAll('${{', '{{');
|
||||
payload = markup.up(payloadString, context);
|
||||
}
|
||||
} catch (error) {
|
||||
// passed in payload file path was invalid
|
||||
console.error(error);
|
||||
@@ -130,14 +133,16 @@ module.exports = async function slackSend(core) {
|
||||
await axios.post(webhookUrl, payload, axiosOpts);
|
||||
} catch (err) {
|
||||
console.log('axios post failed, double check the payload being sent includes the keys Slack expects');
|
||||
console.log(payload);
|
||||
// console.log(err);
|
||||
if ('toJSON' in err) {
|
||||
console.error(JSON.stringify(err.toJSON()));
|
||||
}
|
||||
console.error(`Attempted to POST payload: ${JSON.stringify(payload)}`);
|
||||
|
||||
if (err.response) {
|
||||
core.setFailed(err.response.data);
|
||||
} else {
|
||||
core.setFailed(err.message);
|
||||
}
|
||||
|
||||
core.setFailed(err.message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
+27
-1
@@ -83,10 +83,11 @@ describe('slack-send', () => {
|
||||
assert.equal(chatArgs.text, 'who let the dogs out?', 'Correct message provided to postMessage');
|
||||
});
|
||||
|
||||
it('should accept a payload-file-path and use it\'s content in the message', async () => {
|
||||
it('should send payload-file-path values with replaced context variables', async () => {
|
||||
// Prepare
|
||||
fakeCore.getInput.withArgs('channel-id').returns('C123456');
|
||||
fakeCore.getInput.withArgs('payload-file-path').returns('./test/resources/valid-payload.json');
|
||||
fakeCore.getBooleanInput.withArgs('payload-file-path-parsed').returns(true);
|
||||
fakeGithub.context.actor = 'user123';
|
||||
|
||||
// Run
|
||||
@@ -106,6 +107,31 @@ describe('slack-send', () => {
|
||||
assert.equal(chatArgs.actor, 'user123', 'Correct message provided to postMessage');
|
||||
});
|
||||
|
||||
it('should send payload-file-path values without replacing context variables', async () => {
|
||||
// Prepare
|
||||
fakeCore.getInput.withArgs('channel-id').returns('C123456');
|
||||
fakeCore.getInput.withArgs('payload-file-path').returns('./test/resources/valid-payload.json');
|
||||
fakeCore.getBooleanInput.withArgs('payload-file-path-parsed').returns(false);
|
||||
fakeGithub.context.actor = 'user123';
|
||||
|
||||
// Run
|
||||
await slackSend(fakeCore);
|
||||
|
||||
// Assert
|
||||
assert.equal(fakeCore.setOutput.firstCall.firstArg, 'ts', 'Output name set to ts');
|
||||
assert.equal(fakeCore.setOutput.secondCall.firstArg, 'thread_ts', 'Output name set to thread_ts');
|
||||
assert(fakeCore.setOutput.secondCall.lastArg.length > 0, 'Time output a non-zero-length string');
|
||||
assert.equal(fakeCore.setOutput.lastCall.firstArg, 'time', 'Output name set to time');
|
||||
assert(fakeCore.setOutput.lastCall.lastArg.length > 0, 'Time output a non-zero-length string');
|
||||
const chatArgs = ChatStub.postMessage.lastCall.firstArg;
|
||||
assert.equal(chatArgs.channel, 'C123456', 'Correct channel provided to postMessage');
|
||||
assert.equal(chatArgs.text, '', 'Correct message provided to postMessage');
|
||||
assert.equal(chatArgs.bonny, 'clyde', 'Correct message provided to postMessage');
|
||||
assert.equal(chatArgs.oliver, 'benji', 'Correct message provided to postMessage');
|
||||
/* eslint-disable-next-line no-template-curly-in-string */
|
||||
assert.equal(chatArgs.actor, '${{github.actor}}', 'Correct message provided to postMessage');
|
||||
});
|
||||
|
||||
it('should send the same message to multiple channels', async () => {
|
||||
fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?');
|
||||
fakeCore.getInput.withArgs('channel-id').returns('C123456,C987654');
|
||||
|
||||
Reference in New Issue
Block a user