UNPKG

1.18 kBJavaScriptView Raw
1import request from 'request-promise-native';
2import { SLACK_HOOKS_URL, MAX_ATTACHMENTS } from "./utils";
3export const sendWorkspacesSlackMessage = async (logs, prefixes, options) => {
4 if (typeof options.token !== 'string') {
5 throw new Error('Slack token is required');
6 }
7
8 const allAttachments = logs.map(bump => ({
9 color: options.colors[bump.type],
10 fields: [{
11 title: `${bump.name} v${bump.version}`,
12 value: bump.messages.map(message => `${prefixes.required[message.type].value} ${message.value}`).join('\n')
13 }]
14 }));
15 const numIterations = Math.ceil(allAttachments.length / MAX_ATTACHMENTS);
16 const attachmentsInIteration = Math.ceil(allAttachments.length / numIterations);
17
18 for (let i = 0; i < numIterations; ++i) {
19 await request({
20 uri: `${SLACK_HOOKS_URL}${options.token}`,
21 method: 'POST',
22 json: {
23 channel: options.channel,
24 username: options.username,
25 link_names: '1',
26 icon_emoji: options.iconEmoji,
27 unfurl_media: false,
28 attachments: allAttachments.slice(i * attachmentsInIteration, Math.min((i + 1) * attachmentsInIteration, allAttachments.length))
29 }
30 });
31 }
32};
\No newline at end of file