-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
55 lines (49 loc) · 1.77 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
54
55
const {
env: { PUSHOVER_USER_KEY, PUSHOVER_API_TOKEN, URL },
} = require('process');
const sendPushOverNotification = require('./pushover-helper');
const getSuccessMsg = ({ successMessage }) => {
const defaultMessage = `Hi there, we just deployed the site successfully 🎉`;
return `${successMessage || defaultMessage} \n\n👉 ${URL}`;
};
const getErrorMsg = ({ errorMessage }) => {
const defaultMessage = `Hi there, Latest build failed 😱\n\nCheck your build's log for more details`;
return `${errorMessage || defaultMessage} \n\n👉 ${URL}`;
};
const precheck = () => {
if (!PUSHOVER_USER_KEY || !PUSHOVER_API_TOKEN) {
throw new Error(
'PUSHOVER_USER_KEY or PUSHOVER_API_TOKEN is not available as environment variable'
);
}
};
const pluginFailureHandler = (error, { utils }) =>
utils.build.failPlugin('Failed to send Pushover message', { error });
module.exports = {
async onSuccess(pluginApi) {
try {
precheck();
const { inputs } = pluginApi;
const message = getSuccessMsg(inputs);
console.log('Sending build success message via Pushover');
await sendPushOverNotification({ message });
} catch (error) {
return pluginFailureHandler(error, pluginApi);
}
},
async onError(pluginApi) {
try {
precheck();
const { inputs } = pluginApi;
const message = getErrorMsg(inputs);
console.log('Sending build failed message via Pushover');
await sendPushOverNotification({
message,
priority: 1,
sound: 'siren',
});
} catch (error) {
return pluginFailureHandler(error, pluginApi);
}
},
};