Skip to content
This repository has been archived by the owner on Oct 24, 2019. It is now read-only.

Commit

Permalink
handle bad slack usernames more bettah and don't halt and catch fire
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachariah Mully committed Sep 20, 2017
1 parent 9b30234 commit 0c14e73
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 68 deletions.
4 changes: 2 additions & 2 deletions incoming/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports.fn = function(event, context, callback) {
message.number = res.number;
message.requestId = requestId;
console.log(`${requestId} issue ${res.number} created for ${message.body.github.title}`);
slack.alertToSlack(message, user.slack, client, (err, status) => {
slack.alertToSlack(message, user.slack, client, slackChannel, (err, status) => {
if (err) return callback(err);
return callback(null, status);
});
Expand Down Expand Up @@ -79,7 +79,7 @@ module.exports.fn = function(event, context, callback) {
let q = queue(1);
message.users.forEach((user) => {
user = checkUser(user);
q.defer(slack.alertToSlack, message, user.slack, client);
q.defer(slack.alertToSlack, message, user.slack, client, slackChannel);
});
q.awaitAll(function(err, status) {
if (err) return callback(err);
Expand Down
77 changes: 57 additions & 20 deletions lib/slack.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use strict';

const encode = require('./utils').encode;
const slack = {};

module.exports.alertToSlack = alertToSlack;
module.exports.ingestSNS = ingestSNS;
module.exports.postAlert = postAlert;

function alertToSlack(snsMessage, destination, client, callback) {
slack.alertToSlack = function(snsMessage, destination, client, slackChannel, callback) {
if (!snsMessage.number) {
console.log(`${snsMessage.requestId} No GitHub issue number found in message body`);
return callback('No GitHub issue number found in message body');
Expand All @@ -15,31 +12,44 @@ function alertToSlack(snsMessage, destination, client, callback) {
if (err) return callback(err);
console.log(`${snsMessage.requestId} generated Slack callback_id ${res} for issue ${snsMessage.number} user ${destination}`);
snsMessage.callback_id = res; // eslint-disable-line camelcase
ingestSNS(snsMessage, (err, message, prompt) => {
slack.ingestSNS(snsMessage, (err, message, prompt) => {
if (err) return callback(err);
postAlert(destination, message, client, (err, res) => {
if (err) return callback(err);
slack.postAlert(destination, message, client, slackChannel, snsMessage.requestId, (err, res) => {
console.log('message');
if (err && err == 'badSlack') {
return callback();
} else if (err) {
return callback(err);
}
const status = {
alert: res.ok,
destination: destination,
message: res.message.text,
url: snsMessage.url
};
if (prompt) {
postAlert(destination, prompt, client, (err, res) => {
console.log('prompt');
slack.postAlert(destination, prompt, client, slackChannel, snsMessage.requestId, (err, res) => {
if (err && err == 'badSlack') {
return callback();
};
if (err) return callback(err);
status.message = `${status.message}, Prompt: ${res.message.text}`;
console.log(`${snsMessage.requestId} sent Slack message for issue ${snsMessage.number} to ${destination}`);
return callback(null, status);
});
} else {
console.log(`${snsMessage.requestId} sent Slack message for issue ${snsMessage.number} to ${destination}`);
return callback(null, status);
}
console.log(`${snsMessage.requestId} sent Slack message for issue ${snsMessage.number} sent to ${destination}`);
return callback(null, status);
});
});
});
}
}
};

function ingestSNS(snsMessage, callback) {
slack.ingestSNS = function(snsMessage, callback) {
console.log('ingestSNS called');
try {
let message = {
text: snsMessage.body.slack.message,
Expand Down Expand Up @@ -83,17 +93,44 @@ function ingestSNS(snsMessage, callback) {
return callback(null, message, prompt);
} else return callback(null, message, null);
} catch (err) {
return callback(`${snsMessage.requestId} sns message parsing error`);
return callback(`${snsMessage.requestId} sns message parsing error: ${err}`);
}
}
};

function postAlert(destination, alert, client, callback) {
slack.postAlert = function(destination, alert, client, slackChannel, requestId, callback) {
let options;
if (!alert.text) return callback(`${alert.requestId} missing Slack message body`);

if (!alert.text) return callback(`${requestId} missing Slack message body`);
if (destination.indexOf('@') > -1) options = { 'as_user': true, attachments: alert.attachments };
if (destination.indexOf('#') > -1) options = { attachments: alert.attachments };
client.chat.postMessage(destination, alert.text, options, function(err, res) {
if (err) return callback(res);
return callback(null, res);
if (err) {
console.log(`${requestId} Error sending message to slack destination: ${destination}`);
let postFailure = {
text: `Error sending message to \`${destination}\` for requestId ${requestId}`,
attachments: [
{
title: 'Slack error message',
text: JSON.stringify(err)
}
]
};
client.chat.postMessage(slackChannel, postFailure, options, function(err, res) {
if (err) {
console.log(`${requestId} Error sending message to slack channel ${slackChannel} for slack destination failure ${destination}`);
console.log(`${requestId} ${JSON.stringify(err)}`);
// return a custom error so we can skip generating a second error notification
return callback('badSlack', res);
}
return callback('badSlack', res);
});
} else {
return callback(null, res);
}
});
}
};

// module.exports.alertToSlack = alertToSlack;
// module.exports.ingestSNS = ingestSNS;
// module.exports.postAlert = postAlert;
module.exports = slack;
87 changes: 79 additions & 8 deletions test/fixtures/slack.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ module.exports.sns = {
body: {},
requestId: 123
},
malformedError: '123 sns message parsing error',
malformedError: '123 sns message parsing error: TypeError: Cannot read property \'slack\' of undefined',
malformedMessageError: '123 sns message parsing error: TypeError: Cannot read property \'message\' of undefined',
malformedNoIssueError: 'No GitHub issue number found in message body',
success: {
type: 'self_service',
Expand Down Expand Up @@ -186,7 +187,7 @@ module.exports.slack = {
statusPrompt: {
alert: true,
destination: '@testUser',
message: 'testSlackMessage, Prompt: testSlackMessage',
message: 'testSlackMessage, Prompt: testSlackPrompt',
url: 'https://github.com/testOwner/testRepo/issues/7'
},
statusBroadcast: [
Expand Down Expand Up @@ -252,12 +253,82 @@ module.exports.clients = {
slackAPIUrl:'test-url',
chat: {
postMessage: function(username, message, options, callback) {
return callback('error', {
ok: false,
error: 'no_text',
scopes: [ 'identify', 'bot:basic' ],
acceptedScopes: [ 'chat:write:user', 'client' ]
});
if (username == '@testUser') {
return callback('error', {
ok: false,
error: 'no_text',
scopes: [ 'identify', 'bot:basic' ],
acceptedScopes: [ 'chat:write:user', 'client' ]
});
} else {
return callback(null, {
ok: true,
channel: 'D6G0UU7MW',
ts: '1501777340.256863',
message: {
type: 'message',
user: 'U6GHXJQ1Z',
text: 'testSlackMessage',
'bot_id': 'B6G0UU6HW',
attachments: [ [Object] ],
ts: '1501777340.256863'
},
scopes: [ 'identify', 'bot:basic' ],
acceptedScopes: [ 'chat:write:user', 'client' ]
});
}
}
}
},
promptError: {
_token:'test-token',
slackAPIUrl:'test-url',
chat: {
postMessage: function(username, message, options, callback) {
if (username == '@testUser') {

if (message.attachments.actions) {
console.log('asdfasdf');
return callback('error', {
ok: false,
error: 'no_text',
scopes: [ 'identify', 'bot:basic' ],
acceptedScopes: [ 'chat:write:user', 'client' ]
});
} else {
return callback(null, {
ok: true,
channel: 'D6G0UU7MW',
ts: '1501777340.256863',
message: {
type: 'message',
user: 'U6GHXJQ1Z',
text: 'testSlackMessage',
'bot_id': 'B6G0UU6HW',
attachments: [ [Object] ],
ts: '1501777340.256863'
},
scopes: [ 'identify', 'bot:basic' ],
acceptedScopes: [ 'chat:write:user', 'client' ]
});
}
} else {
return callback(null, {
ok: true,
channel: 'D6G0UU7MW',
ts: '1501777340.256863',
message: {
type: 'message',
user: 'U6GHXJQ1Z',
text: 'testSlackMessage',
'bot_id': 'B6G0UU6HW',
attachments: [ [Object] ],
ts: '1501777340.256863'
},
scopes: [ 'identify', 'bot:basic' ],
acceptedScopes: [ 'chat:write:user', 'client' ]
});
}
}
}
},
Expand Down
Loading

0 comments on commit 0c14e73

Please sign in to comment.