From 3b53905be105709b4b50d0325a8104af01ed0cc4 Mon Sep 17 00:00:00 2001 From: Steve Gill Date: Tue, 3 May 2022 23:08:37 -0700 Subject: [PATCH] #73 added thread-ts as an output (#74) * added thread-ts as an output. Fixes #73 * added example of thread_ts being used in a followup step --- .github/workflows/main.yml | 13 +++++++++++++ action.yml | 2 ++ src/slack-send.js | 10 +++++++++- test/slack-send-test.js | 6 +++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 70baea2..8d7a6ef 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -32,6 +32,19 @@ jobs: - name: Check Action output is not empty run: test -n "${{ steps.slackToken.outputs.time }}" + - name: Post Threaded Response + id: slackThreadResponse + uses: ./ + with: + channel-id: ${{ secrets.SLACK_CHANNEL_ID }} + payload: | + { + "text": "This message should be posted as a response in thread", + "thread_ts": "${{ steps.slackToken.outputs.thread_ts }}" + } + env: + SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} + integration_test_webhook: runs-on: ubuntu-latest steps: diff --git a/action.yml b/action.yml index 2a311e5..4ea5cd9 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,8 @@ inputs: outputs: time: # id of output description: 'The time' + thread_ts: # timestamp on the message that was posted when using bot token + description: 'The timestamp on the message that was posted into Slack when using bot token' runs: using: 'node12' main: 'dist/index.js' diff --git a/src/slack-send.js b/src/slack-send.js index bd6054a..6719320 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -31,6 +31,8 @@ module.exports = async function slackSend(core) { const payloadFilePath = core.getInput('payload-file-path'); + let webResponse; + if (payloadFilePath && !payload) { try { payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8'); @@ -68,7 +70,7 @@ module.exports = async function slackSend(core) { if (message.length > 0 || payload) { // post message - await web.chat.postMessage({ channel: channelId, text: message, ...(payload || {}) }); + webResponse = await web.chat.postMessage({ channel: channelId, text: message, ...(payload || {}) }); } else { console.log('Missing slack-message or payload! Did not send a message via chat.postMessage with botToken', { channel: channelId, text: message, ...(payload) }); throw new Error('Missing message content, please input a valid payload or message to send. No Message has been send.'); @@ -112,6 +114,12 @@ module.exports = async function slackSend(core) { } } + if (webResponse && webResponse.ok) { + // return the thread_ts if it exists, if not return the ts + const thread_ts = webResponse.thread_ts ? webResponse.thread_ts : webResponse.ts; + core.setOutput('thread_ts', thread_ts); + } + const time = (new Date()).toTimeString(); core.setOutput('time', time); } catch (error) { diff --git a/test/slack-send-test.js b/test/slack-send-test.js index 06735b5..c9396e0 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -5,7 +5,7 @@ const github = require('@actions/github'); const rewiremock = require('rewiremock/node'); const ChatStub = { - postMessage: sinon.spy(), + postMessage: sinon.fake.resolves({ ok: true, thread_ts: '1503435956.000247' }), }; /* eslint-disable-next-line global-require */ rewiremock(() => require('@slack/web-api')).with({ @@ -57,6 +57,8 @@ describe('slack-send', () => { fakeCore.getInput.withArgs('slack-message').returns('who let the dogs out?'); fakeCore.getInput.withArgs('channel-id').returns('C123456'); await slackSend(fakeCore); + assert.equal(fakeCore.setOutput.firstCall.firstArg, 'thread_ts', 'Output name set to thread_ts'); + assert(fakeCore.setOutput.firstCall.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; @@ -74,6 +76,8 @@ describe('slack-send', () => { await slackSend(fakeCore); // Assert + assert.equal(fakeCore.setOutput.firstCall.firstArg, 'thread_ts', 'Output name set to thread_ts'); + assert(fakeCore.setOutput.firstCall.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;