Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (68 loc) · 2.58 KB
/
Copy pathindex.js
File metadata and controls
84 lines (68 loc) · 2.58 KB
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
const Promise = require('bluebird');
const Slack = require('./src/Slack');
const Webhook = require('./src/Webhook');
class ServerlessPluginNotification {
custom() {
return Object.assign(
this.serverless.service.custom.notification || {}
);
}
constructor(serverless, options) {
this.serverless = serverless;
this.invocationId = this.serverless.invocationId;
this.options = options;
this.service = this.serverless.service;
this.hooks = {
// before service deployment
'before:deploy:deploy': () => Promise.bind(this)
.then(() => Object.assign(this, this.getDeploymentInfo()))
.then(() => this.buildDeploymentNotification('Deployment started', 'warning'))
.then(notification => this.sendNotification(notification, this.serverless.cli.consoleLog)),
// after service deployment
'after:deploy:deploy': () => Promise.bind(this)
.then(() => Object.assign(this, this.getDeploymentInfo()))
.then(() => this.buildDeploymentNotification('Deployment succeeded', 'good'))
.then(notification => this.sendNotification(notification, this.serverless.cli.consoleLog)),
};
}
getDeploymentInfo() {
const functions = this.service.getAllFunctions();
const endpoints = functions
.map(func => this.service.getAllEventsInFunction(func))
.reduce((acc, curr) => acc.concat(curr.filter(i => i.http)), [])
.map(i => i.http);
return {
provider: this.service.provider,
functions,
endpoints,
};
}
buildDeploymentNotification(message, severity) {
const deployer = process.env.USER || process.env.LOGNAME || process.env.USERNAME || process.env.SUDO_USER || process.env.LNAME || this.custom().deployer || 'Unnamed deployer';
return {
deployer,
invocationId: this.invocationId,
message,
providerName: this.provider.name,
stage: this.options.stage || this.provider.stage,
region: this.options.region || this.provider.region,
runtime: this.provider.runtime,
functions: this.functions,
endpoints: this.endpoints,
severity,
};
}
sendNotification(notification, logger) {
const promises = [];
if (this.custom().slack) {
const slackHandler = new Slack(this.custom().slack);
promises.push(slackHandler.notify(notification, logger));
}
if (this.custom().webhook) {
const webhookHandler = new Webhook(this.custom().webhook);
promises.push(webhookHandler.notify(notification, logger));
}
return Promise.all(promises);
}
}
module.exports = ServerlessPluginNotification;