-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
60 lines (51 loc) · 1.6 KB
/
handler.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
var request = require('request');
const SLACK_URL = process.env.SLACK_URL;
exports.handler = (event, context, callback) => {
request(generateRequestDetails(event, SLACK_URL), function (err, res, body) {
if (res && (res.statusCode === 200 || res.statusCode === 201)) {
callback(null, 'Done');
}
else {
console.log('Error: ' + err + ' ' + res + ' ' + body);
callback('Error');
}
});
};
function generateRequestDetails(event, url) {
if (event['detail-type'] != "CodePipeline Pipeline Execution State Change")
throw new Error ("Unsupported detail type: " + event['detail-type']);
var color;
var text = "CodePipeline " + event.detail.pipeline + " ";
var pipelineState = event.detail.state;
if (pipelineState == 'STARTED') {
color = "#888888";
text += "has started."
}
else if (pipelineState == 'SUCCEEDED') {
color = "good";
text += "has *succeeded*.";
}
else if (pipelineState == 'FAILED') {
color = "danger";
text += "has *failed*.";
}
else {
color = "warning";
text += "has " + pipelineState + " (This is an unknown state to the Slack notifier.)";
}
console.log('Posting following message to Slack: ' + text);
var options = {
url: url,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
json: {
attachments: [ {text: text, color: color}]
}
};
return options;
}
exports.__test__ = {
generateRequestDetails: generateRequestDetails
};