Skip to content

Commit

Permalink
Merge pull request #1 from zimeg/fix-payload-file-path-parsed-default
Browse files Browse the repository at this point in the history
fix: default to true when parsing and replacing values in payload json files
  • Loading branch information
talgendler committed Apr 17, 2024
2 parents d6f3bad + 7fa92a6 commit 84038e9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@ or
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
```

> To keep the payload json file as is, without replacing values from `github.context`, pass an additional parameter `payload-file-path-parsed`
> 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: 'true'
payload-file-path-parsed: false
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
```
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ inputs:
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: 'If payload-file-path is provided instead of json payload, do not replace values in the file from `github.context`.'
description: 'Replace templated variables in the JSON payload file with values from the GitHub context and environment variables'
default: true
required: false
default: '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
Expand Down
8 changes: 3 additions & 5 deletions src/slack-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ module.exports = async function slackSend(core) {

const payloadFilePath = core.getInput('payload-file-path');

const useFileAsIs = core.getInput('payload-file-path-parsed');
/** 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');
if (!useFileAsIs || useFileAsIs !== 'true') {
// parse github context variables
if (payloadFilePathParsed) {
const context = { github: github.context, env: process.env };
const payloadString = payload.replaceAll('${{', '{{');
payload = markup.up(payloadString, context);
Expand Down Expand Up @@ -131,14 +131,12 @@ module.exports = async function slackSend(core) {

try {
await axios.post(webhookUrl, payload, axiosOpts);
// console.log(JSON.stringify(payload));
} catch (err) {
console.log('axios post failed, double check the payload being sent includes the keys Slack expects');
if ('toJSON' in err) {
console.error(JSON.stringify(err.toJSON()));
}
console.error(`Attempted to POST payload: ${JSON.stringify(payload)}`);
// console.log(err);

if (err.response) {
core.setFailed(err.response.data);
Expand Down
8 changes: 5 additions & 3 deletions test/slack-send-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -105,11 +106,12 @@ describe('slack-send', () => {
assert.equal(chatArgs.oliver, 'benji', 'Correct message provided to postMessage');
assert.equal(chatArgs.actor, 'user123', 'Correct message provided to postMessage');
});
it('should accept a payload-file-path and use it\'s content in the message without replacing anything', async () => {

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.getInput.withArgs('payload-file-path-parsed').returns('true');
fakeCore.getBooleanInput.withArgs('payload-file-path-parsed').returns(false);
fakeGithub.context.actor = 'user123';

// Run
Expand Down

0 comments on commit 84038e9

Please sign in to comment.