diff --git a/package-lock.json b/package-lock.json index 57d9da7..3084508 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,8 @@ "@actions/github": "^5.0.0", "@slack/web-api": "^6.7.0", "axios": "^0.25.0", - "flat": "^5.0.2" + "flat": "^5.0.2", + "markup-js": "^1.5.21" }, "devDependencies": { "@vercel/ncc": "^0.33.3", @@ -3687,6 +3688,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/markup-js": { + "version": "1.5.21", + "resolved": "https://registry.npmjs.org/markup-js/-/markup-js-1.5.21.tgz", + "integrity": "sha1-OJEocDpWWWRXTQ41fhQq3TvkbXk=", + "bin": { + "markup-js": "src/markup.js" + } + }, "node_modules/md5.js": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz", @@ -8421,6 +8430,11 @@ "semver": "^6.0.0" } }, + "markup-js": { + "version": "1.5.21", + "resolved": "https://registry.npmjs.org/markup-js/-/markup-js-1.5.21.tgz", + "integrity": "sha1-OJEocDpWWWRXTQ41fhQq3TvkbXk=" + }, "md5.js": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz", diff --git a/package.json b/package.json index 8e64ac6..50378b3 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "@actions/github": "^5.0.0", "@slack/web-api": "^6.7.0", "axios": "^0.25.0", - "flat": "^5.0.2" + "flat": "^5.0.2", + "markup-js": "^1.5.21" }, "devDependencies": { "@vercel/ncc": "^0.33.3", diff --git a/src/slack-send.js b/src/slack-send.js index 15402aa..bd6054a 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -4,6 +4,7 @@ const flatten = require('flat'); const axios = require('axios'); const { promises: fs } = require('fs'); const path = require('path'); +const markup = require('markup-js'); const SLACK_WEBHOOK_TYPES = { WORKFLOW_TRIGGER: 'WORKFLOW_TRIGGER', @@ -33,6 +34,10 @@ module.exports = async function slackSend(core) { if (payloadFilePath && !payload) { try { payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8'); + // parse github context variables + const context = { github: github.context }; + const payloadString = payload.replace('$', ''); + payload = markup.up(payloadString, context); } catch (error) { // passed in payload file path was invalid console.error(error); diff --git a/test/resources/valid-payload.json b/test/resources/valid-payload.json index b3b562b..4caa4f2 100644 --- a/test/resources/valid-payload.json +++ b/test/resources/valid-payload.json @@ -1,4 +1,5 @@ { "oliver": "benji", - "bonny": "clyde" + "bonny": "clyde", + "actor":"${{github.actor}}" } diff --git a/test/slack-send-test.js b/test/slack-send-test.js index a441138..06735b5 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -1,6 +1,7 @@ const { assert } = require('chai'); const sinon = require('sinon'); const core = require('@actions/core'); +const github = require('@actions/github'); const rewiremock = require('rewiremock/node'); const ChatStub = { @@ -30,6 +31,7 @@ const ORIG_WEBHOOK_VAR = process.env.SLACK_WEBHOOK_URL; describe('slack-send', () => { const fakeCore = sinon.stub(core); + const fakeGithub = sinon.stub(github); beforeEach(() => { sinon.reset(); }); @@ -66,6 +68,7 @@ describe('slack-send', () => { // Prepare fakeCore.getInput.withArgs('channel-id').returns('C123456'); fakeCore.getInput.withArgs('payload-file-path').returns('./test/resources/valid-payload.json'); + fakeGithub.context.actor = 'user123'; // Run await slackSend(fakeCore); @@ -78,6 +81,7 @@ describe('slack-send', () => { 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'); + assert.equal(chatArgs.actor, 'user123', 'Correct message provided to postMessage'); }); }); describe('sad path', () => {