generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
53 lines (42 loc) · 1.48 KB
/
index.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
const core = require('@actions/core');
const axios = require('axios');
const crypto = require('crypto');
const VALID_MSGTYPES = ['text', 'url', 'markdown', 'actionCard', 'feedCard'];
async function run () {
try {
const webhook = core.getInput('webhook', { required: true });
const msgtype = core.getInput('msgtype');
const textContent = core.getInput('content', { required: true });
const textAt = core.getInput('at');
const secret = core.getInput('secret');
if (!VALID_MSGTYPES.includes(msgtype)) throw new Error(`msgtype should be one of ${VALID_MSGTYPES.join(',')}`);
const content = JSON.parse(textContent);
const at = textAt ? JSON.parse(textAt) : {};
const payload = {
msgtype,
[msgtype]: content,
at
};
const url = new URL(webhook);
// sign the request if given
if (secret) {
const timestamp = Date.now();
const stringToSign = `${timestamp}\n${secret}`;
const sign = crypto.createHmac('sha256', secret).update(stringToSign).digest('base64');
url.searchParams.append('timestamp', timestamp);
url.searchParams.append('sign', sign);
}
const ret = await axios.post(url.toString(), JSON.stringify(payload), {
headers: {
'Content-Type': 'application/json'
}
});
console.log('response:', ret.data);
if (ret.data.errcode) {
throw new Error(`[${ret.data.errcode}] ${ret.data.message}`);
}
} catch (error) {
core.setFailed(error.message);
}
}
run();