diff --git a/README.md b/README.md index b225c09c..fc9415a2 100644 --- a/README.md +++ b/README.md @@ -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/slack-github-action@v1.25.0 + 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. diff --git a/action.yml b/action.yml index 7575c637..a2fcd2a5 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/slack-send.js b/src/slack-send.js index 759bd9d9..cfe015d1 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -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; } } diff --git a/test/slack-send-test.js b/test/slack-send-test.js index e47d8d51..2b9cefa7 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -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');