deploy-notify-slack 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/notify.js +49 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,6 +17,8 @@ You can use default message template with the following env variables:
|
|
|
17
17
|
|
|
18
18
|
- TITLE - ('Deployment' by default) notification title
|
|
19
19
|
- CHANGELOG_PATH - path of your deployed version details file (`changelog` by default as well as we assume that the package installed locally, so this option is required if the package installed globally)
|
|
20
|
+
- COLOR - ('#7f8583' by default) left bar notification color. Should be hex color code without `#` symbol
|
|
21
|
+
- EMOJI - (':rocket:' by default) emoji to be displayed in the notification title
|
|
20
22
|
|
|
21
23
|
> version details file is a Markdown file having the name like `${STAGE}-v${VERSION}.md`.
|
|
22
24
|
>
|
|
@@ -156,4 +158,4 @@ Then you can run the script with the following command:
|
|
|
156
158
|
npm i --location=global deploy-notify-slack@latest
|
|
157
159
|
CUSTOM_MESSAGE=$(cat message.json)
|
|
158
160
|
SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL} CUSTOM_MESSAGE=$CUSTOM_MESSAGE node /usr/local/lib/node_modules/deploy-notify-slack/notify.js
|
|
159
|
-
```
|
|
161
|
+
```
|
package/notify.js
CHANGED
|
@@ -6,6 +6,8 @@ const slackWebHookURL = process.env.SLACK_WEBHOOK_URL;
|
|
|
6
6
|
const stage = process.env.STAGE;
|
|
7
7
|
const version = process.env.VERSION;
|
|
8
8
|
const title = process.env.TITLE || 'Deployment';
|
|
9
|
+
const color = process.env.COLOR || '7f8583';
|
|
10
|
+
const emoji = process.env.EMOJI || ':rocket:';
|
|
9
11
|
const customMessage = process.env.CUSTOM_MESSAGE;
|
|
10
12
|
const changelogPath = process.env.CHANGELOG_PATH || path.join(__dirname, '../../changelog');
|
|
11
13
|
const failsIfNotSent = process.env.FAILS_IF_NOT_SENT !== undefined
|
|
@@ -37,7 +39,7 @@ function notificationBody() {
|
|
|
37
39
|
"type": "section",
|
|
38
40
|
"text": {
|
|
39
41
|
"type": "mrkdwn",
|
|
40
|
-
"text":
|
|
42
|
+
"text": `${emoji} *${title}*`
|
|
41
43
|
}
|
|
42
44
|
},
|
|
43
45
|
{
|
|
@@ -57,6 +59,7 @@ function notificationBody() {
|
|
|
57
59
|
|
|
58
60
|
const changelog = getChangelog();
|
|
59
61
|
if (changelog) {
|
|
62
|
+
const changelogBlocks = splitTextToBlocks(changelog);
|
|
60
63
|
blocks.push(
|
|
61
64
|
{
|
|
62
65
|
"type": "divider"
|
|
@@ -66,13 +69,25 @@ function notificationBody() {
|
|
|
66
69
|
"type": "section",
|
|
67
70
|
"text": {
|
|
68
71
|
"type": "mrkdwn",
|
|
69
|
-
"text": "*Description:* \n\n" +
|
|
72
|
+
"text": "*Description:* \n\n" + changelogBlocks[0]
|
|
70
73
|
}
|
|
71
74
|
})
|
|
75
|
+
if (changelogBlocks.length > 1) {
|
|
76
|
+
for (let i = 1; i < changelogBlocks.length; i++) {
|
|
77
|
+
blocks.push({
|
|
78
|
+
"type": "section",
|
|
79
|
+
"text": {
|
|
80
|
+
"type": "mrkdwn",
|
|
81
|
+
"text": changelogBlocks[i]
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
}
|
|
72
86
|
}
|
|
73
87
|
return {
|
|
74
88
|
"attachments": [
|
|
75
89
|
{
|
|
90
|
+
"color": `#${color}`,
|
|
76
91
|
"blocks": blocks
|
|
77
92
|
}
|
|
78
93
|
]
|
|
@@ -130,6 +145,37 @@ function sendSlackMessage (webhookURL, messageBody) {
|
|
|
130
145
|
});
|
|
131
146
|
}
|
|
132
147
|
|
|
148
|
+
function splitTextToBlocks(text) {
|
|
149
|
+
let blocks = [];
|
|
150
|
+
let startIndex = 0;
|
|
151
|
+
let endIndex = 0;
|
|
152
|
+
|
|
153
|
+
const MAX_BLOCK_SIZE = 2500;
|
|
154
|
+
while (startIndex < text.length) {
|
|
155
|
+
endIndex = startIndex + MAX_BLOCK_SIZE;
|
|
156
|
+
|
|
157
|
+
// If the block does not end with a newline character, backtrack to the previous newline character.
|
|
158
|
+
if (endIndex < text.length && text[endIndex] !== '\n') {
|
|
159
|
+
while (endIndex > startIndex && text[endIndex] !== '\n') {
|
|
160
|
+
endIndex--;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// If end index is greater than text length, just take the rest of the text
|
|
165
|
+
if (endIndex > text.length) {
|
|
166
|
+
endIndex = text.length;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Add the block to the array
|
|
170
|
+
blocks.push(text.substring(startIndex, endIndex));
|
|
171
|
+
|
|
172
|
+
// Move the start index to the next position, skipping the newline character
|
|
173
|
+
startIndex = endIndex + 1;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return blocks;
|
|
177
|
+
}
|
|
178
|
+
|
|
133
179
|
function stringToBool(str, defaultValue = false){
|
|
134
180
|
switch(str.toLowerCase().trim()){
|
|
135
181
|
case "true":
|
|
@@ -193,4 +239,4 @@ function validate() {
|
|
|
193
239
|
process.exit(5);
|
|
194
240
|
}
|
|
195
241
|
}
|
|
196
|
-
})();
|
|
242
|
+
})();
|