* added thread-ts as an output. Fixes #73 * added example of thread_ts being used in a followup step
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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'
|
||||
|
||||
+9
-1
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user