Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Created March 13, 2023 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wokamoto/f2243b0fa1ff4664ca5eb875034737c5 to your computer and use it in GitHub Desktop.
Save wokamoto/f2243b0fa1ff4664ca5eb875034737c5 to your computer and use it in GitHub Desktop.
pushToSlack.js
'use strict';
const https = require('https');
const util = require('util');
const project = process.env.project;
const slachChannel = process.env.slachChannel;
const slackPath = process.env.slackPath;
async function sendRequest(opts,replyData){
return new Promise(((resolve,reject)=>{
let req = https.request(opts, (response) => {
response.setEncoding('utf8');
let body = '';
response.on('data', (chunk)=>{
body += chunk;
});
response.on('end', ()=>{
resolve(body);
});
}).on('error', (err)=>{
reject(err);
});
req.write(replyData);
req.end();
}));
};
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, 2));
console.log('From SNS:', event.Records[0].Sns.Message);
var message = event.Records[0].Sns.Message,
message_json = {deploymentOverview: JSON.stringify({Succeeded: 0, Failed: 0, Skipped: 0, InProgress: 0, Pending: 0})};
const subject = `[${project}] ` + event.Records[0].Sns.Subject;
const fromCodeDeploy = /deploymentOverview/i.test(message);
return new Promise( (resolve, reject) => {
var overview;
try {
message_json = JSON.parse(event.Records[0].Sns.Message);
message = JSON.stringify(message_json, null, " ");
} catch(e) {
overview = {Succeeded: 0, Failed: 0, Skipped: 0, InProgress: 0, Pending: 0};
message = event.Records[0].Sns.Message;
}
try {
overview = JSON.parse(message_json.deploymentOverview);
} catch(e) {
overview = {Succeeded: 0, Failed: 0, Skipped: 0, InProgress: 0, Pending: 0};
}
resolve({
subject: subject,
message: message,
overview: overview,
postData: {}
});
})
.then(data => {
data.postData = {
"channel": slachChannel,
"username": fromCodeDeploy ? "AWS CodeDeploy via Lamda" : "AWS SNS via Lamda",
"text": `*${subject}*`,
"icon_emoji": fromCodeDeploy ? ":codedeploy:" : ":sns:",
"attachments": []
};
const butWithErrors = data.message.indexOf(" but with errors");
const stateRed = data.message.indexOf(" to RED");
const stateYellow = data.message.indexOf(" to YELLOW");
const noPermission = data.message.indexOf("You do not have permission");
const failedDeploy = data.message.indexOf("Failed to deploy application");
const removedInstance = data.message.indexOf("Removed instance ");
const addingInstance = data.message.indexOf("Adding instance ");
const failedConfig = data.message.indexOf("Failed to deploy configuration");
const failedQuota = data.message.indexOf("Your quota allows for 0 more running instance");
const abortedOperation = data.message.indexOf(" aborted operation.");
var color = "good";
if (stateRed != -1 || butWithErrors != -1 || noPermission != -1 || failedDeploy != -1 || failedConfig != -1 || failedQuota != -1) {
color = "danger";
}
if (stateYellow != -1 || removedInstance != -1 || addingInstance != -1 || abortedOperation != -1) {
color = "warning";
}
if ( data.overview.Failed && data.overview.Failed > 0 ){
color = "danger";
}
if ( fromCodeDeploy ) {
data.postData.attachments = [
{
"color": color,
"text":
"Succeeded: " + data.overview.Succeeded.toString() + "\n" +
"Failed: " + data.overview.Failed.toString() + "\n" +
"Skipped: " + data.overview.Skipped.toString() + "\n" +
"InProgress: " + data.overview.InProgress.toString() + "\n" +
"Pending: " + data.overview.Pending.toString()
},
{
"color": color,
"text": data.message
}
];
} else {
data.postData.attachments = [
{
"color": color,
"text": message
}
];
}
return Promise.resolve(data);
})
// Slack API
.then(data => {
const contentBody = util.format("%j", data.postData);
const options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: slackPath
};
return sendRequest(options, contentBody)
.then(() => { return Promise.resolve(data); });
})
.then(body => Promise.resolve({
'statusCode': 200,
'body': JSON.stringify(body)
}))
.catch(err => {
console.log(err);
return Promise.reject({
'statusCode': 500,
'body': JSON.stringify(err)
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment