-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.test.js
356 lines (308 loc) · 13 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
const assert = require('assert');
const nock = require('nock');
const rewire = require('rewire');
describe('@someimportantcompany/github-actions-slack-notify', () => {
const action = rewire('./index');
const env = {
GITHUB_EVENT_NAME: 'push',
GITHUB_SERVER_URL: 'https://github.com',
GITHUB_API_URL: 'https://api.github.com',
GITHUB_GRAPHQL_URL: 'https://api.github.com/graphql',
GITHUB_WORKFLOW: 'CI/CD',
GITHUB_RUN_ID: '1234',
GITHUB_RUN_NUMBER: '1',
GITHUB_JOB: 'abcd1234',
GITHUB_ACTION: 'a1b2c3d4',
GITHUB_ACTIONS: '',
GITHUB_ACTOR: 'jdrydn',
GITHUB_REPOSITORY: 'jdrydn/github-actions-slack-message',
GITHUB_REF: 'refs/heads/master',
GITHUB_SHA: 'shashasha',
GITHUB_HEAD_REF: '',
};
before(() => Object.assign(process.env, env));
describe('buildAttachmentBlock', () => {
const buildAttachmentBlock = action.__get__('buildAttachmentBlock');
beforeEach(() => Object.assign(process.env, env));
after(() => Object.assign(process.env, env));
it('should create a Slack attachment', () => {
Object.assign(process.env, {
...env,
GITHUB_WORKFLOW: '',
GITHUB_RUN_ID: '',
GITHUB_ACTOR: '',
GITHUB_REPOSITORY: 'a/b',
GITHUB_REF: 'refs/heads/develop',
});
const attachment = buildAttachmentBlock({ color: '#4453A6' });
assert.deepStrictEqual(attachment, {
color: '#4453A6',
fallback: '[a/b] (develop) undefined',
mrkdwn_in: [ 'text' ],
title: undefined,
text: undefined,
footer: '*<https://github.com/a/b|a/b>* (<https://github.com/a/b/tree/develop|develop>)',
footer_icon: 'https://slack.github.com/static/img/favicon-neutral.png',
});
});
it('should create a Slack attachment with an author', () => {
const attachment = buildAttachmentBlock({ color: 'danger', text: 'Hello, world!' });
assert.deepStrictEqual(attachment, {
color: 'danger',
fallback: '[jdrydn/github-actions-slack-message] (master) Hello, world!',
mrkdwn_in: [ 'text' ],
author_name: 'jdrydn',
author_icon: 'https://github.com/jdrydn.png',
author_link: 'https://github.com/jdrydn',
title: 'CI/CD (#shashash)',
title_link: 'https://github.com/jdrydn/github-actions-slack-message/actions/runs/1234',
text: 'Hello, world!',
footer: '*<https://github.com/jdrydn/github-actions-slack-message|jdrydn/github-actions-slack-message>* (<https://github.com/jdrydn/github-actions-slack-message/tree/master|master>)',
footer_icon: 'https://slack.github.com/static/img/favicon-neutral.png',
});
});
it('should create a Slack attachment with a pull-request', () => {
Object.assign(process.env, {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_HEAD_REF: 'refs/heads/hotfix/quick-fix',
GITHUB_ACTOR: '',
});
const attachment = buildAttachmentBlock({});
assert.deepStrictEqual(attachment, {
fallback: '[jdrydn/github-actions-slack-message] (hotfix/quick-fix) undefined',
mrkdwn_in: [ 'text' ],
title: 'CI/CD (#shashash)',
title_link: 'https://github.com/jdrydn/github-actions-slack-message/actions/runs/1234',
text: undefined,
footer: '*<https://github.com/jdrydn/github-actions-slack-message|jdrydn/github-actions-slack-message>* (<https://github.com/jdrydn/github-actions-slack-message/tree/hotfix/quick-fix|hotfix/quick-fix>)',
footer_icon: 'https://slack.github.com/static/img/favicon-neutral.png',
});
});
it('should create a Slack attachment with a pull-request & author', () => {
Object.assign(process.env, {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_HEAD_REF: 'refs/heads/hotfix/quick-fix',
});
const attachment = buildAttachmentBlock({});
assert.deepStrictEqual(attachment, {
fallback: '[jdrydn/github-actions-slack-message] (hotfix/quick-fix) undefined',
mrkdwn_in: [ 'text' ],
title: 'CI/CD (#shashash)',
title_link: 'https://github.com/jdrydn/github-actions-slack-message/actions/runs/1234',
author_icon: 'https://github.com/jdrydn.png',
author_link: 'https://github.com/jdrydn',
author_name: 'jdrydn',
text: undefined,
footer: '*<https://github.com/jdrydn/github-actions-slack-message|jdrydn/github-actions-slack-message>* (<https://github.com/jdrydn/github-actions-slack-message/tree/hotfix/quick-fix|hotfix/quick-fix>)',
footer_icon: 'https://slack.github.com/static/img/favicon-neutral.png',
});
});
it('should create a Slack attachment with a title', () => {
Object.assign(process.env, {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_HEAD_REF: 'refs/heads/hotfix/quick-fix',
});
const attachment = buildAttachmentBlock({ title: 'Hello, world!' });
assert.deepStrictEqual(attachment, {
fallback: '[jdrydn/github-actions-slack-message] (hotfix/quick-fix) undefined',
mrkdwn_in: [ 'text' ],
title: 'Hello, world!',
title_link: 'https://github.com/jdrydn/github-actions-slack-message/actions/runs/1234',
author_icon: 'https://github.com/jdrydn.png',
author_link: 'https://github.com/jdrydn',
author_name: 'jdrydn',
text: undefined,
footer: '*<https://github.com/jdrydn/github-actions-slack-message|jdrydn/github-actions-slack-message>* (<https://github.com/jdrydn/github-actions-slack-message/tree/hotfix/quick-fix|hotfix/quick-fix>)',
footer_icon: 'https://slack.github.com/static/img/favicon-neutral.png',
});
});
it('should create a Slack attachment with a title, image & thumbnail', () => {
Object.assign(process.env, {
...env,
GITHUB_EVENT_NAME: 'pull_request',
GITHUB_HEAD_REF: 'refs/heads/hotfix/quick-fix',
});
const attachment = buildAttachmentBlock({
title: 'Hello, world!',
imageUrl: 'https://www.youtube.com/s/desktop/e213795e/img/favicon_96x96.png',
thumbUrl: 'https://www.youtube.com/s/desktop/e213795e/img/favicon_144x144.png',
});
assert.deepStrictEqual(attachment, {
fallback: '[jdrydn/github-actions-slack-message] (hotfix/quick-fix) undefined',
mrkdwn_in: [ 'text' ],
title: 'Hello, world!',
title_link: 'https://github.com/jdrydn/github-actions-slack-message/actions/runs/1234',
author_icon: 'https://github.com/jdrydn.png',
author_link: 'https://github.com/jdrydn',
author_name: 'jdrydn',
text: undefined,
image_url: 'https://www.youtube.com/s/desktop/e213795e/img/favicon_96x96.png',
thumb_url: 'https://www.youtube.com/s/desktop/e213795e/img/favicon_144x144.png',
footer: '*<https://github.com/jdrydn/github-actions-slack-message|jdrydn/github-actions-slack-message>* (<https://github.com/jdrydn/github-actions-slack-message/tree/hotfix/quick-fix|hotfix/quick-fix>)',
footer_icon: 'https://slack.github.com/static/img/favicon-neutral.png',
});
});
});
describe('sendToSlack', () => {
const sendToSlack = action.__get__('sendToSlack');
before(() => Object.assign(process.env, env));
afterEach(() => nock.cleanAll());
it('should send a message with a bot token', async () => {
const scope = nock('https://slack.com', {
reqheaders: {
authorization: 'Bearer some-important-bot-token',
'user-agent': 'jdrydn/github-actions-slack-message (via someimportantcompany/github-actions-slack-notify)',
},
})
.post('/api/chat.postMessage', { text: 'c' })
.reply(200, { ok: true });
await sendToSlack({ botToken: 'some-important-bot-token' }, { text: 'c' });
scope.done();
});
it('should handle errors returned from Slack', async () => {
const scope = nock('https://slack.com', {
reqheaders: {
authorization: 'Bearer some-important-bot-token',
'user-agent': 'jdrydn/github-actions-slack-message (via someimportantcompany/github-actions-slack-notify)',
},
})
.post('/api/chat.postMessage', { text: 'c' })
.reply(400, { ok: false, error: 'not-the-droids-you-are-looking-for' });
try {
await sendToSlack({ botToken: 'Bearer some-important-bot-token' }, { text: 'c' });
} catch (err) {
assert.ok(err instanceof Error);
assert.strictEqual(err.message, 'Error from Slack: not-the-droids-you-are-looking-for');
}
scope.done();
});
it('should send a message to a webhook URL', async () => {
const scope = nock('https://some-important-webhook', {
reqheaders: {
'user-agent': 'jdrydn/github-actions-slack-message (via someimportantcompany/github-actions-slack-notify)',
},
})
.post('/', { text: 'c' })
.reply(200, 'ok');
await sendToSlack({ webhookUrl: 'https://some-important-webhook' }, { text: 'c' });
scope.done();
});
it('should send an update with a bot token', async () => {
const scope = nock('https://slack.com', {
reqheaders: {
authorization: 'Bearer some-important-bot-token',
'user-agent': 'jdrydn/github-actions-slack-message (via someimportantcompany/github-actions-slack-notify)',
},
})
.post('/api/chat.update', { text: 'c', ts: 1620307308705 })
.reply(200, { ok: true });
await sendToSlack({ botToken: 'some-important-bot-token' }, { text: 'c', ts: 1620307308705 });
scope.done();
});
it('should throw an error if no botToken/webhookUrl is present', async () => {
try {
await sendToSlack({}, {});
assert.fail('Should have failed');
} catch (err) {
assert.ok(err instanceof Error);
assert.strictEqual(err.message, 'Missing botToken/webhookUrl');
}
});
});
const buildAttachmentBlock = () => 'ATTACHMENT';
const mockCore = ({ inputs = {}, outputs = {} } = {}) => ({
debug: () => null,
getInput: key => inputs[key] || null,
getOutput: key => outputs[key] || null,
setOutput: (key, value) => outputs[key] = value,
getFailed: () => outputs.failed || null,
setFailed: value => outputs.failed = value,
});
it('should send a message to Slack', async () => {
const core = mockCore({
inputs: {
'channel': 'some-important-channel-id',
'webhook-url': 'some-important-webhook-url',
'text': 'Some important message',
// 'color': '',
// 'message-id': '',
},
});
const sendToSlack = (conn, args) => {
assert.deepStrictEqual(conn, {
botToken: null,
webhookUrl: 'some-important-webhook-url',
});
assert.deepStrictEqual(args, {
channel: 'some-important-channel-id',
attachments: [ 'ATTACHMENT' ],
});
return {
ts: 'some-message-id',
};
};
await action.__with__({ core, buildAttachmentBlock, sendToSlack })(() => action());
assert.strictEqual(core.getOutput('message-id'), null);
assert.strictEqual(core.getFailed(), null);
});
it('should send a message to Slack with details', async () => {
const core = mockCore({
inputs: {
'webhook-url': 'some-important-webhook-url',
'text': 'Some important message',
'username': 'git-bot',
'icon-emoji': ':rocket:',
'icon-url': 'https://github.com/someimportantcompany.png',
},
});
const sendToSlack = (conn, args) => {
assert.deepStrictEqual(conn, {
botToken: null,
webhookUrl: 'some-important-webhook-url',
});
assert.deepStrictEqual(args, {
username: 'git-bot',
icon_emoji: ':rocket:',
icon_url: 'https://github.com/someimportantcompany.png',
attachments: [ 'ATTACHMENT' ],
});
return {
ts: 'some-message-id',
};
};
await action.__with__({ core, buildAttachmentBlock, sendToSlack })(() => action());
assert.strictEqual(core.getOutput('message-id'), null);
assert.strictEqual(core.getFailed(), null);
});
it('should an update message', async () => {
const core = mockCore({
inputs: {
'channel': 'some-important-channel-id',
'bot-token': 'some-important-bot-token',
'text': 'Some important message',
'color': 'good',
'message-id': 'some-previous-important-message-id',
},
});
const sendToSlack = (conn, args) => {
assert.deepStrictEqual(conn, {
botToken: 'some-important-bot-token',
webhookUrl: null,
});
assert.deepStrictEqual(args, {
channel: 'some-important-channel-id',
ts: 'some-previous-important-message-id',
attachments: [ 'ATTACHMENT' ],
});
return {
ts: 'some-message-id',
};
};
await action.__with__({ core, buildAttachmentBlock, sendToSlack })(() => action());
assert.strictEqual(core.getOutput('message-id'), 'some-message-id');
assert.strictEqual(core.getFailed(), null);
});
});