From b0da83331c54396393d506ee3226c8164440b213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Gaude=CC=82ncio?= Date: Thu, 5 May 2022 15:29:58 +0100 Subject: [PATCH] fixed #84 for env variables in payload json file --- src/slack-send.js | 5 ++--- test/resources/valid-payload.json | 3 ++- test/slack-send-test.js | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/slack-send.js b/src/slack-send.js index e204c049..db6042f6 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -39,8 +39,8 @@ module.exports = async function slackSend(core) { try { payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8'); // parse github context variables - const context = { github: github.context }; - const payloadString = payload.replace('$', ''); + 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 @@ -129,7 +129,6 @@ module.exports = async function slackSend(core) { } 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 (err.response) { core.setFailed(err.response.data); diff --git a/test/resources/valid-payload.json b/test/resources/valid-payload.json index 4caa4f23..8bcc5801 100644 --- a/test/resources/valid-payload.json +++ b/test/resources/valid-payload.json @@ -1,5 +1,6 @@ { "oliver": "benji", "bonny": "clyde", - "actor":"${{github.actor}}" + "actor":"${{github.actor}}", + "env_name": "${{env.ENV_NAME}}" } diff --git a/test/slack-send-test.js b/test/slack-send-test.js index 06d6347a..17971a97 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -51,6 +51,7 @@ describe('slack-send', () => { describe('using a bot token', () => { beforeEach(() => { process.env.SLACK_BOT_TOKEN = 'xoxb-xxxxx'; + process.env.ENV_NAME = 'test'; delete process.env.SLACK_WEBHOOK_URL; }); describe('happy path', () => { @@ -104,6 +105,7 @@ describe('slack-send', () => { assert.equal(chatArgs.bonny, 'clyde', 'Correct message provided to postMessage'); assert.equal(chatArgs.oliver, 'benji', 'Correct message provided to postMessage'); assert.equal(chatArgs.actor, 'user123', 'Correct message provided to postMessage'); + assert.equal(chatArgs.env_name, 'test', 'Correct message provided to postMessage'); }); it('should send the same message to multiple channels', async () => {