Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#73 added thread-ts as an output #74

Merged
merged 2 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
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