Skip to content

Commit

Permalink
added thread-ts as an output. Fixes #73
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengill committed Mar 18, 2022
1 parent 2f8da78 commit 3bdd566
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
10 changes: 9 additions & 1 deletion src/slack-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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.');
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion test/slack-send-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 3bdd566

Please sign in to comment.