Skip to content

Instantly share code, notes, and snippets.

@uncgopher
Last active March 5, 2019 01:18
Show Gist options
  • Save uncgopher/4fb46429745205a52bb717ab2967c20e to your computer and use it in GitHub Desktop.
Save uncgopher/4fb46429745205a52bb717ab2967c20e to your computer and use it in GitHub Desktop.
A NodeJS function for use in AWS Lambda to serve as the middleware between Slack and Rainforest QA.
// used to send API calls to Rainforest
var https = require('https');
// primary function for lambda
exports.handler = (event, context, callback) => {
// API Key
var apiKey = process.env.api_key;
// default msg
var responseTxt = 'Unknown failure';
// check for testing mode
if(event.queryStringParameters && event.queryStringParameters.test_mode){
// test mode is on, return the command if it exists
callback(null, {
isBase64Encoded: false,
statusCode: 200,
headers: {"content-type": "application/json"},
body: 'test_mode'
});
}
// make sure the request body exists
if (event.body !== null && event.body !== undefined) {
// parse the string Slack sends from URL-encoded into an object
var tempBody = JSON.parse('{"' + decodeURI(event.body.replace(/&/g, "\",\"").replace(/=/g,"\":\"")) + '"}');
// process command if it exists
if(tempBody.text){
// this is the text of the command from Slack;
// it should be the run Id of the test run from Rainforest
var runId = tempBody.text;
// create the post call for the API
const options = {
hostname: 'app.rainforestqa.com',
port: 443,
path: '/api/1/run_groups/'+runId+'/runs',
method: 'POST',
headers: {
'CLIENT_TOKEN': apiKey
}
};
// make the call to Rainforest API and read the results
var dataChunks = '';
var req = https.request(options, function(res) {
res.on('data', function (chunk) {
// combine chunks
dataChunks = dataChunks + chunk;
});
// received final chunk
res.on('end', function () {
// parse results
var data = JSON.parse(dataChunks);
if(res.statusCode == 200 || res.statusCode == 201){
responseTxt = "Successfully triggered run '" +
data.run_group.title + "' in environment '" +
data.environment.name + "'";
}else{
responseTxt = "Failed: " + data.error;
}
// send the response back to Slack
const response = {
isBase64Encoded: false,
statusCode: 200,
headers: {"content-type": "application/json"},
body: JSON.stringify({
"text": responseTxt,
"response_type": "in_channel",
})
};
callback(null, response);
});
});
req.end();
}
}else{
// return the unknown failure response to Slack
callback(null, {
isBase64Encoded: false,
statusCode: 200,
headers: {"content-type": "application/json"},
body: JSON.stringify({
"text": responseTxt,
"response_type": "in_channel",
})
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment