UNPKG

1.51 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.sendWorkspacesSlackMessage = void 0;
9
10var _requestPromiseNative = _interopRequireDefault(require("request-promise-native"));
11
12var _utils = require("./utils");
13
14const sendWorkspacesSlackMessage = async (logs, prefixes, options) => {
15 if (typeof options.token !== 'string') {
16 throw new Error('Slack token is required');
17 }
18
19 const allAttachments = logs.map(bump => ({
20 color: options.colors[bump.type],
21 fields: [{
22 title: `${bump.name} v${bump.version}`,
23 value: bump.messages.map(message => `${prefixes.required[message.type].value} ${message.value}`).join('\n')
24 }]
25 }));
26 const numIterations = Math.ceil(allAttachments.length / _utils.MAX_ATTACHMENTS);
27 const attachmentsInIteration = Math.ceil(allAttachments.length / numIterations);
28
29 for (let i = 0; i < numIterations; ++i) {
30 await (0, _requestPromiseNative.default)({
31 uri: `${_utils.SLACK_HOOKS_URL}${options.token}`,
32 method: 'POST',
33 json: {
34 channel: options.channel,
35 username: options.username,
36 link_names: '1',
37 icon_emoji: options.iconEmoji,
38 unfurl_media: false,
39 attachments: allAttachments.slice(i * attachmentsInIteration, Math.min((i + 1) * attachmentsInIteration, allAttachments.length))
40 }
41 });
42 }
43};
44
45exports.sendWorkspacesSlackMessage = sendWorkspacesSlackMessage;
\No newline at end of file