From e8eb4d342eeb9efa11b1d4a6486fbf6ca42b533a Mon Sep 17 00:00:00 2001 From: Dale Date: Fri, 27 Nov 2020 16:27:24 +0100 Subject: [PATCH] feat(message): add custom message to action --- README.md | 4 +++ __tests__/utils.spec.js | 30 ++++++++++++++++++++ action.yml | 3 ++ index.js | 3 +- src/utils.js | 61 +++++++++++++++++++++++------------------ 5 files changed, 73 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index d6d35177..9c2cd4a5 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,10 @@ The ID of a previous Slack message to update instead of posting a new message. T message_id: ${{ steps..outputs.message_id }} ``` +### `message` + +A custom message. + ## Outputs ### `message_id` diff --git a/__tests__/utils.spec.js b/__tests__/utils.spec.js index 7c5b48c2..05f1660b 100644 --- a/__tests__/utils.spec.js +++ b/__tests__/utils.spec.js @@ -59,6 +59,21 @@ describe('Utils', () => { short: true, }); }); + + it('show message', () => { + const attachments = buildSlackAttachments({ + status: 'STARTED', + color: 'good', + github: GITHUB_PUSH_EVENT, + message: 'message', + }); + + expect(attachments[0].fields.find(a => a.title === 'Message')).toEqual({ + title: 'Message', + value: 'message', + short: false, + }); + }); }); describe('for PR events', () => { @@ -91,6 +106,21 @@ describe('Utils', () => { short: true, }); }); + + it('show message', () => { + const attachments = buildSlackAttachments({ + status: 'STARTED', + color: 'good', + github: GITHUB_PR_EVENT, + message: 'message', + }); + + expect(attachments[0].fields.find(a => a.title === 'Message')).toEqual({ + title: 'Message', + value: 'message', + short: false, + }); + }); }); }); }); diff --git a/action.yml b/action.yml index 78031ff7..e225fbca 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,9 @@ inputs: description: 'The color of the Slack attachment.' required: true default: '#cccccc' + message: + description: 'A custom message to show.' + required: false message_id: description: 'The ID of the existing Slack message to update.' required: false diff --git a/index.js b/index.js index 4ad149c0..d76a4c65 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ const { buildSlackAttachments, formatChannelName } = require('./src/utils'); const channel = core.getInput('channel'); const status = core.getInput('status'); const color = core.getInput('color'); + const message = core.getInput('message'); const messageId = core.getInput('message_id'); const token = process.env.SLACK_BOT_TOKEN; const slack = new WebClient(token); @@ -17,7 +18,7 @@ const { buildSlackAttachments, formatChannelName } = require('./src/utils'); return; } - const attachments = buildSlackAttachments({ status, color, github }); + const attachments = buildSlackAttachments({ status, color, github, message }); const channelId = core.getInput('channel_id') || (await lookUpChannelId({ slack, channel })); if (!channelId) { diff --git a/src/utils.js b/src/utils.js index 419ad24d..9a747d03 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,6 +1,6 @@ const { context } = require('@actions/github'); -function buildSlackAttachments({ status, color, github }) { +function buildSlackAttachments({ status, color, github, message }) { const { payload, ref, workflow, eventName } = github.context; const { owner, repo } = context.repo; const event = eventName; @@ -21,32 +21,39 @@ function buildSlackAttachments({ status, color, github }) { short: true, }; - return [ - { - color, - fields: [ - { - title: 'Action', - value: ``, - short: true, - }, - { - title: 'Status', - value: status, - short: true, - }, - referenceLink, - { - title: 'Event', - value: event, - short: true, - }, - ], - footer_icon: 'https://github.githubassets.com/favicon.ico', - footer: ``, - ts: Math.floor(Date.now() / 1000), - }, - ]; + var attachment = { + color, + fields: [ + { + title: 'Action', + value: ``, + short: true, + }, + { + title: 'Status', + value: status, + short: true, + }, + referenceLink, + { + title: 'Event', + value: event, + short: true, + }, + ], + footer_icon: 'https://github.githubassets.com/favicon.ico', + footer: ``, + ts: Math.floor(Date.now() / 1000), + }; + + if (message) + attachment['fields'].push({ + title: 'Message', + value: message, + short: false, + }); + + return [attachment]; } module.exports.buildSlackAttachments = buildSlackAttachments;