UNPKG

@gravityforms/gulp-tasks

Version:
78 lines (68 loc) 2.26 kB
import { WebClient } from '@slack/web-api'; const slackClient = new WebClient( process.env.HAL_SLACK_TOKEN ); const errorMessageEmojis = [ 'alert', 'face_with_symbols_on_mouth', 'facetater', 'sob', 'cool-sob', 'trynottocry', 'trudy-cry', 'oof' ]; /** * @description Select a random emoji for Slack error messages. * * @since 8.4.0 * * @return {string} Returns a Slack emoji shortcode. */ function getRandomErrorEmoji() { const randomIndex = Math.floor( Math.random() * errorMessageEmojis.length ); return `:${ errorMessageEmojis[ randomIndex ] }:`; } /** * @description Send a Slack message for build notifications. * * @since 8.4.0 * * @param {string} [message=''] Message text. * @param {string} [messageType='success'] Message variant. * @param {Array<Object>} [attachments=[]] Slack attachments. * @param {string} [channel=''] Target channel override. * * @return {Promise<void>} Resolves when message handling is complete. */ const sendSlackMessage = async ( message = '', messageType = 'success', attachments = [], channel = '' ) => { if ( process.env.GULP_PRODUCTION_MODE !== 'true' ) { console.log( 'Slack message not sent in non-production mode.' ); return; } if ( messageType === 'error' ) { const emoji = getRandomErrorEmoji(); try { const res = await slackClient.chat.postMessage( { channel: channel || process.env.GRAVITY_BUILD_CHANNEL_ID, text: `${ emoji } *Build Error*\n\nI was unable to complete the build process for you <@${ process.env.SLACK_USER_ID }>.\n\n`, mrkdwn: true, attachments: [ { color: '#f00', text: `Error: ${ message }`, }, { color: '#03DBE8', text: `Check out the <${ process.env.GHA_URL }|GitHub Actions Workflow> for more details.`, }, ], } ); console.log( 'Slack message sent: ', res.ts ); } catch ( error ) { console.error( error ); } } else { try { const res = await slackClient.chat.postMessage( { channel: channel || process.env.GRAVITY_BUILD_CHANNEL_ID, text: message, mrkdwn: true, attachments, } ); console.log( 'Slack message sent: ', res.ts ); } catch ( error ) { console.error( error ); } } }; export default sendSlackMessage;