UNPKG

sfdx-hardis

Version:

Swiss-army-knife Toolbox for Salesforce. Allows you to define a complete CD/CD Pipeline. Orchestrate base commands and assist users with interactive wizards

99 lines 4.23 kB
import { SfError } from "@salesforce/core"; import c from "chalk"; import { NotifProviderRoot } from "./notifProviderRoot.js"; import { WebClient } from "@slack/web-api"; import { getCurrentGitBranch, uxLog } from "../utils/index.js"; import { UtilsNotifs } from "./index.js"; import { getEnvVar } from "../../config/index.js"; export class SlackProvider extends NotifProviderRoot { slackClient; constructor() { super(); this.token = process.env.SLACK_TOKEN || ""; this.slackClient = new WebClient(this.token); } getLabel() { return "sfdx-hardis Slack connector"; } // eslint-disable-next-line @typescript-eslint/no-unused-vars async postNotification(notifMessage) { const mainNotifsChannelId = getEnvVar("SLACK_CHANNEL_ID"); if (mainNotifsChannelId == null) { throw new SfError("[SlackProvider] You need to define a variable SLACK_CHANNEL_ID to use sfdx-hardis Slack Integration. Otherwise, remove variable SLACK_TOKEN"); } const slackChannelsIds = mainNotifsChannelId.split(","); // Add branch custom slack channel if defined const customSlackChannelVariable = `SLACK_CHANNEL_ID_${(await getCurrentGitBranch() || "").toUpperCase()}`; if (getEnvVar(customSlackChannelVariable)) { slackChannelsIds.push(...(getEnvVar(customSlackChannelVariable) || "").split(",")); } // Handle specific channel for Warnings and errors const warningsErrorsChannelId = getEnvVar("SLACK_CHANNEL_ID_ERRORS_WARNINGS"); if (warningsErrorsChannelId && ["critical", "error", "warning"].includes(notifMessage.severity || null)) { slackChannelsIds.push(...warningsErrorsChannelId.split(",")); } // Main block const blocks = []; const block = { type: "section", text: { type: "mrkdwn", text: UtilsNotifs.prefixWithSeverityEmoji(notifMessage.text, notifMessage.severity), }, }; /* Disable until we don't know how to use it cleanly if (notifMessage.sideImage) { block.accessory = { "type": "image", "image_url": notifMessage.sideImage, "alt_text": "sfdx-hardis" } } */ blocks.push(block); // Add action blocks if (notifMessage.buttons?.length && notifMessage.buttons?.length > 0) { const actionElements = []; for (const button of notifMessage.buttons) { // Url button if (button.url) { const actionsElement = { type: "button", text: { type: "plain_text", text: button.text, }, style: button.style || "primary", url: button.url, }; actionElements.push(actionsElement); } } const actionsBlock = { type: "actions", elements: actionElements, }; blocks.push(actionsBlock); } // Post messages for (const slackChannelId of slackChannelsIds) { const slackMessage = { text: notifMessage.text, attachments: notifMessage.attachments, blocks: blocks, channel: slackChannelId, unfurl_links: false, unfurl_media: false, }; try { const resp = await this.slackClient.chat.postMessage(slackMessage); uxLog(this, c.cyan(`[SlackProvider] Sent slack notification to channel ${mainNotifsChannelId}: ${resp.ok}`)); } catch (error) { uxLog(this, c.gray("[SlackProvider] Failed slack message content: \n" + JSON.stringify(slackMessage, null, 2))); uxLog(this, c.red(`[SlackProvider] Error while sending message to channel ${mainNotifsChannelId}\n${error.message}`)); } } return; } } //# sourceMappingURL=slackProvider.js.map